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

#help_index "Help System"

U8 *KeyMapKeyMStrPrint(I64 sc,U0 (*fp_hndlr)(I64 sc),
        U8 *desc,CTask *task=NULL)
{
  I64 i=9,k,c;
  U8 *st,*st2,*res,*ptr;
  CHashTable *old_hash=Fs->hash_table;
  st=ScanCode2KeyName(sc);
  if (sc&SCF_CTRL)      i+=5;
  if (sc&SCF_ALT)       i+=4;
  if (sc&(SCF_SHIFT|SCF_NO_SHIFT)) i+=6;
  if (TaskValidate(task))
    Fs->hash_table=task->hash_table;
  st2=SrcEdLink(fp_hndlr,256);
  Fs->hash_table=old_hash;

  k=*desc(U32 *);
  if (k=='Edit')        c=BLUE;
  else if (k=='Dol ')   c=GREEN;
  else if (k=='Cmd ')   c=RED;
  else                  c=BLACK;

  res=MStrPrint("%-*s $FG,%d$$TX+UL+L+PU,\"%$Q\",A=\"%s\"$$FG$\n",
        i,st,c,desc,st2);
  Free(st);
  Free(st2);

  ptr=res;
  while (*ptr) {
    if (*ptr==CH_SPACE)
      *ptr=CH_SHIFT_SPACE;
    ptr++;
  }

  return res;
}

U0 KeyMapKeyPrint(I64 sc,U0 (*fp_hndlr)(I64 sc),U8 *desc,CTask *task=NULL)
{
  U8 *st=KeyMapKeyMStrPrint(sc,fp_hndlr,desc,task);
  "%s",st;
  Free(st);
}

U0 KeyMapCtrlAltFamily(Bool no_shift,Bool shift)
{
  I64 i,no_shift_f;
  if (no_shift && shift)
    no_shift_f=SCF_NO_SHIFT;
  else
    no_shift_f=0;
  if (no_shift) KeyMapKeyPrint(SC_DELETE+SCF_CTRL+SCF_ALT+no_shift_f,
          &Reboot,"Cmd /Reboot");
  if (no_shift) KeyMapKeyPrint(SC_ESC+SCF_CTRL+SCF_ALT+no_shift_f,
          &User,"Cmd /Terminal Window");
  if (no_shift) KeyMapKeyPrint(SC_TAB+SCF_CTRL+SCF_ALT+no_shift_f,
          &WinToTop,"Cmd /Next Focus Task");

  for (i=0;i<26;i++)
    if (keydev.fp_ctrl_alt_cbs[i]) {
      if (no_shift && keydev.ctrl_alt_no_shift_descs[i])
        KeyMapKeyPrint(Char2ScanCode(i+'a')+SCF_CTRL+SCF_ALT+no_shift_f,
              keydev.fp_ctrl_alt_cbs[i],keydev.ctrl_alt_no_shift_descs[i]);
      if (shift && keydev.ctrl_alt_shift_descs[i])
        KeyMapKeyPrint(Char2ScanCode(i+'a')+SCF_CTRL+SCF_ALT+SCF_SHIFT,
              keydev.fp_ctrl_alt_cbs[i],keydev.ctrl_alt_shift_descs[i]);
    }
}

U0 KMComparePrepare(U8 *buf,I64 *src)
{
  I64 i,*dst=buf;
  U8 *ptr;
  if (src) {
    *dst++=*src++;
    *dst++=*src++;
    *dst++=*src++;
    *dst++=*src++;
    *dst(U8 *)=0;
    if (ptr=StrMatch("SHIFT",buf)) {
      for (i=0;i<5;i++)
        ptr[i]=CH_SHIFT_SPACE;
      if (ptr=StrMatch("$",buf))
        *ptr=255;
    }
  } else
    *buf=0;
}

I64 KMCompare(U8 *e1,U8 *e2)
{
  U8 buf1[STR_LEN],buf2[STR_LEN];
  KMComparePrepare(buf1,e1);
  KMComparePrepare(buf2,e2);
  return StrCmp(buf1,buf2);
}

U0 KeyMapFamily2(U8 **entries,CTask *task,I64 scf)
{
  I64 i,arg1,arg2;
  for (i=0;i<256;i++) {
    arg2=scf|i|SCF_KEY_DESC;
    arg1=ScanCode2Char(arg2);
    *keydev.desc=0;
    keydev.hndlr=NULL;
    if (TaskValidate(task) && !Bt(&task->win_inhibit,WIf_SELF_KEY_DESC)) {
      if (task==Fs)
        PutKey(arg1,arg2);
      else
        PostMsg(task,MSG_KEY_DOWN,arg1,arg2);
      Refresh(0,TRUE);
      Sleep(1); //Open loop because might be no response.  TODO: Drops msgs.
    }
    if (*keydev.desc && StrNCmp(keydev.desc,"Char  /",7))
      entries[i]=KeyMapKeyMStrPrint(arg2,keydev.hndlr,keydev.desc,task);
  }
}

U0 KeyMapFamily(CTask *task,I64 scf,Bool no_shift,Bool shift)
{
  I64 i,cnt=0;
  U8 **entries=CAlloc(2*256*sizeof(U8 *)),**ptr=entries;
  if (no_shift) {
    if (shift)
      KeyMapFamily2(ptr,task,scf+SCF_NO_SHIFT);
    else
      KeyMapFamily2(ptr,task,scf);
    ptr+=256;
    cnt+=256;
  }
  if (shift) {
    KeyMapFamily2(ptr,task,scf+SCF_SHIFT);
    ptr+=256;
    cnt+=256;
  }
  QSortI64(entries,cnt,&KMCompare);
  for (i=0;i<cnt;i++)
    if (entries[i]) {
      "%s",entries[i];
      Free(entries[i]);
    }
  Free(entries);
}

public U0 KeyMap(CTask *task=NULL)
{//Report desc of all keys.
  Bool old_key_desc;
  if (!task) task=Fs;
  old_key_desc=LBtr(&task->win_inhibit,WIf_SELF_KEY_DESC);
  DocMax;
  KeyMapFamily(task,0,TRUE,TRUE);
  KeyMapFamily(task,SCF_CTRL,TRUE,TRUE);
  KeyMapFamily(task,SCF_ALT,TRUE,TRUE);
  KeyMapCtrlAltFamily(TRUE,TRUE);
  LBEqu(&task->win_inhibit,WIf_SELF_KEY_DESC,old_key_desc);
  "\nKeyMap Completed.\n";
}

#help_index "Help System/Training"
public U0 TipOfDay(U8 *tip_file="::/Doc/Tips.DD")
{//Print random tip-of-day from ::/Doc/Tips.DD.
  I64 i=RandU16;
  CDoc *doc=DocRead(tip_file),*doc2=DocNew;
  CDocEntry *doc_e=doc->head.next;
  "$WW,1$\n";
  while (TRUE) {
    if (doc_e->type_u8==DOCT_TEXT && *doc_e->tag=='*')
      if (!i--) break;
    doc_e=doc_e->next;
  }
  if (doc_e->type_u8==DOCT_TEXT && *doc_e->tag=='*') {
    while (doc_e!=doc) {
      if (doc_e->type_u8!=DOCT_ERROR)
        DocInsEntry(doc2,DocEntryCopy(doc2,doc_e));
      doc_e=doc_e->next;
      if (doc_e->type_u8==DOCT_TEXT && *doc_e->tag=='*')
        break;
    }
  }
  DocInsDoc(DocPut,doc2);
  DocDel(doc2);
  DocDel(doc);
}

