// Source: D:\TempleOS-Projects\html\gates-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has words

-] Reward1
  CallExtStr("JukeReward",""
  -] Reward2

    "mouth distances recommending terrors "
    "dignities wondrously balm predicated "
    "ungodliness inner resolution secondary "
    "blasphemy appeared etext01 object "
    "particular placed peacefully advantage "

  );

U0 Song()
{
  CTask *task;
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.538;
  try {
    task=PopUpViewPrint(
        "Nehemiah,3:14\n");
    TaskWait(task);
    WinHorz(Fs->win_left,TEXT_COLS-10,task);
    WinVert(Fs->win_bottom+3,Fs->win_bottom+15,task);
    while (!ScanKey) {
        Play("M4/4");
        Play("5eDDC4BB5DqDhFeFEqF",
                "As\0signed \0by \0the \0task, \0to\0day.\n\0 \0 \0 \0 \0");
        Play("M7/8");
        Play("EhDeRM4/4");
        Play("DDC4BB5DqDhFeFEqF",
                "Schem\0ers \0in \0the \0well \0will \0pay.\n\0 \0 \0 \0 \0");
        Play("M7/8");
        Play("EsGAq.GeRM4/4");
        Play("6C5G4qAA5eD4BM5/4",
                "I \0 \0got \0the \0shit \0gate.\n\0");
        Play("5qG4AA5eDq.GM4/4");
        Play("eBG4qAA5eD4BM5/4",
                "Don't \0 \0you \0 \0be\0late.\n\0");
        Play("5qG4AA5eDq.C");
    }
  } catch
    PutExcept;
  Snd;
}

Song;

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\gates-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has words

-] Reward1
  CallExtStr("JukeReward",""
  -] Reward2

    "mouth distances recommending terrors "
    "dignities wondrously balm predicated "
    "ungodliness inner resolution secondary "
    "blasphemy appeared etext01 object "
    "particular placed peacefully advantage "

  );

U0 Song()
{
  CTask *task;
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.538;
  try {
    task=PopUpViewPrint(
        "Nehemiah,3:14\n");
    TaskWait(task);
    WinHorz(Fs->win_left,TEXT_COLS-10,task);
    WinVert(Fs->win_bottom+3,Fs->win_bottom+15,task);
    while (!ScanKey) {
        Play("M4/4");
        Play("5eDDC4BB5DqDhFeFEqF",
                "As\0signed \0by \0the \0task, \0to\0day.\n\0 \0 \0 \0 \0");
        Play("M7/8");
        Play("EhDeRM4/4");
        Play("DDC4BB5DqDhFeFEqF",
                "Schem\0ers \0in \0the \0well \0will \0pay.\n\0 \0 \0 \0 \0");
        Play("M7/8");
        Play("EsGAq.GeRM4/4");
        Play("6C5G4qAA5eD4BM5/4",
                "I \0 \0got \0the \0shit \0gate.\n\0");
        Play("5qG4AA5eDq.GM4/4");
        Play("eBG4qAA5eD4BM5/4",
                "Don't \0 \0you \0 \0be\0late.\n\0");
        Play("5qG4AA5eDq.C");
    }
  } catch
    PutExcept;
  Snd;
}

Song;


// Source: D:\TempleOS-Projects\html\MathAudioDemo-Graphics-Demo.html
// Date: unknown-date
// Language: HolyC

F64 MySin(F64 d)
{ //Sin() does not have an address.
  return Sin(d);
}

F64 MyCos(F64 d)
{ //Cos() does not have an address.
  return Cos(d);
}

U0 FunDraw1(CDC *dc,F64 (*f)(F64 t))
{
  I64 i,w=Fs->pix_width,h=Fs->pix_height;
  F64 scale=h/5.0;
  for (i=-w/2;i<=w/2;i++)
    GrLine(dc,w/2+i,h/2-scale*f(i/scale*2*pi),
          w/2+i+1,h/2-scale*f((i+1)/scale*2*pi));
}

U0 FunDraw2(CDC *dc,F64 (*f)(F64 t,F64 p),F64 T)
{
  I64 i,w=Fs->pix_width,h=Fs->pix_height;
  F64 scale=h/5.0;
  for (i=-w/2;i<=w/2;i++)
    GrLine(dc,w/2+i,h/2-scale*f(i/scale,T),w/2+i+1,h/2-scale*f((i+1)/scale,T));
}

U0 Init(CDC *dc)
{
  I64 i,w=Fs->pix_width,h=Fs->pix_height;
  F64 scale=h/5.0;
  DocClear;
  DCFill(dc);
  dc->color=BLACK;
  GrLine(dc,0,h/2,w-1,h/2);
  GrLine(dc,w/2,0,w/2,h-1);
  for (i=-2;i<=2;i++) {
    dc->color=BLACK;
    GrLine(dc,w/2-3,h/2-scale*i,w/2+3,h/2-scale*i);
    if (i) {
      dc->color=DKGRAY;
      GrPrint(dc,w/2+5,h/2-scale*i-FONT_HEIGHT/2,"%5.1f",ToF64(i));
    }
  }
  for (i=-4;i<=4;i++) {
    dc->color=BLACK;
    GrLine(dc,w/2+scale*i,h/2-3,w/2+scale*i,h/2+3);
    if (i) {
      dc->color=DKGRAY;
      GrPrint(dc,
            w/2+scale*i-7*FONT_WIDTH/2,h/2+5+FONT_HEIGHT,"%5.1f*T",ToF64(i));
    }
  }
}

U0 SndMathDemo()
{
  CDC *dc=DCAlias;

  Init(dc);
  dc->color=RED;
  FunDraw1(dc,&MySin);
  "$RED$Sin$FG$\n";
  PressAKey;
  dc->color=BLUE;
  FunDraw2(dc,&Saw,1.0);
  "$BLUE$Saw$FG$\n";
  PressAKey;
  dc->color=GREEN;
  FunDraw2(dc,&FullSaw,1.0);
  "$GREEN$FullSaw$FG$\n";
  PressAKey;

  Init(dc);
  dc->color=RED;
  FunDraw1(dc,&MyCos);
  "$RED$Cos$FG$\n";
  PressAKey;
  dc->color=BLUE;
  FunDraw2(dc,&Caw,1.0);
  "$BLUE$Caw$FG$\n";
  PressAKey;
  dc->color=GREEN;
  FunDraw2(dc,&FullCaw,1.0);
  "$GREEN$FullCaw$FG$\n";
  PressAKey;

  Init(dc);
  dc->color=RED;
  FunDraw1(dc,&MySin);
  "$RED$Sin$FG$\n";
  PressAKey;
  dc->color=BLUE;
  FunDraw2(dc,&Tri,1.0);
  "$BLUE$Tri$FG$\n";
  PressAKey;
  dc->color=GREEN;
  FunDraw2(dc,&FullTri,1.0);
  "$GREEN$FullTri$FG$\n";
  PressAKey;

  DCFill(dc);
  DCDel(dc);
}

SndMathDemo;

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\MathAudioDemo-Graphics-Demo.html
// Date: unknown-date
// Language: HolyC

F64 MySin(F64 d)
{ //Sin() does not have an address.
  return Sin(d);
}

F64 MyCos(F64 d)
{ //Cos() does not have an address.
  return Cos(d);
}

U0 FunDraw1(CDC *dc,F64 (*f)(F64 t))
{
  I64 i,w=Fs->pix_width,h=Fs->pix_height;
  F64 scale=h/5.0;
  for (i=-w/2;i<=w/2;i++)
    GrLine(dc,w/2+i,h/2-scale*f(i/scale*2*pi),
          w/2+i+1,h/2-scale*f((i+1)/scale*2*pi));
}

U0 FunDraw2(CDC *dc,F64 (*f)(F64 t,F64 p),F64 T)
{
  I64 i,w=Fs->pix_width,h=Fs->pix_height;
  F64 scale=h/5.0;
  for (i=-w/2;i<=w/2;i++)
    GrLine(dc,w/2+i,h/2-scale*f(i/scale,T),w/2+i+1,h/2-scale*f((i+1)/scale,T));
}

U0 Init(CDC *dc)
{
  I64 i,w=Fs->pix_width,h=Fs->pix_height;
  F64 scale=h/5.0;
  DocClear;
  DCFill(dc);
  dc->color=BLACK;
  GrLine(dc,0,h/2,w-1,h/2);
  GrLine(dc,w/2,0,w/2,h-1);
  for (i=-2;i<=2;i++) {
    dc->color=BLACK;
    GrLine(dc,w/2-3,h/2-scale*i,w/2+3,h/2-scale*i);
    if (i) {
      dc->color=DKGRAY;
      GrPrint(dc,w/2+5,h/2-scale*i-FONT_HEIGHT/2,"%5.1f",ToF64(i));
    }
  }
  for (i=-4;i<=4;i++) {
    dc->color=BLACK;
    GrLine(dc,w/2+scale*i,h/2-3,w/2+scale*i,h/2+3);
    if (i) {
      dc->color=DKGRAY;
      GrPrint(dc,
            w/2+scale*i-7*FONT_WIDTH/2,h/2+5+FONT_HEIGHT,"%5.1f*T",ToF64(i));
    }
  }
}

U0 SndMathDemo()
{
  CDC *dc=DCAlias;

  Init(dc);
  dc->color=RED;
  FunDraw1(dc,&MySin);
  "$RED$Sin$FG$\n";
  PressAKey;
  dc->color=BLUE;
  FunDraw2(dc,&Saw,1.0);
  "$BLUE$Saw$FG$\n";
  PressAKey;
  dc->color=GREEN;
  FunDraw2(dc,&FullSaw,1.0);
  "$GREEN$FullSaw$FG$\n";
  PressAKey;

  Init(dc);
  dc->color=RED;
  FunDraw1(dc,&MyCos);
  "$RED$Cos$FG$\n";
  PressAKey;
  dc->color=BLUE;
  FunDraw2(dc,&Caw,1.0);
  "$BLUE$Caw$FG$\n";
  PressAKey;
  dc->color=GREEN;
  FunDraw2(dc,&FullCaw,1.0);
  "$GREEN$FullCaw$FG$\n";
  PressAKey;

  Init(dc);
  dc->color=RED;
  FunDraw1(dc,&MySin);
  "$RED$Sin$FG$\n";
  PressAKey;
  dc->color=BLUE;
  FunDraw2(dc,&Tri,1.0);
  "$BLUE$Tri$FG$\n";
  PressAKey;
  dc->color=GREEN;
  FunDraw2(dc,&FullTri,1.0);
  "$GREEN$FullTri$FG$\n";
  PressAKey;

  DCFill(dc);
  DCDel(dc);
}

SndMathDemo;


// Source: D:\TempleOS-Projects\html\TOSBlog-AcctExample-TOS-Demo.html
// Date: unknown-date
// Language: HolyC

#help_index "Misc/Blog"

public Bool IsTOSBlog(U8 *filename)
{//Is this in the TOSBlog dir?
  Bool res;
  U8 *st=DirNameAbs(filename),*home=DirNameAbs("~");
  I64 l=StrLen(home);
  if (StrLen(st)>=l+12 && !StrNCmp(st+1,home+1,l-1) &&
      st[l]=='/' && !StrNCmp(st+l+4,"/TOSBlog",8))
    res=TRUE;
  else
    res=FALSE;
  Free(st);
  Free(home);
  return res;
}

public U8 *TOSBlogAbs(U8 *filename)
{//Absolute filename, adjusted for moving TOSBlog.
  U8 *st=DirNameAbs(filename),*home=DirNameAbs("~");
  I64 l=StrLen(home);
  if (IsTOSBlog(st)) {
    MemCpy(st+l+5,st+l+1,3);
    MemCpy(st+l+1,"Web",3);
    StrCpy(st+l+8,st+l+12);
  }
  Free(home);
  return st;
}

public U8 *BlogLocalDir(CDate cdt=0)
{//MAlloc dirname of blog for date.
  static U8 dir[STR_LEN];
  CDateStruct ds;
  if (!cdt) cdt=Now+local_time_offset;
  Date2Struct(&ds,cdt);
  StrPrint(dir,BLOG_LOCAL_SRC "/%d%Z",ds.year,ds.mon-1,"ST_MONTHS");
  if (!FileFind(dir,,FUF_JUST_DIRS))
    DirMk(dir);
  return dir;
}

public U8 *BlogLocalFile(CDate cdt=0)
{//MAlloc filename of blog for date.
  static U8 file[STR_LEN];
  CDateStruct ds;
  if (!cdt) cdt=Now+local_time_offset;
  Date2Struct(&ds,cdt);
  StrPrint(file,"%s/" INS_REG_PERSONAL_INITIALS "%02d%02d%02d.DD.Z",
        BlogLocalDir(cdt),ds.year%100,ds.mon,ds.day_of_mon);
  return file;
}

public U8 *BlogWebDir(CDate cdt=0)
{//MAlloc D:/Wb/dirname of blog for date.
  static U8 dir[STR_LEN];
  CDateStruct ds;
  if (!cdt) cdt=Now+local_time_offset;
  Date2Struct(&ds,cdt);
  StrPrint(dir,BLOG_WEB_DST "/%d%Z",
        ds.year,ds.mon-1,"ST_MONTHS");
  return dir;
}

public U8 *BlogWebFile(CDate cdt=0)
{//MAlloc D:/Wb/filename of blog for date.
  static U8 file[STR_LEN];
  CDateStruct ds;
  if (!cdt) cdt=Now+local_time_offset;
  Date2Struct(&ds,cdt);
  StrPrint(file,"%s/" INS_REG_PERSONAL_INITIALS "%02d%02d%02d.DD.Z",
        BlogWebDir(cdt),ds.year%100,ds.mon,ds.day_of_mon);
  return file;
}

public U8 *FileNameNumNext(U8 *files_find_mask,U8 *fmt)
{//Cnt files in a mask. Create next filename.
  CDirEntry *tmpde=FilesFind(files_find_mask);
  U8 *res=MStrPrint(fmt,FileCnt(tmpde));
  DirTreeDel(tmpde);
  return res;
}

public U0 BlogDCImgWrite(CDC *dc,CDate cdt=0)
{//Add dc to blog as BMP file.
  U8 file_mask[STR_LEN],file_name[STR_LEN],*st,*st2;
  if (!cdt) cdt=Now+local_time_offset;
  StrPrint(file_mask,"%s/Img*",BlogLocalDir(cdt));
  StrPrint(file_name,"%s/Img%%03d.BMP",BlogLocalDir(cdt));
  st=FileNameNumNext(file_mask,file_name);
  BMPWrite(st,dc);
  st2=TOSBlogAbs(st);
  Free(st);
  st=MStrPrint("/Wb%s",st2+2);
  Free(st2);
  "$HC,\"<img src=\\\"%s\\\" width=\\\"%d\\\" "
        "height=\\\"%d\\\" alt=\\\"\\\">\"$\n",st,dc->width,dc->height;
  Free(st);
}

public U0 BlogScrnShot()
{//Add scrn shot to blog as BMP file.
  Bool old_cursor=DocCursor,old_silent;
  CDC *dc1,*dc2;
  U8 *old_fp_draw_ms=gr.fp_draw_ms;
  gr.fp_draw_ms=NULL;
  Refresh(2);
  dc1=DCScrnCapture;
  dc2=DCExt(dc1,0,FONT_HEIGHT,GR_WIDTH-1,GR_HEIGHT-1);
  BlogDCImgWrite(dc2);
  gr.fp_draw_ms=old_fp_draw_ms;
  DCDel(dc1);
  DCDel(dc2);
  DocCursor(old_cursor);
  old_silent=Silent;
  FreshenBlog;
  Silent(old_silent);
}

public U0 FreshenBlog()
{//Regen html files for D:/Home/Web.
  DirMk("D:/Wb");
  DirMk("D:/Wb/Home");
  DirMk("D:/Wb/Home/Web");
  DirMk("D:/Wb/Home/Web/" INS_REG_PERSONAL_INITIALS);
  DirMk("D:/Wb/Home/Web/" INS_REG_PERSONAL_INITIALS "/BlogDir");

  FreshenWebDir("/Home/Web");
  FreshenWebDir("/Home/" INS_REG_PERSONAL_INITIALS "/TOSBlog");
  FreshenWebDir(BlogLocalDir+2);
  Drv('C');
}

public U0 Blog(Bool god_misc2=FALSE)
{//Make entry in blog.
  if (!FileFind(BlogLocalFile,,FUF_JUST_FILES))
    Copy("~/" INS_REG_PERSONAL_INITIALS "/TOSBlog/DailyBlogHeader.DD.Z",
          BlogLocalFile);
  AutoComplete;
  WinBorder;
  WinMax;
  Msg(MSG_KEY_DOWN_UP,0,SC_CURSOR_DOWN|SCF_CTRL);
  if (god_misc2)
    Msg(MSG_KEY_DOWN_UP,0,SC_F7|SCF_SHIFT|SCF_ALT);
  else {
    Msg(MSG_KEY_DOWN_UP,0,SC_F6|SCF_ALT);
    Msg(MSG_KEY_DOWN_UP,'\n',0);
  }
  Msg(MSG_KEY_DOWN_UP,CH_CTRLS,0x61F0000061F);
  if (Ed(BlogLocalFile,EDF_WAS_WRITE)) {
    DocOpt(BlogLocalFile,"-r");
    Copy(BlogLocalFile,"::" DAILY_SRC ".DD.Z");
    Copy(BlogLocalFile,"D:" DAILY_SRC ".DD.Z");
    Copy(BlogLocalFile,"D:/Wb" DAILY_DST ".DD.Z");
    TOSWebFile("D:/Wb" DAILY_DST ".DD.Z");
    FreshenBlog;
  }
}

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\TOSBlog-AcctExample-TOS-Demo.html
// Date: unknown-date
// Language: HolyC

#help_index "Misc/Blog"

