Subversion Repositories Kolibri OS

Rev

Rev 484 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
484 diamond 1
LEVELCONV equ 10
2
SQ equ 8
3
FLDSIZE equ 49
4
FLDSIZE2 equ FLDSIZE*8
5
DELAY  equ 20
6
TICKS  equ 10
7
LEV_START equ 1
8
MOBILITY equ 9;13
9
 
10
  use32
11
  org     0x0
12
 
13
  db     'MENUET01'              ; 8 byte id
14
  dd     0x01                    ; header version
15
  dd     run                   ; start of code
16
  dd     I_END                   ; size of image
17
  dd     stak+0x400             ; memory for app
18
  dd     stak+0x400             ; esp
19
  dd     0x0 , 0x0               ; I_Param , I_Icon
20
include "lang.inc"
21
include "MACROS.INC"
22
;include "DEBUG.INC"
23
 
24
run:
25
if  LEVELCONV eq 1
26
    call compress_levels
27
    jmp  close
28
end if
29
    mcall 3
30
    mov  cl,16
31
    ror  eax,cl
32
    mov  [generator],eax    ; random generator from Tetris
33
reset:
34
    mov  [levptr],level
35
    mov  [levnum],LEV_START
36
  if ~ LEV_START eq 1
37
    mov  ecx,[levnum]
38
    dec  ecx
39
    mov  esi,[levptr]
40
    jecxz start
41
  .lp:
42
    movzx eax,word[esi]
43
    and  eax,0x7fff
44
    add  esi,eax ; limit
45
    loop .lp
46
    mov  [levptr],esi
47
;    sub  esi,level
48
;    dpd  esi
49
  end if
50
start:
51
    mov  ecx,[generator]
52
    and  ecx,0xff
53
  .shuf:
54
    call random
55
    loop .shuf
56
    xor  eax,eax
57
    and  [locked],eax
58
    and  [captured],eax
59
    mov  [ticks],TICKS
60
    mov  [mode],-1
61
    mov  ebx,FLDSIZE
62
    mov  eax,[generator]
63
    xor  edx,edx
64
    div  ebx
65
    mov  cl,dl
66
    call random
67
    mov  eax,[generator]
68
    xor  edx,edx
69
    div  ebx
70
    mov  ch,dl
71
    xchg eax,ecx
72
    movzx ecx,al
73
    imul cx,bx
74
    shr  ax,8
75
    add  cx,ax
76
    mov  esi,[levptr]
77
    mov  [dot],ecx
78
    call decompress_level
79
    mov  ebx,8
80
    call  nokey
81
red:
82
    call  draw_window
83
;    and  [event],0
84
 
85
still:
86
 
87
  .flush:
88
    mcall 2
89
    test al,al
90
    jz   .flush
91
 
92
    mov  ebx,DELAY
93
    mov  eax,[levnum]
94
    shr  eax,1
95
    sub  ebx,eax
96
    mov  eax,23
97
    cmp  [mode],0
98
    jl   .wait
99
    mov  eax,10
100
  .wait:
101
    mcall
102
  .evt:
103
    mov  ebx,8
104
    test eax,eax
105
    jne  .ev_yes
106
    dec  [ticks]
107
    jne  still
108
;    mov  [ticks],TICKS
109
;    cmp  al,[event]
110
    jmp   key.ex
111
;    and  [event],al
112
;    jmp  still
113
  .ev_yes:
114
    cmp  eax,2
115
    je   key
116
    cmp  eax,1                  ; redraw request ?
117
    je   red
118
    cmp  eax,3                  ; button in buffer ?
119
    je   button
120
key:
121
;    mov  [event],al
122
    mcall 2
123
    cmp  ah,13
124
    je   button.mode
125
    cmp  ah,27
126
    je   button.mode
127
    cmp  [mode],-1
128
    jne  still
129
    cmp  ah,104
130
    jne  .nohelp
131
    mov  [mode],4
132
    jmp  red
133
  .nohelp:
134
    cmp  ah,112
135
    jne  .nopau
136
    mov  [mode],3
137
    jmp  red
138
  .nopau:
139
    shr  eax,8
140
;    dpd  eax
141
    cmp  eax,' '
142
    je   .ex
143
    cmp  eax,176
144
    jb   still
145
    cmp  eax,179
146
    ja   still
147
    cmp  [locked],0
148
    jnz  still
149
    lea  ebx,[eax-176]
150
  .ex:
