CoolFace
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes838downloads
Titanium.txt933 linesDownload Raw Back to Titanium
1 2RegDft("TempleOS/Titanium","I64 best_score=0;\n");3RegExe("TempleOS/Titanium");4 5#define MAP_HEIGHT      40966 7#define B_LEN   108#define B_SPEED 59#define B_NUM   12810class Bullet11{12  I64 x,y,dx,dy,dx2,dy2;13  Bool dead,missile,pad[6];14} b[B_NUM];15I64 bullets_fired,missile_bmp;16 17I64 x,y,dx,dy,finish_line;18F64 theta,t0,tf,snd_timeout;19 20#define PHASES_GROUPS   821#define HACK_DIST       522 23#define U_ENEMY_NUM     (PHASES_GROUPS*64)24#define U_FRIENDLY_NUM  (PHASES_GROUPS*16)25#define U_NUM   (U_FRIENDLY_NUM+U_ENEMY_NUM)26class Unit27{28  I64 x,y,best_dd;29  F64 theta,phase;30  Bool friendly,dead,tank,pad[5];31} u[U_NUM];32 33#define ET_MAN          034#define ET_TANK         135#define ET_MISSILE      236#define ET_BUILDING     337#define E_NUM           51238class Explosion39{40  I64 x,y,num,type;41  F64 t0,tf;42  Bool dead,pad[7];43} e[E_NUM];44 45I64 total_score,friendly_fire,enemy_by_friendly,friendly_left,enemy_left,46    main_loop_pass;47Bool game_over;48 49 50 51 52 53        <1>/* Graphics Not Rendered in HTML */54 55 56 57 58 59        <2>/* Graphics Not Rendered in HTML */60 61        <3>/* Graphics Not Rendered in HTML */62 63 64 65 66        <4>/* Graphics Not Rendered in HTML */67 68 69 70        <5>/* Graphics Not Rendered in HTML */71 72 73 74        <6>/* Graphics Not Rendered in HTML */75 76 77 78U8 *friendly_imgs[4]={<5>,<4>,<5>,<6>};79 80 81 82 83        <7>/* Graphics Not Rendered in HTML */84 85 86 87 88        <8>/* Graphics Not Rendered in HTML */89 90 91 92 93        <9>/* Graphics Not Rendered in HTML */94 95 96 97 98U8 *friendly_hacking_imgs[4]={<7>,<8>,<7>,<9>};99 100 101 102 103        <10>/* Graphics Not Rendered in HTML */104 105 106 107        <11>/* Graphics Not Rendered in HTML */108 109 110 111        <12>/* Graphics Not Rendered in HTML */112 113 114 115U8 *enemy_imgs[4]={<11>,<10>,<11>,<12>};116 117 118 119 120        <13>/* Graphics Not Rendered in HTML */121 122 123 124 125        <14>/* Graphics Not Rendered in HTML */126 127 128 129 130        <15>/* Graphics Not Rendered in HTML */131 132 133 134 135 136 137 138 139 140 141 142        <16>/* Graphics Not Rendered in HTML */143 144 145 146 147 148 149 150 151 152U8 *enemy_hacking_imgs[4]={<14>,<13>,<14>,<15>};153 154 155 156 157 158 159 160        <17>/* Graphics Not Rendered in HTML */161 162 163 164 165        <18>/* Graphics Not Rendered in HTML */166 167 168 169 170 171 172 173 174        <19>/* Graphics Not Rendered in HTML */175 176 177 178 179        <20>/* Graphics Not Rendered in HTML */180 181 182 183 184 185#define LS_APTS_NUM             20186#define LS_MOUNTAINS_NUM        3187#define LS_NUM                  128188class LandScapeItem189{190  I64 x,y;191  U8 *img;192} ls[LS_NUM];193 194#define LS_TYPES        4195U8 *landscape_imgs[LS_TYPES]=196  {<17>,<18>,<19>,<20>};197 198U0 ExplosionDraw(CDC *dc,Explosion *tmpe,I64 y)199{200  I64 i,n1,n2,n3,n4,n5;201  F64 t=(tS-tmpe->t0)/(tmpe->tf-tmpe->t0);202  Seed(tmpe->num+1);203 204  switch (tmpe->type) {205    case ET_MAN:        n1=8;   n2=0;  n3=0;  n4=18;  n5=28;    break;206    case ET_TANK:       n1=64;  n2=14; n3=24; n4=60;  n5=90;    break;207    case ET_MISSILE:    n1=128; n2=30; n3=60; n4=100; n5=200;   break;208    case ET_BUILDING:   n1=128; n2=80; n3=80; n4=200; n5=300;   break;209  }210  for (i=0;i<n1;i++) {211    if (i&2)212      switch (tmpe->type) {213        case ET_MAN:214          if (i&1)215            dc->color=WHITE;216          else217            dc->color=LTGRAY;218          break;219        case ET_MISSILE:220          if (i&1) {221            if (i&4)222              dc->color=RED;223            else224              dc->color=DKGRAY;225          } else226            dc->color=YELLOW;227          break;228        case ET_TANK:229          if (i&1) {230            if (i&4)231              dc->color=RED;232            else233              dc->color=DKGRAY;234          } else235            dc->color=LTRED;236          break;237        case ET_BUILDING:238          if (i&1) {239            if (i&4)240              dc->color=BLACK;241            else242              dc->color=DKGRAY;243          } else244            dc->color=LTGRAY;245          break;246      }247    else if (i&1)248      dc->color=WHITE;249    else250      dc->color=LTGRAY;251    GrLine(dc,tmpe->x+n2*(Rand-.5),y+n3/2*Rand,252          tmpe->x+n4*t*(Rand-.5),y-n5/2*t*Rand);253  }254}255 256I64 mp_not_done_flags;257U0 MPMenDraw(CDC *dc2)258{259  CTask *task=dc2->win_task;260  CDC *dc=DCAlias(dc2,task);261  I64 i,r[16],lo=Gs->num*U_NUM/mp_cnt,hi=(Gs->num+1)*U_NUM/mp_cnt,262        yy,phase,scroll_y=MAP_HEIGHT-100-100*(tS-t0);263  Unit *tmpu;264  Explosion *tmpe;265  U8 *tmps,**_tmps;266  F64 tt,ts=tS;267 268  for (i=Gs->num;i<E_NUM;i+=mp_cnt) {269    tmpe=&e[i];270    yy=(tmpe->y-scroll_y)&(MAP_HEIGHT-1);271    if (-32<=yy<=task->pix_bottom+32 && !tmpe->dead) {272      ExplosionDraw(dc,tmpe,yy);273      if (tS>tmpe->tf)274        tmpe->dead=TRUE;275    }276  }277  for (i=lo;i<hi;i++) {278    tmpu=&u[i];279    yy=(tmpu->y-scroll_y)&(MAP_HEIGHT-1);280    if (-32<=yy<=task->pix_bottom+32) {281      if (!tmpu->dead) {282        Mat4x4IdentEqu(r);283        Mat4x4RotY(r,tmpu->theta);284        Mat4x4RotX(r,pi/6);285        Mat4x4Scale(r,0.3);286        if (tmpu->tank)287          Sprite3Mat4x4B(dc,tmpu->x,yy,GR_Z_ALL,<16>,r);288        else {289          if (tmpu->best_dd<(2*HACK_DIST)*(2*HACK_DIST)) {//It's neat so times 2290            if (tmpu->friendly)291              _tmps=friendly_hacking_imgs;292            else293              _tmps=enemy_hacking_imgs;294            tt=4*Wrap(tmpu->phase+20*ts,0)/(2*pi);295          } else {296            if (tmpu->friendly)297              _tmps=friendly_imgs;298            else299              _tmps=enemy_imgs;300            tt=4*Wrap(tmpu->phase+5*ts,0)/(2*pi);301          }302          phase=tt; tt%=1.0;303          tmps=SpriteInterpolate(tt,_tmps[phase&3],_tmps[(phase+1)&3]);304          Sprite3Mat4x4B(dc,tmpu->x,yy,GR_Z_ALL,tmps,r);305          Free(tmps);306        }307      }308    }309  }310 311  dc->depth_buf=NULL;312  DCDel(dc);313  LBtr(&mp_not_done_flags,Gs->num);314  Seed; //Return Seth task to timer-based.315}316 317U0 MissilePos(I64 m,F64 theta,I64 *_x,I64 *_y)318{319  I64 n;320  if (m<2)321    n=-1;322  else323    n=1;324  *_x=x+3.0*Cos(theta)-(15.0-(m&1)<<3)*Cos(theta-n*pi/2);325  *_y=y+3.0*Sin(theta)-(15.0-(m&1)<<3)*Sin(theta-n*pi/2);326}327 328U0 DrawIt(CTask *task,CDC *dc)329{330  I64 i,m,xx,yy,scroll_y=MAP_HEIGHT-100-100*(tS-t0);331  F64 tt,ts=tS;332  Bullet *tmpb;333 334  dc->color=ROPF_DITHER|BROWN<<16|YELLOW;335  GrRect3(dc,0,0,0,dc->width,dc->height);336 337  for (i=0;i<LS_NUM;i++) {338    yy=(ls[i].y-scroll_y)&(MAP_HEIGHT-1);339    if (-32<=yy<=task->pix_bottom+32)340      Sprite3(dc,ls[i].x,yy,0,ls[i].img);341  }342 343  dc->thick=3;344  dc->color=BROWN;345  GrLine3(dc,0,  (finish_line-scroll_y)&(MAP_HEIGHT-1),0,346        GR_WIDTH,(finish_line-scroll_y)&(MAP_HEIGHT-1),0);347 348  dc->color=LTGRAY;349  for (i=0,tmpb=b;i<B_NUM;i++,tmpb++)350    if (!tmpb->dead) {351      if (tmpb->missile)352        Sprite3ZB(dc,tmpb->x>>32,tmpb->y>>32,0,<2>,pi/2+Arg(tmpb->dx,tmpb->dy));353      else354        GrLine(dc,tmpb->x>>32,tmpb->y>>32,355              (tmpb->x+tmpb->dy*B_LEN)>>32,356              (tmpb->y+tmpb->dx*B_LEN)>>32);357    }358 359  DCDepthBufAlloc(dc);360  mp_not_done_flags=1<<mp_cnt-1;361  for (i=0;i<mp_cnt;i++)362    JobQue(&MPMenDraw,dc,i);363  while (mp_not_done_flags)364    Yield;365  Free(dc->depth_buf);366  dc->depth_buf=NULL;367 368  for (m=0;m<4;m++)369    if (Bt(&missile_bmp,m)) {370      MissilePos(m,theta,&xx,&yy);371      Sprite3ZB(dc,xx,yy,0,<3>,theta);372    }373  Sprite3ZB(dc,x,y,0,<1>,theta);374 375  if (tf) {376    tt=tf;377    dc->color=RED;378    if (game_over && Blink)379      GrPrint(dc,task->pix_width/2-9*FONT_WIDTH/2,task->pix_height/2,380            "Game Over");381  } else {382    tt=ts;383    if (!enemy_left || !friendly_left)384      game_over=TRUE;385  }386  dc->color=BLACK;387  GrPrint(dc,0,0,"Enemy:%d Friends:%d Friendly Fire:%d Time:%6.2f Bullets:%d",388        enemy_left,friendly_left,friendly_fire,tt-t0,bullets_fired);389  GrPrint(dc,0,8,"Total Score:%,d High Score:%,d",390        total_score,best_score);391}392 393Explosion *ExplosionNew(I64 x,I64 y)394{395  I64 i;396  for (i=0;i<E_NUM;i++)397    if (e[i].dead) {398      e[i].x=x;399      e[i].y=y;400      e[i].dead=FALSE;401      return &e[i];402    }403  return NULL;404}405 406U0 ManDie(Unit *tmpu,Bool by_human)407{408  Explosion *tmpe;409 410  tmpu->dead=TRUE;411  if (by_human) {412    if (tmpe=ExplosionNew(tmpu->x,tmpu->y)) {413      tmpe->t0=tS;414      if (tmpu->tank) {415        tmpe->type=ET_TANK;416        tmpe->tf=tmpe->t0+0.40;417      } else {418        tmpe->type=ET_MAN;419        tmpe->tf=tmpe->t0+0.20;420      }421    }422  }423 424  if (tmpu->friendly) {425    friendly_left--;426    if (by_human)427      friendly_fire++;428  } else {429    enemy_left--;430    if (!by_human)431      enemy_by_friendly++;432  }433  if (by_human && !snd_timeout) {434    snd_timeout=tS+0.01;435    if (tmpu->friendly)436      Snd(46);437    else438      Snd(22);439  }440}441 442U0 ExplosionDo(I64 x,I64 y,I64 scroll_y)443{444  I64 i,x2,y2;445  Explosion *tmpe;446  Unit *tmpu;447  if (tmpe=ExplosionNew(x,y+scroll_y)) {448    tmpe->t0=tS;449    tmpe->type=ET_MISSILE;450    tmpe->tf=tmpe->t0+1.0;451  }452  for (i=0,tmpu=u;i<U_NUM;i++,tmpu++) {453    if (!tmpu->dead) {454      x2=tmpu->x;455      y2=(tmpu->y-scroll_y)&(MAP_HEIGHT-1);456      if (SqrI64(x-x2)+SqrI64(y-y2)<100*100)457        ManDie(tmpu,TRUE);458    }459  }460  for (i=0;i<LS_APTS_NUM;i++) {461    x2=ls[i].x;462    y2=(ls[i].y-scroll_y)&(MAP_HEIGHT-1);463    if (SqrI64(x-x2)+SqrI64(y-y2)<100*100) {464      if (tmpe=ExplosionNew(x2,y2+scroll_y)) {465        tmpe->t0=tS;466        tmpe->type=ET_BUILDING;467        tmpe->tf=tmpe->t0+2.0;468      }469    }470  }471}472 473U0 CheckCollisions()474{475  I64 i,n1,x,y,scroll_y=MAP_HEIGHT-100-100*(tS-t0);476  Unit *tmpu;477  Bullet *tmpb;478  CDC   *dc2=DCNew(GR_WIDTH,GR_HEIGHT);479  dc2->color=LTRED;480  for (i=0,tmpb=b;i<B_NUM;i++,tmpb++)481    if (!tmpb->dead && !tmpb->missile)  //Bullets not missiles482      GrLine(dc2,tmpb->x>>32,tmpb->y>>32,483            (tmpb->x+tmpb->dx*B_LEN)>>32,484            (tmpb->y+tmpb->dy*B_LEN)>>32);485 486  dc2->color  =ROP_COLLISION;487  dc2->bkcolor=BLACK;488  for (i=0,tmpu=u;i<U_NUM;i++,tmpu++) {489    if (!tmpu->dead) {490      x=tmpu->x;491      y=(tmpu->y-scroll_y)&(MAP_HEIGHT-1);492      if (0<=x<GR_WIDTH && 0<=y<GR_HEIGHT) {493        dc2->collision_cnt=0;494        GrRect(dc2,x-3,y-9,6,8);495        if (dc2->collision_cnt)496          ManDie(tmpu,TRUE);497      }498    }499  }500  DCDel(dc2);501 502  for (i=0,tmpb=b;i<B_NUM;i++,tmpb++)503    if (!tmpb->dead && tmpb->missile)  { //Missiles not bullets504      x=tmpb->x>>32;505      y=tmpb->y>>32;506      for (i=0,tmpu=u;i<U_NUM;i++,tmpu++) {507        if (tmpu->tank)508          n1=16;509        else510          n1=6;511        if (!tmpu->dead && AbsI64(x-tmpu->x-n1)+512              AbsI64(y-(tmpu->y-scroll_y)&(MAP_HEIGHT-1)+n1)<n1<<1) {513          tmpb->dead=TRUE;514          ExplosionDo(x,y,scroll_y);515        }516      }517      if (!tmpb->dead)518        for (i=0;i<LS_APTS_NUM;i++)519          if (2*SqrI64(x-ls[i].x)+520                3*SqrI64(y+35-(ls[i].y-scroll_y)&(MAP_HEIGHT-1))<2*60*60) {521            tmpb->dead=TRUE;522            ExplosionDo(x,y,scroll_y);523          }524    }525}526 527U0 Init()528{529  I64 i,xx,yy,scroll_y=MAP_HEIGHT-100;530  Unit *tmpu;531 532  snd_timeout=0;533  Snd;534 535  total_score=0;536  game_over=FALSE;537  main_loop_pass=0;538 539  x=Fs->pix_width>>1;540  y=0.9*Fs->pix_height;541  finish_line=scroll_y+y;542  dx=0;543  dy=0;544  theta=-pi/2;545 546  for (i=0;i<LS_NUM;i++) {547    ls[i].x=(Fs->pix_width-100)*RandU32/U32_MAX+50;548    ls[i].y=(MAP_HEIGHT-100)*RandU32/U32_MAX+50;549    ls[i].img=landscape_imgs[RandU16%(LS_TYPES-2)];550  }551 552  for (i=0;i<LS_APTS_NUM;i++)553    ls[i].img=landscape_imgs[LS_TYPES-1]; //Apartment554 555  for (;i<LS_APTS_NUM+LS_MOUNTAINS_NUM;i++)556    ls[i].img=landscape_imgs[LS_TYPES-2]; //mountain557 558  MemSet(u,0,sizeof(u));559  for (i=0,tmpu=u;i<U_NUM;i++,tmpu++) {560    if (i<U_FRIENDLY_NUM) {561      tmpu->friendly=TRUE;562      if (!(i&7)) {563        xx=(Fs->pix_width-200)*RandU32/U32_MAX;564        yy=(MAP_HEIGHT-200)*RandU32/U32_MAX;565      }566    } else {567      tmpu->friendly=FALSE;568      if (!(i&31)) {569        xx=(Fs->pix_width-200)*RandU32/U32_MAX;570        yy=(MAP_HEIGHT-200)*RandU32/U32_MAX;571      }572      if (!(i&15))573        tmpu->tank=TRUE;574    }575    tmpu->dead=FALSE;576    tmpu->x=xx+64*RandI32/I32_MAX+100;577    tmpu->y=yy+64*RandU32/I32_MAX-64+100;578    tmpu->best_dd=I64_MAX;579    tmpu->theta=pi/2;580    tmpu->phase=2*pi*Rand;581  }582  for (i=0;i<B_NUM;i++)583    b[i].dead=TRUE;584  for (i=0;i<E_NUM;i++) {585    e[i].dead=TRUE;586    e[i].num=i;587  }588  friendly_left=U_FRIENDLY_NUM;589  enemy_left   =U_ENEMY_NUM;590  enemy_by_friendly=0;591  bullets_fired=0;592  missile_bmp =15;593  friendly_fire=0;594  t0=tS;595  tf=0;596}597 598U0 FireBullet()599{600  I64 i,j;601  F64 a;602  Bullet *tmpb;603  for (i=0;i<B_NUM-1;i++)604    if (b[i].dead)605      break;606  tmpb=&b[i];607 608  j=x+28.0*Cos(theta);609  tmpb->x=j<<32;610  j=y+28.0*Sin(theta);611  tmpb->y=j<<32;612 613  tmpb->dx2=(B_SPEED*Cos(theta)+dx)*0x100000000;614  tmpb->dy2= B_SPEED*Sin(theta)*0x100000000;615  a=Arg(tmpb->dx2,tmpb->dy2);616  tmpb->dx=Sin(a)*0x100000000;617  tmpb->dy=Cos(a)*0x100000000;618  bullets_fired++;619  tmpb->dead=FALSE;620  tmpb->missile=FALSE;621  if (!snd_timeout) {622    snd_timeout=tS+0.0005;623    Snd(74);624  }625}626 627Bool FireMissile(I64 n)628{629  I64 i,m;630  F64 a;631  Bullet *tmpb;632  Bool res=FALSE;633 634  m=n;635  if (Btr(&missile_bmp,++m) || Btr(&missile_bmp,++m))636    res=TRUE;637 638  if (res) {639    for (i=0;i<B_NUM-1;i++)640      if (b[i].dead)641        break;642    tmpb=&b[i];643 644    MissilePos(m,theta,&tmpb->x,&tmpb->y);645    tmpb->x<<=32;646    tmpb->y<<=32;647 648    tmpb->dx2=(B_SPEED*Cos(theta)+dx)*0x100000000;649    tmpb->dy2= B_SPEED*Sin(theta)*0x100000000;650    a=Arg(tmpb->dx2,tmpb->dy2);651    tmpb->dx=Sin(a)*0x100000000;652    tmpb->dy=Cos(a)*0x100000000;653    tmpb->dead=FALSE;654    tmpb->missile=TRUE;655    if (!snd_timeout) {656      snd_timeout=tS+0.0005;657      Snd(74);658    }659  }660}661 662U0 MenMove(I64 phase_group)663{664  I64 i,j,dd,best,best_dd;665  for (i=phase_group;i<U_FRIENDLY_NUM;i+=PHASES_GROUPS) {666    if (!u[i].dead) {667      best=U_FRIENDLY_NUM;668      best_dd=I64_MAX;669      for (j=U_FRIENDLY_NUM;j<U_NUM;j++) {670        if (!u[j].dead) {671          dd=SqrI64(u[i].x-u[j].x)+SqrI64(u[i].y-u[j].y);672          if (dd<best_dd) {673            best_dd=dd;674            best=j;675          }676        }677      }678      u[i].best_dd=best_dd;679      if (best_dd!=I64_MAX) {680        u[i].x+=4*SignI64(u[best].x-u[i].x);681        u[i].y+=4*SignI64(u[best].y-u[i].y);682        u[i].theta=Arg(u[best].x-u[i].x,u[best].y-u[i].y);683      }684    }685  }686  for (i=U_FRIENDLY_NUM+phase_group;i<U_NUM;i+=PHASES_GROUPS) {687    if (!u[i].dead) {688      best=0;689      best_dd=I64_MAX;690      for (j=0;j<U_FRIENDLY_NUM;j++) {691        if (!u[j].dead) {692          dd=SqrI64(u[i].x-u[j].x)+SqrI64(u[i].y-u[j].y);693          if (dd<best_dd) {694            best_dd=dd;695            best=j;696          }697        }698      }699      u[i].best_dd=best_dd;700      if (best_dd!=I64_MAX) {701        u[i].x+=4*SignI64(u[best].x-u[i].x);702        u[i].y+=4*SignI64(u[best].y-u[i].y);703        u[i].theta=Arg(u[best].x-u[i].x,u[best].y-u[i].y);704      }705    }706  }707}708 709U0 MenFight(I64 phase_group)710{711  I64 i,j,dd,best,best_dd;712  for (i=phase_group;i<U_FRIENDLY_NUM;i+=PHASES_GROUPS) {713    if (!u[i].dead) {714      best=U_FRIENDLY_NUM;715      best_dd=I64_MAX;716      for (j=U_FRIENDLY_NUM;j<U_NUM;j++) {717        if (!u[j].dead && u[i].y-u[j].y<8) {718          dd=SqrI64(u[i].x-u[j].x)+SqrI64(u[i].y-u[j].y);719          if (dd<best_dd) {720            best_dd=dd;721            best=j;722          }723        }724      }725      u[i].best_dd=best_dd;726      if (best_dd<HACK_DIST*HACK_DIST && !(RandU16&1))727        ManDie(&u[best],FALSE);728    }729  }730  for (i=U_FRIENDLY_NUM+phase_group;i<U_NUM;i+=PHASES_GROUPS) {731    if (!u[i].dead) {732      best=0;733      best_dd=I64_MAX;734      for (j=0;j<U_FRIENDLY_NUM;j++) {735        if (!u[j].dead&& u[i].y-u[j].y<8) {736          dd=SqrI64(u[i].x-u[j].x)+SqrI64(u[i].y-u[j].y);737          if (dd<best_dd) {738            best_dd=dd;739            best=j;740          }741        }742      }743      u[i].best_dd=best_dd;744      if (best_dd<HACK_DIST*HACK_DIST && !(RandU16&1))745        ManDie(&u[best],FALSE);746    }747  }748}749 750U0 Titanium()751{752  I64 i,msg_code,ch,sc;753  Bool gun_on;754 755  I64 next_update_jiffy;756  SettingsPush; //See SettingsPush757 758  MenuPush(759        "File {"760        "  Abort(,CH_SHIFT_ESC);"761        "  Exit(,CH_ESC);"762        "}"763        "Play {"764        "  Restart(,'\n');"765        "  Fire(,CH_SPACE);"766        "  Left(,,SC_CURSOR_LEFT);"767        "  Right(,,SC_CURSOR_RIGHT);"768        "  LeftMissile(,,SC_CURSOR_LEFT|SCF_CTRL);"769        "  RightMissile(,,SC_CURSOR_RIGHT|SCF_CTRL);"770        "}"771        );772  AutoComplete;773  WinBorder;774  WinMax;775  DocCursor;776  DocClear;777 778  "\nScoring:\n"779        "\tEnemy Killed\t\t+3\n"780        "\tEnemy by Friendly\t+5\n"781        "\tFriendly Fire\t\t-100\n"782        "\tBullets Fired\t\t-1\n"783        "\tGame Time\t\t-10 per second\n"784        "\tSurviving Friendlies\t+250\n\n"785        "Sweep side-to-side while shooting, "786        "holding down $GREEN$<SPACE>$FG$.\n\n";787  PressAKey;788 789  Init;790  DocClear;791  Fs->draw_it=&DrawIt;792 793  gun_on=FALSE;794 795  try {796    while (TRUE) {797      next_update_jiffy=cnts.jiffies+JIFFY_FREQ/100;798      while (msg_code=ScanMsg(&ch,&sc,1<<MSG_KEY_DOWN+1<<MSG_KEY_UP)) {799        switch (msg_code) {800          case MSG_KEY_DOWN:801            switch (ch) {802              case 0:803                switch (sc.u8[0]) {804                  case SC_CURSOR_RIGHT:805                    if (sc&SCF_CTRL)806                      FireMissile(1);807                    break;808                  case SC_CURSOR_LEFT:809                    if (sc&SCF_CTRL)810                      FireMissile(-1);811                    break;812                }813                break;814              case CH_SHIFT_ESC:815              case CH_ESC:816                goto to_done;817              case '\n':818                Init;819                break;820              case CH_SPACE:821                gun_on=TRUE;822                break;823            }824            break;825          case MSG_KEY_UP:826            if (ch==CH_SPACE)827              gun_on=FALSE;828            else if (sc.u8[0]==SC_CURSOR_RIGHT||sc.u8[0]==SC_CURSOR_LEFT)829              theta=-pi/2;830            break;831        }832      }833 834      for (i=0;i<B_NUM;i++) {835        if (!b[i].dead) {836          b[i].x+=b[i].dx2;837          b[i].y+=b[i].dy2;838          if (b[i].y<0||b[i].x<0||839                b[i].x>>32>=Fs->pix_width||840                b[i].y>>32>=Fs->pix_height)841            b[i].dead=TRUE;842        }843      }844 845      dx=0;846      if (!Bt(kbd.down_bitmap,SC_CTRL)) {847        if (Bt(kbd.down_bitmap,SC_CURSOR_LEFT)) {848          theta=-pi/2-15.0*pi/180.0;849          dx=-2;850        } else if (Bt(kbd.down_bitmap,SC_CURSOR_RIGHT)) {851          theta=-pi/2+15.0*pi/180.0;852          dx=2;853        }854      }855      x+=dx;856      while (x>=Fs->pix_width)857        x-=Fs->pix_width;858      while (x<0)859        x+=Fs->pix_width;860 861        //It takes too much CPU do do all these all the time.862      switch [main_loop_pass&7] {863        case 0:864          switch [main_loop_pass>>3&7] {865            case 0:866              if (--y<20)867                game_over=TRUE;868            case 2:869            case 4:870            case 6:871              MenFight(main_loop_pass>>4%PHASES_GROUPS);872              break;873            case 1:874            case 3:875            case 5:876            case 7:877              MenMove (main_loop_pass>>4%PHASES_GROUPS);878              break;879          }880        case 4:881          break;882        case 2:883        case 6:884          if (gun_on)885            FireBullet;886          break;887        case 1:888        case 3:889        case 5:890        case 7:891          CheckCollisions;892          break;893      }894      main_loop_pass++;895 896      if (snd_timeout && tS>snd_timeout) {897        snd_timeout=0;898        Snd;899      }900      SleepUntil(next_update_jiffy);901 902      total_score=3*(U_ENEMY_NUM-enemy_left)+5*enemy_by_friendly903            -100*friendly_fire-10*(tS-t0)-bullets_fired;904      if (game_over) {905        tf=tS;906        Sleep(750);907        FlushMsgs;908        while (!ScanKey(&ch)&&friendly_left||tS-tf<1.5) {909          total_score+=250;910          Snd(86); Sleep(150);911          Snd;    Sleep(50);912          friendly_left--;913        }914        total_score+=250*friendly_left;915        if (total_score>best_score)916          best_score=total_score;917        if (!ch) ch=GetChar(,FALSE);918        if (ch==CH_ESC||ch==CH_SHIFT_ESC)919          goto to_done;920        gun_on=FALSE;921        Init;922      }923    }924to_done:925    GetMsg(,,1<<MSG_KEY_UP);926  }927  catch928    PutExcept;929  SettingsPop;930  MenuPop;931  RegWrite("TempleOS/Titanium","I64 best_score=%d;\n",best_score);932}933