File size: 7,263 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401

/*Sprites were created with <CTRL-r>
and can be edited by moving the
cursor on top and pressing <CTRL-r>.

<CTRL-t> to see the num of the sprite
and <CTRL-t> again to get back.  Nums
are assigned by the editor.
*/







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







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












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



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







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



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

//See ::/Doc/Credits.DD.



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






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








































































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

RegDft("TempleOS/BlackDiamond","I64 best_score=9999;\n");
RegExe("TempleOS/BlackDiamond");

#define MAP_HEIGHT      3000
#define OBJS_NUM        128

I64 x,y,scrn_top_y,penalty,chair_lift_num;
I64 obj_x[OBJS_NUM],obj_y[OBJS_NUM],obj_types[OBJS_NUM],
    obj_saved_x[OBJS_NUM],obj_saved_y[OBJS_NUM];
Bool game_over;

#define T_TREE          0
#define T_LITTLE_ROCK   1
#define T_BIG_ROCK      2
#define T_WOLF          3
#define T_CHAIR_LIFT    4
U8 *imgs[5]={<3>,<4>,<5>,<6>,<9>};

#define DC_WOLF_WIDTH   48
#define DC_WOLF_HEIGHT  38
U0 DrawRunningWolf(CDC *dc,I64 i)
{//Draw wolf on small DC so bottom gets clipped looking like sunk into snow.
//Then, copy onto main DC.
  U8 *tmps;
  CDC *dc_wolf=DCNew(DC_WOLF_WIDTH,DC_WOLF_HEIGHT);
  DCFill(dc_wolf);
  if (x<obj_x[i]) {
    dc_wolf->flags|=DCF_SYMMETRY|DCF_TRANSFORMATION|DCF_JUST_MIRROR;
    DCSymmetrySet(dc_wolf,DC_WOLF_WIDTH/2,0,DC_WOLF_WIDTH/2,1);
  }
  if (tS%1.0<0.5)
    tmps=<6>;
  else
    tmps=<7>;
  Sprite3ZB(dc_wolf,DC_WOLF_WIDTH/2,0.85*DC_WOLF_HEIGHT,
        0,tmps,0.4*Sin(7*tS));
  GrBlot(dc,obj_x[i]-DC_WOLF_WIDTH/2,
        obj_y[i]-scrn_top_y-0.85*DC_WOLF_HEIGHT,dc_wolf);
  DCDel(dc_wolf);
  if (0<=(obj_y[i]-scrn_top_y)<GR_HEIGHT)
    obj_x[i]+=3*SignI64(x-obj_x[i]);
}

U0 DrawMan(CDC *dc)
{
  if(dc->collision_cnt)
    Sprite3(dc,x,y-scrn_top_y,0,<2>); //Red man
  else
    Sprite3(dc,x,y-scrn_top_y,0,<1>); //Normal man
}

#define WIRE_HEIGHT     595
#define WIRE_WIDTH      121
#define WIRE_DIP        800
#define PULLY_WIDTH     13
U0 DrawUpperChairLift(CDC *dc)
{
  I64 i=chair_lift_num,x,x1,y=obj_y[i]-scrn_top_y-WIRE_HEIGHT,y1;
  Sprite3(dc,obj_x[i],y+13,0,<8>);
  dc->thick=2;
  for (x=-WIRE_WIDTH/2;x<GR_WIDTH+WIRE_WIDTH/2;x+=2) { //Pen width is 2, step 2.
    x1=x-obj_x[i];
    y1=y-WIRE_DIP/2+Cosh(Ln(WIRE_DIP)+(Abs(x1)-PULLY_WIDTH)/10000.0)+0.1*x1;
    if (!(-PULLY_WIDTH<x1<PULLY_WIDTH)) {
      GrPlot3(dc,x-WIRE_WIDTH/2,y1,0);
      GrPlot3(dc,x+WIRE_WIDTH/2,y1,0);
    }
  }
  dc->thick=1;
}

U0 DrawIt(CTask *,CDC *dc)
{
  I64 i;
  Bool man_drawn=FALSE;

  for (i=0;i<OBJS_NUM;i++)
    if (obj_y[i]-32<=y<=obj_y[i]) {
      if (obj_types[i]==T_WOLF && AbsI64(x-obj_x[i])>3)
        DrawRunningWolf(dc,i);
      else
        Sprite3(dc,obj_x[i],obj_y[i]-scrn_top_y,0,imgs[obj_types[i]]);
    }

  dc->collision_cnt=0;
  dc->color=ROP_COLLISION;
  dc->bkcolor=WHITE;
  Sprite3(dc,x,y-scrn_top_y,0,<1>);
  if (!game_over) {
    if(dc->collision_cnt) {
      Snd(58);
      penalty++; //Time is irregular. Scoring is imperfect. 30fps more or less.
    } else
      Snd;
  }

  dc->color=ROP_EQU;
  for (i=0;i<OBJS_NUM;i++) {
    if (!man_drawn && obj_y[i]>=y) {
      DrawMan(dc);
      man_drawn=TRUE;
    }
    if (obj_types[i]==T_WOLF && AbsI64(x-obj_x[i])>3)
      DrawRunningWolf(dc,i);
    else
      Sprite3(dc,obj_x[i],obj_y[i]-scrn_top_y,0,imgs[obj_types[i]]);
  }
  if (!man_drawn)
    DrawMan(dc);
  DrawUpperChairLift(dc);

  dc->color=BLACK;
  GrPrint(dc,0,0,"Penalty:%d Best:%d",penalty,best_score);
  if (game_over && Blink) {
    dc->color=RED;
    GrPrint(dc,(Fs->pix_width-9*FONT_WIDTH)/2,
          (Fs->pix_height-FONT_HEIGHT)/2,"Game Over");
  }
}