public Bool IsTOSBlog(U8 *filename)
{//Is this in the TOSBlog dir?
  Bool res;
  U8 *st=DirNameAbs(filename),*home=DirNameAbs("~");
  I64 l=StrLen(home);
  if (StrLen(st)>=l+12 && !StrNCmp(st+1,home+1,l-1) &&
      st[l]=='/' && !StrNCmp(st+l+4,"/TOSBlog",8))
    res=TRUE;
  else
    res=FALSE;
  Free(st);
  Free(home);
  return res;
}

public U8 *TOSBlogAbs(U8 *filename)
{//Absolute filename, adjusted for moving TOSBlog.
  U8 *st=DirNameAbs(filename),*home=DirNameAbs("~");
  I64 l=StrLen(home);
  if (IsTOSBlog(st)) {
    MemCpy(st+l+5,st+l+1,3);
    MemCpy(st+l+1,"Web",3);
    StrCpy(st+l+8,st+l+12);
  }
  Free(home);
  return st;
}

public U8 *BlogLocalDir(CDate cdt=0)
{//MAlloc dirname of blog for date.
  static U8 dir[STR_LEN];
  CDateStruct ds;
  if (!cdt) cdt=Now+local_time_offset;
  Date2Struct(&ds,cdt);
  StrPrint(dir,BLOG_LOCAL_SRC "/%d%Z",ds.year,ds.mon-1,"ST_MONTHS");
  if (!FileFind(dir,,FUF_JUST_DIRS))
    DirMk(dir);
  return dir;
}

public U8 *BlogLocalFile(CDate cdt=0)
{//MAlloc filename of blog for date.
  static U8 file[STR_LEN];
  CDateStruct ds;
  if (!cdt) cdt=Now+local_time_offset;
  Date2Struct(&ds,cdt);
  StrPrint(file,"%s/" INS_REG_PERSONAL_INITIALS "%02d%02d%02d.DD.Z",
        BlogLocalDir(cdt),ds.year%100,ds.mon,ds.day_of_mon);
  return file;
}

public U8 *BlogWebDir(CDate cdt=0)
{//MAlloc D:/Wb/dirname of blog for date.
  static U8 dir[STR_LEN];
  CDateStruct ds;
  if (!cdt) cdt=Now+local_time_offset;
  Date2Struct(&ds,cdt);
  StrPrint(dir,BLOG_WEB_DST "/%d%Z",
        ds.year,ds.mon-1,"ST_MONTHS");
  return dir;
}

public U8 *BlogWebFile(CDate cdt=0)
{//MAlloc D:/Wb/filename of blog for date.
  static U8 file[STR_LEN];
  CDateStruct ds;
  if (!cdt) cdt=Now+local_time_offset;
  Date2Struct(&ds,cdt);
  StrPrint(file,"%s/" INS_REG_PERSONAL_INITIALS "%02d%02d%02d.DD.Z",
        BlogWebDir(cdt),ds.year%100,ds.mon,ds.day_of_mon);
  return file;
}

public U8 *FileNameNumNext(U8 *files_find_mask,U8 *fmt)
{//Cnt files in a mask. Create next filename.
  CDirEntry *tmpde=FilesFind(files_find_mask);
  U8 *res=MStrPrint(fmt,FileCnt(tmpde));
  DirTreeDel(tmpde);
  return res;
}

public U0 BlogDCImgWrite(CDC *dc,CDate cdt=0)
{//Add dc to blog as BMP file.
  U8 file_mask[STR_LEN],file_name[STR_LEN],*st,*st2;
  if (!cdt) cdt=Now+local_time_offset;
  StrPrint(file_mask,"%s/Img*",BlogLocalDir(cdt));
  StrPrint(file_name,"%s/Img%%03d.BMP",BlogLocalDir(cdt));
  st=FileNameNumNext(file_mask,file_name);
  BMPWrite(st,dc);
  st2=TOSBlogAbs(st);
  Free(st);
  st=MStrPrint("/Wb%s",st2+2);
  Free(st2);
  "$HC,\"<img src=\\\"%s\\\" width=\\\"%d\\\" "
        "height=\\\"%d\\\" alt=\\\"\\\">\"$\n",st,dc->width,dc->height;
  Free(st);
}

public U0 BlogScrnShot()
{//Add scrn shot to blog as BMP file.
  Bool old_cursor=DocCursor,old_silent;
  CDC *dc1,*dc2;
  U8 *old_fp_draw_ms=gr.fp_draw_ms;
  gr.fp_draw_ms=NULL;
  Refresh(2);
  dc1=DCScrnCapture;
  dc2=DCExt(dc1,0,FONT_HEIGHT,GR_WIDTH-1,GR_HEIGHT-1);
  BlogDCImgWrite(dc2);
  gr.fp_draw_ms=old_fp_draw_ms;
  DCDel(dc1);
  DCDel(dc2);
  DocCursor(old_cursor);
  old_silent=Silent;
  FreshenBlog;
  Silent(old_silent);
}

public U0 FreshenBlog()
{//Regen html files for D:/Home/Web.
  DirMk("D:/Wb");
  DirMk("D:/Wb/Home");
  DirMk("D:/Wb/Home/Web");
  DirMk("D:/Wb/Home/Web/" INS_REG_PERSONAL_INITIALS);
  DirMk("D:/Wb/Home/Web/" INS_REG_PERSONAL_INITIALS "/BlogDir");

  FreshenWebDir("/Home/Web");
  FreshenWebDir("/Home/" INS_REG_PERSONAL_INITIALS "/TOSBlog");
  FreshenWebDir(BlogLocalDir+2);
  Drv('C');
}

public U0 Blog(Bool god_misc2=FALSE)
{//Make entry in blog.
  if (!FileFind(BlogLocalFile,,FUF_JUST_FILES))
    Copy("~/" INS_REG_PERSONAL_INITIALS "/TOSBlog/DailyBlogHeader.DD.Z",
          BlogLocalFile);
  AutoComplete;
  WinBorder;
  WinMax;
  Msg(MSG_KEY_DOWN_UP,0,SC_CURSOR_DOWN|SCF_CTRL);
  if (god_misc2)
    Msg(MSG_KEY_DOWN_UP,0,SC_F7|SCF_SHIFT|SCF_ALT);
  else {
    Msg(MSG_KEY_DOWN_UP,0,SC_F6|SCF_ALT);
    Msg(MSG_KEY_DOWN_UP,'\n',0);
  }
  Msg(MSG_KEY_DOWN_UP,CH_CTRLS,0x61F0000061F);
  if (Ed(BlogLocalFile,EDF_WAS_WRITE)) {
    DocOpt(BlogLocalFile,"-r");
    Copy(BlogLocalFile,"::" DAILY_SRC ".DD.Z");
    Copy(BlogLocalFile,"D:" DAILY_SRC ".DD.Z");
    Copy(BlogLocalFile,"D:/Wb" DAILY_DST ".DD.Z");
    TOSWebFile("D:/Wb" DAILY_DST ".DD.Z");
    FreshenBlog;
  }
}


// Source: D:\TempleOS-Projects\html\64BitAsmQuiz-Lectures-Demo.html
// Date: unknown-date
// Language: HolyC

64-Bit Assembly Quiz

1) In 64-bit mode, how many bytes are always pushed?

        PUSH    12
        PUSH    EAX

2) What happens to the upper 32-bits?

        XOR     EAX,EAX
        MOV     EAX,0x12345678
        MOV     EAX,0x80000000

3) How do you set FS or GS values?

4) If FS points to current task record, what's wrong with this instruction?

        MOV     RAX,U64 FS:[TSS_SOME_MEMBER]

5) Which instruction takes more bytes?

        MOV     RAX,U64 [R8]
        MOV     RAX,U64 [R13]

6) Are these the same number of bytes?

        MOV     RAX,1234
        MOV     R8,1234
        MOV     EAX,1234

7) True or False

  a) You can access the lowest byte of RAX.

  b) You can access the lowest byte of ESI.

  c) You can access the second-to-lowest byte of RAX.

  d) You can access the second-to-lowest byte of ESI.

8) How do you call a subroutine at 0x10,0000,0000 from code at 0x00,0010,0000?

9) How much faster is a REL32 call instruction compared to a software interrupt
or SYSCALL?

10) How long does an IN or OUT instruction take on a 1GHz machine and on a 3GHz
machine?

11) How do you push all 16 regs?

12) Should you put the regs in a TSS?

13) You can have 4K or 4Meg pages in 32-bit mode.  You can have 4K or what size
pages in 64-bit mode?

14) On a fresh CPU with an empty TLB, how many memory accesses (page tables)
does it take to access one virtual address?

----

TempleOS identity-maps everything, all the time, so the usual convention of
upper memory being for kernel does not apply.  It uses physical addresses,
basically.  It puts all code in the lowest 2-Gig memory range so that it can use
the CALL REL32 instruction, the fastest.  It never changes privilege levels or
messes with page tables, once it is up-and-running.

----

ANSWERS:

1) All stack pushes and pops are 64-bits.

2) The upper 32-bits are set to zero.

3) To set FS or GS, you use WRMSR to write a model specific reg.  See
IA32_FS_BASE and SET_FS_BASE.

4) Displacement addressing is now RIP relative, so RIP would be added to
TSS_SOME_MEMBER.  (Useless)

5) The R13 instruction takes one more byte because it is like REG_RBP in the
ModR.

6) The R8 instruction needs a REX byte prefix to specify upper-8 reg.

7) You can access the lowest byte of any reg.  You can access AH but not the
second-to-lowest byte of ESI.

8) To call a subroutine farther than 2Gig away, you put the address into RAX,
then CALL RAX.

9) CALL REL32 is significantly faster.  See ::/Demo/Lectures/InterruptDemo.HC.

10) IN or OUT instructions happen at a fixed speed based on the original ISA bus
clock.

11) PUSHAD is not available for 64-bit mode, so you do it by hand.

12) The TSS is no longer used to hold the task state because there are 16 regs
and they are 64-bits, not 32-bits.  I guess Intel decided doing it by hand was
better than TSSes.

13) 64-bit mode has 4K or 2Meg page size.

14) For one access, there are 3-4 levels of page tables plus the location
itself.

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\64BitAsmQuiz-Lectures-Demo.html
// Date: unknown-date
// Language: HolyC

64-Bit Assembly Quiz

1) In 64-bit mode, how many bytes are always pushed?

        PUSH    12
        PUSH    EAX

2) What happens to the upper 32-bits?

        XOR     EAX,EAX
        MOV     EAX,0x12345678
        MOV     EAX,0x80000000

3) How do you set FS or GS values?

4) If FS points to current task record, what's wrong with this instruction?

        MOV     RAX,U64 FS:[TSS_SOME_MEMBER]

5) Which instruction takes more bytes?

        MOV     RAX,U64 [R8]
        MOV     RAX,U64 [R13]

6) Are these the same number of bytes?

        MOV     RAX,1234
        MOV     R8,1234
        MOV     EAX,1234

7) True or False

  a) You can access the lowest byte of RAX.

  b) You can access the lowest byte of ESI.

  c) You can access the second-to-lowest byte of RAX.

  d) You can access the second-to-lowest byte of ESI.

8) How do you call a subroutine at 0x10,0000,0000 from code at 0x00,0010,0000?

9) How much faster is a REL32 call instruction compared to a software interrupt
or SYSCALL?

10) How long does an IN or OUT instruction take on a 1GHz machine and on a 3GHz
machine?

11) How do you push all 16 regs?

12) Should you put the regs in a TSS?

13) You can have 4K or 4Meg pages in 32-bit mode.  You can have 4K or what size
pages in 64-bit mode?

14) On a fresh CPU with an empty TLB, how many memory accesses (page tables)
does it take to access one virtual address?

----

TempleOS identity-maps everything, all the time, so the usual convention of
upper memory being for kernel does not apply.  It uses physical addresses,
basically.  It puts all code in the lowest 2-Gig memory range so that it can use
the CALL REL32 instruction, the fastest.  It never changes privilege levels or
messes with page tables, once it is up-and-running.

----

ANSWERS:

1) All stack pushes and pops are 64-bits.

2) The upper 32-bits are set to zero.

3) To set FS or GS, you use WRMSR to write a model specific reg.  See
IA32_FS_BASE and SET_FS_BASE.

4) Displacement addressing is now RIP relative, so RIP would be added to
TSS_SOME_MEMBER.  (Useless)

5) The R13 instruction takes one more byte because it is like REG_RBP in the
ModR.

6) The R8 instruction needs a REX byte prefix to specify upper-8 reg.

7) You can access the lowest byte of any reg.  You can access AH but not the
second-to-lowest byte of ESI.

8) To call a subroutine farther than 2Gig away, you put the address into RAX,
then CALL RAX.

9) CALL REL32 is significantly faster.  See ::/Demo/Lectures/InterruptDemo.HC.

10) IN or OUT instructions happen at a fixed speed based on the original ISA bus
clock.

11) PUSHAD is not available for 64-bit mode, so you do it by hand.

12) The TSS is no longer used to hold the task state because there are 16 regs
and they are 64-bits, not 32-bits.  I guess Intel decided doing it by hand was
better than TSSes.

13) 64-bit mode has 4K or 2Meg page size.

14) For one access, there are 3-4 levels of page tables plus the location
itself.


// Source: D:\TempleOS-Projects\html\MakeUtils-Opt-Utils-Adam.html
// Date: unknown-date
// Language: HolyC

Cd(__DIR__);;
#include "Diff"
#include "FileBMP"
#include "Find"
#include "HeapLog"
#include "LineRep"
#include "LinkChk"
#include "MemRep"
#include "Merge"
#include "Profiler"
#include "StrUtils"
#include "DocUtils"
#include "ToDolDoc"
#include "ToTXT"
#include "TOS"
Cd("..");;

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\MakeUtils-Opt-Utils-Adam.html
// Date: unknown-date
// Language: HolyC

Cd(__DIR__);;
#include "Diff"
#include "FileBMP"
#include "Find"
#include "HeapLog"
#include "LineRep"
#include "LinkChk"
#include "MemRep"
#include "Merge"
#include "Profiler"
#include "StrUtils"
#include "DocUtils"
#include "ToDolDoc"
#include "ToTXT"
#include "TOS"
Cd("..");;


// Source: D:\TempleOS-Projects\html\Run-Sup1-Sup1Games-AfterEgypt-Home.html
// Date: unknown-date
// Language: HolyC

Cd(__DIR__);;
#include "Load"
AfterEgypt;

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\Run-Sup1-Sup1Games-AfterEgypt-Home.html
// Date: unknown-date
// Language: HolyC

Cd(__DIR__);;
#include "Load"
AfterEgypt;


// Source: D:\TempleOS-Projects\html\Mouse-SerialDev-Kernel.html
// Date: unknown-date
// Language: HolyC

U0 GridInit()
{//Init mouse grid struct. See ::/Demo/Graphics/Grid.HC.
  ms_grid.x=ms_grid.y=ms_grid.z=8;
  ms_grid.x_offset=ms_grid.y_offset=ms_grid.z_offset=0;
  ms_grid.x_speed =ms_grid.y_speed =ms_grid.z_speed =1;
  ms_grid.show=ms_grid.snap=ms_grid.coord=FALSE;
}

U0 MsUpdate(I64 x,I64 y,I64 z,Bool l,Bool r)
{
  ms.presnap.x=ToI64(ms.scale.x*x)+ms.offset.x;
  ms.presnap.y=ToI64(ms.scale.y*y)+ms.offset.y;
  ms.presnap.z=ToI64(ms.scale.z*z)+ms.offset.z;
  if (ms_grid.snap) {
    ms.pos.x=Trunc(ms.presnap.x/ms_grid.x)*ms_grid.x+ms_grid.x_offset;
    ms.pos.y=Trunc(ms.presnap.y/ms_grid.y)*ms_grid.y+ms_grid.y_offset;
    ms.pos.z=Trunc(ms.presnap.z/ms_grid.z)*ms_grid.z+ms_grid.z_offset;
  } else {
    ms.pos.x=ms.presnap.x;
    ms.pos.y=ms.presnap.y;
    ms.pos.z=ms.presnap.z;
  }

  ms.pos.x=ClampI64(ms.pos.x,0,GR_WIDTH-1);
  ms.pos.y=ClampI64(ms.pos.y,0,GR_HEIGHT-1);
  ms.pos_text.x=ms.pos.x/FONT_WIDTH;
  if (ms.pos_text.x>=text.cols) {
    ms.pos_text.x=text.cols-1;
    ms.pos.x=text.cols*FONT_WIDTH-1;
  }
  ms.pos_text.y=ms.pos.y/FONT_HEIGHT;
  if (ms.pos_text.y>=text.rows) {
    ms.pos_text.y=text.rows-1;
    ms.pos.y=text.rows*FONT_HEIGHT-1;
  }
  ms.lb=l;
  ms.rb=r;
  LBEqu(&kbd.scan_code,SCf_MS_L_DOWN,ms.lb);
  LBEqu(&kbd.scan_code,SCf_MS_R_DOWN,ms.rb);
}

U0 MsSet(I64 x=I64_MAX,I64 y=I64_MAX,I64 z=I64_MAX,I64 l=I64_MAX,I64 r=I64_MAX)
{//Note: Generates a message. See MsSet().
  if (!(0<=x<GR_WIDTH))
    x=ms.pos.x;
  if (!(0<=y<GR_HEIGHT))
    y=ms.pos.y;
  if (z==I64_MAX)
    z=ms.pos.z;

  if (!(FALSE<=l<=TRUE))
    l=ms.lb;
  if (!(FALSE<=r<=TRUE))
    r=ms.rb;

  x=(x-ms.offset.x)/ms.scale.x;
  y=(y-ms.offset.y)/ms.scale.y;
  z=(z-ms.offset.z)/ms.scale.z;
  MsUpdate(x,y,z,l,r);
  MsHardSet(x,y,z,l,r);
}

U0 MsInit()
{
  MemSet(&ms,0,sizeof(CMsStateGlbls));
  MemSet(&ms_last,0,sizeof(CMsStateGlbls));
  ms.offset.x=ms.offset.y=ms.offset.z=0;
  ms.scale.x=ms.scale.y=ms.scale.z=1.0;
  ms.pos_text.x=ms.pos_text.y=ms.pos_text.z=0;
  ms.has_wheel=FALSE;
  ms.show=TRUE;
  ms.speed=0;
  ms.timestamp=GetTSC;
  ms.dbl_time=0.350;
  GridInit;
}

