File size: 5,658 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

RegDft("TempleOS/ZoneOut","F64 best_score=9999;\n");
RegExe("TempleOS/ZoneOut");










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
























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













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

/* Graphics Not Rendered in HTML */










#define THEM_NUM        10

class Obj
{
  Obj *next,*last;
  F64 t0,theta;
  I64 x,y,z;
  Bool hit,pad[7];
} us,them[THEM_NUM],shots;
I64 num_them;
F64 t0,tf;

#define SCRN_SCALE      512
#define TANK_HEIGHT     32

U0 ZOTransform(CDC *dc,I64 *x,I64 *y,I64 *z)
{
  I64 zz;
  Mat4x4MulXYZ(dc->r,x,y,z);
  zz=*z;
  if (zz<1) zz=1;
  *x=SCRN_SCALE/2* *x/zz;
  *y=SCRN_SCALE/2* (*y+TANK_HEIGHT)/zz;
  *x+=dc->x;
  *y+=dc->y;
  *z+=dc->z;
}

U0 DrawIt(CTask *task,CDC *dc)
{
  Obj *tmpo;
  I64 i,dd,y,w=task->pix_width,h=task->pix_height,cx=w>>1,cy=h>>1;
  U8 *img;
  F64 tt,theta;

  theta=640*Wrap(2*us.theta)/pi;
  Sprite3(dc,theta-1280,90,0,<4>);
  Sprite3(dc,theta         ,90,0,<4>);
  Sprite3(dc,theta+1280,90,0,<4>);

  DCDepthBufAlloc(dc);
  dc->transform=&ZOTransform;
  dc->x=cx;
  dc->y=cy;
  Mat4x4TranslationEqu(dc->r,-us.x,-us.y,-us.z);
  Mat4x4RotY(dc->r,us.theta-pi/2);
  Mat4x4RotX(dc->r,pi/16);

  dc->flags|=DCF_TRANSFORMATION;
  for (i=0;i<THEM_NUM;i++) {
    y=them[i].y;
    tmpo=shots.next;
    while (tmpo!=&shots) {
      dd=SqrI64(them[i].x-tmpo->x)+SqrI64(them[i].z-tmpo->z);
      if (dd<SCRN_SCALE/2*SCRN_SCALE/2) {
        y-=Sqrt(dd);
        if (!them[i].hit) {
          them[i].hit=TRUE;
          if (!--num_them) {
            tf=tS;
            if (tf-t0<best_score)
              best_score=tf-t0;
          }
        }
      }
      tmpo=tmpo->next;
    }
    if (them[i].hit)
      img=<2>;
    else
      img=<1>;
    Sprite3YB(dc,them[i].x,y,them[i].z,img,-them[i].theta);
  }
  tmpo=shots.next;
  while (tmpo!=&shots) {
    Sprite3YB(dc,tmpo->x,tmpo->y,tmpo->z,<3>,-tmpo->theta);
    tmpo=tmpo->next;
  }
  dc->flags&=~DCF_TRANSFORMATION;
  dc->color=LTGREEN;
  GrLine(dc,cx-5,cy,cx+5,cy);
  GrLine(dc,cx,cy-5,cx,cy+5);
  if (tf) {
    dc->color=RED;
    if (Blink)
      GrPrint(dc,cx-(FONT_WIDTH*14)/2,cy-FONT_HEIGHT/2,"Game Completed");
    tt=tf;
  } else {
    dc->color=BLACK;
    GrLine(dc,cx-5,cy,cx+5,cy);
    GrLine(dc,cx,cy-5,cx,cy+5);
    tt=tS;
  }
  dc->color=BLACK;
  GrPrint(dc,0,0,"Enemy:%d Time:%3.2f Best:%3.2f",num_them,tt-t0,best_score);
}

U0 Fire()
{
  Obj *tmpo=MAlloc(sizeof(Obj));
  tmpo->x=us.x;
  tmpo->y=TANK_HEIGHT;
  tmpo->z=us.z;
  tmpo->theta=us.theta;
  tmpo->t0=tS;
  QueIns(tmpo,shots.last);
}

U0 MoveUs(F64 theta)
{
  us.x+=0.1*SCRN_SCALE*Cos(theta);
  us.z+=0.1*SCRN_SCALE*Sin(theta);
}