I64 Compare(I64 e1,I64 e2)
{
  return e1-e2;
}

U0 Init()
{
  I64 i,j;
  for (i=0;i<OBJS_NUM;i++) {
    obj_saved_y[i]=RandU32%MAP_HEIGHT;
    obj_saved_x[i]=RandU32%GR_WIDTH;
    j=RandU16;
    if (j&7)
      obj_types[i]=T_TREE;
    else if (j&31)
      obj_types[i]=T_LITTLE_ROCK;
    else if (j&63)
      obj_types[i]=T_BIG_ROCK;
    else
      obj_types[i]=T_WOLF;
  }
  QSortI64(obj_saved_y,OBJS_NUM,&Compare); //Break associations. Doesn't matter.

  chair_lift_num=RandU16%OBJS_NUM;
  obj_types[chair_lift_num]=T_CHAIR_LIFT;
}

U0 BlackDiamond()
{
  I64 ch,sc;

  MenuPush(
        "File {"
        "  New(,'\n');"
        "  Restart(,CH_SPACE);"
        "  Abort(,CH_SHIFT_ESC);"
        "  Exit(,CH_ESC);"
        "}"
        "Play {"
        "  Up(,,SC_CURSOR_UP);"
        "  Down(,,SC_CURSOR_DOWN);"
        "  Left(,,SC_CURSOR_LEFT);"
        "  Right(,,SC_CURSOR_RIGHT);"
        "}"
        );
  SettingsPush; //See SettingsPush
  AutoComplete;
  WinBorder;
  WinMax;
  DocCursor;
  DocClear;
  Fs->draw_it=&DrawIt;

  Init;
  try {
    while (TRUE) {
bd_restart:
      MemCpy(obj_x,obj_saved_x,sizeof(obj_x)); //Wolves move. We must reset the
      MemCpy(obj_y,obj_saved_y,sizeof(obj_y)); //wolves if running same game.

      game_over=FALSE;
      scrn_top_y=0;
      x=Fs->pix_width>>1; y=0;
      penalty=0;
      while (TRUE) {
        if (ScanKey(&ch,&sc)) {
          switch (ch) {
            case 0:
              if (!game_over)
                switch (sc.u8[0]) {
                  case SC_CURSOR_RIGHT:
                    x+=10;
                    if (x>=Fs->pix_width) x-=Fs->pix_width;
                    break;
                  case SC_CURSOR_LEFT:
                    x-=10;
                    if (x<0) x+=Fs->pix_width;
                    break;
                  case SC_CURSOR_UP:
                    y-=10;
                    break;
                  case SC_CURSOR_DOWN:
                    y+=10;
                    break;
                }
              break;

            case CH_ESC:
            case CH_SHIFT_ESC:
              goto bd_done;

            case '\n':
              Init;
              goto bd_restart;

            case CH_SPACE:
              game_over=TRUE;
              while (scrn_top_y>0||y>0) {//Animate going back to top.
                scrn_top_y=MaxI64(0,scrn_top_y-4);
                y=MaxI64(0,y-4);
                x+=SignI64(Fs->pix_width>>1-x);
                Sleep(1);
              }
              Snd;
              goto bd_restart;
          }
//Don't want keystrokes building-up in the buf.
          FlushMsgs;
        }
        if (!game_over) {
          y+=2;
          scrn_top_y++;
          if (y-scrn_top_y>Fs->pix_height) {//Animate scrolling scrn.
            while (y-scrn_top_y>Fs->pix_height>>1) {
              scrn_top_y+=2;
              Sleep(1);
            }
          }
          if (y>=MAP_HEIGHT) {
            game_over=TRUE;
            Beep;
            if (penalty<=best_score) {
              best_score=penalty;
              Beep;
            }
          }
        }
        Sleep(10);
      }
    }
bd_done:
  } catch
    PutExcept;
  SettingsPop;
  MenuPop;
  RegWrite("TempleOS/BlackDiamond","I64 best_score=%d;\n",best_score);
}

BlackDiamond; //Start game when #included.