151
    mov  [ticks],TICKS
152
    call nokey
153
    call evolution
154
    call  red_field
155
    jmp  still
156
 
157
button:                       ; BUTTON - only close supported
158
    mcall 17
159
    cmp  ah,2
160
    jne  close
161
    mcall 8,,,2+1 shl 31
162
;    mov  [event],2
163
  .mode:
164
    mov  [ticks],TICKS
165
    mov  al,[mode]
166
    cmp  al,1
167
    je   reset
168
    cmp  al,2
169
    je   reset
170
    mov  [mode],-1
171
    test  al,al
172
    jnz  .no0
173
    inc  [levnum]
174
    jmp  start
175
  .no0:
176
    jmp  red
177
  close:
178
    or   eax,-1
179
    int  0x40
180
 
181
;   *********************************************
182
;   *******  WINDOW DEFINITIONS AND DRAW ********
183
;   *********************************************
184
draw_window:
185
    mov  [ticks],TICKS
186
    mcall 12,1
187
    mcall 0,<100,FLDSIZE2+16>,<100,FLDSIZE2+38>,0x03008000
188
    mcall 4,<8,8>,0x10ffffff,header,header.size
189
    mov   esi,ecx
190
  if lang eq ru
191
    mcall 47,0x20000,[levnum],<165,8>
192
  else
193
    mcall 47,0x20000,[levnum],<145,8>
194
  end if
195
    call  red_field
196
    cmp   [mode],0
197
    jl    .edraw
198
    mov   ecx,(FLDSIZE2-20) shl 16+20
199
    cmp   [mode],4
200
    jne    .nohelp
201
    mov   ecx,100 shl 16+FLDSIZE2-35-100+38
202
  .nohelp:
203
    mcall 13,<5+8,FLDSIZE2-8>,,0xf5deb3;0xffff00
204
    mcall 8,,,2,0xc0c000
205
    mcall 4,<363,FLDSIZE2-20+6>,0x100000ff,msg_ok,2
206
    mov   ebx,(5+8+35)shl 16+FLDSIZE2-20+6
207
    movzx ecx,[mode]
208
    mov   edx,messages+4
209
    cmp   ecx,4
210
    jne   .nxt
211
    mov   edx,desc+4
212
    xor   ecx,ecx
213
    mov   ebx,20 shl 16+110
214
  .nxt:
215
    mov   esi,[edx-4]
216
    jecxz .drw
217
    dec   ecx
218
    lea   edx,[edx+esi+4]
219
    jmp   .nxt
220
  .drw:
221
    cmp   esi,-1
222
    je    .edraw
223
    mcall ,,0x000000ff
224
    mov   ecx,1
225
    add   ebx,18
226
    cmp   [mode],4
227
    je    .nxt
228
  .edraw:
229
    mcall 12,2
230
    ret
231
 
232
red_field:
233
    mov   edi,field
234
    mov   ebp,28 shl 16+SQ ; y
235
    mov   ecx,FLDSIZE
236
  .lp1:
237
    push  ecx
238
    mov   ecx,FLDSIZE
239
    mov   ebx,8 shl 16+SQ  ; x
240
  .lp2:
241
    push  ecx
242
    mov   edx,0x8000
243
    cmp   byte[edi],0
244
    je    .zero
245
    cmp   byte[edi],2
246
    jne   .nored
247
    mov   edx,0xff0000
248
    cmp   [locked],0
249
    jz    .zero
250
    mov   edx,0xa00000
251
    jmp   .zero
252
  .nored:
253
    mov   edx,0
254
  .zero:
255
    mcall 13,,ebp
256
    inc   edi
257
    pop   ecx
258
    add   ebx,8 shl 16
259
    loop  .lp2
260
    pop   ecx
261
    add   ebp,8 shl 16
262
    loop  .lp1
263
 
264
    mov   eax,[dot]
265
    mov   cl,FLDSIZE
266
    div   cl
267
 
268
;    movzx ebx,ah
269
;    shl   ebx,19
270
;    add   ebx,8 shl 16+7
271
;    movzx ecx,al
272
;    shl   ecx,19
273
;    add   ecx,28 shl 16+7
274
;    push  eax
275
;    mcall 13,,,0xff
276
;    pop   eax
277
 
278
    movzx ebx,ah
279
    shl   ebx,19
280
    add   ebx,8 shl 16
281
    shld  edx,ebx,16
282
    add   dx,SQ-1
283
    mov   bx,dx