U0 MsHardPktRead()
{
  U8 j;
  if (GetTSC>ms_hard.timestamp+cnts.time_stamp_freq>>3)
    FifoU8Flush(ms_hard.fifo);
  ms_hard.timestamp=GetTSC;
  FifoU8Ins(ms_hard.fifo,InU8(KBD_PORT));
  if (FifoU8Cnt(ms_hard.fifo)==ms_hard.pkt_size)
    while (FifoU8Rem(ms_hard.fifo,&j))
      FifoU8Ins(ms_hard.fifo2,j);
}

interrupt U0 IRQMsHard()
{
  CLD
  OutU8(0xA0,0x20);
  OutU8(0x20,0x20);
  ms_hard.irqs_working=TRUE;
  if (ms_hard.install_in_progress || !ms_hard.installed) {
    kbd.rst=TRUE;
    return;
  }
  MsHardPktRead;
}

U0 MsHardGetType()
{
  I64 b;
  KbdMsCmdAck(0xF2);
  b=KbdCmdRead;
  if (b==3)
    ms_hard.has_wheel=TRUE;
  else if (b==4)
    ms_hard.has_ext_bttns=TRUE;
}

Bool MsHardRst()
{
  U8 b,*_b;
  F64 timeout;
  Bool res=FALSE;

  ms_hard.has_wheel=FALSE;
  ms_hard.has_ext_bttns=FALSE;

  if (*0x40E(U16 *)==0x9FC0) {
    _b=0x9FC00+0x30;
    *_b=1; //This enables my mouse.  It might be for one machine.
//USB DMA packets, set-up by BIOS to make legacy PS/2?
  }

  try {
    KbdCmdFlush;
    KbdCmdSend(KBD_CTRL,0xAD); //Disable Kbd
    KbdCmdSend(KBD_CTRL,0xA8); //Enable Mouse

    KbdMsCmdAck(0xFF); //Rst

    timeout=tS+10.0;
    do
      try {
        KbdCmdRead;
        timeout=0; //force exit
      } catch
        Fs->catch_except=TRUE;
    while (tS<timeout);

    try
            KbdCmdRead;
    catch
      Fs->catch_except=TRUE;

    KbdMsCmdAck(0xF3,200,0xF3,100,0xF3,80);
    MsHardGetType;
    KbdMsCmdAck(0xF3,10);
    MsHardGetType;
    KbdMsCmdAck(0xE8,0x03,0xE6,0xF3,100,0xF4);
    res=TRUE;

    //Enable IRQ 12
    KbdCmdSend(KBD_CTRL,0x20);
    b=KbdCmdRead;
    KbdCmdSend(KBD_CTRL,0x60);
    KbdCmdSend(KBD_PORT,(b|2)&~0x20);

  } catch
    Fs->catch_except=TRUE;

    //This is been added to override failure
    //because the mouse sometimes still works.
  res=TRUE;

  try
          KbdCmdSend(KBD_CTRL,0xAE); //Enable Keyboard
  catch
    Fs->catch_except=TRUE;
  if (ms_hard.has_wheel || ms_hard.has_ext_bttns)
    ms_hard.pkt_size=4;
  else
    ms_hard.pkt_size=3;
  if (!res)
    try
            KbdCmdSend(KBD_CTRL,0xA7); //Disable Mouse
    catch
      Fs->catch_except=TRUE;
  return res;
}

U0 MsHardSpeedSet()
{
  I64 dd,tmp;
  if ((dd=SqrI64(ms_hard_last.pos.x-ms_hard.pos.x)
        +SqrI64(ms_hard_last.pos.y-ms_hard.pos.y)) &&
        (tmp=ms_hard.timestamp-ms_hard_last.timestamp))
    ms_hard.speed=Sqrt(dd)*cnts.time_stamp_freq/tmp;
  ms_hard_last.timestamp=ms_hard.timestamp;
}

U0 MsHardSetPre()
{
  I64 old_timestamp=ms_hard_last.timestamp;
  MemCpy(&ms_hard_last,&ms_hard,sizeof(CMsHardStateGlbls));
  ms_hard_last.timestamp=old_timestamp;
}

U0 MsHardSetPost()
{
  I64 i;
  ms_hard.pos.x=ms_hard.prescale.x*ms_hard.scale.x*ms_grid.x_speed;
  ms_hard.pos.y=ms_hard.prescale.y*ms_hard.scale.y*ms_grid.y_speed;
  ms_hard.pos.z=ms_hard.prescale.z*ms_hard.scale.z*ms_grid.z_speed;

  i=Trunc(ms.scale.x*ms_hard.pos.x/ms_grid.x)*ms_grid.x+ms.offset.x;
//TODO ms_grid.x_offset?
  if (i<0)
    ms.offset.x-=i;
  else if (i>=GR_WIDTH)
    ms.offset.x+=GR_WIDTH-1-i;

  i=Trunc(ms.scale.y*ms_hard.pos.y/ms_grid.y)*ms_grid.y+ms.offset.y;
  if (i<0)
    ms.offset.y-=i;
  else if (i>=GR_HEIGHT)
    ms.offset.y+=GR_HEIGHT-1-i;

  if (ms_hard.pos.x!=ms_hard_last.pos.x || ms_hard.pos.y!=ms_hard_last.pos.y ||
        ms_hard.pos.z!=ms_hard_last.pos.z) {
    ms_hard.evt=TRUE;
    MsHardSpeedSet;
  } else
    for (i=0;i<5;i++)
      if (ms_hard.bttns[i]!=ms_hard_last.bttns[i]) {
        ms_hard.evt=TRUE;
        break;
      }
}

U0 MsHardHndlr()
{
  I64 i,dx,dy,dz;
  U8 ms_buf[4];

  MsHardSetPre;
  for (i=0;i<4;i++)
    ms_buf[i]=0;
  for (i=0;i<ms_hard.pkt_size;i++)
    if (!FifoU8Rem(ms_hard.fifo2,&ms_buf[i]))
      ms_buf[i]=0;

  ms_hard.bttns[0] = ms_buf[0] & 1;
  ms_hard.bttns[1] = (ms_buf[0] & 2) >> 1;
  ms_hard.bttns[2] = (ms_buf[0] & 4) >> 2;
  ms_hard.bttns[3] = (ms_buf[3] & 0x10) >> 4;
  ms_hard.bttns[4] = (ms_buf[3] & 0x20) >> 5;
  if (ms_buf[0] & 0x10)
    dx=ms_buf[1

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\Mouse-SerialDev-Kernel.html
// Date: unknown-date
// Language: HolyC

U0 GridInit()
{//Init mouse grid struct. See ::/Demo/Graphics/Grid.HC.
  ms_grid.x=ms_grid.y=ms_grid.z=8;
  ms_grid.x_offset=ms_grid.y_offset=ms_grid.z_offset=0;
  ms_grid.x_speed =ms_grid.y_speed =ms_grid.z_speed =1;
  ms_grid.show=ms_grid.snap=ms_grid.coord=FALSE;
}

U0 MsUpdate(I64 x,I64 y,I64 z,Bool l,Bool r)
{
  ms.presnap.x=ToI64(ms.scale.x*x)+ms.offset.x;
  ms.presnap.y=ToI64(ms.scale.y*y)+ms.offset.y;
  ms.presnap.z=ToI64(ms.scale.z*z)+ms.offset.z;
  if (ms_grid.snap) {
    ms.pos.x=Trunc(ms.presnap.x/ms_grid.x)*ms_grid.x+ms_grid.x_offset;
    ms.pos.y=Trunc(ms.presnap.y/ms_grid.y)*ms_grid.y+ms_grid.y_offset;
    ms.pos.z=Trunc(ms.presnap.z/ms_grid.z)*ms_grid.z+ms_grid.z_offset;
  } else {
    ms.pos.x=ms.presnap.x;
    ms.pos.y=ms.presnap.y;
    ms.pos.z=ms.presnap.z;
  }

  ms.pos.x=ClampI64(ms.pos.x,0,GR_WIDTH-1);
  ms.pos.y=ClampI64(ms.pos.y,0,GR_HEIGHT-1);
  ms.pos_text.x=ms.pos.x/FONT_WIDTH;
  if (ms.pos_text.x>=text.cols) {
    ms.pos_text.x=text.cols-1;
    ms.pos.x=text.cols*FONT_WIDTH-1;
  }
  ms.pos_text.y=ms.pos.y/FONT_HEIGHT;
  if (ms.pos_text.y>=text.rows) {
    ms.pos_text.y=text.rows-1;
    ms.pos.y=text.rows*FONT_HEIGHT-1;
  }
  ms.lb=l;
  ms.rb=r;
  LBEqu(&kbd.scan_code,SCf_MS_L_DOWN,ms.lb);
  LBEqu(&kbd.scan_code,SCf_MS_R_DOWN,ms.rb);
}

U0 MsSet(I64 x=I64_MAX,I64 y=I64_MAX,I64 z=I64_MAX,I64 l=I64_MAX,I64 r=I64_MAX)
{//Note: Generates a message. See MsSet().
  if (!(0<=x<GR_WIDTH))
    x=ms.pos.x;
  if (!(0<=y<GR_HEIGHT))
    y=ms.pos.y;
  if (z==I64_MAX)
    z=ms.pos.z;

  if (!(FALSE<=l<=TRUE))
    l=ms.lb;
  if (!(FALSE<=r<=TRUE))
    r=ms.rb;

  x=(x-ms.offset.x)/ms.scale.x;
  y=(y-ms.offset.y)/ms.scale.y;
  z=(z-ms.offset.z)/ms.scale.z;
  MsUpdate(x,y,z,l,r);
  MsHardSet(x,y,z,l,r);
}

U0 MsInit()
{
  MemSet(&ms,0,sizeof(CMsStateGlbls));
  MemSet(&ms_last,0,sizeof(CMsStateGlbls));
  ms.offset.x=ms.offset.y=ms.offset.z=0;
  ms.scale.x=ms.scale.y=ms.scale.z=1.0;
  ms.pos_text.x=ms.pos_text.y=ms.pos_text.z=0;
  ms.has_wheel=FALSE;
  ms.show=TRUE;
  ms.speed=0;
  ms.timestamp=GetTSC;
  ms.dbl_time=0.350;
  GridInit;
}

U0 MsHardPktRead()
{
  U8 j;
  if (GetTSC>ms_hard.timestamp+cnts.time_stamp_freq>>3)
    FifoU8Flush(ms_hard.fifo);
  ms_hard.timestamp=GetTSC;
  FifoU8Ins(ms_hard.fifo,InU8(KBD_PORT));
  if (FifoU8Cnt(ms_hard.fifo)==ms_hard.pkt_size)
    while (FifoU8Rem(ms_hard.fifo,&j))
      FifoU8Ins(ms_hard.fifo2,j);
}

interrupt U0 IRQMsHard()
{
  CLD
  OutU8(0xA0,0x20);
  OutU8(0x20,0x20);
  ms_hard.irqs_working=TRUE;
  if (ms_hard.install_in_progress || !ms_hard.installed) {
    kbd.rst=TRUE;
    return;
  }
  MsHardPktRead;
}

U0 MsHardGetType()
{
  I64 b;
  KbdMsCmdAck(0xF2);
  b=KbdCmdRead;
  if (b==3)
    ms_hard.has_wheel=TRUE;
  else if (b==4)
    ms_hard.has_ext_bttns=TRUE;
}

Bool MsHardRst()
{
  U8 b,*_b;
  F64 timeout;
  Bool res=FALSE;

  ms_hard.has_wheel=FALSE;
  ms_hard.has_ext_bttns=FALSE;

  if (*0x40E(U16 *)==0x9FC0) {
    _b=0x9FC00+0x30;
    *_b=1; //This enables my mouse.  It might be for one machine.
//USB DMA packets, set-up by BIOS to make legacy PS/2?
  }

  try {
    KbdCmdFlush;
    KbdCmdSend(KBD_CTRL,0xAD); //Disable Kbd
    KbdCmdSend(KBD_CTRL,0xA8); //Enable Mouse

    KbdMsCmdAck(0xFF); //Rst

    timeout=tS+10.0;
    do
      try {
        KbdCmdRead;
        timeout=0; //force exit
      } catch
        Fs->catch_except=TRUE;
    while (tS<timeout);

    try
            KbdCmdRead;
    catch
      Fs->catch_except=TRUE;

    KbdMsCmdAck(0xF3,200,0xF3,100,0xF3,80);
    MsHardGetType;
    KbdMsCmdAck(0xF3,10);
    MsHardGetType;
    KbdMsCmdAck(0xE8,0x03,0xE6,0xF3,100,0xF4);
    res=TRUE;

    //Enable IRQ 12
    KbdCmdSend(KBD_CTRL,0x20);
    b=KbdCmdRead;
    KbdCmdSend(KBD_CTRL,0x60);
    KbdCmdSend(KBD_PORT,(b|2)&~0x20);

  } catch
    Fs->catch_except=TRUE;

    //This is been added to override failure
    //because the mouse sometimes still works.
  res=TRUE;

  try
          KbdCmdSend(KBD_CTRL,0xAE); //Enable Keyboard
  catch
    Fs->catch_except=TRUE;
  if (ms_hard.has_wheel || ms_hard.has_ext_bttns)
    ms_hard.pkt_size=4;
  else
    ms_hard.pkt_size=3;
  if (!res)
    try
            KbdCmdSend(KBD_CTRL,0xA7); //Disable Mouse
    catch
      Fs->catch_except=TRUE;
  return res;
}

U0 MsHardSpeedSet()
{
  I64 dd,tmp;
  if ((dd=SqrI64(ms_hard_last.pos.x-ms_hard.pos.x)
        +SqrI64(ms_hard_last.pos.y-ms_hard.pos.y)) &&
        (tmp=ms_hard.timestamp-ms_hard_last.timestamp))
    ms_hard.speed=Sqrt(dd)*cnts.time_stamp_freq/tmp;
  ms_hard_last.timestamp=ms_hard.timestamp;
}

U0 MsHardSetPre()
{
  I64 old_timestamp=ms_hard_last.timestamp;
  MemCpy(&ms_hard_last,&ms_hard,sizeof(CMsHardStateGlbls));
  ms_hard_last.timestamp=old_timestamp;
}

U0 MsHardSetPost()
{
  I64 i;
  ms_hard.pos.x=ms_hard.prescale.x*ms_hard.scale.x*ms_grid.x_speed;
  ms_hard.pos.y=ms_hard.prescale.y*ms_hard.scale.y*ms_grid.y_speed;
  ms_hard.pos.z=ms_hard.prescale.z*ms_hard.scale.z*ms_grid.z_speed;

  i=Trunc(ms.scale.x*ms_hard.pos.x/ms_grid.x)*ms_grid.x+ms.offset.x;
//TODO ms_grid.x_offset?
  if (i<0)
    ms.offset.x-=i;
  else if (i>=GR_WIDTH)
    ms.offset.x+=GR_WIDTH-1-i;

  i=Trunc(ms.scale.y*ms_hard.pos.y/ms_grid.y)*ms_grid.y+ms.offset.y;
  if (i<0)
    ms.offset.y-=i;
  else if (i>=GR_HEIGHT)
    ms.offset.y+=GR_HEIGHT-1-i;

  if (ms_hard.pos.x!=ms_hard_last.pos.x || ms_hard.pos.y!=ms_hard_last.pos.y ||
        ms_hard.pos.z!=ms_hard_last.pos.z) {
    ms_hard.evt=TRUE;
    MsHardSpeedSet;
  } else
    for (i=0;i<5;i++)
      if (ms_hard.bttns[i]!=ms_hard_last.bttns[i]) {
        ms_hard.evt=TRUE;
        break;
      }
}

U0 MsHardHndlr()
{
  I64 i,dx,dy,dz;
  U8 ms_buf[4];

  MsHardSetPre;
  for (i=0;i<4;i++)
    ms_buf[i]=0;
  for (i=0;i<ms_hard.pkt_size;i++)
    if (!FifoU8Rem(ms_hard.fifo2,&ms_buf[i]))
      ms_buf[i]=0;

  ms_hard.bttns[0] = ms_buf[0] & 1;
  ms_hard.bttns[1] = (ms_buf[0] & 2) >> 1;
  ms_hard.bttns[2] = (ms_buf[0] & 4) >> 2;
  ms_hard.bttns[3] = (ms_buf[3] & 0x10) >> 4;
  ms_hard.bttns[4] = (ms_buf[3] & 0x20) >> 5;
  if (ms_buf[0] & 0x10)
    dx=ms_buf[1


// Source: D:\TempleOS-Projects\html\sixth-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has words

-] Reward1
  CallExtStr("JukeReward",""
  -] Reward2

    "supplications Berlin's caressed contemporary's "
    "Prudence alternatives chapels Bacchanalia's "
    "economics's countersink's samovar autopsied "
    "pile's psalmist's chucked lapels "
    "long's anchorages protestation's smokiness "

  );

U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.902;
  try {
    while (!ScanKey) {
      Play("5eCCDDqEeCsDDqCE4BG",
            "One \0 \0round \0 \0left...\n\0 \0 \0 \0 \0 \0 \0 \0");
      Play("5eCsCCeDDqEeCsDDqCE4BG",
            "One \0 \0 \0be\0 \0reft...\n\0 \0 \0 \0 \0 \0 \0 \0");
      Play("5eDGsEFEFe.E4sA5eE4sBG5eDE4G5GFG4qG",
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0"
            "Feel \0luck\0y.\n\0 \0");
      Play("5eDGsEFEFe.E4sA5eE4sBG5eDE4G5GFG4qG",
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0"
            "Squeek \0duck\0y.\n\0 \0");
    }
  } catch
    PutExcept;
  Snd;
}

Song;

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\sixth-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has words

-] Reward1
  CallExtStr("JukeReward",""
  -] Reward2

    "supplications Berlin's caressed contemporary's "
    "Prudence alternatives chapels Bacchanalia's "
    "economics's countersink's samovar autopsied "
    "pile's psalmist's chucked lapels "
    "long's anchorages protestation's smokiness "

  );