[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\Training-Adam.html
// Date: unknown-date
// Language: HolyC

#help_index "Help System"

U8 *KeyMapKeyMStrPrint(I64 sc,U0 (*fp_hndlr)(I64 sc),
        U8 *desc,CTask *task=NULL)
{
  I64 i=9,k,c;
  U8 *st,*st2,*res,*ptr;
  CHashTable *old_hash=Fs->hash_table;
  st=ScanCode2KeyName(sc);
  if (sc&SCF_CTRL)      i+=5;
  if (sc&SCF_ALT)       i+=4;
  if (sc&(SCF_SHIFT|SCF_NO_SHIFT)) i+=6;
  if (TaskValidate(task))
    Fs->hash_table=task->hash_table;
  st2=SrcEdLink(fp_hndlr,256);
  Fs->hash_table=old_hash;

  k=*desc(U32 *);
  if (k=='Edit')        c=BLUE;
  else if (k=='Dol ')   c=GREEN;
  else if (k=='Cmd ')   c=RED;
  else                  c=BLACK;

  res=MStrPrint("%-*s $FG,%d$$TX+UL+L+PU,\"%$Q\",A=\"%s\"$$FG$\n",
        i,st,c,desc,st2);
  Free(st);
  Free(st2);

  ptr=res;
  while (*ptr) {
    if (*ptr==CH_SPACE)
      *ptr=CH_SHIFT_SPACE;
    ptr++;
  }

  return res;
}

U0 KeyMapKeyPrint(I64 sc,U0 (*fp_hndlr)(I64 sc),U8 *desc,CTask *task=NULL)
{
  U8 *st=KeyMapKeyMStrPrint(sc,fp_hndlr,desc,task);
  "%s",st;
  Free(st);
}

U0 KeyMapCtrlAltFamily(Bool no_shift,Bool shift)
{
  I64 i,no_shift_f;
  if (no_shift && shift)
    no_shift_f=SCF_NO_SHIFT;
  else
    no_shift_f=0;
  if (no_shift) KeyMapKeyPrint(SC_DELETE+SCF_CTRL+SCF_ALT+no_shift_f,
          &Reboot,"Cmd /Reboot");
  if (no_shift) KeyMapKeyPrint(SC_ESC+SCF_CTRL+SCF_ALT+no_shift_f,
          &User,"Cmd /Terminal Window");
  if (no_shift) KeyMapKeyPrint(SC_TAB+SCF_CTRL+SCF_ALT+no_shift_f,
          &WinToTop,"Cmd /Next Focus Task");

  for (i=0;i<26;i++)
    if (keydev.fp_ctrl_alt_cbs[i]) {
      if (no_shift && keydev.ctrl_alt_no_shift_descs[i])
        KeyMapKeyPrint(Char2ScanCode(i+'a')+SCF_CTRL+SCF_ALT+no_shift_f,
              keydev.fp_ctrl_alt_cbs[i],keydev.ctrl_alt_no_shift_descs[i]);
      if (shift && keydev.ctrl_alt_shift_descs[i])
        KeyMapKeyPrint(Char2ScanCode(i+'a')+SCF_CTRL+SCF_ALT+SCF_SHIFT,
              keydev.fp_ctrl_alt_cbs[i],keydev.ctrl_alt_shift_descs[i]);
    }
}

U0 KMComparePrepare(U8 *buf,I64 *src)
{
  I64 i,*dst=buf;
  U8 *ptr;
  if (src) {
    *dst++=*src++;
    *dst++=*src++;
    *dst++=*src++;
    *dst++=*src++;
    *dst(U8 *)=0;
    if (ptr=StrMatch("SHIFT",buf)) {
      for (i=0;i<5;i++)
        ptr[i]=CH_SHIFT_SPACE;
      if (ptr=StrMatch("$",buf))
        *ptr=255;
    }
  } else
    *buf=0;
}

I64 KMCompare(U8 *e1,U8 *e2)
{
  U8 buf1[STR_LEN],buf2[STR_LEN];
  KMComparePrepare(buf1,e1);
  KMComparePrepare(buf2,e2);
  return StrCmp(buf1,buf2);
}

U0 KeyMapFamily2(U8 **entries,CTask *task,I64 scf)
{
  I64 i,arg1,arg2;
  for (i=0;i<256;i++) {
    arg2=scf|i|SCF_KEY_DESC;
    arg1=ScanCode2Char(arg2);
    *keydev.desc=0;
    keydev.hndlr=NULL;
    if (TaskValidate(task) && !Bt(&task->win_inhibit,WIf_SELF_KEY_DESC)) {
      if (task==Fs)
        PutKey(arg1,arg2);
      else
        PostMsg(task,MSG_KEY_DOWN,arg1,arg2);
      Refresh(0,TRUE);
      Sleep(1); //Open loop because might be no response.  TODO: Drops msgs.
    }
    if (*keydev.desc && StrNCmp(keydev.desc,"Char  /",7))
      entries[i]=KeyMapKeyMStrPrint(arg2,keydev.hndlr,keydev.desc,task);
  }
}

U0 KeyMapFamily(CTask *task,I64 scf,Bool no_shift,Bool shift)
{
  I64 i,cnt=0;
  U8 **entries=CAlloc(2*256*sizeof(U8 *)),**ptr=entries;
  if (no_shift) {
    if (shift)
      KeyMapFamily2(ptr,task,scf+SCF_NO_SHIFT);
    else
      KeyMapFamily2(ptr,task,scf);
    ptr+=256;
    cnt+=256;
  }
  if (shift) {
    KeyMapFamily2(ptr,task,scf+SCF_SHIFT);
    ptr+=256;
    cnt+=256;
  }
  QSortI64(entries,cnt,&KMCompare);
  for (i=0;i<cnt;i++)
    if (entries[i]) {
      "%s",entries[i];
      Free(entries[i]);
    }
  Free(entries);
}

public U0 KeyMap(CTask *task=NULL)
{//Report desc of all keys.
  Bool old_key_desc;
  if (!task) task=Fs;
  old_key_desc=LBtr(&task->win_inhibit,WIf_SELF_KEY_DESC);
  DocMax;
  KeyMapFamily(task,0,TRUE,TRUE);
  KeyMapFamily(task,SCF_CTRL,TRUE,TRUE);
  KeyMapFamily(task,SCF_ALT,TRUE,TRUE);
  KeyMapCtrlAltFamily(TRUE,TRUE);
  LBEqu(&task->win_inhibit,WIf_SELF_KEY_DESC,old_key_desc);
  "\nKeyMap Completed.\n";
}

#help_index "Help System/Training"
public U0 TipOfDay(U8 *tip_file="::/Doc/Tips.DD")
{//Print random tip-of-day from ::/Doc/Tips.DD.
  I64 i=RandU16;
  CDoc *doc=DocRead(tip_file),*doc2=DocNew;
  CDocEntry *doc_e=doc->head.next;
  "$WW,1$\n";
  while (TRUE) {
    if (doc_e->type_u8==DOCT_TEXT && *doc_e->tag=='*')
      if (!i--) break;
    doc_e=doc_e->next;
  }
  if (doc_e->type_u8==DOCT_TEXT && *doc_e->tag=='*') {
    while (doc_e!=doc) {
      if (doc_e->type_u8!=DOCT_ERROR)
        DocInsEntry(doc2,DocEntryCopy(doc2,doc_e));
      doc_e=doc_e->next;
      if (doc_e->type_u8==DOCT_TEXT && *doc_e->tag=='*')
        break;
    }
  }
  DocInsDoc(DocPut,doc2);
  DocDel(doc2);
  DocDel(doc);
}


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

//See PCI.

#if mp_cnt>1
#define DEST_CPU        1
#else
#define DEST_CPU        0
#endif

#assert I_USER==0x40

interrupt U0 IntMy40()
{
  lock {progress1++;}
*(dev.uncached_alias+LAPIC_EOI)(U32 *)=0;
}

interrupt U0 IntMy41()
{
  lock {progress2++;}
*(dev.uncached_alias+LAPIC_EOI)(U32 *)=0;
}

interrupt U0 IntMy42()
{
  lock {progress3++;}
*(dev.uncached_alias+LAPIC_EOI)(U32 *)=0;
}

interrupt U0 IntMy43()
{
  lock {progress4++;}
*(dev.uncached_alias+LAPIC_EOI)(U32 *)=0;
}

I64 vect[4]={&IntMy40,&IntMy41,&IntMy42,&IntMy43};

U0 Main()
{
  I64 i;
  U8 *da=dev.uncached_alias+IOAPIC_REG;
  U32 *_d=dev.uncached_alias+IOAPIC_DATA;

  ProgressBarsRst;
  for (i=I_USER;i<I_USER+4;i++)
    IntEntrySet(i,vect[i&3],IDTET_IRQ);

    //There are like 4*6 vects.  Bus zero
    //has the first four vects.
  for (i=IOREDTAB;i<IOREDTAB+4*2;i+=2) {
    *da=i+1;
    *_d=dev.mp_apic_ids[DEST_CPU]<<24;
    *da=i;
    *_d=0x4000+0x40+(i-IOREDTAB)/2;
  }

  Sleep(1000);
  "The keyboard and timer seem to be active.\n"
        "IntA:%d IntB:%d IntC:%d IntD:%d\n\n"
        "IntB is my keybaord and regs a single KEY-UP.\n"
        "Rerun and press a few keys.\n",
        progress1,progress2,progress3,progress4;

  //Disable vects
  for (i=IOREDTAB;i<IOREDTAB+4*2;i+=2) {
    *da=i+1;
    *_d=dev.mp_apic_ids[DEST_CPU]<<24;
    *da=i;
    *_d=0x14000+0x40+(i-IOREDTAB)/2;
  }
  ProgressBarsRst;
}

Main;

[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\PCIInterrupts-Lectures-Demo.html
// Date: unknown-date
// Language: HolyC

//See PCI.

#if mp_cnt>1
#define DEST_CPU        1
#else
#define DEST_CPU        0
#endif

#assert I_USER==0x40

interrupt U0 IntMy40()
{
  lock {progress1++;}
*(dev.uncached_alias+LAPIC_EOI)(U32 *)=0;
}

interrupt U0 IntMy41()
{
  lock {progress2++;}
*(dev.uncached_alias+LAPIC_EOI)(U32 *)=0;
}

interrupt U0 IntMy42()
{
  lock {progress3++;}
*(dev.uncached_alias+LAPIC_EOI)(U32 *)=0;
}

interrupt U0 IntMy43()
{
  lock {progress4++;}
*(dev.uncached_alias+LAPIC_EOI)(U32 *)=0;
}

I64 vect[4]={&IntMy40,&IntMy41,&IntMy42,&IntMy43};

U0 Main()
{
  I64 i;
  U8 *da=dev.uncached_alias+IOAPIC_REG;
  U32 *_d=dev.uncached_alias+IOAPIC_DATA;

  ProgressBarsRst;
  for (i=I_USER;i<I_USER+4;i++)
    IntEntrySet(i,vect[i&3],IDTET_IRQ);

    //There are like 4*6 vects.  Bus zero
    //has the first four vects.
  for (i=IOREDTAB;i<IOREDTAB+4*2;i+=2) {
    *da=i+1;
    *_d=dev.mp_apic_ids[DEST_CPU]<<24;
    *da=i;
    *_d=0x4000+0x40+(i-IOREDTAB)/2;
  }

  Sleep(1000);
  "The keyboard and timer seem to be active.\n"
        "IntA:%d IntB:%d IntC:%d IntD:%d\n\n"
        "IntB is my keybaord and regs a single KEY-UP.\n"
        "Rerun and press a few keys.\n",
        progress1,progress2,progress3,progress4;

  //Disable vects
  for (i=IOREDTAB;i<IOREDTAB+4*2;i+=2) {
    *da=i+1;
    *_d=dev.mp_apic_ids[DEST_CPU]<<24;
    *da=i;
    *_d=0x14000+0x40+(i-IOREDTAB)/2;
  }
  ProgressBarsRst;
}

Main;


// Source: D:\TempleOS-Projects\html\Tour8-Tour-Misc.html
// Date: unknown-date
// Language: HolyC

"FF(\"/Kernel/*\");";
TourPut(
"Let's list files in the $GREEN$/Kernel$FG$ and sub directories.\n\n"
"TempleOS masks have one base directory and multiple filename masks.\n\n"
"Press $GREEN$<ENTER>$FG$.\n");
InGetStr("\n");

"FF(\"/Kernel/*\",\"-r\");";
TourPut(
"The recurse $GREEN$+r$FG$ flag is usually set.  Let's turn it off.\n\n"
"Press $GREEN$<ENTER>$FG$.\n");
InGetStr("\n");

"FF(\"/Kernel/St*;*.HH*\");";
TourPut(
"Let's have two mask of $GREEN$St*$FG$ plus $GREEN$*.HH*$FG$\n\n"
"Press $GREEN$<ENTER>$FG$.\n");
InGetStr("\n");

"FF(\"/Kernel/St*;*.HH*;!*Print*\");";
TourPut(
"Let's unsel $GREEN$*Print*$FG$.\n\n"
"Press $GREEN$<ENTER>$FG$.\n");
InGetStr("\n");
TourPut("There is no way to include multiple base directories.\n",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\Tour8-Tour-Misc.html
// Date: unknown-date
// Language: HolyC

"FF(\"/Kernel/*\");";
TourPut(
"Let's list files in the $GREEN$/Kernel$FG$ and sub directories.\n\n"
"TempleOS masks have one base directory and multiple filename masks.\n\n"
"Press $GREEN$<ENTER>$FG$.\n");
InGetStr("\n");

"FF(\"/Kernel/*\",\"-r\");";
TourPut(
"The recurse $GREEN$+r$FG$ flag is usually set.  Let's turn it off.\n\n"
"Press $GREEN$<ENTER>$FG$.\n");
InGetStr("\n");

"FF(\"/Kernel/St*;*.HH*\");";
TourPut(
"Let's have two mask of $GREEN$St*$FG$ plus $GREEN$*.HH*$FG$\n\n"
"Press $GREEN$<ENTER>$FG$.\n");
InGetStr("\n");

"FF(\"/Kernel/St*;*.HH*;!*Print*\");";
TourPut(
"Let's unsel $GREEN$*Print*$FG$.\n\n"
"Press $GREEN$<ENTER>$FG$.\n");
InGetStr("\n");
TourPut("There is no way to include multiple base directories.\n",TRUE);


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

//5 has words
U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo=2.5;
  try {
    while (!ScanKey) {
      Play("5sE4A5E4A5eFF4sAGAG5FGFGqCC4BA",
            "All \0 \0 \0 \0hell \0 \0breaks \0 \0 \0 \0"
            "loose.\n\0 \0 \0 \0I \0will \0get \0through.\n\0");
      Play("5sE4A5E4A5eFF4sAGAG5FGFGqCC4BA",
            "All \0 \0 \0 \0hell \0 \0breaks \0 \0 \0 \0"
            "loose.\n\0 \0 \0 \0I \0will \0get \0through.\n\0");
      Play("4A5CeCC4sBBBB5qEsCCCC4qA5E");
      Play("4A5CeCC4sBBBB5qEsCCCC4qA5E");
    }
  } 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\determination-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has words
U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo=2.5;
  try {
    while (!ScanKey) {
      Play("5sE4A5E4A5eFF4sAGAG5FGFGqCC4BA",
            "All \0 \0 \0 \0hell \0 \0breaks \0 \0 \0 \0"
            "loose.\n\0 \0 \0 \0I \0will \0get \0through.\n\0");
      Play("5sE4A5E4A5eFF4sAGAG5FGFGqCC4BA",
            "All \0 \0 \0 \0hell \0 \0breaks \0 \0 \0 \0"
            "loose.\n\0 \0 \0 \0I \0will \0get \0through.\n\0");
      Play("4A5CeCC4sBBBB5qEsCCCC4qA5E");
      Play("4A5CeCC4sBBBB5qEsCCCC4qA5E");
    }
  } catch
    PutExcept;
  Snd;
}

Song;


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

#help_index "Games"

Cd(__DIR__);;
#include "TTFGlbls"
#include "TTFLib"
#include "TTFInit"
#include "TTFVis"
#include "TTFFire"
#include "TTFMove"
#include "TTFDraw"
#include "ToTheFront"

#help_index ""

[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\Load-ToTheFront-Apps.html
// Date: unknown-date
// Language: HolyC

#help_index "Games"

Cd(__DIR__);;
#include "TTFGlbls"
#include "TTFLib"
#include "TTFInit"
#include "TTFVis"
#include "TTFFire"
#include "TTFMove"
#include "TTFDraw"
#include "ToTheFront"

#help_index ""


// Source: D:\TempleOS-Projects\html\OptPass012-Compiler.html
// Date: unknown-date
// Language: HolyC

/*OptPass012

Pass#0
When parsing the arg expressions to a function
call, there is a call to OptPass012 to determine
the type of the expression. OptPass012

Pass#1&2
Constant expressions are simplified.
Eliminated opcodes are set to NOP.
Types are determined by reconstructing an
expression tree for operators
CIntermediateCode.CICTreeLinks.

Pointer arithmetic size is set, once
the type is determined.

Branches are expressed with short-circuit
logic. 3-Arg comparisons are established.

*/

CIntermediateCode *OptPass012(CCmpCtrl *cc)
{/*Simplify CONST arithmetic.
Sets the class throughout Expression trees.
Returns the type of an Expression for use
in int<-->F64 conversions of fun
args.
*/
  I64 code,i;
  Bool is_unsigned;
  CHashClass *tmpc,*tmpc1,*tmpc2;
  CIntermediateCode *tmpi,*tmpi1,*tmpi2,*tmpi3,*tmpi_push,
        *last_with_class=NULL;
  CCodeMisc *lb,*lb1,*lb2;
  CPrsStk *ps;
  if (!(ps=cc->ps))
    ps=cc->ps=MAlloc(sizeof(CPrsStk));
  ps->ptr=0;
  ps->ptr2=0;
  tmpi=cc->coc.coc_head.next;
  while (code=tmpi->ic_code) {
    tmpc=tmpi->ic_class;
    tmpi->ic_class2=tmpc;
    tmpi_push=tmpi;
    MemSet(&tmpi->arg1,0,3*sizeof(CICArg));
    tmpi->arg1_type_pointed_to=0;
    switch [intermediate_code_table[code].arg_cnt] {
      case IS_V_ARG:
        ps->ptr-=tmpi->ic_data>>2;
        break;
      case IS_2_ARG:
        tmpi2=PrsPop(ps);
        tmpc2=tmpi2->ic_class;
        tmpi->t.arg2_tree=tmpi2;
        tmpi->t.arg2_class=PrsPop(ps);
      case IS_1_ARG:
        tmpi1=PrsPop(ps);
        tmpc1=tmpi1->ic_class;
        tmpi->t.arg1_tree=tmpi1;
        tmpi->t.arg1_class=PrsPop(ps);
        break;
      case IS_0_ARG: //nobound switch
        break;
    }
    if (intermediate_code_table[code].not_const)
      cc->flags|=CCF_NOT_CONST;
    switch [code] {
      case IC_IMM_F64:
        tmpi->ic_flags&=~ICF_RES_TO_F64;
        if (cc->pass==2 && tmpi->ic_flags&ICF_RES_TO_INT) {
          tmpi->ic_data=ToI64(tmpi->ic_data(F64));
          tmpi->ic_flags&=~ICF_RES_TO_INT;
          tmpi->ic_code=IC_IMM_I64;
          tmpi->ic_class=cmp.internal_types[RT_I64];
        }
        break;
      case IC_IMM_I64:
        tmpi->ic_flags&=~ICF_RES_TO_INT;
        if (cc->pass==2 && tmpi->ic_flags&ICF_RES_TO_F64) {
          tmpi->ic_data(F64)=ToF64(tmpi->ic_data);
          tmpi->ic_flags&=~ICF_RES_TO_F64;
          tmpi->ic_code=IC_IMM_F64;
          tmpi->ic_class=cmp.internal_types[RT_F64];
        }
        break;
      case IC_HOLYC_TYPECAST:
        if (tmpi1->ic_code==IC_IMM_I64 || tmpi1->ic_code==IC_IMM_F64) {
          if (tmpi->ic_class->raw_type==RT_F64)
            tmpi1->ic_code=IC_IMM_F64;
          else
            tmpi1->ic_code=IC_IMM_I64;
          tmpi1->ic_class=tmpi->ic_class;
          tmpi1->ic_flags|=tmpi->ic_flags;
          tmpi_push=tmpi1;
          OptSetNOP1(tmpi);
        } else {
          if (tmpi->ic_data) {//was paren
            if (!tmpi_push->ic_class->ptr_stars_cnt) {
              if (tmpi_push->ic_class->raw_type==RT_F64)
                tmpi_push->ic_class2=cmp.internal_types[RT_F64];
              else
                tmpi_push->ic_class2=cmp.internal_types[RT_I64];
            }
          } else {
            tmpi1->ic_class=tmpi->ic_class;
            tmpi1->ic_flags|=tmpi->ic_flags;
            tmpi_push=tmpi1;
            OptSetNOP1(tmpi);
          }
        }
        break;
      case IC_FS:
      case IC_GS:
//CALL,FS/GS,CALL_END,IMM,ADD,DEREF-->MOV_FS/GS
        tmpi1=tmpi->next->next; //IMM
        tmpi2=tmpi1->next; //ADD
        tmpi3=tmpi2->next; //DEREF
        if (tmpi1->ic_code==IC_IMM_I64 && tmpi2->ic_code==IC_ADD &&
              tmpi3->ic_code==IC_DEREF &&
              !(tmpi3->ic_flags&~ICG_NO_CVT_MASK)) {
          tmpi->ic_flags|=tmpi1->ic_flags|tmpi2->ic_flags|tmpi3->ic_flags;
          if (tmpi->ic_code==IC_FS)
            tmpi->ic_code=IC_MOV_FS;
          else
            tmpi->ic_code=IC_MOV_GS;
          tmpi->ic_data=tmpi1->ic_data;
          tmpi->ic_class =tmpi3->ic_class;
          tmpi->ic_class2=tmpi3->ic_class2;
          OptSetNOP1(tmpi1);
          OptSetNOP1(tmpi2);
          OptSetNOP1(tmpi3);

          tmpi1=tmpi->last; //CALL
          tmpi2=tmpi->next; //CALL_END
          tmpi->ic_flags|=tmpi1->ic_flags|tmpi2->ic_flags;
          OptSetNOP1(tmpi1);
          OptSetNOP1(tmpi2);
        }
        break;
      case IC_PUSH_CMP:
        if (tmpi1=OptLag(tmpi)) {
          if (tmpi1->ic_code==IC_AND_AND)
            tmpi1=OptLag(tmpi1);
          if (tmpi1)
            tmpi->ic_class=tmpi1->ic_class;
        }
        tmpi->ic_class2=tmpi->ic_class;
        tmpi->ic_data=0;
        if (tmpi->ic_class->raw_type==RT_F64)
          tmpi->ic_flags|=ICF_USE_F64;
        break;
      case IC_COM:
        if (tmpi1->ic_code==IC_IMM_I64) {
          tmpi->ic_data=~tmpi1->ic_data;
          tmpi->ic_code=IC_IMM_I64;
          tmpi->ic_flags|=tmpi1->ic_flags;
          OptSetNOP1(tmpi1);
        }
        tmpi_push->ic_class2=cmp.internal_types[RT_I64];
        break;
      start:
        case IC_NOT:
          if (tmpc->raw_type==RT_F64) {
            if (tmpi1->ic_code==IC_IMM_F64) {
              tmpi->ic_data(F64)=!tmpi1->ic_data(F64);
              tmpi->ic_code=IC_IMM_F64;
              tmpi->ic_flags|=tmpi1->ic_flags;
              OptSetNOP1(tmpi1);
            }
            break;
          }
          if (tmpi1->ic_code==IC_IMM_I64) {
            tmpi->ic_data=!tmpi1->ic_data;
            tmpi->ic_code=IC_IMM_I64;
            tmpi->ic_flags|=tmpi1->ic_flags;
            OptSetNOP1(tmpi1);
          }
          break;
        case IC_UNARY_MINUS:
          if (i=OptFixupUnaryOp(tmpi,tmpi1,&is_unsigned)) {
            if (i==FBO1_INT) {
              tmpi->ic_data=-tmpi1->ic_data(I64);
              tmpi->ic_code=IC_IMM_I64;
            } else {
              tmpi->ic_data(F64)=-tmpi1->ic_data(F64);
              tmpi->ic_code=IC_IMM_F64;
            }
          }
          if (tmpc1->type&HTT_INTERNAL_TYPE && tmpc1->raw_type&RTF_UNSIGNED)
            tmpi

[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\OptPass012-Compiler.html
// Date: unknown-date
// Language: HolyC

/*OptPass012

Pass#0
When parsing the arg expressions to a function
call, there is a call to OptPass012 to determine
the type of the expression. OptPass012

Pass#1&2
Constant expressions are simplified.
Eliminated opcodes are set to NOP.
Types are determined by reconstructing an
expression tree for operators
CIntermediateCode.CICTreeLinks.

Pointer arithmetic size is set, once
the type is determined.

Branches are expressed with short-circuit
logic. 3-Arg comparisons are established.

*/

CIntermediateCode *OptPass012(CCmpCtrl *cc)
{/*Simplify CONST arithmetic.
Sets the class throughout Expression trees.
Returns the type of an Expression for use
in int<-->F64 conversions of fun
args.
*/
  I64 code,i;
  Bool is_unsigned;
  CHashClass *tmpc,*tmpc1,*tmpc2;
  CIntermediateCode *tmpi,*tmpi1,*tmpi2,*tmpi3,*tmpi_push,
        *last_with_class=NULL;
  CCodeMisc *lb,*lb1,*lb2;
  CPrsStk *ps;
  if (!(ps=cc->ps))
    ps=cc->ps=MAlloc(sizeof(CPrsStk));
  ps->ptr=0;
  ps->ptr2=0;
  tmpi=cc->coc.coc_head.next;
  while (code=tmpi->ic_code) {
    tmpc=tmpi->ic_class;
    tmpi->ic_class2=tmpc;
    tmpi_push=tmpi;
    MemSet(&tmpi->arg1,0,3*sizeof(CICArg));
    tmpi->arg1_type_pointed_to=0;
    switch [intermediate_code_table[code].arg_cnt] {
      case IS_V_ARG:
        ps->ptr-=tmpi->ic_data>>2;
        break;
      case IS_2_ARG:
        tmpi2=PrsPop(ps);
        tmpc2=tmpi2->ic_class;
        tmpi->t.arg2_tree=tmpi2;
        tmpi->t.arg2_class=PrsPop(ps);
      case IS_1_ARG:
        tmpi1=PrsPop(ps);
        tmpc1=tmpi1->ic_class;
        tmpi->t.arg1_tree=tmpi1;
        tmpi->t.arg1_class=PrsPop(ps);
        break;
      case IS_0_ARG: //nobound switch
        break;
    }
    if (intermediate_code_table[code].not_const)
      cc->flags|=CCF_NOT_CONST;
    switch [code] {
      case IC_IMM_F64:
        tmpi->ic_flags&=~ICF_RES_TO_F64;
        if (cc->pass==2 && tmpi->ic_flags&ICF_RES_TO_INT) {
          tmpi->ic_data=ToI64(tmpi->ic_data(F64));
          tmpi->ic_flags&=~ICF_RES_TO_INT;
          tmpi->ic_code=IC_IMM_I64;
          tmpi->ic_class=cmp.internal_types[RT_I64];
        }
        break;
      case IC_IMM_I64:
        tmpi->ic_flags&=~ICF_RES_TO_INT;
        if (cc->pass==2 && tmpi->ic_flags&ICF_RES_TO_F64) {
          tmpi->ic_data(F64)=ToF64(tmpi->ic_data);
          tmpi->ic_flags&=~ICF_RES_TO_F64;
          tmpi->ic_code=IC_IMM_F64;
          tmpi->ic_class=cmp.internal_types[RT_F64];
        }
        break;
      case IC_HOLYC_TYPECAST:
        if (tmpi1->ic_code==IC_IMM_I64 || tmpi1->ic_code==IC_IMM_F64) {
          if (tmpi->ic_class->raw_type==RT_F64)
            tmpi1->ic_code=IC_IMM_F64;
          else
            tmpi1->ic_code=IC_IMM_I64;
          tmpi1->ic_class=tmpi->ic_class;
          tmpi1->ic_flags|=tmpi->ic_flags;
          tmpi_push=tmpi1;
          OptSetNOP1(tmpi);
        } else {
          if (tmpi->ic_data) {//was paren
            if (!tmpi_push->ic_class->ptr_stars_cnt) {
              if (tmpi_push->ic_class->raw_type==RT_F64)
                tmpi_push->ic_class2=cmp.internal_types[RT_F64];
              else
                tmpi_push->ic_class2=cmp.internal_types[RT_I64];
            }
          } else {
            tmpi1->ic_class=tmpi->ic_class;
            tmpi1->ic_flags|=tmpi->ic_flags;
            tmpi_push=tmpi1;
            OptSetNOP1(tmpi);
          }
        }
        break;
      case IC_FS:
      case IC_GS:
//CALL,FS/GS,CALL_END,IMM,ADD,DEREF-->MOV_FS/GS
        tmpi1=tmpi->next->next; //IMM
        tmpi2=tmpi1->next; //ADD
        tmpi3=tmpi2->next; //DEREF
        if (tmpi1->ic_code==IC_IMM_I64 && tmpi2->ic_code==IC_ADD &&
              tmpi3->ic_code==IC_DEREF &&
              !(tmpi3->ic_flags&~ICG_NO_CVT_MASK)) {
          tmpi->ic_flags|=tmpi1->ic_flags|tmpi2->ic_flags|tmpi3->ic_flags;
          if (tmpi->ic_code==IC_FS)
            tmpi->ic_code=IC_MOV_FS;
          else
            tmpi->ic_code=IC_MOV_GS;
          tmpi->ic_data=tmpi1->ic_data;
          tmpi->ic_class =tmpi3->ic_class;
          tmpi->ic_class2=tmpi3->ic_class2;
          OptSetNOP1(tmpi1);
          OptSetNOP1(tmpi2);
          OptSetNOP1(tmpi3);

          tmpi1=tmpi->last; //CALL
          tmpi2=tmpi->next; //CALL_END
          tmpi->ic_flags|=tmpi1->ic_flags|tmpi2->ic_flags;
          OptSetNOP1(tmpi1);
          OptSetNOP1(tmpi2);
        }
        break;
      case IC_PUSH_CMP:
        if (tmpi1=OptLag(tmpi)) {
          if (tmpi1->ic_code==IC_AND_AND)
            tmpi1=OptLag(tmpi1);
          if (tmpi1)
            tmpi->ic_class=tmpi1->ic_class;
        }
        tmpi->ic_class2=tmpi->ic_class;
        tmpi->ic_data=0;
        if (tmpi->ic_class->raw_type==RT_F64)
          tmpi->ic_flags|=ICF_USE_F64;
        break;
      case IC_COM:
        if (tmpi1->ic_code==IC_IMM_I64) {
          tmpi->ic_data=~tmpi1->ic_data;
          tmpi->ic_code=IC_IMM_I64;
          tmpi->ic_flags|=tmpi1->ic_flags;
          OptSetNOP1(tmpi1);
        }
        tmpi_push->ic_class2=cmp.internal_types[RT_I64];
        break;
      start:
        case IC_NOT:
          if (tmpc->raw_type==RT_F64) {
            if (tmpi1->ic_code==IC_IMM_F64) {
              tmpi->ic_data(F64)=!tmpi1->ic_data(F64);
              tmpi->ic_code=IC_IMM_F64;
              tmpi->ic_flags|=tmpi1->ic_flags;
              OptSetNOP1(tmpi1);
            }
            break;
          }
          if (tmpi1->ic_code==IC_IMM_I64) {
            tmpi->ic_data=!tmpi1->ic_data;
            tmpi->ic_code=IC_IMM_I64;
            tmpi->ic_flags|=tmpi1->ic_flags;
            OptSetNOP1(tmpi1);
          }
          break;
        case IC_UNARY_MINUS:
          if (i=OptFixupUnaryOp(tmpi,tmpi1,&is_unsigned)) {
            if (i==FBO1_INT) {
              tmpi->ic_data=-tmpi1->ic_data(I64);
              tmpi->ic_code=IC_IMM_I64;
            } else {
              tmpi->ic_data(F64)=-tmpi1->ic_data(F64);
              tmpi->ic_code=IC_IMM_F64;
            }
          }
          if (tmpc1->type&HTT_INTERNAL_TYPE && tmpc1->raw_type&RTF_UNSIGNED)
            tmpi


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

/*
There is a coarse and a fine-grained.
The coarse gets flood-filled but the
fine grained is only outlines.
*/

class Photon
{
  Photon *next,*last;
  CD3 p,v,n,p_normal_inhibit;
} p_root[mp_cnt];
I64 p_root_locks;

#define ANIMATE_JIFFIES (JIFFY_FREQ*0.01)
I64 master_sleep_jiffy;
CTask *animate_tasks[mp_cnt];

#define LENS_COLOR      WHITE
#define MIRROR_COLOR    DKGRAY
CDC *map;

I64 photon_cnt,mirror_cnt,snell_cnt,normal_inhibit,zero_normal;
Bool full_speed,show_normals;

U8      *bmp_refract,*bmp_reflect;
F64     bmp_scale,find_normal_dist_sqr;
I64     bmp_mem,bmp_width,bmp_height,bmp_norm_radius;

#define BORDER  10
I64 BmpPeek(U8 *bmp,I64 x,I64 y)
{
  return Bt(bmp,y*bmp_width+x);
}

U0 BmpPlot(U8 *bmp,I64 x,I64 y,I64)
{
  if (0<=x<bmp_width && 0<=y<bmp_height)
    Bts(bmp,y*bmp_width+x);
}

U0 BmpLine(U8 *bmp,F64 x1,F64 y1,F64 x2,F64 y2)
{
  Line(bmp,x1*bmp_scale,y1*bmp_scale,0,x2*bmp_scale,y2*bmp_scale,0,&BmpPlot);
}

Photon *PhotonNew()
{
  I64 num=photon_cnt++%mp_cnt;
  Photon *res=CAlloc(sizeof(Photon));
  while (LBts(&p_root_locks,num))
    Yield;
  QueIns(res,p_root[num].last);
  LBtr(&p_root_locks,num);
  return res;
}

#define VECTOR 20
U0 DrawIt(CTask *,CDC *dc)
{
  I64 i;
  Photon *tmpp;
  GrBlot(dc,0,0,map);
  dc->color=WHITE;
  GrPrint(dc,0,0,"Mem:0x%X %,dMeg Scale:%0.3f (%d,%d)-->(%d,%d)",
        bmp_mem,bmp_mem/1024/1024,bmp_scale,
        map->width,map->height,bmp_width,bmp_height);
  GrPrint(dc,0,FONT_HEIGHT,
        "PhotonCnt:%d MirrorCnt:%d SnellCnt:%d SnellInhibit:%d ZeroNormal:%d",
        photon_cnt,mirror_cnt,snell_cnt,normal_inhibit,zero_normal);
  for (i=0;i<mp_cnt;i++) {
    while (LBts(&p_root_locks,i))
      Yield;
    tmpp=p_root[i].next;
    while (tmpp!=&p_root[i]) {
      dc->color=LTRED;
      GrLine(dc,tmpp->p.x-VECTOR*tmpp->v.x,tmpp->p.y-VECTOR*tmpp->v.y,
            tmpp->p.x,tmpp->p.y);
      if (show_normals) {
        dc->color=LTGREEN;
        GrLine(dc,tmpp->p.x,tmpp->p.y,
              tmpp->p.x+VECTOR*tmpp->n.x,tmpp->p.y+VECTOR*tmpp->n.y);
      }
      tmpp=tmpp->next;
    }
    LBtr(&p_root_locks,i);
  }
}

#define WING    9

U0 RayBurst(I64 x1,I64 y1,I64 x2,I64 y2)
{
  CD3 p,v,n,n2;
  I64 i;
  Photon *tmpp;
  if ((x1!=x2 || y1!=y2) && BORDER+WING<x2<map->width-BORDER-WING &&
        BORDER+WING<y2<map->height-BORDER-WING) {
    D3Equ(&p,x2,y2);
    D3Equ(&v,x2-x1,y2-y1);
    D3Unit(&v);
    D3Equ(&n,v.y,-v.x);

    tmpp=PhotonNew;
    D3Copy(&tmpp->p,&p);
    D3Copy(&tmpp->v,&v);

    for (i=2;i<=WING;i+=3) {
      D3Mul(&n2,i,&n);

      tmpp=PhotonNew;
      D3Add(&tmpp->p,&p,&n2);
      D3Copy(&tmpp->v,&v);

      tmpp=PhotonNew;
      D3Sub(&tmpp->p,&p,&n2);
      D3Copy(&tmpp->v,&v);
    }
  }
}

U0 RandomBurst()
{
  I64 i;
  F64 theta;
  Photon *tmpp;
  for (i=0;i<256;i++) {
    tmpp=PhotonNew;
    D3Equ(&tmpp->p,(Fs->pix_width-BORDER*2)*Rand+BORDER,
                (Fs->pix_height-BORDER*2)*Rand+BORDER);
    theta=2*pi*Rand;
    D3Equ(&tmpp->v,Cos(theta),Sin(theta));
  }
}

U0 FindNormal(U8 *bmp,Photon *tmpp)
{
  CD3 p,p1,p2;
  F64 step,x,y,theta=Arg(tmpp->v.x,tmpp->v.y),phi;
  I64 state;

  D3Copy(&tmpp->p_normal_inhibit,&tmpp->p);

  //Coarse grains has black and white filled-in BSplines.
  //Fine grained has only white outline without being filled-in.

  //Back-up a step and move fwd to get a fined-grained value
  //for the point of contact.
  D3SubEqu(&tmpp->p,&tmpp->v);
  D3Mul(&p,bmp_scale,&tmpp->p);
  D3Copy(&p1,&p);
  while (BmpPeek(bmp,p1.x,p1.y)==BLACK &&
        D3DistSqr(&p,&p1)<find_normal_dist_sqr)
    D3AddEqu(&p1,&tmpp->v);
  D3Copy(&p,&p1);
  D3Div(&tmpp->p,&p,bmp_scale);

  //Draw an arc one direction, finding point of contact.
  for (step=1.0;step>=0.01;step/=4) {
    for (phi=0;phi<=pi/4;phi+=step*pi/bmp_norm_radius) {
      x=p.x+bmp_norm_radius*Cos(theta+pi-pi/4-phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi-pi/4-phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p1;
      x=p.x+bmp_norm_radius*Cos(theta+pi-pi/4+phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi-pi/4+phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p1;
    }
    for (;phi<=3*pi/4;phi+=step*pi/bmp_norm_radius) {
      x=p.x+bmp_norm_radius*Cos(theta+pi-pi/4-phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi-pi/4-phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p1;
    }
  }
fn_p1:
  if (state)
    D3Equ(&p1,x,y);
  else
    D3Copy(&p1,&tmpp->p);

    //Draw an arc other direction, finding point of contact.
  for (step=1.0;step>=0.01;step/=4) {
    for (phi=0;phi<=pi/4;phi+=step*pi/bmp_norm_radius) {
      x=p.x+bmp_norm_radius*Cos(theta+pi+pi/4+phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi+pi/4+phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p2;
      x=p.x+bmp_norm_radius*Cos(theta+pi+pi/4-phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi+pi/4-phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p2;
    }
    for (;phi<=3*pi/4;phi+=step*pi/bmp_norm_radius) {
      x=p.x+bmp_norm_radius*Cos(theta+pi+pi/4+phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi+pi/4+phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p2;
    }
  }
fn_p2:
  if (state)
    D3Equ(&p2,x,y);
  else
    D3Copy(&p2,&tmpp->p);

  D3Sub(&p,&p1,&p2);
  if (D3NormSqr(&p)<0.01) {
    D3Equ(&tmpp->n,Cos(theta),Sin(theta));
    lock {zero_normal++;}
  } else {
    D3Equ(&tmpp->n,p.y,-p.x);
    if (D3Dot(&tmpp->n,&tmpp->v)<0)
      D3Equ(&tmpp->n,-p.y,p.x);
    D3Unit(&tmpp->n);
  }
}

U0 Mirror(Photon *tmpp)
{/*<1>/* Graphics Not Rendered in HTML */

thetaout = pi+thetan -  (thetain-thetan)

*/
  F64 theta=Arg(tmpp->v.x,tmpp->v.y),thetan;
  FindNormal(bmp_reflect,tmpp);
  thetan=Arg(tmpp->n.x,tmpp->n.y);

  D3Equ(&tmpp->v,Cos(2*thetan+pi-theta),Sin(2*thetan+pi-theta));
  lock {mirror_cnt++;}
}

U0 SnellsLaw(Photon *tmpp,I64 last,I64 next)
{
//n1 and n2 are refraction index.
//n1 Sin(theta1) == n2 Sin(theta2)
  F64 theta=Arg(tmpp->v.x,tmpp->v.y),thetan,n1,n2,theta1,theta2;
  if (last==LENS_COLOR)
    n1=1.5;
  else
    n1=1.0;
  if (next==LENS_COLOR)
    n2=1.5;
  else
    n2=1.0;

[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\LightTable-Graphics-Demo.html
// Date: unknown-date
// Language: HolyC

/*
There is a coarse and a fine-grained.
The coarse gets flood-filled but the
fine grained is only outlines.
*/

class Photon
{
  Photon *next,*last;
  CD3 p,v,n,p_normal_inhibit;
} p_root[mp_cnt];
I64 p_root_locks;

#define ANIMATE_JIFFIES (JIFFY_FREQ*0.01)
I64 master_sleep_jiffy;
CTask *animate_tasks[mp_cnt];

#define LENS_COLOR      WHITE
#define MIRROR_COLOR    DKGRAY
CDC *map;

I64 photon_cnt,mirror_cnt,snell_cnt,normal_inhibit,zero_normal;
Bool full_speed,show_normals;

U8      *bmp_refract,*bmp_reflect;
F64     bmp_scale,find_normal_dist_sqr;
I64     bmp_mem,bmp_width,bmp_height,bmp_norm_radius;

#define BORDER  10
I64 BmpPeek(U8 *bmp,I64 x,I64 y)
{
  return Bt(bmp,y*bmp_width+x);
}

U0 BmpPlot(U8 *bmp,I64 x,I64 y,I64)
{
  if (0<=x<bmp_width && 0<=y<bmp_height)
    Bts(bmp,y*bmp_width+x);
}

U0 BmpLine(U8 *bmp,F64 x1,F64 y1,F64 x2,F64 y2)
{
  Line(bmp,x1*bmp_scale,y1*bmp_scale,0,x2*bmp_scale,y2*bmp_scale,0,&BmpPlot);
}

Photon *PhotonNew()
{
  I64 num=photon_cnt++%mp_cnt;
  Photon *res=CAlloc(sizeof(Photon));
  while (LBts(&p_root_locks,num))
    Yield;
  QueIns(res,p_root[num].last);
  LBtr(&p_root_locks,num);
  return res;
}

#define VECTOR 20
U0 DrawIt(CTask *,CDC *dc)
{
  I64 i;
  Photon *tmpp;
  GrBlot(dc,0,0,map);
  dc->color=WHITE;
  GrPrint(dc,0,0,"Mem:0x%X %,dMeg Scale:%0.3f (%d,%d)-->(%d,%d)",
        bmp_mem,bmp_mem/1024/1024,bmp_scale,
        map->width,map->height,bmp_width,bmp_height);
  GrPrint(dc,0,FONT_HEIGHT,
        "PhotonCnt:%d MirrorCnt:%d SnellCnt:%d SnellInhibit:%d ZeroNormal:%d",
        photon_cnt,mirror_cnt,snell_cnt,normal_inhibit,zero_normal);
  for (i=0;i<mp_cnt;i++) {
    while (LBts(&p_root_locks,i))
      Yield;
    tmpp=p_root[i].next;
    while (tmpp!=&p_root[i]) {
      dc->color=LTRED;
      GrLine(dc,tmpp->p.x-VECTOR*tmpp->v.x,tmpp->p.y-VECTOR*tmpp->v.y,
            tmpp->p.x,tmpp->p.y);
      if (show_normals) {
        dc->color=LTGREEN;
        GrLine(dc,tmpp->p.x,tmpp->p.y,
              tmpp->p.x+VECTOR*tmpp->n.x,tmpp->p.y+VECTOR*tmpp->n.y);
      }
      tmpp=tmpp->next;
    }
    LBtr(&p_root_locks,i);
  }
}

#define WING    9

U0 RayBurst(I64 x1,I64 y1,I64 x2,I64 y2)
{
  CD3 p,v,n,n2;
  I64 i;
  Photon *tmpp;
  if ((x1!=x2 || y1!=y2) && BORDER+WING<x2<map->width-BORDER-WING &&
        BORDER+WING<y2<map->height-BORDER-WING) {
    D3Equ(&p,x2,y2);
    D3Equ(&v,x2-x1,y2-y1);
    D3Unit(&v);
    D3Equ(&n,v.y,-v.x);

    tmpp=PhotonNew;
    D3Copy(&tmpp->p,&p);
    D3Copy(&tmpp->v,&v);

    for (i=2;i<=WING;i+=3) {
      D3Mul(&n2,i,&n);

      tmpp=PhotonNew;
      D3Add(&tmpp->p,&p,&n2);
      D3Copy(&tmpp->v,&v);

      tmpp=PhotonNew;
      D3Sub(&tmpp->p,&p,&n2);
      D3Copy(&tmpp->v,&v);
    }
  }
}

U0 RandomBurst()
{
  I64 i;
  F64 theta;
  Photon *tmpp;
  for (i=0;i<256;i++) {
    tmpp=PhotonNew;
    D3Equ(&tmpp->p,(Fs->pix_width-BORDER*2)*Rand+BORDER,
                (Fs->pix_height-BORDER*2)*Rand+BORDER);
    theta=2*pi*Rand;
    D3Equ(&tmpp->v,Cos(theta),Sin(theta));
  }
}

U0 FindNormal(U8 *bmp,Photon *tmpp)
{
  CD3 p,p1,p2;
  F64 step,x,y,theta=Arg(tmpp->v.x,tmpp->v.y),phi;
  I64 state;

  D3Copy(&tmpp->p_normal_inhibit,&tmpp->p);

  //Coarse grains has black and white filled-in BSplines.
  //Fine grained has only white outline without being filled-in.

  //Back-up a step and move fwd to get a fined-grained value
  //for the point of contact.
  D3SubEqu(&tmpp->p,&tmpp->v);
  D3Mul(&p,bmp_scale,&tmpp->p);
  D3Copy(&p1,&p);
  while (BmpPeek(bmp,p1.x,p1.y)==BLACK &&
        D3DistSqr(&p,&p1)<find_normal_dist_sqr)
    D3AddEqu(&p1,&tmpp->v);
  D3Copy(&p,&p1);
  D3Div(&tmpp->p,&p,bmp_scale);

  //Draw an arc one direction, finding point of contact.
  for (step=1.0;step>=0.01;step/=4) {
    for (phi=0;phi<=pi/4;phi+=step*pi/bmp_norm_radius) {
      x=p.x+bmp_norm_radius*Cos(theta+pi-pi/4-phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi-pi/4-phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p1;
      x=p.x+bmp_norm_radius*Cos(theta+pi-pi/4+phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi-pi/4+phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p1;
    }
    for (;phi<=3*pi/4;phi+=step*pi/bmp_norm_radius) {
      x=p.x+bmp_norm_radius*Cos(theta+pi-pi/4-phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi-pi/4-phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p1;
    }
  }
fn_p1:
  if (state)
    D3Equ(&p1,x,y);
  else
    D3Copy(&p1,&tmpp->p);

    //Draw an arc other direction, finding point of contact.
  for (step=1.0;step>=0.01;step/=4) {
    for (phi=0;phi<=pi/4;phi+=step*pi/bmp_norm_radius) {
      x=p.x+bmp_norm_radius*Cos(theta+pi+pi/4+phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi+pi/4+phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p2;
      x=p.x+bmp_norm_radius*Cos(theta+pi+pi/4-phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi+pi/4-phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p2;
    }
    for (;phi<=3*pi/4;phi+=step*pi/bmp_norm_radius) {
      x=p.x+bmp_norm_radius*Cos(theta+pi+pi/4+phi);
      y=p.y+bmp_norm_radius*Sin(theta+pi+pi/4+phi);
      if (state=BmpPeek(bmp,x,y))
        goto fn_p2;
    }
  }
fn_p2:
  if (state)
    D3Equ(&p2,x,y);
  else
    D3Copy(&p2,&tmpp->p);

  D3Sub(&p,&p1,&p2);
  if (D3NormSqr(&p)<0.01) {
    D3Equ(&tmpp->n,Cos(theta),Sin(theta));
    lock {zero_normal++;}
  } else {
    D3Equ(&tmpp->n,p.y,-p.x);
    if (D3Dot(&tmpp->n,&tmpp->v)<0)
      D3Equ(&tmpp->n,-p.y,p.x);
    D3Unit(&tmpp->n);
  }
}

U0 Mirror(Photon *tmpp)
{/*<1>/* Graphics Not Rendered in HTML */

thetaout = pi+thetan -  (thetain-thetan)

*/
  F64 theta=Arg(tmpp->v.x,tmpp->v.y),thetan;
  FindNormal(bmp_reflect,tmpp);
  thetan=Arg(tmpp->n.x,tmpp->n.y);

  D3Equ(&tmpp->v,Cos(2*thetan+pi-theta),Sin(2*thetan+pi-theta));
  lock {mirror_cnt++;}
}

U0 SnellsLaw(Photon *tmpp,I64 last,I64 next)
{
//n1 and n2 are refraction index.
//n1 Sin(theta1) == n2 Sin(theta2)
  F64 theta=Arg(tmpp->v.x,tmpp->v.y),thetan,n1,n2,theta1,theta2;
  if (last==LENS_COLOR)
    n1=1.5;
  else
    n1=1.0;
  if (next==LENS_COLOR)
    n2=1.5;
  else
    n2=1.0;


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

U0 Toward(I64 *_row,I64 *_col,I64 direction)
{
  switch (direction) {
    case 0:
      *_row-=2;
      break;
    case 1:
      if (*_row&1) *_col+=1;
      *_row-=1;
      break;
    case 2:
      if (*_row&1) *_col+=1;
      *_row+=1;
      break;
    case 3:
      *_row+=2;
      break;
    case 4:
      if (!(*_row&1)) *_col-=1;
      *_row+=1;
      break;
    case 5:
      if (!(*_row&1)) *_col-=1;
      *_row-=1;
      break;
  }
}

I64 FacingChg(I64 f1,I64 f2)
{
  I64 res=(f1+6-f2)%6;
  if (res>=3)
    return 6-res;
  else
    return res;
}

U0 RowCol2XY(F64 *_x,F64 *_y,I64 row,I64 col)
{
  Pt *c;
  row=ClampI64(row,0,map_rows);
  col=ClampI64(col,0,map_cols);
  c=&hex_centers[row][col];
  *_x=c->x;
  *_y=c->y;
}

U0 XY2RowCol(I64 *_row,I64 *_col,F64 x,F64 y)
{
  *_col=(x-DCOS/2)/(HEX_SIDE+DCOS);
  if (*_col&1)
    *_row=ToI64((y-DSIN)/(2*DSIN))*2+1;
  else
    *_row=ToI64(y/(2*DSIN))*2;
  *_col>>=1;
  *_row=ClampI64(*_row,0,map_rows-1);
  *_col=ClampI64(*_col,0,map_cols-1);
}

Unit *UnitFind(I64 row,I64 col)
{//Finds unit in a hexagon.
  I64 i,j;
  for (j=0;j<2;j++)
    for (i=0;i<UNITS_NUM;i++)
      if (units[j][i].life>0 &&
            units[j][i].row==row &&
            units[j][i].col==col)
        return &units[j][i];
  return NULL;
}

Bool CursorInWin(CTask *task,I64 x,I64 y)
{
  if (0<=x+task->scroll_x<task->pix_width &&
        0<=y+task->scroll_y<task->pix_height)
    return TRUE;
  else
    return FALSE;
}

U0 CursorUpdate(CTask *task,I64 x,I64 y)
{
  if (CursorInWin(task,x,y))
    XY2RowCol(&cursor_row,&cursor_col,x,y);
}

class LOSCtrl
{
  I64 r1,c1,r2,c2,distance;
};

Bool LOSPlot(LOSCtrl *l,I64 x,I64 y,I64 z)
{//We got tricky and used z as the distance from the start of the line.
  I64 row,col;
  XY2RowCol(&row,&col,x,y);
  if ((row!=l->r1 || col!=l->c1) &&
        (row!=l->r2 || col!=l->c2) &&
        terrain[row][col]!=PLAINS) {
    if (terrain[l->r1][l->c1]==MOUNTAINS) {
      if (terrain[row][col]==MOUNTAINS || z>l->distance>>1)
        return FALSE;
    } else if (terrain[l->r2][l->c2]==MOUNTAINS) {
      if (terrain[row][col]==MOUNTAINS || z<=l->distance>>1)
        return FALSE;
    } else
      return FALSE;
  }
  return TRUE;
}

Bool LOS(I64 r1,I64 c1,I64 r2,I64 c2)
{
  F64 x1,y1,x2,y2;
  LOSCtrl l;
  RowCol2XY(&x1,&y1,r1,c1);
  RowCol2XY(&x2,&y2,r2,c2);
  l.r1=r1; l.c1=c1;
  l.r2=r2; l.c2=c2;
  l.distance=Sqrt(SqrI64(x1-x2)+SqrI64(y1-y2));
  return Line(&l,x1,y1,0,x2,y2,l.distance,&LOSPlot);
}

[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\TTFLib-ToTheFront-Apps.html
// Date: unknown-date
// Language: HolyC

U0 Toward(I64 *_row,I64 *_col,I64 direction)
{
  switch (direction) {
    case 0:
      *_row-=2;
      break;
    case 1:
      if (*_row&1) *_col+=1;
      *_row-=1;
      break;
    case 2:
      if (*_row&1) *_col+=1;
      *_row+=1;
      break;
    case 3:
      *_row+=2;
      break;
    case 4:
      if (!(*_row&1)) *_col-=1;
      *_row+=1;
      break;
    case 5:
      if (!(*_row&1)) *_col-=1;
      *_row-=1;
      break;
  }
}

I64 FacingChg(I64 f1,I64 f2)
{
  I64 res=(f1+6-f2)%6;
  if (res>=3)
    return 6-res;
  else
    return res;
}

U0 RowCol2XY(F64 *_x,F64 *_y,I64 row,I64 col)
{
  Pt *c;
  row=ClampI64(row,0,map_rows);
  col=ClampI64(col,0,map_cols);
  c=&hex_centers[row][col];
  *_x=c->x;
  *_y=c->y;
}

U0 XY2RowCol(I64 *_row,I64 *_col,F64 x,F64 y)
{
  *_col=(x-DCOS/2)/(HEX_SIDE+DCOS);
  if (*_col&1)
    *_row=ToI64((y-DSIN)/(2*DSIN))*2+1;
  else
    *_row=ToI64(y/(2*DSIN))*2;
  *_col>>=1;
  *_row=ClampI64(*_row,0,map_rows-1);
  *_col=ClampI64(*_col,0,map_cols-1);
}

Unit *UnitFind(I64 row,I64 col)
{//Finds unit in a hexagon.
  I64 i,j;
  for (j=0;j<2;j++)
    for (i=0;i<UNITS_NUM;i++)
      if (units[j][i].life>0 &&
            units[j][i].row==row &&
            units[j][i].col==col)
        return &units[j][i];
  return NULL;
}

Bool CursorInWin(CTask *task,I64 x,I64 y)
{
  if (0<=x+task->scroll_x<task->pix_width &&
        0<=y+task->scroll_y<task->pix_height)
    return TRUE;
  else
    return FALSE;
}

U0 CursorUpdate(CTask *task,I64 x,I64 y)
{
  if (CursorInWin(task,x,y))
    XY2RowCol(&cursor_row,&cursor_col,x,y);
}

class LOSCtrl
{
  I64 r1,c1,r2,c2,distance;
};

Bool LOSPlot(LOSCtrl *l,I64 x,I64 y,I64 z)
{//We got tricky and used z as the distance from the start of the line.
  I64 row,col;
  XY2RowCol(&row,&col,x,y);
  if ((row!=l->r1 || col!=l->c1) &&
        (row!=l->r2 || col!=l->c2) &&
        terrain[row][col]!=PLAINS) {
    if (terrain[l->r1][l->c1]==MOUNTAINS) {
      if (terrain[row][col]==MOUNTAINS || z>l->distance>>1)
        return FALSE;
    } else if (terrain[l->r2][l->c2]==MOUNTAINS) {
      if (terrain[row][col]==MOUNTAINS || z<=l->distance>>1)
        return FALSE;
    } else
      return FALSE;
  }
  return TRUE;
}

Bool LOS(I64 r1,I64 c1,I64 r2,I64 c2)
{
  F64 x1,y1,x2,y2;
  LOSCtrl l;
  RowCol2XY(&x1,&y1,r1,c1);
  RowCol2XY(&x2,&y2,r2,c2);
  l.r1=r1; l.c1=c1;
  l.r2=r2; l.c2=c2;
  l.distance=Sqrt(SqrI64(x1-x2)+SqrI64(y1-y2));
  return Line(&l,x1,y1,0,x2,y2,l.distance,&LOSPlot);
}


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

//5 has words
U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.902;
  try {
    while (!ScanKey) {
      "$GREEN$To God...$FG$\n";
      Play("M5/4");
      Play("5eEqCeEFEDC4qB5eEqCeEFEDC4qB",
            "Fa\0ther, \0for\0give \0them \0if \0you \0must.\n\0"
            "I'd \0like \0to \0see \0them \0turn \0to \0dust.\n\0");
      Play("M4/4");
      Play("5sG4B5G4BetA5F4G5eEFsEGEGqD4sB5D4B5DGFGF4etG5G4B");
    }
  } 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\environeth-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has words
U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.902;
  try {
    while (!ScanKey) {
      "$GREEN$To God...$FG$\n";
      Play("M5/4");
      Play("5eEqCeEFEDC4qB5eEqCeEFEDC4qB",
            "Fa\0ther, \0for\0give \0them \0if \0you \0must.\n\0"
            "I'd \0like \0to \0see \0them \0turn \0to \0dust.\n\0");
      Play("M4/4");
      Play("5sG4B5G4BetA5F4G5eEFsEGEGqD4sB5D4B5DGFGF4etG5G4B");
    }
  } catch
    PutExcept;
  Snd;
}

Song;


// Source: D:\TempleOS-Projects\html\KeyAlloc-Doc.html
// Date: unknown-date
// Language: HolyC

Key Allocations

See Char for definition of scan codes.

See Key Map for a detailed list of key commands.

When you are at the cmd line, editing documents, browsing documentation and
help, entering items in forms or in menu's, the DolDoc editor handles keys.  It
allows you to define your own key hndlrs in a MyPutKey() function.  If you
choose, you can catch keys, overriding the default hndlrs.  See DocPutKey().
The following is an overview of key allocations.

<ALT-keys> and <ALT-SHIFT-keys>  Free for user configurations in your MyPutKey()
hndlr, except for ALT-BACKSPACE (undo).  There are a few examples pre-defined,
but you can change them if you wish.

<CTRL-ALT-keys> and <CTRL-ALT-SHIFT-keys>  Handled at a system level, NOT by the
CDoc editor.  I reserve the right to alloc these, but in the mean time, you can
define your own hndlrs with CtrlAltCBSet().  They operate either in a interrupt
environment or in the window mgr when it queues kbd msgs.  You can do Raw()
output.  <CTRL-ALT-letter> hndlrs take a scan_code as an arg.

<CTRL-function key>  Auto-completes local words.

<CTRL-digit key>  Auto-completes dictionary words.

<CTRL-SHIFT-function key>  Jumps to src code.

<CTRL-SHIFT-digit key>  Jumps to dictionary definition.

<function keys> and <SHIFT-function keys>  I reserve the right to alloc these,
but there are some free now.

<CTRL-key> and <CTRL-SHIFT-key>  I reserve the right to alloc to these.  There
are not many free.

See Keyboard Devices.

[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\KeyAlloc-Doc.html
// Date: unknown-date
// Language: HolyC

Key Allocations

See Char for definition of scan codes.

See Key Map for a detailed list of key commands.

When you are at the cmd line, editing documents, browsing documentation and
help, entering items in forms or in menu's, the DolDoc editor handles keys.  It
allows you to define your own key hndlrs in a MyPutKey() function.  If you
choose, you can catch keys, overriding the default hndlrs.  See DocPutKey().
The following is an overview of key allocations.

<ALT-keys> and <ALT-SHIFT-keys>  Free for user configurations in your MyPutKey()
hndlr, except for ALT-BACKSPACE (undo).  There are a few examples pre-defined,
but you can change them if you wish.

<CTRL-ALT-keys> and <CTRL-ALT-SHIFT-keys>  Handled at a system level, NOT by the
CDoc editor.  I reserve the right to alloc these, but in the mean time, you can
define your own hndlrs with CtrlAltCBSet().  They operate either in a interrupt
environment or in the window mgr when it queues kbd msgs.  You can do Raw()
output.  <CTRL-ALT-letter> hndlrs take a scan_code as an arg.

<CTRL-function key>  Auto-completes local words.

<CTRL-digit key>  Auto-completes dictionary words.

<CTRL-SHIFT-function key>  Jumps to src code.

<CTRL-SHIFT-digit key>  Jumps to dictionary definition.

<function keys> and <SHIFT-function keys>  I reserve the right to alloc these,
but there are some free now.

<CTRL-key> and <CTRL-SHIFT-key>  I reserve the right to alloc to these.  There
are not many free.

See Keyboard Devices.


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

//5 has words
U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 1.765;
  music.stacatto_factor= 0.681;
  try {
    while (!ScanKey) {
      Play("5etC4B5CDDEqCDetEFGGGGEFAqE",
            "Stab\0 \0bed \0right \0in \0the \0back.\n\0"
            " \0Right \0in \0the \0back \0 \0 \0by \0 \0my \0friends.\n\0");
      Play("5etC4B5CDDEqCDetEFGGGGEFAqE",
            "Stab\0 \0bed \0right \0in \0the \0back.\n\0"
            " \0Right \0in \0the \0back \0 \0 \0by \0 \0my \0friends.\n\0");
      Play("5eEFetCGCeFCetG4B5GDEEeCEsGEGEeEG",
            " \0How \0 \0could \0 \0you?\n\0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0");
      Play("5EFetCGCeFCetG4B5GDEEeCEsGEGEeEG",
            " \0How \0 \0could \0 \0you?\n\0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \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\caesar-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has words
U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 1.765;
  music.stacatto_factor= 0.681;
  try {
    while (!ScanKey) {
      Play("5etC4B5CDDEqCDetEFGGGGEFAqE",
            "Stab\0 \0bed \0right \0in \0the \0back.\n\0"
            " \0Right \0in \0the \0back \0 \0 \0by \0 \0my \0friends.\n\0");
      Play("5etC4B5CDDEqCDetEFGGGGEFAqE",
            "Stab\0 \0bed \0right \0in \0the \0back.\n\0"
            " \0Right \0in \0the \0back \0 \0 \0by \0 \0my \0friends.\n\0");
      Play("5eEFetCGCeFCetG4B5GDEEeCEsGEGEeEG",
            " \0How \0 \0could \0 \0you?\n\0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0");
      Play("5EFetCGCeFCetG4B5GDEEeCEsGEGEeEG",
            " \0How \0 \0could \0 \0you?\n\0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0");
    }
  } catch
    PutExcept;
  Snd;
}

Song;


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

/*Uses fixed-point arithmetic
because it used to be faster than floating
point.  See ::/Demo/Lectures/FixedPoint.HC.

The decimal place is between
bits 31 and 32.
*/

#define BALLS_NUM       64
#define BALL_RADIUS     5

I64 ball_x[BALLS_NUM],ball_y[BALLS_NUM],
   ball_velocity_x[BALLS_NUM],ball_velocity_y[BALLS_NUM];

U0 DrawIt(CTask *,CDC *dc)
{
  I64 i;
  dc->color=RED;
  for (i=0;i<BALLS_NUM;i++)
    GrCircle(dc,ball_x[i].i32[1],ball_y[i].i32[1],BALL_RADIUS);
}

/****

/* Graphics Not Rendered in HTML */

Initial and final velocity vects
with normal and tangential components.
All masses are ident, so they
have been dropped from the equations.

Conservation of Momentum:

V1it+V2it=V1ft+V2ft

V1in+V2in=V1fn+V2fn

Conservation of Energy:

|V1i|2+|V2i|2=|V1f|2+|V2f|2
****/

U0 AnimateTask(I64)
{
  CTask *task=Fs->parent_task;
  I64 i,j,h,v,distdist,
        dia=(2*BALL_RADIUS)<<32,
        diadia=SqrI64(2*BALL_RADIUS)<<32,
        delta_x,delta_y,v_t1,v_n1,v_t2,v_n2;
  F64 dist;
  while (TRUE) {
    h=task->pix_width;
    v=task->pix_height;
    for (i=0;i<BALLS_NUM;i++) {
      ball_x[i]+=ball_velocity_x[i];
      ball_y[i]+=ball_velocity_y[i];
      if (ball_x[i]<BALL_RADIUS<<32) {
        ball_velocity_x[i]*=-1;
        ball_x[i]=BALL_RADIUS<<32;
      }
      if (ball_x[i]>=(h-BALL_RADIUS)<<32) {
        ball_velocity_x[i]*=-1;
        ball_x[i]=(h-BALL_RADIUS)<<32;
      }
      if (ball_y[i]<BALL_RADIUS<<32) {
        ball_velocity_y[i]*=-1;
        ball_y[i]=BALL_RADIUS<<32;
      }
      if (ball_y[i]>=(v-BALL_RADIUS)<<32) {
        ball_velocity_y[i]*=-1;
        ball_y[i]=(v-BALL_RADIUS)<<32;
      }
    }
    for (i=0;i<BALLS_NUM;i++) {
      for (j=i+1;j<BALLS_NUM;j++) {
        delta_x=ball_x[i]-ball_x[j];
        delta_y=ball_y[i]-ball_y[j];

        //We shift 16 because multiplying
        //two 32 shifted would yield 64 shifted
        //and we want a 32 shifted res.
        distdist=SqrI64(delta_x>>16)+SqrI64(delta_y>>16);

        //We work with square instead of sqrt
        //to avoid unnecessarily calculating
        //square heads (They are slow.)
        if (distdist && distdist<=diadia) {
          dist=Sqrt(distdist); //shifted 16 bits
          delta_x/=dist; //shifted 16
          delta_y/=dist;

          v_t1=(ball_velocity_x[i]>>16*delta_y-
                ball_velocity_y[i]>>16*delta_x)>>16;
          v_n1=(ball_velocity_x[i]>>16*delta_x+
                ball_velocity_y[i]>>16*delta_y)>>16;
          v_t2=(ball_velocity_x[j]>>16*delta_y-
                ball_velocity_y[j]>>16*delta_x)>>16;
          v_n2=(ball_velocity_x[j]>>16*delta_x+
                ball_velocity_y[j]>>16*delta_y)>>16;

          if (ball_velocity_x[i]>>16*ball_velocity_x[j]>>16+
                ball_velocity_y[i]>>16*ball_velocity_y[j]>>16<=0) {
            ball_velocity_x[i]= v_t1*delta_y-v_n1*delta_x;
            ball_velocity_y[i]=-v_t1*delta_x-v_n1*delta_y;
            ball_velocity_x[j]= v_t2*delta_y-v_n2*delta_x;
            ball_velocity_y[j]=-v_t2*delta_x-v_n2*delta_y;
          } else {
            ball_velocity_x[i]= v_t1*delta_y+v_n2*delta_x;
            ball_velocity_y[i]=-v_t1*delta_x+v_n2*delta_y;
            ball_velocity_x[j]= v_t2*delta_y+v_n1*delta_x;
            ball_velocity_y[j]=-v_t2*delta_x+v_n1*delta_y;
          }

          //Correct for overlap
          dist=0x10000+(dia/0x10000-dist)/2;
          ball_x[i]+=dist*delta_x;
          ball_y[i]+=dist*delta_y;
          ball_x[j]-=dist*delta_x;
          ball_y[j]-=dist*delta_y;
        }
      }
    }
    Sleep(1);
  }
}

U0 Init()
{
  I64 i;
  for (i=0;i<BALLS_NUM;i++) {
    ball_x[i]=(RandU16%(Fs->pix_width-BALL_RADIUS*2)+BALL_RADIUS)<<32;
    ball_y[i]=(RandU16%(Fs->pix_height-BALL_RADIUS*2)+BALL_RADIUS)<<32;
    ball_velocity_x[i]=RandI32/4;
    ball_velocity_y[i]=RandI32/4;
  }
}

U0 Collision()
{
  SettingsPush; //See SettingsPush
  Init;
  Fs->animate_task=Spawn(&AnimateTask,NULL,"Animate",,Fs);
  DocCursor;
  DocClear;
  Fs->draw_it=&DrawIt;
  GetChar;
  SettingsPop;
}

Collision;

[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\Collision-Games-Demo.html
// Date: unknown-date
// Language: HolyC

/*Uses fixed-point arithmetic
because it used to be faster than floating
point.  See ::/Demo/Lectures/FixedPoint.HC.

The decimal place is between
bits 31 and 32.
*/

#define BALLS_NUM       64
#define BALL_RADIUS     5

I64 ball_x[BALLS_NUM],ball_y[BALLS_NUM],
   ball_velocity_x[BALLS_NUM],ball_velocity_y[BALLS_NUM];

U0 DrawIt(CTask *,CDC *dc)
{
  I64 i;
  dc->color=RED;
  for (i=0;i<BALLS_NUM;i++)
    GrCircle(dc,ball_x[i].i32[1],ball_y[i].i32[1],BALL_RADIUS);
}

/****

/* Graphics Not Rendered in HTML */

Initial and final velocity vects
with normal and tangential components.
All masses are ident, so they
have been dropped from the equations.

Conservation of Momentum:

V1it+V2it=V1ft+V2ft

V1in+V2in=V1fn+V2fn

Conservation of Energy:

|V1i|2+|V2i|2=|V1f|2+|V2f|2
****/

U0 AnimateTask(I64)
{
  CTask *task=Fs->parent_task;
  I64 i,j,h,v,distdist,
        dia=(2*BALL_RADIUS)<<32,
        diadia=SqrI64(2*BALL_RADIUS)<<32,
        delta_x,delta_y,v_t1,v_n1,v_t2,v_n2;
  F64 dist;
  while (TRUE) {
    h=task->pix_width;
    v=task->pix_height;
    for (i=0;i<BALLS_NUM;i++) {
      ball_x[i]+=ball_velocity_x[i];
      ball_y[i]+=ball_velocity_y[i];
      if (ball_x[i]<BALL_RADIUS<<32) {
        ball_velocity_x[i]*=-1;
        ball_x[i]=BALL_RADIUS<<32;
      }
      if (ball_x[i]>=(h-BALL_RADIUS)<<32) {
        ball_velocity_x[i]*=-1;
        ball_x[i]=(h-BALL_RADIUS)<<32;
      }
      if (ball_y[i]<BALL_RADIUS<<32) {
        ball_velocity_y[i]*=-1;
        ball_y[i]=BALL_RADIUS<<32;
      }
      if (ball_y[i]>=(v-BALL_RADIUS)<<32) {
        ball_velocity_y[i]*=-1;
        ball_y[i]=(v-BALL_RADIUS)<<32;
      }
    }
    for (i=0;i<BALLS_NUM;i++) {
      for (j=i+1;j<BALLS_NUM;j++) {
        delta_x=ball_x[i]-ball_x[j];
        delta_y=ball_y[i]-ball_y[j];

        //We shift 16 because multiplying
        //two 32 shifted would yield 64 shifted
        //and we want a 32 shifted res.
        distdist=SqrI64(delta_x>>16)+SqrI64(delta_y>>16);

        //We work with square instead of sqrt
        //to avoid unnecessarily calculating
        //square heads (They are slow.)
        if (distdist && distdist<=diadia) {
          dist=Sqrt(distdist); //shifted 16 bits
          delta_x/=dist; //shifted 16
          delta_y/=dist;

          v_t1=(ball_velocity_x[i]>>16*delta_y-
                ball_velocity_y[i]>>16*delta_x)>>16;
          v_n1=(ball_velocity_x[i]>>16*delta_x+
                ball_velocity_y[i]>>16*delta_y)>>16;
          v_t2=(ball_velocity_x[j]>>16*delta_y-
                ball_velocity_y[j]>>16*delta_x)>>16;
          v_n2=(ball_velocity_x[j]>>16*delta_x+
                ball_velocity_y[j]>>16*delta_y)>>16;

          if (ball_velocity_x[i]>>16*ball_velocity_x[j]>>16+
                ball_velocity_y[i]>>16*ball_velocity_y[j]>>16<=0) {
            ball_velocity_x[i]= v_t1*delta_y-v_n1*delta_x;
            ball_velocity_y[i]=-v_t1*delta_x-v_n1*delta_y;
            ball_velocity_x[j]= v_t2*delta_y-v_n2*delta_x;
            ball_velocity_y[j]=-v_t2*delta_x-v_n2*delta_y;
          } else {
            ball_velocity_x[i]= v_t1*delta_y+v_n2*delta_x;
            ball_velocity_y[i]=-v_t1*delta_x+v_n2*delta_y;
            ball_velocity_x[j]= v_t2*delta_y+v_n1*delta_x;
            ball_velocity_y[j]=-v_t2*delta_x+v_n1*delta_y;
          }

          //Correct for overlap
          dist=0x10000+(dia/0x10000-dist)/2;
          ball_x[i]+=dist*delta_x;
          ball_y[i]+=dist*delta_y;
          ball_x[j]-=dist*delta_x;
          ball_y[j]-=dist*delta_y;
        }
      }
    }
    Sleep(1);
  }
}

U0 Init()
{
  I64 i;
  for (i=0;i<BALLS_NUM;i++) {
    ball_x[i]=(RandU16%(Fs->pix_width-BALL_RADIUS*2)+BALL_RADIUS)<<32;
    ball_y[i]=(RandU16%(Fs->pix_height-BALL_RADIUS*2)+BALL_RADIUS)<<32;
    ball_velocity_x[i]=RandI32/4;
    ball_velocity_y[i]=RandI32/4;
  }
}

U0 Collision()
{
  SettingsPush; //See SettingsPush
  Init;
  Fs->animate_task=Spawn(&AnimateTask,NULL,"Animate",,Fs);
  DocCursor;
  DocClear;
  Fs->draw_it=&DrawIt;
  GetChar;
  SettingsPop;
}

Collision;


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

#help_index "DolDoc/Misc"

U8 *ctrl_L_footer=
        "\n$MU-X+Q,\"Abort\",LE=DOCM_CANCEL$\n"
        "\n\n$LK+PU,\"Click for Help\",A=\"FI:::/Doc/Widget.DD\"$\n";

I64 PopUpLinkType(Bool include_anchor)
{
  I64 i;
  CDoc *doc=DocNew;
  DocPrint(doc,"$PURPLE$$TX+CX,\"Link Type Menu\"$\n"
        "\n$LTBLUE$$MU,\"To file\",LE=LK_FILE$\n"
        "$MU,\"To anchor in file\",LE=LK_FILE_ANCHOR$\n"
        "$MU,\"To str in file\",LE=LK_FILE_FIND$\n"
        "$MU,\"To line in file\",LE=LK_FILE_LINE$\n"
        "$MU,\"To man page\",LE=LK_MAN_PAGE$\n"
        "$MU,\"To plain-text file\",LE=LK_PLAIN$\n"
        "$MU,\"To str in plain-text file\",LE=LK_PLAIN_FIND$\n"
        "$MU,\"To line in plain-text file\",LE=LK_PLAIN_LINE$\n"
        "$MU,\"To Bible chapter line and verse\",LE=LK_BIBLE_FIND$\n"
        "$MU,\"To Dictionary Definition\",LE=LK_DEF$\n"
        "$MU,\"To Help Index\",LE=LK_HELP_INDEX$\n"
        "$MU,\"To Addr\",LE=LK_ADDR$\n");
  if (include_anchor)
    DocPrint(doc,"$MU,\"Place Anchor\",LE=LK_PLACE_ANCHOR$\n");
  DocPrint(doc,"%s",ctrl_L_footer);
  i=PopUpMenu(doc);
  DocDel(doc);
  return i;
}

class CEdFileLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512];
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdFileAnchorLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Anchor Label  :%s\"$\n";
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdFileFindLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Str        :%s\"$\n";
  I64   num             format "$DA-TRM,A=\"Occurrence Num:%04d\"$\n";
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdFileLineLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512];
  I64   num             format "$DA-TRM,A=\"Line Num      :%04d\"$\n";
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdManPageLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Label         :%s\"$\n";
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdAddrLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Addr Exp   :%s\"$\n";
  I64   num             format "$DA-TRM,A=\"Bin Size      :%04d\"$\n";
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdPlaceAnchor
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Anchor Label  :%s\"$\n";
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up,
        quote           format "$CB,\"Quote\"$\n",
        hide            format "$CB,\"Hide\"$\n";
};

class CEdBibleLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book            format "$LS,D=\"ST_BIBLE_BOOKS\"$\n";
  U8    aux[512]        format "$DA-P,A=\"Chapter Verse :%s\"$\n";
  I64   num;

  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdDefLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Word          :%s\"$\n";
  I64   num             format "$DA-TRM,A=\"Def Num       :%4d\"$\n";
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdProject
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512];
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

Bool GetLink(I64 type,U8 **_tag,U8 **_link,Bool *_pop_up,
        Bool *_quote,U8 **_html_link)
{
  CEdFileLink *e=CAlloc(sizeof(CEdFileLink));
  Bool res=FALSE;
  if (type>=0) {
    e->num=1;
    switch (type) {
      case LK_FILE:
        if (DocForm(e,,,,ctrl_L_footer)) {
          res=TRUE;
          if (!*e->tag)
            *_tag=StrNew(e->file);
          else
            *_tag=StrNew(e->tag);
          *_link=MStrPrint("FI:%s",e->file);
        }
        break;
      case LK_PLAIN:
        if (DocForm(e,,,,ctrl_L_footer)) {
          res=TRUE;
          if (!*e->tag)
            *_tag=StrNew(e->file);
          else
            *_tag=StrNew

[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\DocWidgetWiz-DolDoc-Adam.html
// Date: unknown-date
// Language: HolyC

#help_index "DolDoc/Misc"

U8 *ctrl_L_footer=
        "\n$MU-X+Q,\"Abort\",LE=DOCM_CANCEL$\n"
        "\n\n$LK+PU,\"Click for Help\",A=\"FI:::/Doc/Widget.DD\"$\n";

I64 PopUpLinkType(Bool include_anchor)
{
  I64 i;
  CDoc *doc=DocNew;
  DocPrint(doc,"$PURPLE$$TX+CX,\"Link Type Menu\"$\n"
        "\n$LTBLUE$$MU,\"To file\",LE=LK_FILE$\n"
        "$MU,\"To anchor in file\",LE=LK_FILE_ANCHOR$\n"
        "$MU,\"To str in file\",LE=LK_FILE_FIND$\n"
        "$MU,\"To line in file\",LE=LK_FILE_LINE$\n"
        "$MU,\"To man page\",LE=LK_MAN_PAGE$\n"
        "$MU,\"To plain-text file\",LE=LK_PLAIN$\n"
        "$MU,\"To str in plain-text file\",LE=LK_PLAIN_FIND$\n"
        "$MU,\"To line in plain-text file\",LE=LK_PLAIN_LINE$\n"
        "$MU,\"To Bible chapter line and verse\",LE=LK_BIBLE_FIND$\n"
        "$MU,\"To Dictionary Definition\",LE=LK_DEF$\n"
        "$MU,\"To Help Index\",LE=LK_HELP_INDEX$\n"
        "$MU,\"To Addr\",LE=LK_ADDR$\n");
  if (include_anchor)
    DocPrint(doc,"$MU,\"Place Anchor\",LE=LK_PLACE_ANCHOR$\n");
  DocPrint(doc,"%s",ctrl_L_footer);
  i=PopUpMenu(doc);
  DocDel(doc);
  return i;
}

class CEdFileLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512];
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdFileAnchorLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Anchor Label  :%s\"$\n";
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdFileFindLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Str        :%s\"$\n";
  I64   num             format "$DA-TRM,A=\"Occurrence Num:%04d\"$\n";
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdFileLineLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512];
  I64   num             format "$DA-TRM,A=\"Line Num      :%04d\"$\n";
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdManPageLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Label         :%s\"$\n";
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdAddrLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Addr Exp   :%s\"$\n";
  I64   num             format "$DA-TRM,A=\"Bin Size      :%04d\"$\n";
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdPlaceAnchor
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Anchor Label  :%s\"$\n";
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up,
        quote           format "$CB,\"Quote\"$\n",
        hide            format "$CB,\"Hide\"$\n";
};

class CEdBibleLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book            format "$LS,D=\"ST_BIBLE_BOOKS\"$\n";
  U8    aux[512]        format "$DA-P,A=\"Chapter Verse :%s\"$\n";
  I64   num;

  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdDefLink
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512];
  I64   book;
  U8    aux[512]        format "$DA-P,A=\"Word          :%s\"$\n";
  I64   num             format "$DA-TRM,A=\"Def Num       :%4d\"$\n";
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

class CEdProject
{
  U8    tag[512]        format "$DA-P,A=\"Tag Text      :%s\"$\n",
        file[512]       format "$DA-P,A=\"File          :%s\"$\n";
  I64   book;
  U8    aux[512];
  I64   num;
  U8    html_link[512]  format "$DA-P,A=\"Html Link     :%s\"$\n";
  Bool  pop_up          format "$CB,\"PopUp\"$\n",
        quote           format "$CB,\"Quote\"$\n",
        hide;
};

Bool GetLink(I64 type,U8 **_tag,U8 **_link,Bool *_pop_up,
        Bool *_quote,U8 **_html_link)
{
  CEdFileLink *e=CAlloc(sizeof(CEdFileLink));
  Bool res=FALSE;
  if (type>=0) {
    e->num=1;
    switch (type) {
      case LK_FILE:
        if (DocForm(e,,,,ctrl_L_footer)) {
          res=TRUE;
          if (!*e->tag)
            *_tag=StrNew(e->file);
          else
            *_tag=StrNew(e->tag);
          *_link=MStrPrint("FI:%s",e->file);
        }
        break;
      case LK_PLAIN:
        if (DocForm(e,,,,ctrl_L_footer)) {
          res=TRUE;
          if (!*e->tag)
            *_tag=StrNew(e->file);
          else
            *_tag=StrNew


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

I64 FSize(CFile *f)
{//Report size of opened file in bytes.
  if (f)
    return f->de.size;
  else
    return 0;
}

CFile *FOpen(U8 *filename,U8 *flags,I64 cnt=0)
{//Allows flags "r","w","w+". "c" for contiguous.
//(It uses StrOcc() for 'w', 'r', '+', 'c')
  CFile *f=CAlloc(sizeof(CFile));
  CDirContext *dirc;
  U8 *full_name;
  Bool contiguous=StrOcc(flags,'c');

  f->clus=INVALID_CLUS;
  f->fblk_num=0;
  if (cnt>0)
    f->max_blk=cnt-1;
  else
    f->max_blk=I64_MAX;
  f->file_clus_num=INVALID_CLUS;
  full_name=FileNameAbs(filename);
  f->dv=Let2Drv(*full_name);
  if (f->dv->fs_type==FSt_REDSEA)
    contiguous=TRUE;
  if (contiguous) {
    f->flags|=FF_CONTIGUOUS;
    if (f->dv->fs_type!=FSt_REDSEA &&
          !(FileAttr(filename) & RS_ATTR_CONTIGUOUS))
      throw('File');
  }
  f->clus_buf=CAlloc(f->dv->spc<<BLK_SIZE_BITS);
  if (StrOcc(flags,'w')) {
    f->flags=f->flags|FF_WRITE|FF_NEEDS_WRITE;
    if (StrOcc(flags,'+')) {
      if (FileFind(full_name,&f->de,FUF_JUST_FILES)) {
        Free(full_name);
        if (contiguous)
          f->max_blk=(FSize(f)+BLK_SIZE-1)>>BLK_SIZE_BITS-1;
        return f;
      }
    } else
      Del(full_name,,,FALSE);
    f->de.full_name=full_name;
    f->flags|=FF_NEW_FILE;
    if (dirc=DirContextNew(full_name)) {
      StrCpy(f->de.name,dirc->mask);
      if (cnt>0) {//We pre-alloc the whole thing.
        f->de.clus=ClusAlloc(f->dv,0,
              (cnt+f->dv->spc-1)/f->dv->spc,contiguous);
        f->de.size=cnt<<BLK_SIZE_BITS;
        DirNew(dirc->dv,Fs->cur_dir,&f->de,TRUE);
        f->flags&=~FF_NEW_FILE;
      }
      DirContextDel(dirc);
      return f;
    }
  } else {
    if (FileFind(full_name,&f->de,FUF_JUST_FILES)) {
      Free(full_name);
      f->max_blk=(FSize(f)+BLK_SIZE-1)>>BLK_SIZE_BITS-1;
      return f;
    }
  }
  Free(f->clus_buf);
  Free(full_name);
  Free(f);
  return NULL;
}

U0 FClose(CFile *f)
{//Close CFile, updating directory.
  CDirContext *dirc;
  if (f) {
    if (f->flags & FF_BUF_DIRTY) {
      ClusWrite(f->dv,f->clus_buf,f->clus,1);
      f->flags&=~FF_BUF_DIRTY;
    }
    if (f->flags & FF_NEEDS_WRITE) {
      if (dirc=DirContextNew(f->de.full_name)) {
        if (!(f->flags & FF_USE_OLD_DATETIME))
          f->de.datetime=Now;
        if (f->flags & FF_NEW_FILE)
          DirNew(dirc->dv,Fs->cur_dir,&f->de,TRUE);
        else
          DirNew(dirc->dv,Fs->cur_dir,&f->de,FALSE);
        DirContextDel(dirc);
      } else
        throw('File');
    }
    Free(f->clus_buf);
    Free(f->de.full_name);
    Free(f);
  }
}

I64 FSetClus(CFile *f,I64 c,I64 blk,Bool read)
{
  CDrv *dv=f->dv;
  I64 i;
  if (f->clus!=c) {
    if (f->flags & FF_BUF_DIRTY) {
      i=dv->spc;
      if (f->max_blk!=I64_MAX) {
        i=f->max_blk+1-f->file_clus_num*dv->spc;
        if (i>dv->spc)
          i=dv->spc;
      }
      ClusBlkWrite(dv,f->clus_buf,f->clus,i);
      f->flags=f->flags & ~FF_BUF_DIRTY;
    }
    f->clus=c;
    f->file_clus_num=blk/dv->spc;
    if (read) {
      i=dv->spc;
      if (f->max_blk!=I64_MAX) {
        i=f->max_blk+1-f->file_clus_num*dv->spc;
        if (i>dv->spc)
          i=dv->spc;
      }
      c=ClusBlkRead(dv,f->clus_buf,c,i);
    }
  }
  return c;
}

Bool FBlkRead(CFile *f,U8 *buf,I64 blk=FFB_NEXT_BLK,I64 cnt=1)
{//Read [nth,n+cnt) blks of file.
  CDrv *dv=f->dv;
  I64 spc=dv->spc,i,j,c=f->de.clus;
  if (!f || !dv) return FALSE;
  if (blk==FFB_NEXT_BLK)
    blk=f->fblk_num;
  if (blk+cnt-1>f->max_blk)
    return FALSE;
  if (cnt<=0) return TRUE;

  if (f->flags & FF_CONTIGUOUS) {
    BlkRead(dv,buf,Clus2Blk(dv,c)+blk,cnt);
    blk+=cnt;
  } else {
    i=blk/spc;
    if (0<=f->file_clus_num<=i) {
      c=f->clus;
      i-=f->file_clus_num;
    }
    if (i>0)
      c=ClusNumNext(dv,c,i);

    if (i=blk%spc) {
      c=FSetClus(f,c,blk,TRUE);
      if (cnt<spc-i)
        j=cnt;
      else
        j=spc-i;
      MemCpy(buf,f->clus_buf+i<<BLK_SIZE_BITS,j<<BLK_SIZE_BITS);
      buf+=j<<BLK_SIZE_BITS;
      cnt-=j;
      blk+=j;
    }
    while (cnt>=spc) {
      c=FSetClus(f,c,blk,TRUE);
      MemCpy(buf,f->clus_buf,spc<<BLK_SIZE_BITS);
      buf+=spc<<BLK_SIZE_BITS;
      cnt-=spc;
      blk+=spc;
    }
    if (cnt>0) {
      c=FSetClus(f,c,blk,TRUE);
      MemCpy(buf,f->clus_buf,cnt<<BLK_SIZE_BITS);
      buf+=cnt<<BLK_SIZE_BITS;
      blk+=cnt;
    }
  }
  f->fblk_num=blk;
  return TRUE;
}

Bool FBlkWrite(CFile *f,U8 *buf,I64 blk=FFB_NEXT_BLK,I64 cnt=1)
{//Write [nth,n+cnt) blks of file.
  CDrv *dv=f->dv;
  I64 spc=dv->spc,i,j,c=f->de.clus,c1;
  if (!f || !dv) return FALSE;
  if (blk==FFB_NEXT_BLK)
    blk=f->fblk_num;
  if (blk+cnt-1>f->max_blk)
    return FALSE;
  if (!(f->flags & FF_WRITE))
    return FALSE;
  if (cnt<=0) return TRUE;
  if (f->flags & FF_CONTIGUOUS) {
    BlkWrite(dv,buf,Clus2Blk(dv,c)+blk,cnt);
    blk+=cnt;
  } else {
    if (!c) {
      c=ClusAlloc(dv,0,1,FALSE);
      f->file_clus_num=0;
      f->clus=c;
      f->de.clus=c;
      f->flags|=FF_NEEDS_WRITE|FF_NEW_FILE;
    }
    i=blk/spc;
    if (0<=f->file_clus_num<=i) {
      c=f->clus;
      i-=f->file_clus_num;
    }
    while (i>0) {
      c1=c;
      c=ClusNumNext(dv,c1,1);
      if (c==INVALID_CLUS) {
        c=ClusAlloc(dv,c1,i,FALSE);
        if (i>1)
          c=ClusNumNext(dv,c,i-1);
        break;
      } else
        i--;
    }

    if (i=blk%spc) {
      FSetClus(f,c,blk,TRUE);
      if (cnt<spc-i)
        j=cnt;
      else
        j=spc-i;
      MemCpy(f->clus_buf+BLK_SIZE*i,buf,j<<BLK_SIZE_BITS);
      f->flags|=FF_BUF_DIRTY;
      buf+=j<<BLK_SIZE_BITS;
      cnt-=j;
      blk+=j;
      if (cnt>0) {
        c1=c;
        c=ClusNumNext(dv,c1,1);
        if (c==INVALID_CLUS)
          c=ClusAlloc(dv,c1,1,FALSE);
      }
    }
    while (cnt>=spc) {
      FSetClus(f,c,blk,FALSE);
      MemCpy(f->clus_buf,buf,spc<<BLK_SIZE_BITS);
      f->flags|=FF_BUF_DIRTY;
      buf+=spc<<BLK_SIZE_BITS;
      cnt-=spc;
      blk+=spc;
      if (cnt>0) {
        c1=c;
        c=ClusNumNext(dv,c1,1);
        if (c==INVALID_CLUS)
          c=ClusAlloc(dv,c1,1,FALSE);
      }
    }
    if (cnt>0) {
      FSetClus(f,c,blk,TRUE);
      MemCpy(f->clus_buf,buf,cnt<<BLK_SIZE_BITS);
      f->flags|=FF_BUF_DIRTY;
      buf+=cnt<<BLK_SIZE_BITS;
      blk+=cnt;
    }
    if (f->de.size<blk<<BLK_SIZE_BITS)
      f->de.size=blk<<BLK_SIZE_BITS;
  }
  f->fblk_num=blk;
  return 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\DskCFile-BlkDev-Kernel.html
// Date: unknown-date
// Language: HolyC

I64 FSize(CFile *f)
{//Report size of opened file in bytes.
  if (f)
    return f->de.size;
  else
    return 0;
}

CFile *FOpen(U8 *filename,U8 *flags,I64 cnt=0)
{//Allows flags "r","w","w+". "c" for contiguous.
//(It uses StrOcc() for 'w', 'r', '+', 'c')
  CFile *f=CAlloc(sizeof(CFile));
  CDirContext *dirc;
  U8 *full_name;
  Bool contiguous=StrOcc(flags,'c');

  f->clus=INVALID_CLUS;
  f->fblk_num=0;
  if (cnt>0)
    f->max_blk=cnt-1;
  else
    f->max_blk=I64_MAX;
  f->file_clus_num=INVALID_CLUS;
  full_name=FileNameAbs(filename);
  f->dv=Let2Drv(*full_name);
  if (f->dv->fs_type==FSt_REDSEA)
    contiguous=TRUE;
  if (contiguous) {
    f->flags|=FF_CONTIGUOUS;
    if (f->dv->fs_type!=FSt_REDSEA &&
          !(FileAttr(filename) & RS_ATTR_CONTIGUOUS))
      throw('File');
  }
  f->clus_buf=CAlloc(f->dv->spc<<BLK_SIZE_BITS);
  if (StrOcc(flags,'w')) {
    f->flags=f->flags|FF_WRITE|FF_NEEDS_WRITE;
    if (StrOcc(flags,'+')) {
      if (FileFind(full_name,&f->de,FUF_JUST_FILES)) {
        Free(full_name);
        if (contiguous)
          f->max_blk=(FSize(f)+BLK_SIZE-1)>>BLK_SIZE_BITS-1;
        return f;
      }
    } else
      Del(full_name,,,FALSE);
    f->de.full_name=full_name;
    f->flags|=FF_NEW_FILE;
    if (dirc=DirContextNew(full_name)) {
      StrCpy(f->de.name,dirc->mask);
      if (cnt>0) {//We pre-alloc the whole thing.
        f->de.clus=ClusAlloc(f->dv,0,
              (cnt+f->dv->spc-1)/f->dv->spc,contiguous);
        f->de.size=cnt<<BLK_SIZE_BITS;
        DirNew(dirc->dv,Fs->cur_dir,&f->de,TRUE);
        f->flags&=~FF_NEW_FILE;
      }
      DirContextDel(dirc);
      return f;
    }
  } else {
    if (FileFind(full_name,&f->de,FUF_JUST_FILES)) {
      Free(full_name);
      f->max_blk=(FSize(f)+BLK_SIZE-1)>>BLK_SIZE_BITS-1;
      return f;
    }
  }
  Free(f->clus_buf);
  Free(full_name);
  Free(f);
  return NULL;
}

U0 FClose(CFile *f)
{//Close CFile, updating directory.
  CDirContext *dirc;
  if (f) {
    if (f->flags & FF_BUF_DIRTY) {
      ClusWrite(f->dv,f->clus_buf,f->clus,1);
      f->flags&=~FF_BUF_DIRTY;
    }
    if (f->flags & FF_NEEDS_WRITE) {
      if (dirc=DirContextNew(f->de.full_name)) {
        if (!(f->flags & FF_USE_OLD_DATETIME))
          f->de.datetime=Now;
        if (f->flags & FF_NEW_FILE)
          DirNew(dirc->dv,Fs->cur_dir,&f->de,TRUE);
        else
          DirNew(dirc->dv,Fs->cur_dir,&f->de,FALSE);
        DirContextDel(dirc);
      } else
        throw('File');
    }
    Free(f->clus_buf);
    Free(f->de.full_name);
    Free(f);
  }
}

I64 FSetClus(CFile *f,I64 c,I64 blk,Bool read)
{
  CDrv *dv=f->dv;
  I64 i;
  if (f->clus!=c) {
    if (f->flags & FF_BUF_DIRTY) {
      i=dv->spc;
      if (f->max_blk!=I64_MAX) {
        i=f->max_blk+1-f->file_clus_num*dv->spc;
        if (i>dv->spc)
          i=dv->spc;
      }
      ClusBlkWrite(dv,f->clus_buf,f->clus,i);
      f->flags=f->flags & ~FF_BUF_DIRTY;
    }
    f->clus=c;
    f->file_clus_num=blk/dv->spc;
    if (read) {
      i=dv->spc;
      if (f->max_blk!=I64_MAX) {
        i=f->max_blk+1-f->file_clus_num*dv->spc;
        if (i>dv->spc)
          i=dv->spc;
      }
      c=ClusBlkRead(dv,f->clus_buf,c,i);
    }
  }
  return c;
}

Bool FBlkRead(CFile *f,U8 *buf,I64 blk=FFB_NEXT_BLK,I64 cnt=1)
{//Read [nth,n+cnt) blks of file.
  CDrv *dv=f->dv;
  I64 spc=dv->spc,i,j,c=f->de.clus;
  if (!f || !dv) return FALSE;
  if (blk==FFB_NEXT_BLK)
    blk=f->fblk_num;
  if (blk+cnt-1>f->max_blk)
    return FALSE;
  if (cnt<=0) return TRUE;

  if (f->flags & FF_CONTIGUOUS) {
    BlkRead(dv,buf,Clus2Blk(dv,c)+blk,cnt);
    blk+=cnt;
  } else {
    i=blk/spc;
    if (0<=f->file_clus_num<=i) {
      c=f->clus;
      i-=f->file_clus_num;
    }
    if (i>0)
      c=ClusNumNext(dv,c,i);

    if (i=blk%spc) {
      c=FSetClus(f,c,blk,TRUE);
      if (cnt<spc-i)
        j=cnt;
      else
        j=spc-i;
      MemCpy(buf,f->clus_buf+i<<BLK_SIZE_BITS,j<<BLK_SIZE_BITS);
      buf+=j<<BLK_SIZE_BITS;
      cnt-=j;
      blk+=j;
    }
    while (cnt>=spc) {
      c=FSetClus(f,c,blk,TRUE);
      MemCpy(buf,f->clus_buf,spc<<BLK_SIZE_BITS);
      buf+=spc<<BLK_SIZE_BITS;
      cnt-=spc;
      blk+=spc;
    }
    if (cnt>0) {
      c=FSetClus(f,c,blk,TRUE);
      MemCpy(buf,f->clus_buf,cnt<<BLK_SIZE_BITS);
      buf+=cnt<<BLK_SIZE_BITS;
      blk+=cnt;
    }
  }
  f->fblk_num=blk;
  return TRUE;
}

Bool FBlkWrite(CFile *f,U8 *buf,I64 blk=FFB_NEXT_BLK,I64 cnt=1)
{//Write [nth,n+cnt) blks of file.
  CDrv *dv=f->dv;
  I64 spc=dv->spc,i,j,c=f->de.clus,c1;
  if (!f || !dv) return FALSE;
  if (blk==FFB_NEXT_BLK)
    blk=f->fblk_num;
  if (blk+cnt-1>f->max_blk)
    return FALSE;
  if (!(f->flags & FF_WRITE))
    return FALSE;
  if (cnt<=0) return TRUE;
  if (f->flags & FF_CONTIGUOUS) {
    BlkWrite(dv,buf,Clus2Blk(dv,c)+blk,cnt);
    blk+=cnt;
  } else {
    if (!c) {
      c=ClusAlloc(dv,0,1,FALSE);
      f->file_clus_num=0;
      f->clus=c;
      f->de.clus=c;
      f->flags|=FF_NEEDS_WRITE|FF_NEW_FILE;
    }
    i=blk/spc;
    if (0<=f->file_clus_num<=i) {
      c=f->clus;
      i-=f->file_clus_num;
    }
    while (i>0) {
      c1=c;
      c=ClusNumNext(dv,c1,1);
      if (c==INVALID_CLUS) {
        c=ClusAlloc(dv,c1,i,FALSE);
        if (i>1)
          c=ClusNumNext(dv,c,i-1);
        break;
      } else
        i--;
    }

    if (i=blk%spc) {
      FSetClus(f,c,blk,TRUE);
      if (cnt<spc-i)
        j=cnt;
      else
        j=spc-i;
      MemCpy(f->clus_buf+BLK_SIZE*i,buf,j<<BLK_SIZE_BITS);
      f->flags|=FF_BUF_DIRTY;
      buf+=j<<BLK_SIZE_BITS;
      cnt-=j;
      blk+=j;
      if (cnt>0) {
        c1=c;
        c=ClusNumNext(dv,c1,1);
        if (c==INVALID_CLUS)
          c=ClusAlloc(dv,c1,1,FALSE);
      }
    }
    while (cnt>=spc) {
      FSetClus(f,c,blk,FALSE);
      MemCpy(f->clus_buf,buf,spc<<BLK_SIZE_BITS);
      f->flags|=FF_BUF_DIRTY;
      buf+=spc<<BLK_SIZE_BITS;
      cnt-=spc;
      blk+=spc;
      if (cnt>0) {
        c1=c;
        c=ClusNumNext(dv,c1,1);
        if (c==INVALID_CLUS)
          c=ClusAlloc(dv,c1,1,FALSE);
      }
    }
    if (cnt>0) {
      FSetClus(f,c,blk,TRUE);
      MemCpy(f->clus_buf,buf,cnt<<BLK_SIZE_BITS);
      f->flags|=FF_BUF_DIRTY;
      buf+=cnt<<BLK_SIZE_BITS;
      blk+=cnt;
    }
    if (f->de.size<blk<<BLK_SIZE_BITS)
      f->de.size=blk<<BLK_SIZE_BITS;
  }
  f->fblk_num=blk;
  return TRUE;
}


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

//5 has words
U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.902;
  try {
    while (!ScanKey) {
      Play("5eC4BB5C4B5CCEE4A5GG4AA5sE4AA5E",
            "I'm \0 \0 \0in \0 \0trou\0ble.\n\0 \0 \0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0");
      Play("5eC4BB5C4B5CCEE4A5GG4AA5sE4AA5E",
            "Bursted \0 \0 \0my \0 \0bub\0ble.\n\0 \0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0");
      "$GREEN$God talking...\n$FG$";
      Play("4qG5eDDsCECE4qGG5eF4A5etEEEsC4AA5C",
            " \0$RED$Yep, \0 \0you're \0 \0 \0 \0screwed.\n\0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0");
      Play("4qG5eDDsCECE4qGG5eF4A5etEEEsC4AA5C",
            " \0Re\0 \0pent, \0 \0 \0 \0dude.$FG$\n\0 \0 \0"
            " \0 \0 \0 \0 \0 \0 \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\tormenting-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has words
U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.902;
  try {
    while (!ScanKey) {
      Play("5eC4BB5C4B5CCEE4A5GG4AA5sE4AA5E",
            "I'm \0 \0 \0in \0 \0trou\0ble.\n\0 \0 \0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0");
      Play("5eC4BB5C4B5CCEE4A5GG4AA5sE4AA5E",
            "Bursted \0 \0 \0my \0 \0bub\0ble.\n\0 \0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0");
      "$GREEN$God talking...\n$FG$";
      Play("4qG5eDDsCECE4qGG5eF4A5etEEEsC4AA5C",
            " \0$RED$Yep, \0 \0you're \0 \0 \0 \0screwed.\n\0"
            " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0");
      Play("4qG5eDDsCECE4qGG5eF4A5etEEEsC4AA5C",
            " \0Re\0 \0pent, \0 \0 \0 \0dude.$FG$\n\0 \0 \0"
            " \0 \0 \0 \0 \0 \0 \0 \0");
    }
  } catch
    PutExcept;
  Snd;
}

Song;


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

//This is a drawing program

U0 Doodle()
{
  I64 msg_code,i,x1,y1,x2,y2,arg1,arg2,color=BLACK;
  CDC *dc=DCAlias;

  SettingsPush; //See SettingsPush
  Fs->win_inhibit=WIG_TASK_DFT-WIF_SELF_FOCUS-WIF_SELF_BORDER;

  AutoComplete;
  WinBorder;
  WinMax;
  DocClear;
  DCFill;
  do {
    msg_code=GetMsg(&arg1,&arg2,
          1<<MSG_KEY_DOWN+1<<MSG_MS_L_DOWN+1<<MSG_MS_R_UP);
    switch (msg_code) {
      case MSG_MS_R_UP:
        i=PopUpColor;
        if (i>=0) color=i;
        break;
      case MSG_MS_L_DOWN:
        x1=arg1; y1=arg2;
        x2=arg1; y2=arg2;
        dc->color=ROP_XOR+color^TRANSPARENT;
        dc->thick=7;
        do {
          GrLine3(dc,x1,y1,0,x2,y2,0);
          msg_code=GetMsg(&arg1,&arg2,1<<MSG_MS_L_UP+1<<MSG_MS_MOVE);
          GrLine3(dc,x1,y1,0,x2,y2,0);
          x2=arg1; y2=arg2;
        } while (msg_code!=MSG_MS_L_UP);
        GrLine3(dc,x1,y1,0,x2,y2,0);
        break;
      case MSG_KEY_DOWN:
        break;
    }
  } while (msg_code!=MSG_KEY_DOWN || !arg1);
  GetMsg(,,1<<MSG_KEY_UP);
  DCFill;
  DCDel(dc);
  SettingsPop;
}

Doodle;  //Execute when #included

[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\Doodle-Graphics-Demo.html
// Date: unknown-date
// Language: HolyC

//This is a drawing program

U0 Doodle()
{
  I64 msg_code,i,x1,y1,x2,y2,arg1,arg2,color=BLACK;
  CDC *dc=DCAlias;

  SettingsPush; //See SettingsPush
  Fs->win_inhibit=WIG_TASK_DFT-WIF_SELF_FOCUS-WIF_SELF_BORDER;

  AutoComplete;
  WinBorder;
  WinMax;
  DocClear;
  DCFill;
  do {
    msg_code=GetMsg(&arg1,&arg2,
          1<<MSG_KEY_DOWN+1<<MSG_MS_L_DOWN+1<<MSG_MS_R_UP);
    switch (msg_code) {
      case MSG_MS_R_UP:
        i=PopUpColor;
        if (i>=0) color=i;
        break;
      case MSG_MS_L_DOWN:
        x1=arg1; y1=arg2;
        x2=arg1; y2=arg2;
        dc->color=ROP_XOR+color^TRANSPARENT;
        dc->thick=7;
        do {
          GrLine3(dc,x1,y1,0,x2,y2,0);
          msg_code=GetMsg(&arg1,&arg2,1<<MSG_MS_L_UP+1<<MSG_MS_MOVE);
          GrLine3(dc,x1,y1,0,x2,y2,0);
          x2=arg1; y2=arg2;
        } while (msg_code!=MSG_MS_L_UP);
        GrLine3(dc,x1,y1,0,x2,y2,0);
        break;
      case MSG_KEY_DOWN:
        break;
    }
  } while (msg_code!=MSG_KEY_DOWN || !arg1);
  GetMsg(,,1<<MSG_KEY_UP);
  DCFill;
  DCDel(dc);
  SettingsPop;
}

Doodle;  //Execute when #included


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

//5 has words
U0 Song()
{
  CTask *task;
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.902;
  try {
    task=PopUpViewPrint("Luke,13:25\n");
    TaskWait(task);
    WinHorz(Fs->win_left,TEXT_COLS-10,task);
    WinVert(Fs->win_bottom+3,Fs->win_bottom+15,task);
    while (!ScanKey) {
      Play("5qGeEFqE4B5CG4qtB5DA",
            "God, \0let \0me \0in!\n\0 \0 \0 \0 \0 \0 \0");
      Play("5qGeEFqE4B5CG4qtB5DF",
            "God, \0did \0I \0sin?\n\0 \0 \0 \0 \0 \0 \0");
      "$GREEN$God talking...\n$FG$";
      Play("5qE4eG5FqDE4eB5EqCeCD4B5C",
            "$RED$Who's \0 \0 \0 \0there?\n\0 \0 \0 \0 \0 \0 \0 \0");
      Play("4qBeAsBA5qE4eG5FqDE4eB5EqC",
            " \0 \0 \0 \0Who's \0 \0 \0 \0where?$FG$\n\0 \0 \0 \0");
      Play("5eCD4B5C4qBeAB");
    }
  } 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\whos-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has words
U0 Song()
{
  CTask *task;
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo= 2.480;
  music.stacatto_factor= 0.902;
  try {
    task=PopUpViewPrint("Luke,13:25\n");
    TaskWait(task);
    WinHorz(Fs->win_left,TEXT_COLS-10,task);
    WinVert(Fs->win_bottom+3,Fs->win_bottom+15,task);
    while (!ScanKey) {
      Play("5qGeEFqE4B5CG4qtB5DA",
            "God, \0let \0me \0in!\n\0 \0 \0 \0 \0 \0 \0");
      Play("5qGeEFqE4B5CG4qtB5DF",
            "God, \0did \0I \0sin?\n\0 \0 \0 \0 \0 \0 \0");
      "$GREEN$God talking...\n$FG$";
      Play("5qE4eG5FqDE4eB5EqCeCD4B5C",
            "$RED$Who's \0 \0 \0 \0there?\n\0 \0 \0 \0 \0 \0 \0 \0");
      Play("4qBeAsBA5qE4eG5FqDE4eB5EqC",
            " \0 \0 \0 \0Who's \0 \0 \0 \0where?$FG$\n\0 \0 \0 \0");
      Play("5eCD4B5C4qBeAB");
    }
  } catch
    PutExcept;
  Snd;
}

Song;


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

//Practice your ASCII knowledge ;-)

U0 SafePutChar(U8 ch)
{
  if (CH_SHIFT_SPACE<=ch<0x7F) {
    if (ch=='$')
      "%02X $RED$$$$FG$ ",ch;
    else
      "%02X $RED$%c$FG$ ",ch,ch;
  } else
    "%02X $RED$ $FG$ ",ch;
}

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

U0 ASCIIOrgan()
{
  I64 i,ch;

  for (i=0;i<32;i++) {
    SafePutChar(i);
    SafePutChar(i+32);
    SafePutChar(i+64);
    SafePutChar(i+96);
    '\n';
  }

  "\nPress keys to make snds\n"
        "and press $GREEN$<SHIFT-ESC>$FG$ to exit.\n";
  do {
//We don't want echo or scan codes, so we use NULL and FALSE...
    ch=GetChar(,FALSE);
    Snd(ch-CH_ESC);
  } while (ch!=CH_ESC && ch!=CH_SHIFT_ESC);
  Snd;

  //Note the %d in the string for sprite number.
  '\n\n\n\n\n\n\n';
  Sprite(<1>,"$SP+PU,\"Click for better organ\","
        "LM=\"#include \\\"::/Apps/Psalmody/Run\\\";\",BI=%d$");
  '\n\n\n\n\n';
}

ASCIIOrgan;

[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\ASCIIOrgan-Snd-Demo.html
// Date: unknown-date
// Language: HolyC

//Practice your ASCII knowledge ;-)

U0 SafePutChar(U8 ch)
{
  if (CH_SHIFT_SPACE<=ch<0x7F) {
    if (ch=='$')
      "%02X $RED$$$$FG$ ",ch;
    else
      "%02X $RED$%c$FG$ ",ch,ch;
  } else
    "%02X $RED$ $FG$ ",ch;
}

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

U0 ASCIIOrgan()
{
  I64 i,ch;

  for (i=0;i<32;i++) {
    SafePutChar(i);
    SafePutChar(i+32);
    SafePutChar(i+64);
    SafePutChar(i+96);
    '\n';
  }

  "\nPress keys to make snds\n"
        "and press $GREEN$<SHIFT-ESC>$FG$ to exit.\n";
  do {
//We don't want echo or scan codes, so we use NULL and FALSE...
    ch=GetChar(,FALSE);
    Snd(ch-CH_ESC);
  } while (ch!=CH_ESC && ch!=CH_SHIFT_ESC);
  Snd;

  //Note the %d in the string for sprite number.
  '\n\n\n\n\n\n\n';
  Sprite(<1>,"$SP+PU,\"Click for better organ\","
        "LM=\"#include \\\"::/Apps/Psalmody/Run\\\";\",BI=%d$");
  '\n\n\n\n\n';
}

ASCIIOrgan;


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

//5 has graphics
<1>/* Graphics Not Rendered in HTML */

U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo=2.5;
  try {
    while (!ScanKey) {
      Sprite(<1>);
      "\n\n\n\n\n\n$GREEN$To God...\n$FG$";
      Play("5etD4GRsR5GRGqFetEDCCRCRC4BqGsGAGA",
            "O\0kay, \0 \0 \0what \0 \0is \0it \0"
            "You \0 \0 \0want \0 \0us \0 \0to \0 \0do?\n\0 \0 \0 \0 \0");
      Play("5etD4GRsR5GRGqFetEDCCRCRC4BqGsGAGA",
            " \0 \0 \0 \0Let \0 \0me \0guess, \0"
            " \0 \0 \0we \0 \0are \0 \0to \0 \0choose.\n\0 \0 \0 \0 \0");
      Play("4eB5FqDC4eBB5RGetERFqGE",
            " \0 \0 \0 \0 \0 \0 \0I'm \0so \0 \0con\0fused.\n\0 \0");
      Play("4eB5FqDC4eBB5RGetERFqGE",
            " \0 \0 \0 \0 \0 \0 \0I \0must \0 \0have \0snoozed.\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\purpose-Sup1-Sup1Hymns-Home.html
// Date: unknown-date
// Language: HolyC

//5 has graphics
<1>/* Graphics Not Rendered in HTML */

U0 Song()
{
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  music.tempo=2.5;
  try {
    while (!ScanKey) {
      Sprite(<1>);
      "\n\n\n\n\n\n$GREEN$To God...\n$FG$";
      Play("5etD4GRsR5GRGqFetEDCCRCRC4BqGsGAGA",
            "O\0kay, \0 \0 \0what \0 \0is \0it \0"
            "You \0 \0 \0want \0 \0us \0 \0to \0 \0do?\n\0 \0 \0 \0 \0");
      Play("5etD4GRsR5GRGqFetEDCCRCRC4BqGsGAGA",
            " \0 \0 \0 \0Let \0 \0me \0guess, \0"
            " \0 \0 \0we \0 \0are \0 \0to \0 \0choose.\n\0 \0 \0 \0 \0");
      Play("4eB5FqDC4eBB5RGetERFqGE",
            " \0 \0 \0 \0 \0 \0 \0I'm \0so \0 \0con\0fused.\n\0 \0");
      Play("4eB5FqDC4eBB5RGetERFqGE",
            " \0 \0 \0 \0 \0 \0 \0I \0must \0 \0have \0snoozed.\n\0 \0");
    }
  } catch
    PutExcept;
  Snd;
}

Song;