U0 AnimateTask(I64)
{
  I64 i;
  Obj *tmpo,*tmpo1;
  while (TRUE) {
    for (i=0;i<THEM_NUM;i++) {
      them[i].x+=SCRN_SCALE/32*Cos(them[i].theta);
      them[i].z+=SCRN_SCALE/32*Sin(them[i].theta);
      them[i].theta+=Rand/100.0;
    }
    tmpo=shots.next;
    while (tmpo!=&shots) {
      tmpo1=tmpo->next;
      if (tS-tmpo->t0>1.0) {
        QueRem(tmpo);
        Free(tmpo);
      } else {
        tmpo->x+=0.25*SCRN_SCALE*Cos(tmpo->theta);
        tmpo->z+=0.25*SCRN_SCALE*Sin(tmpo->theta);
      }
      tmpo=tmpo1;
    }
    Sleep(20);
  }
}

U0 Init()
{
  I64 i;
  DocClear;
  "$BG,LTCYAN$%h12c",'\n';
  QueInit(&shots);
  MemSet(&us,0,sizeof(us));
  MemSet(them,0,sizeof(them));
  num_them=THEM_NUM;
  for (i=0;i<THEM_NUM;i++) {
    them[i].x=10000*Rand-5000;
    them[i].z=10000*Rand-5000;
    them[i].theta=2*pi*Rand;
    them[i].hit=FALSE;
  }
  tf=0;
  t0=tS;
}

U0 CleanUp()
{
  QueDel(&shots,TRUE);
}

U0 SongTask(I64)
{//Randomly generate (by God :-)
  Fs->task_end_cb=&SndTaskEndCB;
  MusicSettingsRst;
  while (TRUE) {
    Play("5sD4B5D4B5qEsG4B5G4B5eD4GsB5F4B5FeD4A5qFG");
    Play("5sD4B5D4B5qEsG4B5G4B5eD4GsB5F4B5FeD4A5qFG");
    Play("5eGDsFGFGqDE4eB5E4sG5D4G5DqF4sGAGA");
    Play("5eGDsFGFGqDE4eB5E4sG5D4G5DqF4sGAGA");
  }
}

U0 ZoneOut()
{
  I64 sc;

  PopUpOk(
        "I refuse to rip-off the original\n"
        "so this is intentionally crappy\n"
        "and included for demonstration\n"
        "purposes.\n\n"
        "Write games, don't play them.\n");

  MenuPush(
        "File {"
        "  Abort(,CH_SHIFT_ESC);"
        "  Exit(,CH_ESC);"
        "}"
        "Play {"
        "  Restart(,'\n');"
        "  Fwd(,,SC_CURSOR_UP);"
        "  Bwd(,,SC_CURSOR_DOWN);"
        "  Left(,,SC_CURSOR_LEFT);"
        "  Right(,,SC_CURSOR_RIGHT);"
        "  Fire(,CH_SPACE);"
        "}"
        );

  SettingsPush; //See SettingsPush
  Fs->text_attr=YELLOW<<4+WHITE;
  AutoComplete;
  WinBorder;
  WinMax;
  DocCursor;
  Init;
  Fs->animate_task=Spawn(&AnimateTask,NULL,"Animate",,Fs);
  Fs->song_task=Spawn(&SongTask,NULL,"Song",,Fs);
  Fs->draw_it=&DrawIt;
  try {
    while (TRUE) {
      switch (GetKey(&sc)) {
        case CH_SPACE:
          Fire;
          break;
        case '\n':
          CleanUp;
          Init;
          break;
        case CH_ESC:
        case CH_SHIFT_ESC:
          goto zo_done;
        case 0:
          switch (sc.u8[0]) {
            case SC_CURSOR_RIGHT:
              us.theta-=pi/256;
              break;
            case SC_CURSOR_LEFT:
              us.theta+=pi/256;
              break;
            case SC_CURSOR_UP:
              MoveUs(us.theta);
              break;
            case SC_CURSOR_DOWN:
              MoveUs(us.theta+pi);
              break;
          }
          break;
      }
    }
zo_done:
  } catch
    PutExcept;
  CleanUp;
  DocClear;
  SettingsPop;
  MenuPop;
  RegWrite("TempleOS/ZoneOut","F64 best_score=%5.4f;\n",best_score);
}

ZoneOut;