U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.902;
  try {
    while (!ScanKey) {
      Play("5eCCDDqEeCsDDqCE4BG",
            "One \0 \0round \0 \0left...\n\0 \0 \0 \0 \0 \0 \0 \0");
      Play("5eCsCCeDDqEeCsDDqCE4BG",
            "One \0 \0 \0be\0 \0reft...\n\0 \0 \0 \0 \0 \0 \0 \0");
      Play("5eDGsEFEFe.E4sA5eE4sBG5eDE4G5GFG4qG",
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0"
            "Feel \0luck\0y.\n\0 \0");
      Play("5eDGsEFEFe.E4sA5eE4sBG5eDE4G5GFG4qG",
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0"
            "Squeek \0duck\0y.\n\0 \0");
    }
  } catch
    PutExcept;
  Snd;
}

Song;


// Source: D:\TempleOS-Projects\html\Pick-Graphics-Demo.html
// Date: unknown-date
// Language: HolyC

<1>/* Graphics Not Rendered in HTML */

#define APPLES_NUM 128

I64     x[APPLES_NUM],y[APPLES_NUM],
        cur_apple,cur_dist;
Bool    trigger;

U0 DrawIt(CTask *,CDC *dc)
{
  I64 i,best_dist=I64_MAX,best_apple=0;

  dc->flags|=DCF_LOCATE_NEAREST;
  dc->cur_x=ms.pos.x;
  dc->cur_y=ms.pos.y;
  dc->cur_z=0;
  for (i=0;i<APPLES_NUM;i++) {
    Sprite3(dc,x[i],y[i],0,<1>);
    if (dc->nearest_dist<=best_dist) {
//Distance to nearest pix, squared
      //Sqrt() is expensive so we work with distance squared.
      best_dist=dc->nearest_dist;
      best_apple=i;
    }
  }
  if (trigger) {
    cur_apple=best_apple;
    cur_dist=best_dist;
    trigger=FALSE;
  }
  GrPrint(dc,FONT_WIDTH,FONT_HEIGHT,"Dist Squared:%d",cur_dist);
}

U0 Pick()
{
  I64 i,msg_code,arg1,arg2,delta_x,delta_y;
  for (i=0;i<APPLES_NUM;i++) {
    x[i]=RandU16%GR_WIDTH;
    y[i]=RandU16%(GR_HEIGHT-FONT_HEIGHT*3)+FONT_HEIGHT*3;
  }
  SettingsPush; //See SettingsPush
  AutoComplete;
  DocCursor;
  DocClear;

  trigger=FALSE;
  cur_apple=cur_dist=0;

  Fs->draw_it=&DrawIt;
  Fs->win_inhibit=WIG_TASK_DFT-WIF_SELF_FOCUS-WIF_SELF_BORDER;

  try {
    while (TRUE) {
      if (GetMsg(&arg1,&arg2,1<<MSG_KEY_DOWN|1<<MSG_MS_L_DOWN)==MSG_KEY_DOWN)
        goto pi_done;

        //Wait for window mgr to call DrawIt()
      trigger=TRUE;
      do Refresh;
      while (trigger);

      //Use scrn coordinates, not window (arg1,arg2)
      //ms.pos.x and ms.pos.y are global vars updated
      //updated by the window mgr to hold the
      //scrn coordinates of the mouse.
      delta_x=x[cur_apple]-ms.pos.x;
      delta_y=y[cur_apple]-ms.pos.y;

      do {
        msg_code=GetMsg(&arg1,&arg2,
              1<<MSG_KEY_DOWN|1<<MSG_MS_MOVE|1<<MSG_MS_L_UP);
        if (msg_code==MSG_KEY_DOWN)
          goto pi_done;
        x[cur_apple]=ms.pos.x+delta_x;
        y[cur_apple]=ms.pos.y+delta_y;
      } while (msg_code!=MSG_MS_L_UP);
    }
pi_done:
    GetMsg(,,1<<MSG_KEY_UP);
  } catch
    PutExcept;
  SettingsPop;
}

Pick;

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\Pick-Graphics-Demo.html
// Date: unknown-date
// Language: HolyC

<1>/* Graphics Not Rendered in HTML */

#define APPLES_NUM 128

I64     x[APPLES_NUM],y[APPLES_NUM],
        cur_apple,cur_dist;
Bool    trigger;

U0 DrawIt(CTask *,CDC *dc)
{
  I64 i,best_dist=I64_MAX,best_apple=0;

  dc->flags|=DCF_LOCATE_NEAREST;
  dc->cur_x=ms.pos.x;
  dc->cur_y=ms.pos.y;
  dc->cur_z=0;
  for (i=0;i<APPLES_NUM;i++) {
    Sprite3(dc,x[i],y[i],0,<1>);
    if (dc->nearest_dist<=best_dist) {
//Distance to nearest pix, squared
      //Sqrt() is expensive so we work with distance squared.
      best_dist=dc->nearest_dist;
      best_apple=i;
    }
  }
  if (trigger) {
    cur_apple=best_apple;
    cur_dist=best_dist;
    trigger=FALSE;
  }
  GrPrint(dc,FONT_WIDTH,FONT_HEIGHT,"Dist Squared:%d",cur_dist);
}

U0 Pick()
{
  I64 i,msg_code,arg1,arg2,delta_x,delta_y;
  for (i=0;i<APPLES_NUM;i++) {
    x[i]=RandU16%GR_WIDTH;
    y[i]=RandU16%(GR_HEIGHT-FONT_HEIGHT*3)+FONT_HEIGHT*3;
  }
  SettingsPush; //See SettingsPush
  AutoComplete;
  DocCursor;
  DocClear;

  trigger=FALSE;
  cur_apple=cur_dist=0;

  Fs->draw_it=&DrawIt;
  Fs->win_inhibit=WIG_TASK_DFT-WIF_SELF_FOCUS-WIF_SELF_BORDER;

  try {
    while (TRUE) {
      if (GetMsg(&arg1,&arg2,1<<MSG_KEY_DOWN|1<<MSG_MS_L_DOWN)==MSG_KEY_DOWN)
        goto pi_done;

        //Wait for window mgr to call DrawIt()
      trigger=TRUE;
      do Refresh;
      while (trigger);

      //Use scrn coordinates, not window (arg1,arg2)
      //ms.pos.x and ms.pos.y are global vars updated
      //updated by the window mgr to hold the
      //scrn coordinates of the mouse.
      delta_x=x[cur_apple]-ms.pos.x;
      delta_y=y[cur_apple]-ms.pos.y;

      do {
        msg_code=GetMsg(&arg1,&arg2,
              1<<MSG_KEY_DOWN|1<<MSG_MS_MOVE|1<<MSG_MS_L_UP);
        if (msg_code==MSG_KEY_DOWN)
          goto pi_done;
        x[cur_apple]=ms.pos.x+delta_x;
        y[cur_apple]=ms.pos.y+delta_y;
      } while (msg_code!=MSG_MS_L_UP);
    }
pi_done:
    GetMsg(,,1<<MSG_KEY_UP);
  } catch
    PutExcept;
  SettingsPop;
}

Pick;


// Source: D:\TempleOS-Projects\html\SpriteCode-Gr-Adam.html
// Date: unknown-date
// Language: HolyC

#help_index "Graphics/Sprite;Sprites"

U0 SpriteElem2Code(CDoc *doc,CSprite *tmpg)
{
  U8 buf1[STR_LEN],buf2[STR_LEN];
  I32 *ptr;
  I64 i,j,k,col,width_internal;
  CD3I32 *p;
  CMeshTri *tri;
  if (!doc) doc=DocPut;
  DocPrint(doc,"%Z",tmpg->type&SPG_TYPE_MASK,"ST_SPRITE_ELEM_CODES");
  switch (tmpg->type&SPG_TYPE_MASK) {
    case SPT_COLOR:
      DocPrint(doc,"{%s}",
            Color2Str(buf1,tmpg->c.color));
      break;
    case SPT_DITHER_COLOR:
      DocPrint(doc,"{%s}",
            Color2Str(buf2,ROPF_DITHER|tmpg->d.dither_color.u8[0]|
            tmpg->d.dither_color.u8[1]<<COLORROP_BITS));
      break;
    case SPT_TRANSFORM_ON:
      DocPrint(doc,"{ON}");
      break;
    case SPT_TRANSFORM_OFF:
      DocPrint(doc,"{OFF}");
      break;
    case SPT_LINE:
    case SPT_ARROW:
    case SPT_PLANAR_SYMMETRY:
      DocPrint(doc,"{(%d,%d),(%d,%d)}",tmpg->pp.x1,tmpg->pp.y1,
            tmpg->pp.x2,tmpg->pp.y2);
      break;
    case SPT_RECT:
      DocPrint(doc,"{(%d,%d):(%d,%d)}",tmpg->pp.x1,tmpg->pp.y1,
            tmpg->pp.x2-tmpg->pp.x1,tmpg->pp.y2-tmpg->pp.y1);
      break;
    case SPT_ROTATED_RECT:
      DocPrint(doc,"{(%d,%d):(%d,%d),%0.4f}",tmpg->ppa.x1,tmpg->ppa.y1,
            tmpg->ppa.x2-tmpg->ppa.x1,tmpg->ppa.y2-tmpg->ppa.y1,
            180/pi*Wrap(tmpg->ppa.angle));
      break;
    case SPT_PT:
    case SPT_FLOOD_FILL:
    case SPT_SHIFT:
      DocPrint(doc,"{(%d,%d)}",tmpg->p.x1,tmpg->p.y1);
      break;
    case SPT_FLOOD_FILL_NOT:
      DocPrint(doc,"{(%d,%d),TRUE}",tmpg->p.x1,tmpg->p.y1);
      break;
    case SPT_CIRCLE:
      DocPrint(doc,"{(%d,%d):%d}",tmpg->pr.x1,tmpg->pr.y1,tmpg->pr.radius);
      break;
    case SPT_THICK:
      DocPrint(doc,"{%d}",tmpg->t.thick);
      break;
    case SPT_ELLIPSE:
      DocPrint(doc,"{(%d,%d):(%d,%d),%0.4f}",tmpg->pwha.x1,tmpg->pwha.y1,
            tmpg->pwha.width,tmpg->pwha.height,180/pi*Wrap(tmpg->pwha.angle));
      break;
    case SPT_POLYGON:
      DocPrint(doc,"{%d,(%d,%d):(%d,%d),%0.4f}",tmpg->pwhas.sides,
            tmpg->pwhas.x1,tmpg->pwhas.y1,
            tmpg->pwhas.width,tmpg->pwhas.height,
            180/pi*Wrap(tmpg->pwhas.angle));
      break;
    case SPT_TEXT:
    case SPT_TEXT_BOX:
    case SPT_TEXT_DIAMOND:
      DocPrint(doc,"{(%d,%d),\"%Q\"}",tmpg->ps.x1,tmpg->ps.y1,tmpg->ps.st);
      break;
    case SPT_POLYLINE:
      ptr=&tmpg->nu.u;
      DocPrint(doc,"{");
      for (i=0;i<tmpg->nu.num;i++,ptr+=2) {
        DocPrint(doc,"(%d,%d)",ptr[0],ptr[1]);
        if (i+1<tmpg->nu.num) DocPrint(doc,",");
        if (i&3==3 && i+1<tmpg->nu.num)
          DocPrint(doc,"\n");
      }
      DocPrint(doc,"}");
      break;
    case SPT_BSPLINE2:
    case SPT_BSPLINE3:
    case SPT_BSPLINE2_CLOSED:
    case SPT_BSPLINE3_CLOSED:
      ptr=&tmpg->nu.u;
      DocPrint(doc,"{");
      for (i=0;i<tmpg->nu.num;i++,ptr+=3) {
        DocPrint(doc,"(%d,%d,%d)",ptr[0],ptr[1],ptr[2]);
        if (i+1<tmpg->nu.num) DocPrint(doc,",");
        if (i&3==3 && i+1<tmpg->nu.num)
          DocPrint(doc,"\n");
      }
      if (tmpg->type&SPG_TYPE_MASK==SPT_BSPLINE2||
            tmpg->type&SPG_TYPE_MASK==SPT_BSPLINE3)
        DocPrint(doc,",FALSE}");
      else
        DocPrint(doc,",TRUE}");
      break;
    case SPT_POLYPT:
      DocPrint(doc,"{(%d,%d),",tmpg->npu.x,tmpg->npu.y);
      ptr=&tmpg->npu.u;
      col=16;
      for (i=0;i<tmpg->npu.num;i++) {
        DocPrint(doc,"%d",BFieldExtU32(ptr,i*3,3));
        if (++col>=64 && i+1<tmpg->npu.num) {
          DocPrint(doc,"\n");
          col=0;
        }
      }
      DocPrint(doc,"}");
      break;
    case SPT_BITMAP:
      DocPrint(doc,"{(%d,%d):(%d,%d),\n",tmpg->pwhu.x1,tmpg->pwhu.y1,
            tmpg->pwhu.width,tmpg->pwhu.height);
      width_internal=(tmpg->pwhu.width+7)&~7;
      if (width_internal<80)
        k=width_internal;
      else
        k=64;
      ptr=&tmpg->pwhu.u;
      col=0;
      for (j=0;j<tmpg->pwhu.height;j++)
        for (i=0;i<width_internal;i++,ptr(U8 *)++) {
          if (i>=tmpg->pwhu.width)
            DocPrint(doc,"_");
          else if (*ptr(U8 *)<16)
            DocPrint(doc,"%X",*ptr(U8 *));
          else
            DocPrint(doc,"%c",CH_SHIFT_SPACE);
          if (++col>=k && (i+1<width_internal||j+1<tmpg->pwhu.height)) {
            DocPrint(doc,"\n");
            col=0;
          }
        }
      DocPrint(doc,"}");
      break;
    case SPT_MESH:
      DocPrint(doc,"{FALSE,");
      p=&tmpg->mu.u;
      col=0;
      for (i=0;i<tmpg->mu.vertex_cnt;i++,p++) {
        DocPrint(doc,"(%d,%d,%d)",p->x,p->y,p->z);
        if (i+1<tmpg->mu.vertex_cnt) DocPrint(doc,",");
        if (++col==4) {
          DocPrint(doc,"\t//%d\n",i);
          col=0;
        }
      }
      DocPrint(doc,":");
      tri=p;
      for (i=0;i<tmpg->mu.tri_cnt;i++,tri++) {
        DocPrint(doc,"(%s,%d,%d,%d)",Color2Str(buf1,tri->color),
              tri->nums[0],tri->nums[1],tri->nums[2]);
        if (i+1<tmpg->mu.tri_cnt) DocPrint(doc,",");
        if (++col>=3 && i+1<tmpg->mu.tri_cnt) {
          DocPrint(doc,"\n");
          col=0;
        }
      }
      DocPrint(doc,"}");
      break;
    case SPT_SHIFTABLE_MESH:
      DocPrint(doc,"{TRUE,(%d,%d,%d):",tmpg->pmu.x,tmpg->pmu.y,tmpg->pmu.z);
      p=&tmpg->pmu.u;
      col=1;
      for (i=0;i<tmpg->pmu.vertex_cnt;i++,p++) {
        DocPrint(doc,"(%d,%d,%d)",p->x,p->y,p->z);
        if (i+1<tmpg->pmu.vertex_cnt) DocPrint(doc,",");
        if (++col==4) {
          DocPrint(doc,"\t//%d\n",i);
          col=0;
        }
      }
      DocPrint(doc,":");
      tri=p;
      for (i=0;i<tmpg->pmu.tri_cnt;i++,tri++) {
        DocPrint(doc,"(%s,%d,%d,%d)",Color2Str(buf1,tri->color),
              tri->nums[0],tri->nums[1],tri->nums[2]);
        if (i+1<tmpg->pmu.tri_cnt) DocPrint(doc,",");
        if (++col>=3 && i+1<tmpg->pmu.tri_cnt) {
          DocPrint(doc,"\n");
          col=0;
        }
      }
      DocPrint(doc,"}");
      break;
  }
    DocPrint(doc,";\n");
}