284
    movzx ecx,al
285
    shl   ecx,19
286
    add   ecx,28 shl 16
287
    shld  edx,ecx,16
288
    add   dx,SQ-1
289
    mov   cx,dx
290
    mcall 38,,,0xffffff
291
    ror   ecx,16
292
    mcall
293
    ret
294
 
295
nokey:
296
    xor  eax,eax
297
    mov  edi,buff
298
    mov  ecx,FLDSIZE*FLDSIZE+3
299
    push ecx edi
300
    rep  stosb
301
    pop  edi
302
    ;mov  esi,field
303
    mov  edi,field
304
    mov  edx,FLDSIZE
305
    pop  ecx
306
  .llp:
307
    mov  eax,2
308
    repne scasb
309
    jecxz .exx
310
    and  byte[edi-1],0
311
    push ecx
312
    lea  eax,[edi-field-1]
313
    div  dl
314
    call get_cell
315
    mov  byte[buff+ecx],2
316
    pop  ecx
317
    loop .llp
318
  .exx:
319
    mov  edi,field
320
    mov  esi,buff
321
    mov  ecx,FLDSIZE*FLDSIZE
322
  .lp4:
323
    lodsb
324
    cmp  al,2
325
    jne  .skip3
326
    mov  [edi],al
327
  .skip3:
328
    inc  edi
329
    loop  .lp4
330
 
331
    ret
332
 
333
get_cell:
334
; ax - source cell [x][y]
335
; ebx - direction
336
;     4 2 5
337
;     0 . 3
338
;     6 1 7
339
; out - ecx cell ptr
340
 
341
    push eax ebx
342
    add  ax,[ebx*2+dirs]
343
    mov  ebx,FLDSIZE
344
  .c0:
345
    cmp  al,bl
346
    jb   .c1
347
    sub  al,bl
348
    jmp  .c0
349
  .c1:
350
    cmp  ah,bl
351
    jb   .c2
352
    sub  ah,bl
353
    jmp  .c1
354
  .c2:
355
    movzx ecx,al
356
    imul cx,bx
357
    shr  ax,8
358
    add  cx,ax
359
    pop  ebx eax
360
    ret
361
 
362
evolution:
363
    xor   edi,edi
364
    and   [locked],edi
365
    mov   edx,FLDSIZE
366
    mov   ecx,FLDSIZE*FLDSIZE
367
  .l1:
368
    push  ecx
369
    mov   eax,edi
370
    div   dl
371
    mov   ecx,8
372
    xor   ebx,ebx
373
    mov   word[neib],bx ; neighbour count
374
  .l2:
375
    push  ecx
376
    call  get_cell
377
    movzx esi,byte[field+ecx]
378
    test   esi,esi ; 0?
379
    jz    .skip2
380
    inc   byte[neib-1+esi]
381
  .skip2:
382
    inc   ebx
383
    pop   ecx
384
    loop  .l2
385
    mov   cl,[neib]
386
    add   cl,[neib+1]
387
    cmp   cl,2
388
    jne   .no2
389
    mov   al,[field+edi]
390
    jmp   .writebuf
391
  .no2:
392
    xor   al,al
393
    cmp   cl,3
394
    jne   .writebuf
395
    inc   al
396
    mov   cl,[neib+1]
397
    cmp   cl,[neib]
398
    jb    .writebuf
399
    inc   al
400
  .writebuf:
401
    mov   [buff+edi],al
402
    pop   ecx
403
    inc   edi
404
    loop  .l1
405
 
406
    mov   esi,buff
407
    mov   edi,field
408
    mov   ecx,FLDSIZE*FLDSIZE
409
    rep   movsb
410
    call  square_check
411
    call  dot_move
412
    ret
413
 
414
square_check:
415
    mov   ecx,FLDSIZE*FLDSIZE+3
416
    mov   edx,FLDSIZE
417
    mov   edi,field
418
    xor   eax,eax
419
    pusha
420
    and   dword[cells],eax
421
    and   [locked],eax
422
  .nored:
423
    mov   al,[edi]
424
    inc   edi
425
    dec   ecx
426
    jecxz .ex1
427
    test   al,al
428
    jz    .nored
429
    inc   [cells-2+eax*2]
430
    jmp   .nored
431
  .ex1:
432
    mov   ax,[cells+2]
433
    cmp   ax,[cells]
434
    ja    dot_move.next_lev
435
    cmp   [cells+2],4