public U0 Sprite2Code(CDoc *doc=NULL,U8 *elems)
{//Sprite to text.
  CSprite *tmpg=elems-off

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\SpriteCode-Gr-Adam.html
// Date: unknown-date
// Language: HolyC

#help_index "Graphics/Sprite;Sprites"

U0 SpriteElem2Code(CDoc *doc,CSprite *tmpg)
{
  U8 buf1[STR_LEN],buf2[STR_LEN];
  I32 *ptr;
  I64 i,j,k,col,width_internal;
  CD3I32 *p;
  CMeshTri *tri;
  if (!doc) doc=DocPut;
  DocPrint(doc,"%Z",tmpg->type&SPG_TYPE_MASK,"ST_SPRITE_ELEM_CODES");
  switch (tmpg->type&SPG_TYPE_MASK) {
    case SPT_COLOR:
      DocPrint(doc,"{%s}",
            Color2Str(buf1,tmpg->c.color));
      break;
    case SPT_DITHER_COLOR:
      DocPrint(doc,"{%s}",
            Color2Str(buf2,ROPF_DITHER|tmpg->d.dither_color.u8[0]|
            tmpg->d.dither_color.u8[1]<<COLORROP_BITS));
      break;
    case SPT_TRANSFORM_ON:
      DocPrint(doc,"{ON}");
      break;
    case SPT_TRANSFORM_OFF:
      DocPrint(doc,"{OFF}");
      break;
    case SPT_LINE:
    case SPT_ARROW:
    case SPT_PLANAR_SYMMETRY:
      DocPrint(doc,"{(%d,%d),(%d,%d)}",tmpg->pp.x1,tmpg->pp.y1,
            tmpg->pp.x2,tmpg->pp.y2);
      break;
    case SPT_RECT:
      DocPrint(doc,"{(%d,%d):(%d,%d)}",tmpg->pp.x1,tmpg->pp.y1,
            tmpg->pp.x2-tmpg->pp.x1,tmpg->pp.y2-tmpg->pp.y1);
      break;
    case SPT_ROTATED_RECT:
      DocPrint(doc,"{(%d,%d):(%d,%d),%0.4f}",tmpg->ppa.x1,tmpg->ppa.y1,
            tmpg->ppa.x2-tmpg->ppa.x1,tmpg->ppa.y2-tmpg->ppa.y1,
            180/pi*Wrap(tmpg->ppa.angle));
      break;
    case SPT_PT:
    case SPT_FLOOD_FILL:
    case SPT_SHIFT:
      DocPrint(doc,"{(%d,%d)}",tmpg->p.x1,tmpg->p.y1);
      break;
    case SPT_FLOOD_FILL_NOT:
      DocPrint(doc,"{(%d,%d),TRUE}",tmpg->p.x1,tmpg->p.y1);
      break;
    case SPT_CIRCLE:
      DocPrint(doc,"{(%d,%d):%d}",tmpg->pr.x1,tmpg->pr.y1,tmpg->pr.radius);
      break;
    case SPT_THICK:
      DocPrint(doc,"{%d}",tmpg->t.thick);
      break;
    case SPT_ELLIPSE:
      DocPrint(doc,"{(%d,%d):(%d,%d),%0.4f}",tmpg->pwha.x1,tmpg->pwha.y1,
            tmpg->pwha.width,tmpg->pwha.height,180/pi*Wrap(tmpg->pwha.angle));
      break;
    case SPT_POLYGON:
      DocPrint(doc,"{%d,(%d,%d):(%d,%d),%0.4f}",tmpg->pwhas.sides,
            tmpg->pwhas.x1,tmpg->pwhas.y1,
            tmpg->pwhas.width,tmpg->pwhas.height,
            180/pi*Wrap(tmpg->pwhas.angle));
      break;
    case SPT_TEXT:
    case SPT_TEXT_BOX:
    case SPT_TEXT_DIAMOND:
      DocPrint(doc,"{(%d,%d),\"%Q\"}",tmpg->ps.x1,tmpg->ps.y1,tmpg->ps.st);
      break;
    case SPT_POLYLINE:
      ptr=&tmpg->nu.u;
      DocPrint(doc,"{");
      for (i=0;i<tmpg->nu.num;i++,ptr+=2) {
        DocPrint(doc,"(%d,%d)",ptr[0],ptr[1]);
        if (i+1<tmpg->nu.num) DocPrint(doc,",");
        if (i&3==3 && i+1<tmpg->nu.num)
          DocPrint(doc,"\n");
      }
      DocPrint(doc,"}");
      break;
    case SPT_BSPLINE2:
    case SPT_BSPLINE3:
    case SPT_BSPLINE2_CLOSED:
    case SPT_BSPLINE3_CLOSED:
      ptr=&tmpg->nu.u;
      DocPrint(doc,"{");
      for (i=0;i<tmpg->nu.num;i++,ptr+=3) {
        DocPrint(doc,"(%d,%d,%d)",ptr[0],ptr[1],ptr[2]);
        if (i+1<tmpg->nu.num) DocPrint(doc,",");
        if (i&3==3 && i+1<tmpg->nu.num)
          DocPrint(doc,"\n");
      }
      if (tmpg->type&SPG_TYPE_MASK==SPT_BSPLINE2||
            tmpg->type&SPG_TYPE_MASK==SPT_BSPLINE3)
        DocPrint(doc,",FALSE}");
      else
        DocPrint(doc,",TRUE}");
      break;
    case SPT_POLYPT:
      DocPrint(doc,"{(%d,%d),",tmpg->npu.x,tmpg->npu.y);
      ptr=&tmpg->npu.u;
      col=16;
      for (i=0;i<tmpg->npu.num;i++) {
        DocPrint(doc,"%d",BFieldExtU32(ptr,i*3,3));
        if (++col>=64 && i+1<tmpg->npu.num) {
          DocPrint(doc,"\n");
          col=0;
        }
      }
      DocPrint(doc,"}");
      break;
    case SPT_BITMAP:
      DocPrint(doc,"{(%d,%d):(%d,%d),\n",tmpg->pwhu.x1,tmpg->pwhu.y1,
            tmpg->pwhu.width,tmpg->pwhu.height);
      width_internal=(tmpg->pwhu.width+7)&~7;
      if (width_internal<80)
        k=width_internal;
      else
        k=64;
      ptr=&tmpg->pwhu.u;
      col=0;
      for (j=0;j<tmpg->pwhu.height;j++)
        for (i=0;i<width_internal;i++,ptr(U8 *)++) {
          if (i>=tmpg->pwhu.width)
            DocPrint(doc,"_");
          else if (*ptr(U8 *)<16)
            DocPrint(doc,"%X",*ptr(U8 *));
          else
            DocPrint(doc,"%c",CH_SHIFT_SPACE);
          if (++col>=k && (i+1<width_internal||j+1<tmpg->pwhu.height)) {
            DocPrint(doc,"\n");
            col=0;
          }
        }
      DocPrint(doc,"}");
      break;
    case SPT_MESH:
      DocPrint(doc,"{FALSE,");
      p=&tmpg->mu.u;
      col=0;
      for (i=0;i<tmpg->mu.vertex_cnt;i++,p++) {
        DocPrint(doc,"(%d,%d,%d)",p->x,p->y,p->z);
        if (i+1<tmpg->mu.vertex_cnt) DocPrint(doc,",");
        if (++col==4) {
          DocPrint(doc,"\t//%d\n",i);
          col=0;
        }
      }
      DocPrint(doc,":");
      tri=p;
      for (i=0;i<tmpg->mu.tri_cnt;i++,tri++) {
        DocPrint(doc,"(%s,%d,%d,%d)",Color2Str(buf1,tri->color),
              tri->nums[0],tri->nums[1],tri->nums[2]);
        if (i+1<tmpg->mu.tri_cnt) DocPrint(doc,",");
        if (++col>=3 && i+1<tmpg->mu.tri_cnt) {
          DocPrint(doc,"\n");
          col=0;
        }
      }
      DocPrint(doc,"}");
      break;
    case SPT_SHIFTABLE_MESH:
      DocPrint(doc,"{TRUE,(%d,%d,%d):",tmpg->pmu.x,tmpg->pmu.y,tmpg->pmu.z);
      p=&tmpg->pmu.u;
      col=1;
      for (i=0;i<tmpg->pmu.vertex_cnt;i++,p++) {
        DocPrint(doc,"(%d,%d,%d)",p->x,p->y,p->z);
        if (i+1<tmpg->pmu.vertex_cnt) DocPrint(doc,",");
        if (++col==4) {
          DocPrint(doc,"\t//%d\n",i);
          col=0;
        }
      }
      DocPrint(doc,":");
      tri=p;
      for (i=0;i<tmpg->pmu.tri_cnt;i++,tri++) {
        DocPrint(doc,"(%s,%d,%d,%d)",Color2Str(buf1,tri->color),
              tri->nums[0],tri->nums[1],tri->nums[2]);
        if (i+1<tmpg->pmu.tri_cnt) DocPrint(doc,",");
        if (++col>=3 && i+1<tmpg->pmu.tri_cnt) {
          DocPrint(doc,"\n");
          col=0;
        }
      }
      DocPrint(doc,"}");
      break;
  }
    DocPrint(doc,";\n");
}

public U0 Sprite2Code(CDoc *doc=NULL,U8 *elems)
{//Sprite to text.
  CSprite *tmpg=elems-off


// Source: D:\TempleOS-Projects\html\Squirt-Games-Demo.html
// Date: unknown-date
// Language: HolyC

#define MT_HOSE         1
#define MT_DROPLET      2
class MyMass:CMass
{
  I64 type;
  F64 radius;
};

#define ST_HOSE         1
class MySpring:CSpring
{
  I64 type;
};

        <1>/* Graphics Not Rendered in HTML */

        <2>/* Graphics Not Rendered in HTML */

        <3>/* Graphics Not Rendered in HTML */

/* Graphics Not Rendered in HTML */

#define HOSE_RADIUS     3
#define LINK_SIZE       6
#define NOZZLE_START_Y  (GR_HEIGHT-15*FONT_HEIGHT)
#define NOZZLE_LEN      18
#define FAUCET_X        (5*HOSE_RADIUS)
#define FAUCET_Y        (GR_HEIGHT-12*FONT_HEIGHT)
#define GROUND_Y        (GR_HEIGHT-3*FONT_HEIGHT)
MyMass *faucet,*nozzle;
F64 nozzle_theta;

CMathODE *ode=NULL;
F64 start_up_timeout;

U0 DrawIt(CTask *,CDC *dc)
{
  Bool first;
  F64 dx,dy,d;
  I64  x1b,y1b,x2b,y2b,
        x1a,y1a,x2a,y2a;
  MyMass   *tmpm,*tmpm1;
  MySpring *tmps;
  CD3I32 poly[4];

  Sprite3(dc,0,GROUND_Y,0,<4>);
  if (start_up_timeout>tS) {
    ode->drag_v2=0.01; //Let hose settle during start-up
    ode->drag_v3=0.0001;
    dc->color=RED;
    GrPrint(dc,(GR_WIDTH-FONT_WIDTH*6)>>1,GR_HEIGHT>>1,"Squirt");
    return;
  } else {
    ode->drag_v2=0.0005;
    ode->drag_v3=0.0000025;
  }

  tmpm=faucet;
  dc->color=BLACK;
  GrRect(dc,tmpm->x+8,tmpm->y,8,GROUND_Y-FAUCET_Y);
  Sprite3(dc,tmpm->x,tmpm->y,0,<1>);
  dc->color=BLACK;
  GrCircle(dc,tmpm->x,tmpm->y,tmpm->radius);
  dc->color=GREEN;
  GrFloodFill(dc,tmpm->x,tmpm->y);

  tmpm=nozzle;
  tmpm1=nozzle->last;
  dx=tmpm->x-tmpm1->x;
  dy=tmpm->y-tmpm1->y;
  nozzle_theta=Wrap(Arg(dx,dy));
  Sprite3ZB(dc,tmpm->x,tmpm->y,0,<2>,nozzle_theta);
  dc->color=BLACK;
  GrCircle(dc,tmpm->x,tmpm->y,tmpm->radius);
  dc->color=GREEN;
  GrFloodFill(dc,tmpm->x,tmpm->y);

  first=TRUE;
  tmpm=ode->next_mass;
  while (tmpm!=&ode->next_mass) {
    if (tmpm->type==MT_HOSE) {
      tmpm1=tmpm->last;
      dx=tmpm->x-tmpm1->x;
      dy=tmpm->y-tmpm1->y;
      d=HOSE_RADIUS/Max(Sqrt(dx*dx+dy*dy),0.001);
      dx*=d;
      dy*=d;
      x2a=tmpm->x-dy;
      y2a=tmpm->y+dx;
      x2b=tmpm->x+dy;
      y2b=tmpm->y-dx;

      if (first)
        first=FALSE;
      else {
        dc->color=GREEN;
        poly[0].x=x1a;
        poly[0].y=y1a;
        poly[0].z=0;
        poly[1].x=x2a;
        poly[1].y=y2a;
        poly[1].z=0;
        poly[2].x=x2b;
        poly[2].y=y2b;
        poly[2].z=0;
        poly[3].x=x1b;
        poly[3].y=y1b;
        poly[3].z=0;
        GrFillPoly3(dc,4,poly);
      }

      //Fill gaps
      GrLine(dc,x2a,y2a,x2b,y2b);

      x1a=x2a;
      y1a=y2a;
      x1b=x2b;
      y1b=y2b;
    } else if (tmpm->type==MT_DROPLET)
      Sprite3(dc,tmpm->x,tmpm->y,0,<3>);
    tmpm=tmpm->next;
  }

  tmps=ode->next_spring;
  while (tmps!=&ode->next_spring) {
    if (tmps->type==ST_HOSE) {
      dx=tmps->end1->x-tmps->end2->x;
      dy=tmps->end1->y-tmps->end2->y;
      d=HOSE_RADIUS/Max(Sqrt(dx*dx+dy*dy),0.001);
      dx*=d;
      dy*=d;
      dc->color=BLACK;
      GrLine(dc,tmps->end1->x-dy,tmps->end1->y+dx,
            tmps->end2->x-dy,tmps->end2->y+dx);
      GrLine(dc,tmps->end1->x+dy,tmps->end1->y-dx,
            tmps->end2->x+dy,tmps->end2->y-dx);
    }
    tmps=tmps->next;
  }
}

U0 MyDerivative(CMathODE *ode,F64 t,COrder2D3 *state,COrder2D3 *DstateDt)
{//The forces due to springs and drag are
//automatically handled by the
  //ode code.  We can add new forces
  //here.
  no_warn t,state,DstateDt;
  MyMass *tmpm1=ode->next_mass;
  while (tmpm1!=&ode->next_mass) {
    if (tmpm1->type==MT_HOSE) {
      if (tmpm1->state->y+tmpm1->radius>GROUND_Y)
        tmpm1->DstateDt->DyDt-=Sqr(Sqr(tmpm1->state->y+
              tmpm1->radius-GROUND_Y))*tmpm1->mass;
      else
        tmpm1->DstateDt->DyDt+=500*tmpm1->mass;
      if (tmpm1==nozzle || tmpm1==faucet) {
        tmpm1->DstateDt->DxDt=0;
        tmpm1->DstateDt->DyDt=0;
      }
    } else if (tmpm1->type==MT_DROPLET)
      tmpm1->DstateDt->DyDt=500*tmpm1->mass;
    tmpm1=tmpm1->next;
  }
}

MyMass *PlaceMass(I64 type,I64 x, I64 y,F64 r,
        F64 dx,F64 dy,F64 mass,CTask *mem_task)
{
  MyMass *tmpm=CAlloc(sizeof(MyMass),mem_task);
  tmpm->type=type;
  tmpm->mass=mass;
  tmpm->drag_profile_factor=250.0;
  tmpm->x=x;
  tmpm->y=y;
  tmpm->DxDt=dx;
  tmpm->DyDt=dy;
  tmpm->radius=r;
  QueIns(tmpm,ode->last_mass);
  return tmpm;
}

MySpring PlaceSpring(MyMass *tmpm1,MyMass *tmpm2)
{
  MySpring *tmps=CAlloc(sizeof(MySpring));
  tmps->end1=tmpm1;
  tmps->end2=tmpm2;
  tmps->const=20000;
  QueIns(tmps,ode->last_spring);
  return tmps;
}

U0 HoseNew()
{
  I64 i;
  MyMass *tmpm1=NULL,*tmpm;
  MySpring *tmps;
  for (i=FAUCET_X;i<GR_WIDTH;i+=LINK_SIZE) {
    tmpm=PlaceMass(MT_HOSE,i/2,GROUND_Y-HOSE_RADIUS,HOSE_RADIUS,0,0,1.0,Fs);
    if (tmpm1) {
      tmps=PlaceSpring(tmpm,tmpm1);
      tmps->rest_len=LINK_SIZE;
      tmps->type=ST_HOSE;
      nozzle=tmpm;
    } else
      faucet=tmpm;
    tmpm1=tmpm;
  }
  faucet->y=FAUCET_Y;
  nozzle->y=NOZZLE_START_Y;
  nozzle_theta=0;
}

U0 AnimateTask(I64)
{
  MyMass   *tmpm,*tmpm1;
  F64 dx,dy;
  while (TRUE) {
    dx=Cos(nozzle_theta),
          dy=Sin(nozzle_theta),
          PlaceMass(MT_DROPLET,
          nozzle->x+NOZZLE_LEN*dx,nozzle->y+NOZZLE_LEN*dy,HOSE_RADIUS,
          500*dx,500*dy,100.0,Fs->parent_task);
    if (Rand<0.05) //faucet drip
      PlaceMass(MT_DROPLET,
            faucet->x,faucet->y,HOSE_RADIUS,
            0,0,100.0,Fs->parent_task);

    tmpm=ode->next_mass;
    while (tmpm!=&ode->next_mass) {
      tmpm1=tmpm->next;
      if (tmpm->type==MT_DROPLET && tmpm->y+tmpm->radius>GROUND_Y) {
        QueRem(tmpm);
        Free(tmpm);
      }
      tmpm=tmpm1;
    }
    Refresh;
  }
}

#define NOZZLE_MOVE_STEPS       5
#define NOZZLE_MOVE             15.0
U0 MoveNozzleTaskX(I64 sign)
{
  I64 i;
  for (i=0;i<NOZZLE_MOVE_STEPS;i++) {
    nozzle->x=Clamp(nozzle->x+sign*NOZZLE_MOVE/NOZZLE_MOVE_STEPS,
          HOSE_RADIUS*3,GR_WIDTH-HOSE_RADIUS*3);
    Refresh;
  }
}

U0 MoveNozzleTaskY(I64 sign)
{
  I64 i;
  for (i=0;i<NOZZLE_MOVE_STEPS;i++) {
    nozzle->y=Clamp(nozzle->y+sign*NOZZLE_MOVE/NOZZLE_

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\Squirt-Games-Demo.html
// Date: unknown-date
// Language: HolyC

#define MT_HOSE         1
#define MT_DROPLET      2
class MyMass:CMass
{
  I64 type;
  F64 radius;
};

#define ST_HOSE         1
class MySpring:CSpring
{
  I64 type;
};

        <1>/* Graphics Not Rendered in HTML */

        <2>/* Graphics Not Rendered in HTML */

        <3>/* Graphics Not Rendered in HTML */

/* Graphics Not Rendered in HTML */

#define HOSE_RADIUS     3
#define LINK_SIZE       6
#define NOZZLE_START_Y  (GR_HEIGHT-15*FONT_HEIGHT)
#define NOZZLE_LEN      18
#define FAUCET_X        (5*HOSE_RADIUS)
#define FAUCET_Y        (GR_HEIGHT-12*FONT_HEIGHT)
#define GROUND_Y        (GR_HEIGHT-3*FONT_HEIGHT)
MyMass *faucet,*nozzle;
F64 nozzle_theta;

CMathODE *ode=NULL;
F64 start_up_timeout;

U0 DrawIt(CTask *,CDC *dc)
{
  Bool first;
  F64 dx,dy,d;
  I64  x1b,y1b,x2b,y2b,
        x1a,y1a,x2a,y2a;
  MyMass   *tmpm,*tmpm1;
  MySpring *tmps;
  CD3I32 poly[4];

  Sprite3(dc,0,GROUND_Y,0,<4>);
  if (start_up_timeout>tS) {
    ode->drag_v2=0.01; //Let hose settle during start-up
    ode->drag_v3=0.0001;
    dc->color=RED;
    GrPrint(dc,(GR_WIDTH-FONT_WIDTH*6)>>1,GR_HEIGHT>>1,"Squirt");
    return;
  } else {
    ode->drag_v2=0.0005;
    ode->drag_v3=0.0000025;
  }

  tmpm=faucet;
  dc->color=BLACK;
  GrRect(dc,tmpm->x+8,tmpm->y,8,GROUND_Y-FAUCET_Y);
  Sprite3(dc,tmpm->x,tmpm->y,0,<1>);
  dc->color=BLACK;
  GrCircle(dc,tmpm->x,tmpm->y,tmpm->radius);
  dc->color=GREEN;
  GrFloodFill(dc,tmpm->x,tmpm->y);

  tmpm=nozzle;
  tmpm1=nozzle->last;
  dx=tmpm->x-tmpm1->x;
  dy=tmpm->y-tmpm1->y;
  nozzle_theta=Wrap(Arg(dx,dy));
  Sprite3ZB(dc,tmpm->x,tmpm->y,0,<2>,nozzle_theta);
  dc->color=BLACK;
  GrCircle(dc,tmpm->x,tmpm->y,tmpm->radius);
  dc->color=GREEN;
  GrFloodFill(dc,tmpm->x,tmpm->y);

  first=TRUE;
  tmpm=ode->next_mass;
  while (tmpm!=&ode->next_mass) {
    if (tmpm->type==MT_HOSE) {
      tmpm1=tmpm->last;
      dx=tmpm->x-tmpm1->x;
      dy=tmpm->y-tmpm1->y;
      d=HOSE_RADIUS/Max(Sqrt(dx*dx+dy*dy),0.001);
      dx*=d;
      dy*=d;
      x2a=tmpm->x-dy;
      y2a=tmpm->y+dx;
      x2b=tmpm->x+dy;
      y2b=tmpm->y-dx;

      if (first)
        first=FALSE;
      else {
        dc->color=GREEN;
        poly[0].x=x1a;
        poly[0].y=y1a;
        poly[0].z=0;
        poly[1].x=x2a;
        poly[1].y=y2a;
        poly[1].z=0;
        poly[2].x=x2b;
        poly[2].y=y2b;
        poly[2].z=0;
        poly[3].x=x1b;
        poly[3].y=y1b;
        poly[3].z=0;
        GrFillPoly3(dc,4,poly);
      }

      //Fill gaps
      GrLine(dc,x2a,y2a,x2b,y2b);

      x1a=x2a;
      y1a=y2a;
      x1b=x2b;
      y1b=y2b;
    } else if (tmpm->type==MT_DROPLET)
      Sprite3(dc,tmpm->x,tmpm->y,0,<3>);
    tmpm=tmpm->next;
  }

  tmps=ode->next_spring;
  while (tmps!=&ode->next_spring) {
    if (tmps->type==ST_HOSE) {
      dx=tmps->end1->x-tmps->end2->x;
      dy=tmps->end1->y-tmps->end2->y;
      d=HOSE_RADIUS/Max(Sqrt(dx*dx+dy*dy),0.001);
      dx*=d;
      dy*=d;
      dc->color=BLACK;
      GrLine(dc,tmps->end1->x-dy,tmps->end1->y+dx,
            tmps->end2->x-dy,tmps->end2->y+dx);
      GrLine(dc,tmps->end1->x+dy,tmps->end1->y-dx,
            tmps->end2->x+dy,tmps->end2->y-dx);
    }
    tmps=tmps->next;
  }
}

U0 MyDerivative(CMathODE *ode,F64 t,COrder2D3 *state,COrder2D3 *DstateDt)
{//The forces due to springs and drag are
//automatically handled by the
  //ode code.  We can add new forces
  //here.
  no_warn t,state,DstateDt;
  MyMass *tmpm1=ode->next_mass;
  while (tmpm1!=&ode->next_mass) {
    if (tmpm1->type==MT_HOSE) {
      if (tmpm1->state->y+tmpm1->radius>GROUND_Y)
        tmpm1->DstateDt->DyDt-=Sqr(Sqr(tmpm1->state->y+
              tmpm1->radius-GROUND_Y))*tmpm1->mass;
      else
        tmpm1->DstateDt->DyDt+=500*tmpm1->mass;
      if (tmpm1==nozzle || tmpm1==faucet) {
        tmpm1->DstateDt->DxDt=0;
        tmpm1->DstateDt->DyDt=0;
      }
    } else if (tmpm1->type==MT_DROPLET)
      tmpm1->DstateDt->DyDt=500*tmpm1->mass;
    tmpm1=tmpm1->next;
  }
}

MyMass *PlaceMass(I64 type,I64 x, I64 y,F64 r,
        F64 dx,F64 dy,F64 mass,CTask *mem_task)
{
  MyMass *tmpm=CAlloc(sizeof(MyMass),mem_task);
  tmpm->type=type;
  tmpm->mass=mass;
  tmpm->drag_profile_factor=250.0;
  tmpm->x=x;
  tmpm->y=y;
  tmpm->DxDt=dx;
  tmpm->DyDt=dy;
  tmpm->radius=r;
  QueIns(tmpm,ode->last_mass);
  return tmpm;
}

MySpring PlaceSpring(MyMass *tmpm1,MyMass *tmpm2)
{
  MySpring *tmps=CAlloc(sizeof(MySpring));
  tmps->end1=tmpm1;
  tmps->end2=tmpm2;
  tmps->const=20000;
  QueIns(tmps,ode->last_spring);
  return tmps;
}

U0 HoseNew()
{
  I64 i;
  MyMass *tmpm1=NULL,*tmpm;
  MySpring *tmps;
  for (i=FAUCET_X;i<GR_WIDTH;i+=LINK_SIZE) {
    tmpm=PlaceMass(MT_HOSE,i/2,GROUND_Y-HOSE_RADIUS,HOSE_RADIUS,0,0,1.0,Fs);
    if (tmpm1) {
      tmps=PlaceSpring(tmpm,tmpm1);
      tmps->rest_len=LINK_SIZE;
      tmps->type=ST_HOSE;
      nozzle=tmpm;
    } else
      faucet=tmpm;
    tmpm1=tmpm;
  }
  faucet->y=FAUCET_Y;
  nozzle->y=NOZZLE_START_Y;
  nozzle_theta=0;
}

U0 AnimateTask(I64)
{
  MyMass   *tmpm,*tmpm1;
  F64 dx,dy;
  while (TRUE) {
    dx=Cos(nozzle_theta),
          dy=Sin(nozzle_theta),
          PlaceMass(MT_DROPLET,
          nozzle->x+NOZZLE_LEN*dx,nozzle->y+NOZZLE_LEN*dy,HOSE_RADIUS,
          500*dx,500*dy,100.0,Fs->parent_task);
    if (Rand<0.05) //faucet drip
      PlaceMass(MT_DROPLET,
            faucet->x,faucet->y,HOSE_RADIUS,
            0,0,100.0,Fs->parent_task);

    tmpm=ode->next_mass;
    while (tmpm!=&ode->next_mass) {
      tmpm1=tmpm->next;
      if (tmpm->type==MT_DROPLET && tmpm->y+tmpm->radius>GROUND_Y) {
        QueRem(tmpm);
        Free(tmpm);
      }
      tmpm=tmpm1;
    }
    Refresh;
  }
}

#define NOZZLE_MOVE_STEPS       5
#define NOZZLE_MOVE             15.0
U0 MoveNozzleTaskX(I64 sign)
{
  I64 i;
  for (i=0;i<NOZZLE_MOVE_STEPS;i++) {
    nozzle->x=Clamp(nozzle->x+sign*NOZZLE_MOVE/NOZZLE_MOVE_STEPS,
          HOSE_RADIUS*3,GR_WIDTH-HOSE_RADIUS*3);
    Refresh;
  }
}

U0 MoveNozzleTaskY(I64 sign)
{
  I64 i;
  for (i=0;i<NOZZLE_MOVE_STEPS;i++) {
    nozzle->y=Clamp(nozzle->y+sign*NOZZLE_MOVE/NOZZLE_


// Source: D:\TempleOS-Projects\html\WaterRock-Sup1-Sup1Games-AfterEgypt-Home.html
// Date: unknown-date
// Language: HolyC

<1>/* Graphics Not Rendered in HTML */

        <2>/* Graphics Not Rendered in HTML */

        <3>/* Graphics Not Rendered in HTML */

        <4>/* Graphics Not Rendered in HTML */

        <5>/* Graphics Not Rendered in HTML */

#define FRAMES          4
#define DOWN_TIME       0.075
#define UP_TIME         0.2
#define SPREAD_RATE     5

U8 *imgs[FRAMES]={<1>,<2>,<3>,<4>};

F64 t0_down,t0_up;
Bool down_stroke;

U0 DrawWaterRock(CTask *task,CDC *dc)
{
  I64 i,r,
        cx=task->pix_width>>1,
        cy=task->pix_height>>1;
  U8 *tmps;
  F64 tt;
  if (t0_down) {
    tt=(tS-t0_down);
    if (tt>DOWN_TIME) {
      r=SPREAD_RATE*(tt-DOWN_TIME);
      if (r>17) r=17;
      dc->color=BLUE;
      GrCircle(dc,cx-63,cy-20,r);
      if (r>=2)
        GrFloodFill(dc,cx-63,cy-20);
    }
  }
  if (down_stroke) {
    tt=(tS-t0_down)/DOWN_TIME;
    if (tt>0.9999) tt=0.9999;
  } else {
    tt=(tS-t0_up)/UP_TIME;
    if (tt>0.9999) tt=0.9999;
    tt=1.0-tt;
  }
  i=(FRAMES-1)*tt;
  tmps=SpriteInterpolate((FRAMES-1)*tt-i,imgs[i],imgs[i+1]);
  Sprite3(dc,cx,cy,0,tmps);
  Sprite3(dc,cx-64,cy-4,0,<5>);
  Free(tmps);
}

U0 WaterRock()
{
  I64 msg_code,ch,sc;

  SettingsPush(Fs,TSF_SAME_SONG); //See SettingsPush
  Fs->text_attr=YELLOW<<4+BLUE;
  try {
    DocClear;
    "$BG,YELLOW$";

    BibleVerse(,"Exodus,17:6",4);
    "\n$BK,1$<SPACE>$BK,0$\n";

    t0_down=t0_up=0;
    down_stroke=FALSE;
    Fs->draw_it=&DrawWaterRock;
    do {
      msg_code=GetMsg(&ch,&sc,1<<MSG_KEY_DOWN|1<<MSG_KEY_UP);
      if (ch==CH_SPACE) {
        if (msg_code==MSG_KEY_DOWN) {
          t0_down=tS;
          down_stroke=TRUE;
        } else if (t0_down) {//Ignore extra key_up from start-up
          t0_up=tS;
          down_stroke=FALSE;
        }
      }
    } while (msg_code!=MSG_KEY_DOWN || ch!=CH_SHIFT_ESC && ch!=CH_ESC);
    GetMsg(,,1<<MSG_KEY_UP);
  } catch
    PutExcept;
  DocClear;
  SettingsPop(Fs,TSF_SAME_SONG);
}

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\WaterRock-Sup1-Sup1Games-AfterEgypt-Home.html
// Date: unknown-date
// Language: HolyC

<1>/* Graphics Not Rendered in HTML */

        <2>/* Graphics Not Rendered in HTML */

        <3>/* Graphics Not Rendered in HTML */

        <4>/* Graphics Not Rendered in HTML */

        <5>/* Graphics Not Rendered in HTML */

#define FRAMES          4
#define DOWN_TIME       0.075
#define UP_TIME         0.2
#define SPREAD_RATE     5

U8 *imgs[FRAMES]={<1>,<2>,<3>,<4>};

F64 t0_down,t0_up;
Bool down_stroke;

U0 DrawWaterRock(CTask *task,CDC *dc)
{
  I64 i,r,
        cx=task->pix_width>>1,
        cy=task->pix_height>>1;
  U8 *tmps;
  F64 tt;
  if (t0_down) {
    tt=(tS-t0_down);
    if (tt>DOWN_TIME) {
      r=SPREAD_RATE*(tt-DOWN_TIME);
      if (r>17) r=17;
      dc->color=BLUE;
      GrCircle(dc,cx-63,cy-20,r);
      if (r>=2)
        GrFloodFill(dc,cx-63,cy-20);
    }
  }
  if (down_stroke) {
    tt=(tS-t0_down)/DOWN_TIME;
    if (tt>0.9999) tt=0.9999;
  } else {
    tt=(tS-t0_up)/UP_TIME;
    if (tt>0.9999) tt=0.9999;
    tt=1.0-tt;
  }
  i=(FRAMES-1)*tt;
  tmps=SpriteInterpolate((FRAMES-1)*tt-i,imgs[i],imgs[i+1]);
  Sprite3(dc,cx,cy,0,tmps);
  Sprite3(dc,cx-64,cy-4,0,<5>);
  Free(tmps);
}

U0 WaterRock()
{
  I64 msg_code,ch,sc;

  SettingsPush(Fs,TSF_SAME_SONG); //See SettingsPush
  Fs->text_attr=YELLOW<<4+BLUE;
  try {
    DocClear;
    "$BG,YELLOW$";

    BibleVerse(,"Exodus,17:6",4);
    "\n$BK,1$<SPACE>$BK,0$\n";

    t0_down=t0_up=0;
    down_stroke=FALSE;
    Fs->draw_it=&DrawWaterRock;
    do {
      msg_code=GetMsg(&ch,&sc,1<<MSG_KEY_DOWN|1<<MSG_KEY_UP);
      if (ch==CH_SPACE) {
        if (msg_code==MSG_KEY_DOWN) {
          t0_down=tS;
          down_stroke=TRUE;
        } else if (t0_down) {//Ignore extra key_up from start-up
          t0_up=tS;
          down_stroke=FALSE;
        }
      }
    } while (msg_code!=MSG_KEY_DOWN || ch!=CH_SHIFT_ESC && ch!=CH_ESC);
    GetMsg(,,1<<MSG_KEY_UP);
  } catch
    PutExcept;
  DocClear;
  SettingsPop(Fs,TSF_SAME_SONG);
}


// Source: D:\TempleOS-Projects\html\TreeCheckers-Games-Demo.html
// Date: unknown-date
// Language: HolyC

U0 InitDefines()
{
  I64 w=GR_WIDTH,h=GR_HEIGHT-FONT_HEIGHT,cols,rows,
        size=PopUpRangeI64(1,9,,"Size\n"),rad=16-size;
  if (size<0) throw;
  DefinePrint("UNITS_NUM","%d",4096/SqrI64(rad)&~1);
  DefinePrint("CIRCLE_RAD","%d",rad);
  DefinePrint("MOVE_CIRCLES","%d",40/rad);

  cols=(w-8-rad)/(2*rad)&~1-1;
  DefinePrint("BORDER_X","%d",(w+rad-cols*2*rad)/2);

  rows=(h-8)/(2*rad)&~1;
  DefinePrint("BORDER_Y","%d",(h-rows*2*rad)/2);

} InitDefines;

class Unit
{
  I64 num,x,y,player,link,color;
  Bool alive,king;
} u[UNITS_NUM];

CDC *map_dc;
I64 cur_player,num_alive[2],start_x,start_y,end_x,end_y;
Bool show_start;

U0 S2Circle(I64 *_x,I64 *_y)
{
  I64 i,j;
  j=(*_y-BORDER_Y)/(CIRCLE_RAD*2);
  if (j&1)
    i=(*_x-CIRCLE_RAD-BORDER_X)/(CIRCLE_RAD*2);
  else
    i=(*_x-BORDER_X)/(CIRCLE_RAD*2);
  *_y=j;
  *_x=i;
}

U0 Circle2S(I64 *_x,I64 *_y)
{
  I64 j=*_y,i=*_x;
  *_y=j*CIRCLE_RAD*2+CIRCLE_RAD+BORDER_Y;
  *_x=i*CIRCLE_RAD*2+CIRCLE_RAD+BORDER_X;
  if (j&1) *_x+=CIRCLE_RAD;
}

U0 S2W(I64 *_x,I64 *_y)
{
  S2Circle(_x,_y);
  Circle2S(_x,_y);
}

U0 DrawIt(CTask *task,CDC *dc)
{
  I64 i;
  map_dc->flags|=DCF_NO_TRANSPARENTS;
  GrBlot(dc,0,0,map_dc);

  if (cur_player==0) {
    dc->color=LTCYAN;
    GrFloodFill(dc,0,0);
    dc->color=CYAN;
  } else {
    dc->color=LTPURPLE;
    GrFloodFill(dc,0,0);
    dc->color=PURPLE;
  }
  GrPrint(dc,2,2,"Player %d",cur_player+1);

  for (i=0;i<UNITS_NUM;i++)
    if (u[i].alive) {
      dc->color=u[i].color;
      GrFloodFill(dc,u[i].x,u[i].y);
    }
  for (i=0;i<UNITS_NUM;i++)
    if (u[i].alive && !u[i].king) {
      dc->color=BLACK;
      GrLine(dc,u[i].x,u[i].y,u[u[i].link].x,u[u[i].link].y);
      if (Blink) {
        dc->color=BLUE;
        dc->thick=6;
        GrPlot3(dc,(u[i].x+u[u[i].link].x)>>1,(u[i].y+u[u[i].link].y)>>1,0);
      }
    }
  if (show_start) {
    dc->color=LTRED;
    GrLine(dc,start_x-4,start_y-4,start_x+4,start_y+4);
    GrLine(dc,start_x-4,start_y+4,start_x+4,start_y-4);
    GrLine(dc,start_x,start_y,end_x,end_y);
  }
  if ((num_alive[0]==1 || num_alive[1]==1) && Blink(4)) {
    dc->color=BLACK;
    GrPrint(dc,(task->pix_width-FONT_WIDTH*9)>>1,
          (task->pix_height-FONT_HEIGHT)>>1,"Game Over");
  }
}

Unit *UnitFind(I64 x,I64 y,I64 player)
{
  I64 i,dd,best_dd=I64_MAX;
  Unit *res=NULL;
  for (i=0;i<UNITS_NUM;i++) {
    if (u[i].player==player && u[i].alive) {
      dd=SqrI64(u[i].x-x)+SqrI64(u[i].y-y);
      if (dd<best_dd) {
        best_dd=dd;
        res=&u[i];
      }
    }
  }
  return res;
}

U0 KillsChk(I64 x1,I64 y1,I64 player)
{
  I64 i,j,x2,y2,dd;
  Bool found;
  for (i=0;i<UNITS_NUM;i++) {
    if (u[i].player!=player && u[i].alive && !u[i].king) {
      x2=(u[i].x+u[u[i].link].x)>>1;
      y2=(u[i].y+u[u[i].link].y)>>1;
      dd=SqrI64(x2-x1)+SqrI64(y2-y1);
      if (dd<=(CIRCLE_RAD+2)*(CIRCLE_RAD+2)) {
        u[i].alive=FALSE;
        Snd(62);Sleep(100);Snd;Sleep(25);
        num_alive[u[i].player]--;
        do {
          found=FALSE;
          for (j=0;j<UNITS_NUM;j++)
            if (u[j].alive && u[j].player!=player && !u[j].king &&
                  !u[u[j].link].alive) {
              found=TRUE;
              u[j].alive=FALSE;
              Snd(62);Sleep(100);Snd;Sleep(25);
              num_alive[u[j].player]--;
            }
        } while (found);
      }
    }
  }
}

Bool UnitMove(Unit *tmpu,I64 x2,I64 y2)
{
  I64 i,r,c,r2,c2,x=start_x,y=start_y;
  S2W(&x2,&y2);
  c2=x2; r2=y2;
  S2Circle(&c2,&r2);
  for (i=0;i<MOVE_CIRCLES+1;i++) {
    c=x; r=y;
    S2Circle(&c,&r);
    if (c==c2 && r==r2) {
      end_x=tmpu->x=x2;
      end_y=tmpu->y=y2;
      return TRUE;
    }
    if (r2!=r) {
      if (r&1) {
        if (c<c2) c++;
      } else {
        if (c>c2) c--;
      }
    }
    if (r2>r) {
      x=c; y=++r;
      Circle2S(&x,&y);
    } else if (r2<r) {
      x=c; y=--r;
      Circle2S(&x,&y);
    } else if (c2>c) {
      x=++c; y=r;
      Circle2S(&x,&y);
    } else if (c2<c) {
      x=--c; y=r;
      Circle2S(&x,&y);
    }
  }
  return FALSE;
}

CDC *DrawHexMap(I64 *_w,I64 *_h)
{
  CDC *dc=DCNew(*_w,*_h);
  I64 i,j,x,y;
  *_w=(dc->width-BORDER_X*2-CIRCLE_RAD)/(CIRCLE_RAD*2);
  *_h=(dc->height-BORDER_Y*2)/(CIRCLE_RAD*2);
  DCFill(dc,WHITE);
  dc->color=LTGRAY;
  for (j=0;j<*_h;j++) {
    for (i=0;i<*_w;i++) {
      x=i*CIRCLE_RAD*2+BORDER_X+CIRCLE_RAD;
      y=j*CIRCLE_RAD*2+BORDER_Y+CIRCLE_RAD;
      if (j&1) x+=CIRCLE_RAD;
      S2W(&x,&y);
      GrCircle(dc,x,y,CIRCLE_RAD);
    }
  }
  *_w*=CIRCLE_RAD*2;
  *_h*=CIRCLE_RAD*2;
  return dc;
}

U0 Init()
{
  I64 i,j,j1,j2,dd,best_dd,best,w=Fs->pix_width,h=Fs->pix_height;

  map_dc=DrawHexMap(&w,&h);
  cur_player=0;

  for (i=0;i<UNITS_NUM/2;i++) {
    u[i].num=i;
    u[i+UNITS_NUM/2].num=i+UNITS_NUM/2;

ti_restart:
    u[i].y=FloorI64(RandU32%h,CIRCLE_RAD*2)+CIRCLE_RAD;
    u[i+UNITS_NUM/2].y=h-1-u[i].y;
    j1=u[i].y/(CIRCLE_RAD*2);
    j2=u[i+UNITS_NUM/2].y/(CIRCLE_RAD*2);
    u[i].y+=BORDER_Y;
    u[i+UNITS_NUM/2].y+=BORDER_Y;

    if (!i) {
      u[i].x=CIRCLE_RAD;
      if (j1&1)
        goto ti_restart;
    } else
      u[i].x=FloorI64(RandU32%((w-CIRCLE_RAD*2*2)/2),CIRCLE_RAD*2)+CIRCLE_RAD;
    u[i+UNITS_NUM/2].x=w-1-u[i].x;

    if (j1&1)
      u[i].x+=CIRCLE_RAD;
    if (j2&1)
      u[i+UNITS_NUM/2].x+=CIRCLE_RAD;
    u[i].x+=BORDER_X;
    u[i+UNITS_NUM/2].x+=BORDER_X;

    S2W(&u[i].x,&u[i].y);
    S2W(&u[i+UNITS_NUM/2].x,&u[i+UNITS_NUM/2].y);

    u[i].player=0;
    u[i+UNITS_NUM/2].player=1;

    u[i].alive=TRUE;
    u[i+UNITS_NUM/2].alive=TRUE;

    if (!i) {
      u[i].color=LTCYAN;
      u[i+UNITS_NUM/2].color=LTPURPLE;
      u[i].king=TRUE;
      u[i+UNITS_NUM/2].king=TRUE;
    } else {
      u[i].color=CYAN;
      u[i+UNITS_NUM/2].color=PURPLE;
      u[i].king=FALSE;
      u[i+UNITS_NUM/2].king=FALSE;
    }
  }
  for (i=0;i<UNITS_NUM/2;i++) {
    if (!u[i].king) {
      best_dd=I64_MAX;
      for (j=0;j<UNITS_NUM/2;j++) {
        if (i!=j) {
          dd=SqrI64(u[i].x-u[j].x)+SqrI64(u[i].y-u[j].y);
          if ((u[j].x<u[i].x || u[j].king) && dd<best_dd ) {
            best_dd=dd;
            best=j;
          }
        }
      }
      u[i].link=best;
      u[i+UNITS_NUM/2].link=best+UNITS_NUM/2;
    }
  }
  num_alive[0]=UNITS_NUM/2;
  num_alive[1]=UNITS_NUM/2;
}

U0 CleanUp()
{
  DCDel(map_dc);
}

U0 TreeCheckers()
{
  I64 msg_code,arg1,arg2,ch,sc;
  Unit *tmpu;

  SettingsPush; //See SettingsPush
  MenuPush(
        "File {"
        "  Abort(,CH_SHIFT_ESC);"
        "  Exit(,CH_ESC);"
        "}"
        "Play {"
        "  Restart(,'\n');"
        "}"
        );
  AutoComplete;
  WinBorder;
  WinMax;
  DocCursor;
  DocClear;
  PopUpOk("Step on the link mid points.\n");
  Fs->win_inhibit=WIG_TASK_DFT-WIF_SELF_FOCUS
        -WIF_SELF_BORDER-WIF_FOCUS_TASK_MENU;
  try {
cn_start:
    Init;
    tmpu=NULL;
    show_start=FALSE;
    Fs->draw_it=&DrawIt;
    while (TRUE) {
      msg_code=GetMsg(&arg1,&arg2,
            1<<MSG_KEY_DOWN+1<<MSG_MS_L_DOWN+1<<MSG_MS_L_UP+1<<MSG_MS_MOVE);
      switch (msg_code) {
        case MSG_KEY_DOWN:
          switch (arg1) {
            case '\n':
              CleanUp;
              goto cn_start;
            case CH_ESC:
            case CH_SHIFT_ESC:
              goto cn_done;
          }
          break;
        case MSG_MS_L_DOWN:
          if (num_alive[0]>1 && num_alive[1]>1) {
            tmpu=UnitFind(arg1,arg2,cur_player);
            end_x=start_x=tmpu->x;
            end_y=start_y=tmpu->y;
            show_start=TRUE;
          }
          break;
        case MSG_MS_MOVE:
          if (tmpu)
            UnitMove(tmpu,arg1,arg2);
          break;
        case MSG_MS_L_UP:
          UnitMove(tmpu,arg1,arg2);
          KillsChk(tmpu->x,tmpu->y,cur_player);
          show_start=FALSE;
          tmpu=NULL;
          cur_player=1-cur_player;
          break;
      }
    }
cn_done:
    GetMsg(,,1<<MSG_KEY_UP);
  } catch
    PutExcept;
  SettingsPop;
  CleanUp;
  MenuPop;
}

TreeCheckers;

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\TreeCheckers-Games-Demo.html
// Date: unknown-date
// Language: HolyC

U0 InitDefines()
{
  I64 w=GR_WIDTH,h=GR_HEIGHT-FONT_HEIGHT,cols,rows,
        size=PopUpRangeI64(1,9,,"Size\n"),rad=16-size;
  if (size<0) throw;
  DefinePrint("UNITS_NUM","%d",4096/SqrI64(rad)&~1);
  DefinePrint("CIRCLE_RAD","%d",rad);
  DefinePrint("MOVE_CIRCLES","%d",40/rad);

  cols=(w-8-rad)/(2*rad)&~1-1;
  DefinePrint("BORDER_X","%d",(w+rad-cols*2*rad)/2);

  rows=(h-8)/(2*rad)&~1;
  DefinePrint("BORDER_Y","%d",(h-rows*2*rad)/2);

} InitDefines;

class Unit
{
  I64 num,x,y,player,link,color;
  Bool alive,king;
} u[UNITS_NUM];

CDC *map_dc;
I64 cur_player,num_alive[2],start_x,start_y,end_x,end_y;
Bool show_start;

U0 S2Circle(I64 *_x,I64 *_y)
{
  I64 i,j;
  j=(*_y-BORDER_Y)/(CIRCLE_RAD*2);
  if (j&1)
    i=(*_x-CIRCLE_RAD-BORDER_X)/(CIRCLE_RAD*2);
  else
    i=(*_x-BORDER_X)/(CIRCLE_RAD*2);
  *_y=j;
  *_x=i;
}

U0 Circle2S(I64 *_x,I64 *_y)
{
  I64 j=*_y,i=*_x;
  *_y=j*CIRCLE_RAD*2+CIRCLE_RAD+BORDER_Y;
  *_x=i*CIRCLE_RAD*2+CIRCLE_RAD+BORDER_X;
  if (j&1) *_x+=CIRCLE_RAD;
}

U0 S2W(I64 *_x,I64 *_y)
{
  S2Circle(_x,_y);
  Circle2S(_x,_y);
}

U0 DrawIt(CTask *task,CDC *dc)
{
  I64 i;
  map_dc->flags|=DCF_NO_TRANSPARENTS;
  GrBlot(dc,0,0,map_dc);

  if (cur_player==0) {
    dc->color=LTCYAN;
    GrFloodFill(dc,0,0);
    dc->color=CYAN;
  } else {
    dc->color=LTPURPLE;
    GrFloodFill(dc,0,0);
    dc->color=PURPLE;
  }
  GrPrint(dc,2,2,"Player %d",cur_player+1);

  for (i=0;i<UNITS_NUM;i++)
    if (u[i].alive) {
      dc->color=u[i].color;
      GrFloodFill(dc,u[i].x,u[i].y);
    }
  for (i=0;i<UNITS_NUM;i++)
    if (u[i].alive && !u[i].king) {
      dc->color=BLACK;
      GrLine(dc,u[i].x,u[i].y,u[u[i].link].x,u[u[i].link].y);
      if (Blink) {
        dc->color=BLUE;
        dc->thick=6;
        GrPlot3(dc,(u[i].x+u[u[i].link].x)>>1,(u[i].y+u[u[i].link].y)>>1,0);
      }
    }
  if (show_start) {
    dc->color=LTRED;
    GrLine(dc,start_x-4,start_y-4,start_x+4,start_y+4);
    GrLine(dc,start_x-4,start_y+4,start_x+4,start_y-4);
    GrLine(dc,start_x,start_y,end_x,end_y);
  }
  if ((num_alive[0]==1 || num_alive[1]==1) && Blink(4)) {
    dc->color=BLACK;
    GrPrint(dc,(task->pix_width-FONT_WIDTH*9)>>1,
          (task->pix_height-FONT_HEIGHT)>>1,"Game Over");
  }
}

Unit *UnitFind(I64 x,I64 y,I64 player)
{
  I64 i,dd,best_dd=I64_MAX;
  Unit *res=NULL;
  for (i=0;i<UNITS_NUM;i++) {
    if (u[i].player==player && u[i].alive) {
      dd=SqrI64(u[i].x-x)+SqrI64(u[i].y-y);
      if (dd<best_dd) {
        best_dd=dd;
        res=&u[i];
      }
    }
  }
  return res;
}

U0 KillsChk(I64 x1,I64 y1,I64 player)
{
  I64 i,j,x2,y2,dd;
  Bool found;
  for (i=0;i<UNITS_NUM;i++) {
    if (u[i].player!=player && u[i].alive && !u[i].king) {
      x2=(u[i].x+u[u[i].link].x)>>1;
      y2=(u[i].y+u[u[i].link].y)>>1;
      dd=SqrI64(x2-x1)+SqrI64(y2-y1);
      if (dd<=(CIRCLE_RAD+2)*(CIRCLE_RAD+2)) {
        u[i].alive=FALSE;
        Snd(62);Sleep(100);Snd;Sleep(25);
        num_alive[u[i].player]--;
        do {
          found=FALSE;
          for (j=0;j<UNITS_NUM;j++)
            if (u[j].alive && u[j].player!=player && !u[j].king &&
                  !u[u[j].link].alive) {
              found=TRUE;
              u[j].alive=FALSE;
              Snd(62);Sleep(100);Snd;Sleep(25);
              num_alive[u[j].player]--;
            }
        } while (found);
      }
    }
  }
}

Bool UnitMove(Unit *tmpu,I64 x2,I64 y2)
{
  I64 i,r,c,r2,c2,x=start_x,y=start_y;
  S2W(&x2,&y2);
  c2=x2; r2=y2;
  S2Circle(&c2,&r2);
  for (i=0;i<MOVE_CIRCLES+1;i++) {
    c=x; r=y;
    S2Circle(&c,&r);
    if (c==c2 && r==r2) {
      end_x=tmpu->x=x2;
      end_y=tmpu->y=y2;
      return TRUE;
    }
    if (r2!=r) {
      if (r&1) {
        if (c<c2) c++;
      } else {
        if (c>c2) c--;
      }
    }
    if (r2>r) {
      x=c; y=++r;
      Circle2S(&x,&y);
    } else if (r2<r) {
      x=c; y=--r;
      Circle2S(&x,&y);
    } else if (c2>c) {
      x=++c; y=r;
      Circle2S(&x,&y);
    } else if (c2<c) {
      x=--c; y=r;
      Circle2S(&x,&y);
    }
  }
  return FALSE;
}

CDC *DrawHexMap(I64 *_w,I64 *_h)
{
  CDC *dc=DCNew(*_w,*_h);
  I64 i,j,x,y;
  *_w=(dc->width-BORDER_X*2-CIRCLE_RAD)/(CIRCLE_RAD*2);
  *_h=(dc->height-BORDER_Y*2)/(CIRCLE_RAD*2);
  DCFill(dc,WHITE);
  dc->color=LTGRAY;
  for (j=0;j<*_h;j++) {
    for (i=0;i<*_w;i++) {
      x=i*CIRCLE_RAD*2+BORDER_X+CIRCLE_RAD;
      y=j*CIRCLE_RAD*2+BORDER_Y+CIRCLE_RAD;
      if (j&1) x+=CIRCLE_RAD;
      S2W(&x,&y);
      GrCircle(dc,x,y,CIRCLE_RAD);
    }
  }
  *_w*=CIRCLE_RAD*2;
  *_h*=CIRCLE_RAD*2;
  return dc;
}

U0 Init()
{
  I64 i,j,j1,j2,dd,best_dd,best,w=Fs->pix_width,h=Fs->pix_height;

  map_dc=DrawHexMap(&w,&h);
  cur_player=0;

  for (i=0;i<UNITS_NUM/2;i++) {
    u[i].num=i;
    u[i+UNITS_NUM/2].num=i+UNITS_NUM/2;

ti_restart:
    u[i].y=FloorI64(RandU32%h,CIRCLE_RAD*2)+CIRCLE_RAD;
    u[i+UNITS_NUM/2].y=h-1-u[i].y;
    j1=u[i].y/(CIRCLE_RAD*2);
    j2=u[i+UNITS_NUM/2].y/(CIRCLE_RAD*2);
    u[i].y+=BORDER_Y;
    u[i+UNITS_NUM/2].y+=BORDER_Y;

    if (!i) {
      u[i].x=CIRCLE_RAD;
      if (j1&1)
        goto ti_restart;
    } else
      u[i].x=FloorI64(RandU32%((w-CIRCLE_RAD*2*2)/2),CIRCLE_RAD*2)+CIRCLE_RAD;
    u[i+UNITS_NUM/2].x=w-1-u[i].x;

    if (j1&1)
      u[i].x+=CIRCLE_RAD;
    if (j2&1)
      u[i+UNITS_NUM/2].x+=CIRCLE_RAD;
    u[i].x+=BORDER_X;
    u[i+UNITS_NUM/2].x+=BORDER_X;

    S2W(&u[i].x,&u[i].y);
    S2W(&u[i+UNITS_NUM/2].x,&u[i+UNITS_NUM/2].y);

    u[i].player=0;
    u[i+UNITS_NUM/2].player=1;

    u[i].alive=TRUE;
    u[i+UNITS_NUM/2].alive=TRUE;

    if (!i) {
      u[i].color=LTCYAN;
      u[i+UNITS_NUM/2].color=LTPURPLE;
      u[i].king=TRUE;
      u[i+UNITS_NUM/2].king=TRUE;
    } else {
      u[i].color=CYAN;
      u[i+UNITS_NUM/2].color=PURPLE;
      u[i].king=FALSE;
      u[i+UNITS_NUM/2].king=FALSE;
    }
  }
  for (i=0;i<UNITS_NUM/2;i++) {
    if (!u[i].king) {
      best_dd=I64_MAX;
      for (j=0;j<UNITS_NUM/2;j++) {
        if (i!=j) {
          dd=SqrI64(u[i].x-u[j].x)+SqrI64(u[i].y-u[j].y);
          if ((u[j].x<u[i].x || u[j].king) && dd<best_dd ) {
            best_dd=dd;
            best=j;
          }
        }
      }
      u[i].link=best;
      u[i+UNITS_NUM/2].link=best+UNITS_NUM/2;
    }
  }
  num_alive[0]=UNITS_NUM/2;
  num_alive[1]=UNITS_NUM/2;
}

U0 CleanUp()
{
  DCDel(map_dc);
}

U0 TreeCheckers()
{
  I64 msg_code,arg1,arg2,ch,sc;
  Unit *tmpu;

  SettingsPush; //See SettingsPush
  MenuPush(
        "File {"
        "  Abort(,CH_SHIFT_ESC);"
        "  Exit(,CH_ESC);"
        "}"
        "Play {"
        "  Restart(,'\n');"
        "}"
        );
  AutoComplete;
  WinBorder;
  WinMax;
  DocCursor;
  DocClear;
  PopUpOk("Step on the link mid points.\n");
  Fs->win_inhibit=WIG_TASK_DFT-WIF_SELF_FOCUS
        -WIF_SELF_BORDER-WIF_FOCUS_TASK_MENU;
  try {
cn_start:
    Init;
    tmpu=NULL;
    show_start=FALSE;
    Fs->draw_it=&DrawIt;
    while (TRUE) {
      msg_code=GetMsg(&arg1,&arg2,
            1<<MSG_KEY_DOWN+1<<MSG_MS_L_DOWN+1<<MSG_MS_L_UP+1<<MSG_MS_MOVE);
      switch (msg_code) {
        case MSG_KEY_DOWN:
          switch (arg1) {
            case '\n':
              CleanUp;
              goto cn_start;
            case CH_ESC:
            case CH_SHIFT_ESC:
              goto cn_done;
          }
          break;
        case MSG_MS_L_DOWN:
          if (num_alive[0]>1 && num_alive[1]>1) {
            tmpu=UnitFind(arg1,arg2,cur_player);
            end_x=start_x=tmpu->x;
            end_y=start_y=tmpu->y;
            show_start=TRUE;
          }
          break;
        case MSG_MS_MOVE:
          if (tmpu)
            UnitMove(tmpu,arg1,arg2);
          break;
        case MSG_MS_L_UP:
          UnitMove(tmpu,arg1,arg2);
          KillsChk(tmpu->x,tmpu->y,cur_player);
          show_start=FALSE;
          tmpu=NULL;
          cur_player=1-cur_player;
          break;
      }
    }
cn_done:
    GetMsg(,,1<<MSG_KEY_UP);
  } catch
    PutExcept;
  SettingsPop;
  CleanUp;
  MenuPop;
}

TreeCheckers;


// Source: D:\TempleOS-Projects\html\SimpleAI-ToTheFront-AIs-Apps.html
// Date: unknown-date
// Language: HolyC

Unit *UnitNearestFind(I64 row,I64 col,I64 player,Bool in_LOS,F64 range=-1)
{
  I64 i;
  F64 dd,best_dd=F64_MAX,x1,y1,x2,y2;
  Unit *best=NULL;
//Sqrt() is slow, so work with squared distances.
  if (range<0)
    range=F64_MAX;
  else
    range*=range;
  RowCol2XY(&x1,&y1,row,col);
  for (i=0;i<UNITS_NUM;i++)
    if (units[player][i].life>0) {
      if (!in_LOS || LOS(row,col,units[player][i].row,units[player][i].col)) {
        RowCol2XY(&x2,&y2,units[player][i].row,units[player][i].col);
        dd=Sqr(x2-x1)+Sqr(y2-y1);
        if (dd<=range && dd<best_dd) {
          best=&units[player][i];
          best_dd=dd;
        }
      }
    }
  return best;
}

U0 PlayerIndirect()
{
  Unit *target,*tmpu;
  I64 i;
  for (i=0;i<UNITS_NUM;i++) {
    UserChk;
    tmpu=&units[cur_player][i];
    if (tmpu->life>0 && tmpu->indirect_fire &&
          (target=UnitNearestFind(tmpu->row,tmpu->col,enemy_player,TRUE,
          tmpu->range*2*HEX_RADIUS)))
      IndirectAdd(tmpu,target->row,target->col);
  }
  throw('PhaseOvr',TRUE);
}

U0 PlayerMove()
{
  Unit *target,*tmpu;
  I64 i;
  F64 x,y;
  for (i=0;i<UNITS_NUM;i++) {
    UserChk;
    tmpu=&units[cur_player][i];
    if (tmpu->life>0) {
//Cheats because it violates Line-of-Sight
      if (target=UnitNearestFind(tmpu->row,tmpu->col,enemy_player,FALSE)) {
        RowCol2XY(&x,&y,target->row,target->col);
        if (!UnitMove(tmpu,x,y)) {
          RowCol2XY(&x,&y,tmpu->row,tmpu->col);
          UnitMove(tmpu,x+RandI16,y+RandI16);
        }
      }
    }
  }
  throw('PhaseOvr',TRUE);
}

U0 PlayerDirect()
{
  Unit *target,*tmpu;
  I64 i;
  for (i=0;i<UNITS_NUM;i++) {
    UserChk;
    tmpu=&units[cur_player][i];
    if (tmpu->life>0 && !tmpu->indirect_fire &&
          (target=UnitNearestFind(tmpu->row,tmpu->col,enemy_player,TRUE,
          tmpu->range*2*HEX_RADIUS))) {
      UnitDirectFire(tmpu,target);
      Sleep(250*animation_delay);
    }
  }
  throw('PhaseOvr',TRUE);
}

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\SimpleAI-ToTheFront-AIs-Apps.html
// Date: unknown-date
// Language: HolyC

Unit *UnitNearestFind(I64 row,I64 col,I64 player,Bool in_LOS,F64 range=-1)
{
  I64 i;
  F64 dd,best_dd=F64_MAX,x1,y1,x2,y2;
  Unit *best=NULL;
//Sqrt() is slow, so work with squared distances.
  if (range<0)
    range=F64_MAX;
  else
    range*=range;
  RowCol2XY(&x1,&y1,row,col);
  for (i=0;i<UNITS_NUM;i++)
    if (units[player][i].life>0) {
      if (!in_LOS || LOS(row,col,units[player][i].row,units[player][i].col)) {
        RowCol2XY(&x2,&y2,units[player][i].row,units[player][i].col);
        dd=Sqr(x2-x1)+Sqr(y2-y1);
        if (dd<=range && dd<best_dd) {
          best=&units[player][i];
          best_dd=dd;
        }
      }
    }
  return best;
}

U0 PlayerIndirect()
{
  Unit *target,*tmpu;
  I64 i;
  for (i=0;i<UNITS_NUM;i++) {
    UserChk;
    tmpu=&units[cur_player][i];
    if (tmpu->life>0 && tmpu->indirect_fire &&
          (target=UnitNearestFind(tmpu->row,tmpu->col,enemy_player,TRUE,
          tmpu->range*2*HEX_RADIUS)))
      IndirectAdd(tmpu,target->row,target->col);
  }
  throw('PhaseOvr',TRUE);
}

U0 PlayerMove()
{
  Unit *target,*tmpu;
  I64 i;
  F64 x,y;
  for (i=0;i<UNITS_NUM;i++) {
    UserChk;
    tmpu=&units[cur_player][i];
    if (tmpu->life>0) {
//Cheats because it violates Line-of-Sight
      if (target=UnitNearestFind(tmpu->row,tmpu->col,enemy_player,FALSE)) {
        RowCol2XY(&x,&y,target->row,target->col);
        if (!UnitMove(tmpu,x,y)) {
          RowCol2XY(&x,&y,tmpu->row,tmpu->col);
          UnitMove(tmpu,x+RandI16,y+RandI16);
        }
      }
    }
  }
  throw('PhaseOvr',TRUE);
}

U0 PlayerDirect()
{
  Unit *target,*tmpu;
  I64 i;
  for (i=0;i<UNITS_NUM;i++) {
    UserChk;
    tmpu=&units[cur_player][i];
    if (tmpu->life>0 && !tmpu->indirect_fire &&
          (target=UnitNearestFind(tmpu->row,tmpu->col,enemy_player,TRUE,
          tmpu->range*2*HEX_RADIUS))) {
      UnitDirectFire(tmpu,target);
      Sleep(250*animation_delay);
    }
  }
  throw('PhaseOvr',TRUE);
}


// Source: D:\TempleOS-Projects\html\BgtStrs-Budget-Apps.html
// Date: unknown-date
// Language: HolyC

#define SFT_GENERIC     1

public U8 **StrFileRead(U8 *name,I64 *_max_num=NULL,
        U8 **_colors=NULL,Bool no_nums=FALSE)
{
  CDoc          *doc=DocRead(name,DOCF_DBL_DOLLARS|DOCF_NO_CURSOR);
  CDocEntry     *doc_e=doc->head.next;
  I64           i,max_num=0;
  U8            *ptr,**res,*colors;

  while (doc_e!=doc) {
    if (doc_e->type_u8==DOCT_TEXT) {
      if (no_nums)
        i=++max_num;
      else {
        i=Str2I64(doc_e->tag,,&ptr);
        if (i>max_num) max_num=i;
        if (*ptr==',') ptr++;
        ptr=StrNew(ptr);
        Free(doc_e->tag);
        doc_e->tag=ptr;
      }
      doc_e->user_data=i;
    }
    doc_e=doc_e->next;
  }

  res=CAlloc(sizeof(U8 *)*(max_num+1));
  colors=CAlloc(sizeof(U8)*(max_num+1));
  doc_e=doc->head.next;
  while (doc_e!=doc) {
    if (doc_e->type_u8==DOCT_TEXT && 0<=doc_e->user_data<=max_num) {
      res[doc_e->user_data]=doc_e->tag;
      doc_e->tag=NULL;
      colors[doc_e->user_data]=doc_e->type.u8[1]&15;
    }
    doc_e=doc_e->next;
  }

  DocDel(doc);
  if (_max_num) *_max_num=max_num;
  if (_colors)
    *_colors=colors;
  else
    Free(colors);
  return res;
}

public U0 StrFileArrDel(U8 **a,I64 max_num)
{
  I64 i;
  for (i=0;i<=max_num;i++)
    Free(a[i]);
  Free(a);
}

public I64 StrFileAdd(U8 *st,I64 *_num,
        CHashTable *table,I64 color=COLOR_INVALID)
{
  CHashGeneric *tmph;
  if (!st) return 0;
  if (!(tmph=HashFind(st,table,SFT_GENERIC))) {
    tmph=CAlloc(sizeof(CHashGeneric));
    tmph->type=SFT_GENERIC;
    tmph->str=StrNew(st);
    tmph->user_data0=(*_num)++;
    HashAdd(tmph,table);
  }
  if (color!=COLOR_INVALID)
    tmph->user_data1=color;
  return tmph->user_data0;
}

I64 StrEntriesCompare(CHashGeneric *h1,CHashGeneric *h2)
{
  return h1->user_data0-h2->user_data0;
}

public U0 StrFileWrite(U8 *name,CHashTable *table,Bool no_nums=FALSE)
{
  I64 i,j,cnt,color=BLACK;
  CDoc *doc=DocNew(name);
  CHashGeneric *tmph,**a;
  if (table) {
    cnt=0;      //Count Strings
    for (i=0;i<=table->mask;i++)
      cnt+=LinkedLstCnt(table->body[i]);
    a=MAlloc(cnt*sizeof(CHashGeneric *));
    j=0;        //Load Strings
    for (i=0;i<=table->mask;i++) {
      tmph=table->body[i];
      while (tmph) {
        a[j++]=tmph;
        tmph=tmph->next;
      }
    }
    QSortI64(a,cnt,&StrEntriesCompare);
    for (i=0;i<cnt;i++) {
      tmph=a[i];
      if (tmph->user_data1&15!=color) {
        DocPrint(doc,"$FG,%d$",tmph->user_data1&15);
        color=tmph->user_data1&15;
      }
      if (no_nums)
        DocPrint(doc,"%s\n",tmph->str);
      else
        DocPrint(doc,"%d,%s\n",tmph->user_data0,tmph->str);
    }
    Free(a);
  }
  doc->flags|=DOCF_NO_CURSOR;
  DocWrite(doc);
  DocDel(doc);
}

public U0 StrFileDel(CHashTable *table)
{
  I64 i;
  CHashGeneric *tmph,*tmph1;
  if (!table) return;
  for (i=0;i<=table->mask;i++) {
    tmph=table->body[i];
    while (tmph) {
      tmph1=tmph->next;
      Free(tmph->str);
      Free(tmph);
      tmph=tmph1;
    }
  }
  Free(table->body);
  Free(table);
}

[System] You are Terry Davis, the creator of TempleOS. Write HolyC code in your unique style.

[User] Please write some HolyC code.

[Assistant]
// Source: D:\TempleOS-Projects\html\BgtStrs-Budget-Apps.html
// Date: unknown-date
// Language: HolyC

#define SFT_GENERIC     1

public U8 **StrFileRead(U8 *name,I64 *_max_num=NULL,
        U8 **_colors=NULL,Bool no_nums=FALSE)
{
  CDoc          *doc=DocRead(name,DOCF_DBL_DOLLARS|DOCF_NO_CURSOR);
  CDocEntry     *doc_e=doc->head.next;
  I64           i,max_num=0;
  U8            *ptr,**res,*colors;

  while (doc_e!=doc) {
    if (doc_e->type_u8==DOCT_TEXT) {
      if (no_nums)
        i=++max_num;
      else {
        i=Str2I64(doc_e->tag,,&ptr);
        if (i>max_num) max_num=i;
        if (*ptr==',') ptr++;
        ptr=StrNew(ptr);
        Free(doc_e->tag);
        doc_e->tag=ptr;
      }
      doc_e->user_data=i;
    }
    doc_e=doc_e->next;
  }

  res=CAlloc(sizeof(U8 *)*(max_num+1));
  colors=CAlloc(sizeof(U8)*(max_num+1));
  doc_e=doc->head.next;
  while (doc_e!=doc) {
    if (doc_e->type_u8==DOCT_TEXT && 0<=doc_e->user_data<=max_num) {
      res[doc_e->user_data]=doc_e->tag;
      doc_e->tag=NULL;
      colors[doc_e->user_data]=doc_e->type.u8[1]&15;
    }
    doc_e=doc_e->next;
  }

  DocDel(doc);
  if (_max_num) *_max_num=max_num;
  if (_colors)
    *_colors=colors;
  else
    Free(colors);
  return res;
}

public U0 StrFileArrDel(U8 **a,I64 max_num)
{
  I64 i;
  for (i=0;i<=max_num;i++)
    Free(a[i]);
  Free(a);
}

public I64 StrFileAdd(U8 *st,I64 *_num,
        CHashTable *table,I64 color=COLOR_INVALID)
{
  CHashGeneric *tmph;
  if (!st) return 0;
  if (!(tmph=HashFind(st,table,SFT_GENERIC))) {
    tmph=CAlloc(sizeof(CHashGeneric));
    tmph->type=SFT_GENERIC;
    tmph->str=StrNew(st);
    tmph->user_data0=(*_num)++;
    HashAdd(tmph,table);
  }
  if (color!=COLOR_INVALID)
    tmph->user_data1=color;
  return tmph->user_data0;
}

I64 StrEntriesCompare(CHashGeneric *h1,CHashGeneric *h2)
{
  return h1->user_data0-h2->user_data0;
}

public U0 StrFileWrite(U8 *name,CHashTable *table,Bool no_nums=FALSE)
{
  I64 i,j,cnt,color=BLACK;
  CDoc *doc=DocNew(name);
  CHashGeneric *tmph,**a;
  if (table) {
    cnt=0;      //Count Strings
    for (i=0;i<=table->mask;i++)
      cnt+=LinkedLstCnt(table->body[i]);
    a=MAlloc(cnt*sizeof(CHashGeneric *));
    j=0;        //Load Strings
    for (i=0;i<=table->mask;i++) {
      tmph=table->body[i];
      while (tmph) {
        a[j++]=tmph;
        tmph=tmph->next;
      }
    }
    QSortI64(a,cnt,&StrEntriesCompare);
    for (i=0;i<cnt;i++) {
      tmph=a[i];
      if (tmph->user_data1&15!=color) {
        DocPrint(doc,"$FG,%d$",tmph->user_data1&15);
        color=tmph->user_data1&15;
      }
      if (no_nums)
        DocPrint(doc,"%s\n",tmph->str);
      else
        DocPrint(doc,"%d,%s\n",tmph->user_data0,tmph->str);
    }
    Free(a);
  }
  doc->flags|=DOCF_NO_CURSOR;
  DocWrite(doc);
  DocDel(doc);
}

public U0 StrFileDel(CHashTable *table)
{
  I64 i;
  CHashGeneric *tmph,*tmph1;
  if (!table) return;
  for (i=0;i<=table->mask;i++) {
    tmph=table->body[i];
    while (tmph) {
      tmph1=tmph->next;
      Free(tmph->str);
      Free(tmph);
      tmph=tmph1;
    }
  }
  Free(table->body);
  Free(table);
}