436
    je    .sq_check
437
    add   esp,32
438
    cmp   [cells+2],0
439
    jne   .loc
440
    mov   [mode],1
441
    mov   dword[esp+4],red
442
  .loc:
443
    inc   [locked]
444
    ret
445
  .sq_check:
446
    popa
447
  .nored2:
448
    mov   eax,2
449
    repne scasb
450
    jecxz .loc
451
    lea   eax,[edi-field-1]
452
    div   dl
453
    xor   dh,dh
454
    push  ecx
455
    mov   ebx,1
456
    call  get_cell
457
    add   dh,[field+ecx]
458
    mov   ebx,3
459
    call  get_cell
460
    add   dh,[field+ecx]
461
    mov   ebx,7
462
    call  get_cell
463
    add   dh,[field+ecx]
464
    pop   ecx
465
    cmp   dh,6
466
    jne   .nored2
467
    ret
468
 
469
random:
470
    mov  eax, [generator]
471
    sub  eax,0x43ab45b5    ; next random number
472
    ror  eax,1
473
    xor  eax,0x32c4324f
474
    ror  eax,1
475
    mov  [generator],eax
476
    ret
477
 
478
dot_move:
479
    call random
480
    mov  eax,[generator]
481
    xor  edx,edx
482
    mov  ebx,MOBILITY
483
    div  ebx
484
    cmp  edx,8
485
    jb   .nostay
486
    mov  edx,16
487
  .nostay:
488
    mov  ebx,edx
489
    shr  ebx,1
490
    mov  eax,[dot]
491
;    dpd  eax
492
    mov  cl,FLDSIZE
493
    div  cl
494
    call get_cell
495
    mov  [dot],ecx
496
    cmp  byte[field+ecx],2
497
    jne  .nocap
498
    inc  [captured]
499
    cmp  [captured],2
500
    jne  .ex
501
  .next_lev:
502
    mov  [mode],0
503
    pop  eax
504
    mov  eax,[levptr]
505
    cmp  word[eax],0
506
    jne  .nxt
507
    mov  [mode],2
508
  .nxt:
509
    mov  dword[esp],red
510
    ret
511
  .nocap:
512
    and  [captured],0
513
  .ex:
514
    ret
515
 
516
if LEVELCONV eq 1
517
fileinfo:
518
 
519
     dd   2
520
     dd   0x0
521
     dd   0x0
522
.fsize dd   10000
523
.ptr dd   0x20000
539 spraid 524
     db   '/sys/newlev.bin',0
484 diamond 525
 
526
macro flush
527
{
528
  mov  [edi],dh
529
  inc  edi
530
}
531
 
532
compress_levels:
533
    mov  esi,raw_level
534
    mov  edi,I_END
535
    mov  [fileinfo.ptr],edi
536
    mov  ecx,(raw_level_size-raw_level)/(FLDSIZE*FLDSIZE*4) ; 19
537
  .lp1:
538
    push ecx
539
    mov  ecx,FLDSIZE*FLDSIZE
540
    mov  ebx,edi
541
    mov  eax,[esi]
542
;    movzx eax,byte[esi]
543
    movzx edx,al
544
    dpd  edx
545
    shl  eax,15
546
    stosw
547
  .lp2:
548
    lodsd ;lodsb
549
    cmp  al,0xd
550
    jne  .nored
551
    flush
552
    xor  al,al
553
    stosb
554
    add  esi,4
555
    dec  ecx
556
    xor  dh,dh
557
    jmp  .eloop
558
  .nored:
559
    cmp  al,dl
560
    jne  .change
561
    inc  dh
562
    cmp  dh,0xff
563
    jb   .eloop
564
    flush
565
    xor  dh,dh
566
    jmp  .eloop
567
  .change:
568
    flush
569
    xor  dl,1
570
    mov  dh,1
571
  .eloop:
572
    loop .lp2
573
    flush
574
    mov  eax,edi
575
    sub  eax,ebx
576
    add  [ebx],ax
577
    pop  ecx
578
    loop .lp1
579
    xor  eax,eax
580
    stosw
581
    sub  edi,I_END
582
    mov  [fileinfo.fsize],edi
583
    mcall 70,fileinfo
584
    ret
585
 
586
raw_level:
587
    file 'noname1.dat'
588
raw_level_size:
589
 
590
end if
591
 
592
decompress_level:
593
; esi - level begin
594
    mov  edi,field
595
    movzx edx,word[esi]
596
    xor  ecx,ecx
597
    mov  eax,edx
598
    and  edx,0x7fff
599
    add  edx,esi ; limit
600
    mov  [levptr],edx
601
    add  esi,2
602
    shr  eax,15
603
  .next:
604
    cmp  esi,edx
605
    jae  .exloop
606
    movzx ebx,byte[esi]
607
    inc  esi
608
    test ebx,ebx
609
    jne  .nored
610
    rep  stosb
611
    xor  al,1
612
    mov  word[edi],0x0202
613
    add  edi,2
614
    jmp  .next
615
  .nored:
616
    add  ecx,ebx
617
    cmp  ebx,0xff
618
    je   .next
619
    rep  stosb
620
    xor  al,1
621
    jmp  .next
622
  .exloop:
623
    rep  stosb
624
    ret
625
 
626
; DATA AREA
627
 
628
FS1 equ (FLDSIZE-1)
629
dirs  dw  FS1 shl 8,\
630
          1,\
631
          FS1,\
632
          1 shl 8,\
633
          FS1 shl 8+FS1,\
634
          1 shl 8+FS1,\
635
          FS1 shl 8+1,\
636
          1 shl 8+1,\
637
 
638
level:
639
file 'rlevels.bin'
640
 
641
msg_ok db 'OK'
642
if lang eq ru
643
header db 'Red Square - Уровень       h - Помощь, p - Пауза'
644
   .size = $ - header
645
messages mstr 'Отлично! Вы переходите на следующий уровень.',\
646
              'Красные клетки уничтожены. Вы проиграли.',\
647
              'Вы прошли все уровни. Поздравляем!',\
648
              'Игра приостановлена...'
649
 
650
desc mstr   '   Вы играете красным квадратиком. Цель игры - выжить среди',\
651
            'развивающихся черных клеток. Если Вам удастся поймать белый',\
652
            'крестик, Вы пройдете уровень.',\
653
            '   Чтобы поймать белый крестик, он должен быть накрыт одной',\
654
            'из красных клеток на протяжении 2 поколений подряд.',\
655
            '   Если красный квадрат разрушен, это еще не проигрыш. Может',\
656
            'быть, Вам повезет и если в процессе эволюции количество Ваших',\
657
            'красных клеток станет больше, чем черных, уровень будет',\
658
            "пройден.",\
659
            '---',\
660
            'Джон Хортон Конуэй изобрел прекрасную игру. Почти каждый',\
661
            'программист начинает свою практику с нее. Однако реальные',\
662
            'возможности этой игры еще плохо изучены.',\
663
            '---',\
664
            'Оригинал игры под Win32 написал Владимир Привалов, 2002',\
665
            'Запрограммировал на ассемблере Willow, 2005'
666
else
667
header db 'Red Square - Level         h - Help,  p - Pause'
668
   .size = $ - header
669
messages mstr 'Well done! You are on the next level',\
670
              'Your red cells are vanished! Game over.',\
671
              "You've completed the game. Congratulations!",\
672
              'Game paused...'
673
 
674
desc mstr   '   The main goal of the game for your red square is to stay',\
675
            'alive going through the evolving black cells. If you manage to',\
676
            'catch the white cross, you will pass the level.',\
677
            '   To catch the white cross, it must be covered by red cell',\
678
            'during 2 generations.',\
679
            "   If your red square is corrupted, you haven't lost yet. You",\
680
            'may have a luck, and if your red cells is developing and the',\
681
            'quantity of black cells is bigger than that of black cells,',\
682
            "you'll pass the level.",\
683
            '---',\
684
            'John Horton Conway has created a great game. Almost every',\
685
            'programmer begins his professional work from it. But the real',\
686
            'possibilities of this game have not discovered yet.',\
687
            '---',\
688
            'Original game under Win32 by Vladimir Privalov, 2002',\
689
            'Programmed in assembly by Willow, 2005'
690
end if
691
I_END:
692
neib db ?,?
693
cells dw ?,?
694
mode db ?
695
levptr dd ?
696
levnum dd ?
697
reds dd ?
698
locked dd ?
699
generator dd ?
700
dot  dd ?
701
ticks dd ?
702
captured dd ?
703
if LEVELCONV eq 1
704
  os_work rb 4096
705
end if
706
field:
707
    rb FLDSIZE*FLDSIZE
708
rd 1
709
buff:
710
    rb FLDSIZE*FLDSIZE
711
stak: