SquirrelInBox

Untitled

May 30th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. locals
  2. .model tiny
  3. .code
  4. org 100h
  5. .386
  6.  
  7.  
  8. start:
  9. jmp main
  10.  
  11. delay       db  12h
  12. cur_teak    db  0h
  13.  
  14. sounds dw 3225, 3044, 2712, 2416, 3044, 3620, 2416, 2712, 3044, 3255, 3044, 2712, 2416, 3044, 3620, 3620, 40;, 2280, 2712, 1810, 2032, 2280, 2416, 2280, 2416, 2712, 3044, 3225, 3044, 2712, 2416, 3044, 3620, 3620
  15. sound_times db 9, 3, 6, 6, 6, 6, 6, 3, 3, 9, 3, 6, 6, 6, 3, 9, 3;, 9, 3, 6, 3, 3, 9, 3, 6, 3, 3, 9, 3, 6, 6, 6, 6, 12
  16. sound_size db 17
  17. cur_melody  db  ?
  18.  
  19. m08_handler proc
  20.     inc cur_teak   
  21.     iret
  22. m08_handler endp
  23.  
  24. move_snake proc
  25.     mov cl, head_x
  26.     mov dl, head_y
  27.     xor ch, ch
  28.     xor dh, dh
  29.     mov ah, 0ch
  30.     mov al, direct
  31.     mov bh, 01h
  32.     int 10h
  33.  
  34.     cmp direct, 4
  35.     jne @@is_up
  36.     sub head_x, 1
  37.     inc found_next_step
  38.     jmp @@end
  39. @@is_up:
  40.     cmp direct, 1
  41.     jne @@is_right
  42.     sub head_y, 1
  43.     inc found_next_step
  44.     jmp @@end
  45.  
  46. @@is_right:
  47.     cmp direct, 2
  48.     jne @@down
  49.     add head_x, 1
  50.     inc found_next_step
  51.     jmp @@end
  52. @@down:
  53.     add head_y, 1
  54.     inc found_next_step
  55. @@end:
  56.     mov cl, head_x
  57.     xor ch, ch
  58.     mov dl, head_y
  59.     xor dh, dh
  60.     mov ah, 0dh
  61.     int 10h
  62.    
  63.     ; бетонный пол?
  64.     cmp al, 08h
  65.     je @@set_end
  66.    
  67.     ; артефакт с увеличением длины?
  68.     cmp al, 09h
  69.     jne @@next
  70.     inc food_count
  71.     inc found_incr_food
  72.     inc snake_length
  73.     call draw_head
  74.     jmp @@end_t
  75. @@set_end:
  76.     mov ext_flag, 1
  77.     jmp @@end_t
  78. @@next:
  79.     ; артефакт, который убил?
  80.     cmp al, 0ch
  81.     jne @@not_kill
  82.     inc food_count
  83.     jmp @@set_end
  84.    
  85. @@not_kill:
  86.     ;нашли ли уменьшающий артефакт
  87.     cmp al, 0eh
  88.     jne @@non_art
  89.     inc food_count
  90.     inc found_dec_food
  91.     dec snake_length
  92.     cmp snake_length, 0
  93.     je @@set_end
  94.    
  95.     call draw_head
  96.     call draw_tail
  97.     call move_tail
  98.     jmp @@draw_tail
  99.  
  100. @@non_art:
  101.     ; эластичная стенка?
  102.     cmp al, 0bh
  103.     jne @@non_elastic
  104.     inc found_elastic_wall
  105.     call turn_snake
  106.     call draw_head
  107.     jmp @@end_t
  108.    
  109. @@non_elastic:
  110.     ; портал?
  111.     cmp al, 0dh
  112.     jne @@non_portal
  113.     inc found_portal
  114.     call handle_portal
  115.    
  116.  
  117. @@non_portal:
  118.     cmp al, 04h
  119.     je @@self_inter
  120.     cmp al, 01h
  121.     je @@self_inter
  122.     cmp al, 02h
  123.     je @@self_inter
  124.     cmp al, 03h
  125.     je @@self_inter
  126.     jmp @@next_step
  127.    
  128. @@self_inter:
  129.     cmp for_inter, 00h
  130.     je @@set_end
  131.     cmp for_inter, 01h
  132.     je @@cut_tail
  133.     jmp @@set_end
  134. @@cut_tail:
  135.     call cut_tail
  136.     jmp @@end_t
  137. @@next_step:
  138.     call draw_head
  139.  
  140. @@draw_tail:
  141.    
  142.     call draw_tail
  143.     call move_tail
  144.    
  145. @@end_t:
  146.    
  147.     ret
  148. move_snake endp
  149.  
  150. cut_tail proc
  151.     push ax
  152. @@loop:
  153.     dec snake_length
  154.    
  155.     mov al, tail_x
  156.     cmp al, head_x
  157.     jne @@next_step
  158.     mov al, tail_y
  159.     cmp al, head_y
  160.     je @@end
  161. @@next_step:   
  162.     call draw_tail
  163.     call move_tail
  164.     jmp @@loop
  165.  
  166. @@end: 
  167.     pop ax
  168.     call draw_tail
  169.     call move_tail
  170.     call draw_head
  171.     ret
  172. cut_tail endp
  173.  
  174. handle_inter proc
  175.    
  176.     ret
  177. handle_inter endp
  178.  
  179. handle_portal proc
  180.     cmp direct, 04h
  181.     je @@correct_direct
  182.     cmp direct, 02h
  183.     je @@correct_direct
  184.     jmp @@end
  185.    
  186. @@correct_direct:
  187.     mov al, tail_y
  188.     mov ah, tail_x
  189.     call del_old_snake
  190.     call move_map
  191.  
  192.     mov al, head_y
  193.     mov tail_y, al
  194.     cmp direct, 04h
  195.     jne @@non_left
  196.     mov tail_x, 62
  197.     mov bh, 62
  198.     sub bh, snake_length
  199.     mov head_x, bh
  200.     jmp @@end
  201. @@non_left:
  202.     mov tail_x, 15
  203.     mov bh, 15
  204.     add bh, snake_length
  205.     mov head_x, bh
  206.  
  207. @@end:
  208.     call draw_new_snake
  209.     ret
  210. handle_portal endp
  211.  
  212. draw_new_snake proc
  213.     mov al, head_y
  214.     mov ah, head_x
  215.     push ax
  216.  
  217.     mov bl, tail_x
  218.     mov head_x, bl
  219.     mov bl, tail_y
  220.     mov head_y, bl
  221. @@loop:
  222.     push ax
  223.     call draw_head
  224.     xor cx, cx
  225.     xor dx, dx
  226.     mov cl, head_x
  227.     mov dl, head_y
  228.     mov ah, 0dh
  229.     mov bh, 01h
  230.     int 10h
  231.    
  232.     cmp al, 04h
  233.     jne @@not_left
  234.     dec head_x
  235.     jmp @@end
  236. @@not_left:
  237.     cmp al, 02h
  238.     jne @@end
  239.     inc head_x
  240. @@end: 
  241.     pop ax
  242.     cmp ah, head_x
  243.     jne @@loop
  244.     cmp al, head_y
  245.     jne @@loop
  246.    
  247.     pop ax
  248.     mov head_y, al
  249.     mov head_x, ah
  250.     ret
  251. draw_new_snake endp
  252.  
  253. del_old_snake proc
  254. @@loop:
  255.     mov al, tail_x
  256.     cmp al, head_x
  257.     jne @@next_step
  258.     mov al, tail_y
  259.     cmp al, head_y
  260.     je @@end
  261. @@next_step:
  262.     call draw_tail
  263.     call move_tail
  264.    
  265.     jmp @@loop
  266. @@end:
  267.     ret
  268. del_old_snake endp
  269.  
  270. move_map proc
  271.     mov dl, head_y
  272.     cmp direct, 04h
  273.     je @@left
  274.     cmp direct, 02h
  275.     je @@right
  276.    
  277. @@left:
  278.     mov dh, 62
  279.     jmp @@next
  280.    
  281. @@right:
  282.     mov dh, 15
  283.     jmp @@next
  284. @@next:
  285.     mov cl, snake_length
  286.     xor ch, ch
  287. @@loop:
  288.     push cx dx
  289.     mov cl, dh
  290.     xor dh, dh
  291.     mov al, direct
  292.     mov bh, 01h
  293.     mov ah, 0ch
  294.     int 10h
  295.     pop dx cx
  296.     cmp direct, 04h
  297.     je @@add_left
  298.     inc dh
  299.     jmp @@t_end
  300. @@add_left:
  301.     dec dh
  302. @@t_end:
  303.     loop @@loop
  304.     ret
  305. move_map endp
  306.  
  307. turn_snake proc
  308.     pusha
  309.     mov cl, tail_x
  310.     xor ch, ch
  311.     mov dl, tail_y
  312.     xor dh, dh
  313.     mov ah, 0dh
  314.     mov bh, 01h
  315.    
  316.     int 10h
  317.    
  318.     mov direct, al
  319.     cmp direct, 04h
  320.     jne @@not_left
  321.     mov direct, 02h
  322. ;   inc tail_x
  323.     jmp @@next
  324.    
  325. @@not_left:
  326.     cmp direct, 01h
  327.     jne @@not_up
  328.     mov direct, 03h
  329. ;   inc tail_y
  330.     jmp @@next
  331.    
  332. @@not_up:
  333.     cmp direct, 02h
  334.     jne @@not_right
  335.     mov direct, 04h
  336. ;   dec tail_x
  337.     jmp @@next
  338.    
  339. @@not_right:
  340.     cmp direct, 03h
  341.     jne @@next
  342.     mov direct, 01h
  343. ;   dec tail_y
  344.  
  345.     mov ah, 0ch
  346.     mov al, direct_tail
  347.     mov cl, tail_x
  348.     xor ch, ch
  349.     mov dl, tail_y
  350.     xor dh, dh
  351.     mov bl, 01h
  352.     int 10h
  353.  
  354.  
  355. @@next:
  356.     inc head_x
  357.     mov al, head_x
  358.     mov ah, tail_x
  359.     mov head_x, ah
  360.     mov tail_x, al
  361.  
  362.     mov al, head_y
  363.     mov ah, tail_y
  364.     mov head_y, ah
  365.     mov tail_y, al
  366.    
  367.     call redraw_path   
  368.    
  369.     popa
  370.     ret
  371. turn_snake endp
  372.  
  373.  
  374. redraw_path proc
  375.     ; изменить направление в стартовой клетке, начальное направление запомнить
  376.    
  377.     mov cl, head_x
  378.     xor ch, ch
  379.     mov dl, head_y
  380.     xor dh, dh
  381.     call get_dir
  382.     mov bh, head_x
  383.     mov bl, head_y
  384.     call turn_dir1
  385.    
  386. @@loop:
  387.     xor cx,cx
  388.     xor dx,dx
  389.     mov cl, bh
  390.     mov dl, bl
  391.     call get_next_coords
  392.    
  393.     push ax ; предыдущее направление
  394.    
  395.     xor cx,cx
  396.     xor dx,dx
  397.     mov cl, bh
  398.     mov dl, bl
  399.     call get_dir
  400.    
  401.     mov bh, cl
  402.     mov bl, dl
  403.    
  404.     xchg dx, ax ; временно сохранили текущее направление
  405.     pop ax ; предыдущее направление
  406.    
  407.     call turn_dir1
  408.    
  409.     mov ax, dx ; вернули текущее направление
  410.    
  411.     cmp bh, tail_x
  412.     jne @@loop
  413.     cmp bl, tail_y
  414.     jne @@loop
  415.    
  416.     ret
  417. redraw_path endp
  418.  
  419. get_dir proc
  420.     ; (cx, dx) - текущие координаты
  421.     mov ah, 0dh
  422.     mov bh, 01h
  423.     int 10h
  424.     ret
  425. get_dir endp
  426.  
  427. get_next_coords proc
  428.     ; al - текущее направление, (cx, dx) - текущие координаты
  429.     ; (bh, bl) - новые координаты
  430.     mov bh, cl
  431.     mov bl, dl
  432.    
  433.     cmp al, 04h
  434.     jne @@not_left
  435. @@left:
  436.     dec bh
  437.     jmp @@end
  438. @@not_left:
  439.     cmp al, 01h
  440.     jne @@not_up
  441. @@up:
  442.     dec bl
  443.     jmp @@end
  444. @@not_up:
  445.     cmp al, 02h
  446.     jne @@not_right
  447.     inc bh
  448.     jmp @@end
  449. @@not_right:
  450.     cmp al, 03h
  451.     jne @@end
  452.     inc bl
  453.     jmp @@end
  454.    
  455. @@end: 
  456.     ret
  457. get_next_coords endp
  458.  
  459. turn_dir1 proc
  460.     ; al - предыдущее направление, (bh, bl) - текущие координаты
  461.     pusha
  462.    
  463.     cmp al, 04h
  464.     jne @@not_left
  465. @@left:
  466.     mov al, 02h
  467.     jmp @@change
  468. @@not_left:
  469.     cmp al, 01h
  470.     jne @@not_up
  471. @@up:
  472.     mov al, 03h
  473.     jmp @@change
  474. @@not_up:
  475.     cmp al, 02h
  476.     jne @@not_right
  477.     mov al, 04h
  478.     jmp @@change
  479. @@not_right:
  480.     cmp al, 03h
  481.     jne @@end
  482.     mov al, 01h
  483.     jmp @@change
  484.    
  485. @@change:
  486.     xor cx, cx
  487.     xor dx, dx
  488.     mov cl, bh
  489.     mov dl, bl
  490.    
  491.     mov ah, 0ch
  492.     mov bh, 01h
  493.    
  494.     int 10h
  495. @@end:
  496.     popa
  497.     ret
  498. turn_dir1 endp
  499.  
  500.  
  501. move_tail proc
  502.     mov cl, tail_x
  503.     mov dl, tail_y
  504.     xor ch, ch
  505.     xor dh, dh
  506.     mov ah, 0dh
  507.     mov bh, 01h
  508.    
  509.     push bx cx dx
  510.    
  511.     int 10h
  512.    
  513.     pop dx cx bx
  514.    
  515.     mov direct_tail, al
  516.    
  517.     mov cl, tail_x
  518.     mov dl, tail_y
  519.     xor ch, ch
  520.     xor dh, dh
  521.     mov ah, 0ch
  522.     mov al, 05h
  523.     mov bh, 01h
  524.     int 10h
  525.    
  526.     cmp direct_tail, 4
  527.     jne @@is_up_t
  528.     sub tail_x, 1
  529.     jmp @@end_t
  530. @@is_up_t:
  531.     cmp direct_tail, 1
  532.     jne @@is_right_t
  533.     sub tail_y, 1
  534.     jmp @@end_t
  535.  
  536. @@is_right_t:
  537.     cmp direct_tail, 2
  538.     jne @@down_t
  539.     add tail_x, 1
  540.     jmp @@end_t
  541. @@down_t:
  542.     add tail_y, 1
  543. @@end_t:
  544.     ret
  545. move_tail endp
  546.  
  547. draw_head proc
  548.     mov al, head_x
  549.     mov bl, 05h
  550.     mul bl
  551.     mov start_x, ax
  552.     mov al, head_y
  553.     mul bl
  554.     mov start_y, ax
  555.    
  556.     mov ax, snake_point_size
  557.     mov cur_point_size, ax
  558.    
  559.     mov al, snake_color
  560.     mov cur_color, al
  561.  
  562.     call draw_point
  563.     ret
  564. draw_head endp
  565.  
  566. draw_tail proc
  567.     mov al, tail_x
  568.     mov bl, 05h
  569.     mul bl
  570.     mov start_x, ax
  571.     mov al, tail_y
  572.     mul bl
  573.     mov start_y, ax
  574.    
  575.     mov ax, snake_point_size
  576.     mov cur_point_size, ax
  577.    
  578.     mov al, 00h
  579.     mov cur_color, al
  580.  
  581.     call draw_point
  582.     ret
  583. draw_tail endp
  584.  
  585.  
  586. m09_handler proc
  587.     pusha
  588.     in al, 60h
  589.    
  590.     cmp al, 1
  591.     jnz non_esc
  592.     mov ext_flag, 1
  593.     jmp interrupt_end
  594.    
  595. non_esc:
  596.     cmp al, 4bh
  597.     je @@left
  598.    
  599.     cmp al, 48h
  600.     je @@up
  601.    
  602.     cmp al, 4dh
  603.     je @@right
  604.    
  605.     cmp al, 50h
  606.     je @@down
  607.    
  608.     cmp al, 0ch
  609.     je @@reduse_speed
  610.    
  611.     cmp al, 0dh
  612.     je @@increase_speed
  613.    
  614.     jmp interrupt_end
  615.    
  616. @@reduse_speed:
  617.     mov al, delay
  618.     cmp al, 12h
  619.     jge @@reduse_end
  620.     shl al, 1
  621.     mov delay, al
  622. @@reduse_end:
  623.     jmp @@end
  624.  
  625. @@increase_speed:
  626.     mov al, delay
  627.     cmp al, 01h
  628.     jle @@reduse_end
  629.     shr al, 1
  630.     mov delay, al
  631. @@temp_end:
  632.     jmp @@end
  633.    
  634. @@left:
  635.     cmp direct, 02h
  636.     je interrupt_end
  637.     mov direct, 4
  638.     jmp @@end
  639. @@up:
  640.     cmp direct, 03h
  641.     je interrupt_end
  642.     mov direct, 1
  643.     jmp @@end
  644. @@right:
  645.     cmp direct, 04h
  646.     je interrupt_end
  647.     mov direct, 2
  648.     jmp @@end
  649. @@down:
  650.     cmp direct, 01h
  651.     je interrupt_end
  652.     mov direct, 3
  653.     jmp @@end
  654. @@end:
  655.  
  656. interrupt_end:
  657.     in al, 61h
  658.     mov ah, al
  659.     or al, 80h
  660.     out 61H, al
  661.     xchg ah, al
  662.     out 61H, al
  663.  
  664.     mov al, 20h
  665.     out 20h, al
  666.     popa
  667.     iret
  668. m09_handler endp
  669.  
  670. get_old_mode proc
  671.     push    ax
  672.     push    bx
  673.     push    ds
  674.    
  675.     xor     ax,ax
  676.     mov     ds, ax
  677.     mov     bh, ds:449h
  678.    
  679.     pop     ds
  680.     mov     [old_vm], bh
  681.     pop     bx 
  682.     pop     ax
  683.     ret
  684. get_old_mode endp
  685.  
  686.  
  687. get_old_page proc
  688.     push    ax
  689.     push    bx
  690.     push    ds
  691.    
  692.     xor     ax,ax
  693.     mov     ds, ax
  694.     mov     bh, ds:462h
  695.    
  696.     pop     ds
  697.     mov     [old_page], bh
  698.     pop     bx
  699.     pop     ax 
  700.     ret
  701. get_old_page endp
  702.  
  703. change_vm proc
  704.     push bp
  705.     mov bp, sp
  706.     push ax
  707.    
  708.     ; в стеке vm -> ap -> bp -> ax
  709.     mov ah, 00h
  710.     mov al, [bp+6] ;vm
  711.     int 10h
  712.    
  713.     mov ah, 05h
  714.     mov al, [bp+4] ;ap
  715.     int 10h
  716.    
  717.     pop ax
  718.     pop bp
  719.     ret 4
  720. change_vm endp
  721.  
  722. change_mode proc
  723.     call get_old_mode
  724.     call get_old_page
  725.  
  726.     xor ax, ax
  727.     mov al, our_vm
  728.     push ax
  729.     mov al, our_page
  730.     push ax
  731.    
  732.     call change_vm
  733.    
  734.     ret
  735. change_mode endp
  736.  
  737. init_snake proc
  738.    
  739.     mov head_x, 30
  740.     mov head_y, 20
  741.     mov al, head_x
  742.     add al, snake_length
  743.     dec al
  744.     mov tail_x, al
  745.     mov tail_y, 20
  746.    
  747.    
  748.     mov al, head_x
  749.     mov bl, 05h
  750.     mul bl
  751.     mov start_x, ax
  752.     mov al, head_y
  753.     mul bl
  754.     mov start_y, ax
  755.    
  756.     mov ax, snake_point_size
  757.     mov cur_point_size, ax
  758.    
  759.     mov al, snake_color
  760.     mov cur_color, al
  761.     call draw_snake
  762.     ret
  763. init_snake endp
  764.  
  765. draw_snake proc
  766.     mov cl, snake_length
  767.     xor ch, ch
  768. @@loop:
  769.     call draw_point
  770.     cmp is_vertical_snake, 00h
  771.     pusha
  772.     mov ax, start_x
  773.     mov bl, 5
  774.     div bl
  775.     xor ah, ah
  776.     mov cx, ax
  777.    
  778.     mov ax, start_y
  779.     mov bl, 5
  780.     div bl
  781.     xor ah, ah
  782.     mov dx, ax
  783.     mov al, 04h
  784.     call draw_snake_part_map
  785.     popa
  786.     jne @@vert_snake
  787.     add start_x, 5
  788.     jmp @@next
  789. @@vert_snake:
  790.     add start_y, 5
  791.  
  792. @@next:
  793.    
  794.     mov ax, 4
  795.     mov cur_point_size, ax
  796.     dec cx
  797.     test cx, cx
  798.     jne @@loop
  799.     ret
  800. draw_snake endp
  801.  
  802. draw_snake_part_map proc
  803.     ; al - цвет, (cx, dx) - координаты
  804.     mov ah, 0ch
  805.     mov bh, 01h
  806.     int 10h
  807.     ret
  808. draw_snake_part_map endp
  809.  
  810. draw_point proc
  811.     ;   устанавливаем размер клетки в cur_point_size,
  812.     ;   цвет клетки в cur_color,
  813.     ;   начальную позицию в start_y, start_x
  814.     pusha; ax sp bp si di
  815.     mov ax, cur_point_size
  816.     mov temp_size_x, ax
  817.     mov temp_size_y, ax
  818.    
  819.     mov ax, start_y
  820.     mov temp_line, ax
  821.     mov ax, start_x
  822.     mov temp_column, ax
  823.    
  824.     mov ah, 0ch
  825.     mov al, cur_color
  826.     mov bh, our_page
  827.    
  828. @@loop:
  829.     mov cx, temp_column
  830.     mov dx, temp_line
  831.     int 10h
  832.    
  833.     dec temp_size_x
  834.     inc temp_column
  835.     cmp temp_size_x, 0
  836.     jnz @@loop
  837.    
  838.     dec temp_size_y
  839.     inc temp_line
  840.     mov cx, cur_point_size
  841.     mov temp_size_x, cx
  842.    
  843.     mov cx, start_x
  844.     mov temp_column, cx
  845.     mov temp_column, cx
  846.    
  847.     cmp temp_size_y, 0
  848.     jnz @@loop
  849.    
  850.     popa; di si bp sp ax
  851.    
  852.     ret
  853. draw_point endp
  854.  
  855. draw_bottom proc
  856.    
  857.     mov cur_color, 08h
  858.     mov start_x, 72
  859.     mov start_y, 190
  860.    
  861. @@loop:
  862.     call draw_point
  863.     pusha
  864.     mov al, 08h
  865.     call draw_art_in_map
  866.     popa
  867.     add start_x, 1
  868.     cmp start_x, 316
  869.     jl @@loop
  870.    
  871.     ret
  872. draw_bottom endp
  873.  
  874. draw_concrete_wall proc
  875.     mov cur_color, 08h
  876.     mov start_x, 315
  877.     mov start_y, 3
  878.    
  879. @@loop:
  880.     call draw_point
  881.     pusha
  882.     mov al, 08h
  883.     call draw_art_in_map
  884.     popa
  885.     add start_y, 2
  886.     cmp start_y, 190
  887.     jl @@loop
  888.    
  889.     ret
  890. draw_concrete_wall endp
  891.  
  892. draw_concrete_ceiling proc
  893.     mov cur_color, 08h
  894.     mov start_x, 72
  895.     mov start_y, 3
  896.    
  897. @@loop:
  898.     call draw_point
  899.     pusha
  900.     mov al, 08h
  901.     call draw_art_in_map
  902.     popa
  903.     add start_x, 2
  904.     cmp start_x, 316
  905.     jl @@loop
  906.     ret
  907. draw_concrete_ceiling endp
  908.  
  909. draw_elastic_wall proc
  910.     mov cur_color, 0bh
  911.     mov start_x, 72
  912.     mov start_y, 3
  913. @@loop:
  914.     call draw_point
  915.     pusha
  916.     mov al, 0bh
  917.     call draw_art_in_map
  918.     popa
  919.     add start_y, 1
  920.     cmp start_y, 191
  921.     jl @@loop
  922.     ret
  923. draw_elastic_wall endp
  924.  
  925.  
  926.  
  927. draw_portal proc
  928.     mov cur_color, 0dh
  929.     mov start_x, 72
  930.     mov start_y, 90
  931. @@loop:
  932.     call draw_point
  933.     pusha
  934.     mov al, 0dh
  935.     call draw_art_in_map
  936.     popa
  937.     add start_y, 2
  938.     cmp start_y, 110
  939.     jl @@loop
  940.    
  941.     mov start_x, 315
  942.     mov start_y, 90
  943. @@loop1:
  944.     call draw_point
  945.     pusha
  946.     mov al, 0dh
  947.     call draw_art_in_map
  948.     popa
  949.     add start_y, 2
  950.     cmp start_y, 110
  951.     jl @@loop1
  952.    
  953.     ret
  954. draw_portal endp
  955.  
  956. draw_walls proc
  957.     pusha
  958.     mov cur_point_size, 02h
  959.    
  960.     call draw_bottom
  961.     call draw_concrete_wall
  962.     call draw_concrete_ceiling
  963.     ;mov cur_point_size, 02h
  964.     call draw_elastic_wall
  965.    
  966.     call draw_portal
  967.     popa
  968.     ret
  969. draw_walls endp
  970.  
  971. draw_good_art proc
  972.     mov cur_color, 09h
  973.     pusha
  974.     mov al, 09h
  975.     call draw_art_in_map
  976.     popa
  977.     call draw_point
  978.     ret
  979. draw_good_art endp
  980.  
  981. draw_dead_art proc
  982.     mov cur_color, 0ch
  983.     pusha
  984.     mov al, 0ch
  985.     call draw_art_in_map
  986.     popa
  987.     call draw_point
  988.     ret
  989. draw_dead_art endp
  990.  
  991. draw_hurt_art proc
  992.     mov cur_color, 0eh
  993.     pusha
  994.     mov al, 0eh
  995.     call draw_art_in_map
  996.     popa
  997.     call draw_point
  998.     ret
  999. draw_hurt_art endp
  1000.  
  1001. draw_art_in_map proc
  1002.     ; al - цвет
  1003.     push ax
  1004.     mov ax, start_x
  1005.     mov bl, 5
  1006.     div bl
  1007.     xor ch, ch
  1008.     mov cl, al
  1009.    
  1010.     mov ax, start_y
  1011.     mov bl, 5
  1012.     div bl
  1013.     xor dh, dh
  1014.     mov dl, al
  1015.     pop ax
  1016.     call draw_in_map
  1017.     ret
  1018. draw_art_in_map endp
  1019.  
  1020. draw_in_map proc
  1021.     ; al - цвет, cx - горизонт координата, dx - вертикальная координата
  1022.     mov ah, 0ch
  1023.     mov bh, 01h
  1024.     int 10h
  1025.    
  1026.     ret
  1027. draw_in_map endp
  1028.  
  1029. draw_art proc
  1030.     mov cl, art_count
  1031.     mov start_x, 90
  1032.     mov start_y, 45
  1033.     mov cur_point_size, 4
  1034. @@loop:
  1035.     call draw_good_art
  1036.     call inc_start_positions
  1037.     dec cl
  1038.     cmp cl, 00h
  1039.     je @@end
  1040.    
  1041.     call draw_dead_art
  1042.     call inc_start_positions
  1043.     dec cl
  1044.     cmp cl, 00h
  1045.     je @@end
  1046.    
  1047.     call draw_hurt_art
  1048.     call inc_start_positions
  1049.     dec cl
  1050.     cmp cl, 00h
  1051.     jne @@loop
  1052. @@end:
  1053.     ret
  1054. draw_art endp
  1055.  
  1056. inc_start_positions proc
  1057.     add start_x, 30
  1058.     cmp start_x, 316
  1059.     jl @@next
  1060.     sub start_x, 220
  1061.     add start_y, 15
  1062.  
  1063. @@next:
  1064.     ret
  1065. inc_start_positions endp
  1066.  
  1067. play_sounds proc
  1068.     push ax
  1069.    
  1070.     cmp found_elastic_wall, 1
  1071.     jne @@2
  1072.     dec found_elastic_wall
  1073.     mov ax, elastic_wall_sound
  1074.     jmp @@end
  1075.    
  1076. @@2:
  1077.     cmp found_incr_food, 1
  1078.     jne @@3
  1079.     dec found_incr_food
  1080.     mov ax, inc_food_sound
  1081.     jmp @@end
  1082.    
  1083. @@3:
  1084.     cmp found_dec_food, 1
  1085.     jne @@4
  1086.     dec found_dec_food
  1087.     mov ax, dec_food_sound
  1088.     jmp @@end
  1089.    
  1090. @@4:
  1091.     cmp found_portal, 1
  1092.     jne @@5
  1093.     dec found_portal
  1094.     mov ax, portal_sound
  1095.     jmp @@end
  1096.    
  1097. @@5:
  1098.     jmp @@exit
  1099.    
  1100. @@end:
  1101.     in al, 61h                   ;получаем текущий статус
  1102.     or al,00000011b              ;разрешаем динамик и таймер
  1103.     out 61h, al                 ;заменяем байт  
  1104.     mov al, 10110110b              ;установка для канала 2
  1105.     out 43h, al
  1106.     mov cx, 5000
  1107. @@loop:
  1108.     out 42h, al
  1109.     mov al, ah
  1110.     out 42h, al
  1111.  
  1112.     in al,61h
  1113.     or al,0
  1114.     out 61h,al
  1115.     loop @@loop
  1116.    
  1117.     in al, 61h   ;полчаем статус порта В
  1118.     and al, 11111100b    ;выключаем динамик
  1119.     out 61H,al
  1120. @@exit:
  1121.     pop ax
  1122.     ret
  1123. play_sounds endp
  1124.  
  1125. final_music proc
  1126.    
  1127.     in al, 61h                   ;получаем текущий статус
  1128.     or al,00000011b              ;разрешаем динамик и таймер
  1129.     out 61h, al               ;заменяем байт  
  1130.     mov si, 0                          ;инициализируем указатель
  1131.     mov al, 10110110b              ;установка для канала 2
  1132.     out 43h, al ;посылаем в командный регистр
  1133.     mov cur_melody, 1
  1134.    
  1135.    
  1136. ;---смотрим ноту, получаем ее частоту и помещаем в канал 2
  1137. next_note:
  1138.     mov al, cur_melody
  1139.     inc cur_melody
  1140.     cmp al, sound_size
  1141.     je no_more
  1142.     cbw                    ;переводим в слово
  1143. ;---получение частоты
  1144.     mov bx,offset sounds;смещение таблицы частот
  1145.     dec ax                                      ;начинаем отсчет с 0
  1146.     shl ax,1                                   ;умножаем на 2, т.к. слова
  1147.     mov di,ax                                ;адресуем через DI
  1148.     mov dx,[bx][di]                       ;получаем частоту из таблицы
  1149. ;начинаем исполнение ноты    
  1150.     mov al, dl                 ;готовим младший байт частоты
  1151.     out 42h, al          ;посылаем его
  1152.     mov al,dh                 ;готовим старший байт частоты
  1153.     out 42h, al          ;посылаем его  
  1154. ;---создание цикла задержки  
  1155.     mov ah,0                          ;номер функции чтения счетчика
  1156.     int 1ah                             ;получаем значение счетчика
  1157.     mov bx,offset sound_times     ;смещение таблицы длин
  1158.     mov cl,[bx][si]               ;берем длину очередной ноты
  1159.     mov ch,0
  1160.     mov bx,dx                      ;берем младшее слово счетчика
  1161.     add bx,cx                       ;определяем момент окончания
  1162. still_sound:
  1163.     int 1ah                          ;берем значение счетчика
  1164.     cmp dx,bx                    ;сравниваем с окончанием
  1165.     jne still_sound          ;неравны-продолжаем звук
  1166.     inc si                             ;переходим к следующей ноте
  1167.     jmp next_note            ;
  1168. ;---завершение
  1169. no_more:
  1170.     in al, 61h   ;полчаем статус порта В
  1171.     and al, 11111100b    ;выключаем динамик
  1172.     out 61H,al
  1173.    
  1174.     ret
  1175. final_music endp
  1176.  
  1177. parse_args proc
  1178.    
  1179.     mov cx, cs:[80h]
  1180.     xor ch, ch
  1181.    
  1182.     cmp cx, 0
  1183.     je @@end
  1184.    
  1185.     dec cx
  1186.    
  1187.     mov si, 82h
  1188.    
  1189. @@start:
  1190.     xor al, al
  1191.     call pass_spaces
  1192.    
  1193.    
  1194. @@next_step:
  1195.     mov bl, 2fh  ; код /
  1196.     cmp al, bl
  1197.     jne @@temp_error
  1198.  
  1199.     inc si
  1200.     dec cx
  1201.    
  1202.     cmp cx, 0
  1203.     je @@temp_error; прочитали только /, а строка закончилась
  1204.    
  1205.    
  1206. @@is_help:
  1207.     mov al, cs:[si]    
  1208.     mov bl, 68h  ; код h
  1209.     cmp al, bl
  1210.     jne @@is_length
  1211.     inc si
  1212.     dec cx
  1213.    
  1214.     mov ax, [setting_flags]
  1215.     cmp ax, 0000h
  1216.     jne @@error
  1217.     or ax, 1000h
  1218.     mov [setting_flags], ax
  1219.    
  1220.     cmp cx, 0000h
  1221.     je @@temp_end
  1222.  
  1223.     jmp @@temp_error  ; если ключ /h, то больше ничего не может там быть
  1224.    
  1225.  
  1226. @@is_length:
  1227.     mov bl, 6ch  ; код l
  1228.     cmp al, bl
  1229.     jne @@is_artef
  1230.     inc si
  1231.     dec cx
  1232.     cmp cx, 0000h
  1233.     je @@temp_error ; прочитали только /l, длины нет
  1234.    
  1235.     mov al, cs:[si]
  1236.     mov bl, 20h
  1237.     cmp al, bl
  1238.     jne @@temp_error  ; не разделены пробелом /l и длина
  1239.     call pass_spaces
  1240.    
  1241.     call get_num
  1242.     cmp bx, 0000h  ; возникла ошибка в получении числа(0000h - все хорошо, 0100h - некорректное число)
  1243.     jne @@temp_error
  1244.    
  1245.     cmp dx, 0001h ; длина змейки не меньше 2
  1246.     je @@temp_error
  1247.    
  1248.     cmp dx, 15h     ; длина змейки не больше 16
  1249.     jg @@temp_error
  1250.    
  1251.     mov di, offset snake_length
  1252.     mov ax, dx
  1253.     stosb  ; записали начальную длину змейки
  1254.    
  1255.     mov bx, [setting_flags]
  1256.     or bx, 0100h
  1257.     mov [setting_flags], bx
  1258.    
  1259.     cmp cx, 0000h
  1260.     je @@temp_end
  1261.    
  1262.     jmp @@start
  1263.    
  1264. @@temp_error:
  1265.     jmp @@error
  1266. @@temp_end:
  1267.     jmp @@end
  1268.    
  1269. @@is_artef:
  1270.     call pass_spaces
  1271.     mov bl, 61h
  1272.     cmp al, bl
  1273.     jne @@is_intersect
  1274.    
  1275.     inc si
  1276.     dec cx
  1277.     cmp cx, 0000h
  1278.     je @@error ; прочитали только /a, количества нет
  1279.    
  1280.     mov al, cs:[si]
  1281.     mov bl, 20h
  1282.     cmp al, bl
  1283.     jne @@error  ; не разделены пробелом /a и количество
  1284.     call pass_spaces
  1285.        
  1286.     call get_num
  1287.     cmp bx, 0000h
  1288.     jne @@error
  1289.    
  1290.     mov di, offset art_count
  1291.     mov ax, dx
  1292.     stosb
  1293.    
  1294.     mov bx, [setting_flags]
  1295.     or bx, 0010h
  1296.     mov [setting_flags], bx
  1297.    
  1298.     cmp cx, 0000h
  1299.     je @@temp_end
  1300.    
  1301.     jmp @@start
  1302.  
  1303. @@is_intersect:
  1304.     call pass_spaces
  1305.     mov bl, 69h
  1306.     cmp al, bl
  1307.     jne @@error
  1308.    
  1309.     inc si
  1310.     dec cx
  1311.     cmp cx, 0000h
  1312.     je @@error
  1313.    
  1314.     mov al, cs:[si]
  1315.     mov bl, 20h
  1316.     cmp al, bl
  1317.     jne @@error
  1318.     call pass_spaces
  1319.    
  1320.     cmp al, 30h
  1321.     jl @@error
  1322.     cmp al, 32h
  1323.     jg @@error
  1324.    
  1325.     sub ax, 30h
  1326.     mov di, offset for_inter
  1327.     stosb
  1328.    
  1329.     mov bx, [setting_flags]
  1330.     or bx, 0001h
  1331.     mov [setting_flags], bx
  1332.    
  1333.     inc si
  1334.     dec cx
  1335.     cmp cx, 0000h
  1336.     je @@temp_end
  1337.    
  1338.     jmp @@start
  1339.    
  1340. @@error:
  1341.     mov ax, 0100h
  1342.     jmp @@all_end  
  1343. @@end:
  1344.     mov ax, 0000h
  1345.     jmp @@all_end
  1346.    
  1347. @@all_end:
  1348.  
  1349. ret
  1350. parse_args endp
  1351.  
  1352. pass_spaces proc
  1353. @@start:
  1354.     xor al, al
  1355.     mov al, cs:[si]
  1356.    
  1357.     mov bl, 20h ; пропускаем все пробелы
  1358.     cmp al, bl  ;
  1359.     jne @@end  ;
  1360.     inc si
  1361.     dec cx
  1362.     jmp @@start
  1363.  
  1364. @@end:
  1365.  
  1366.     ret
  1367. pass_spaces endp
  1368.  
  1369.  
  1370. get_num proc
  1371.     ; возвращает в bx код ошибки(0100h - некорректное число), в dx - само число
  1372.     xor dx, dx
  1373. @@read_dec:
  1374.     mov al, cs:[si]
  1375.     mov bl, 39h
  1376.     cmp al, bl
  1377.     jg @@t_error
  1378.     mov bl, 30h
  1379.     cmp al, bl
  1380.     jl @@t_error
  1381.     jmp @@next_step
  1382. @@t_error:
  1383.     cmp al, 20h
  1384.     jne @@error
  1385.     jmp @@next_dec
  1386.    
  1387. @@next_step:   
  1388.     sub al, 30h
  1389.     push ax bx
  1390.     mov ax, dx
  1391.     mov bl, 0ah
  1392.     mul bl
  1393.     mov dx, ax
  1394.     pop bx ax
  1395.    
  1396.     add dx, ax
  1397.     inc si
  1398.     dec cx
  1399.     cmp cx, 0000h
  1400.     je @@next_dec
  1401.     jmp @@read_dec
  1402.    
  1403. @@next_dec:
  1404.     cmp dx, 30
  1405.     jg @@error  ; длина змейки не больше 30
  1406.    
  1407.     cmp dx, 0
  1408.     jle @@error
  1409.     jmp @@correct_num
  1410.  
  1411. @@error:
  1412.     mov bx, 0100h
  1413.     jmp @@end
  1414.    
  1415. @@correct_num:
  1416.     mov bx, 0000h
  1417.     jmp @@end
  1418.    
  1419. @@end:
  1420.  
  1421.     ret
  1422. get_num endp
  1423.  
  1424. cls proc
  1425.     push ax cx di es
  1426.     mov ax,0A000h
  1427.     mov es,ax
  1428.  
  1429.     xor ax, ax
  1430.     mov cx, 64000   ; 64000 = 320*200
  1431.     mov di, ax
  1432.  
  1433.     rep stosb
  1434.     pop es di cx ax
  1435.     ret
  1436. cls endp
  1437.  
  1438. write_number proc
  1439.  
  1440.     push ax bx dx si di
  1441.         xor ax, ax
  1442.     lodsb
  1443.  
  1444.     push -1
  1445.         mov bx, 10
  1446.         @@stacknumber:
  1447.         xor dx,dx
  1448.         div bx
  1449.         push dx
  1450.         cmp ax,0
  1451.         jne @@stacknumber
  1452.  
  1453.         pop dx
  1454.         @@addnumber:
  1455.         add dx,'0'
  1456.         mov ah, 02h
  1457.         int 21h
  1458.         pop dx
  1459.         cmp dx,-1
  1460.         jne @@addnumber
  1461.  
  1462.     pop di si dx bx ax
  1463.  
  1464.     ret
  1465. write_number endp
  1466.  
  1467. draw_enter proc
  1468.     push ax dx
  1469.     mov dl, 0dh
  1470.     mov ah, 02h
  1471.     int 21h
  1472.    
  1473.     mov dl, 0ah
  1474.     mov ah, 02h
  1475.     int 21h
  1476.     pop dx ax
  1477.     ret
  1478. draw_enter endp
  1479.  
  1480. draw_tab proc
  1481.     push ax dx
  1482.     mov dl, 09h
  1483.     mov ah, 02h
  1484.     int 21h
  1485.     pop dx ax
  1486.     ret
  1487. draw_tab endp
  1488.  
  1489. statistics proc
  1490.     call cls
  1491.    
  1492.     call draw_enter
  1493.     call draw_enter
  1494.     call draw_tab
  1495.     lea dx, results_msg
  1496.     mov ah, 09h
  1497.     int 21h
  1498.    
  1499.     call draw_enter
  1500.     call draw_enter
  1501.     call draw_enter
  1502.    
  1503.     push si cx
  1504.    
  1505.     xor si, si
  1506.     xor cx, cx
  1507.    
  1508.     call draw_tab
  1509.     lea dx, length_msg
  1510.     mov ah, 09h
  1511.     int 21h
  1512.    
  1513.     mov si, offset snake_length
  1514.     call write_number
  1515.    
  1516.     call draw_enter
  1517.     call draw_enter
  1518.     call draw_tab
  1519.    
  1520.     lea dx, art_msg
  1521.     mov ah, 09h
  1522.     int 21h
  1523.    
  1524.     mov si, offset food_count
  1525.     call write_number
  1526.    
  1527.     pop cx si
  1528.     ret
  1529. statistics endp
  1530.  
  1531.  
  1532. main:
  1533.     call parse_args
  1534.    
  1535.     cmp ax, 0000h
  1536.     je @@next_step
  1537.     lea dx, error_msg
  1538.     mov ah, 09h
  1539.     int 21h
  1540.     jmp exit
  1541.        
  1542. @@next_step:
  1543.     mov bx, [setting_flags]
  1544.     cmp bx, 1000h
  1545.     jne @@draw_picture
  1546.    
  1547.     lea dx, help
  1548.     mov ah, 09h
  1549.     int 21h
  1550.     jmp exit
  1551.    
  1552.  
  1553. @@draw_picture:
  1554.     pusha
  1555.     call change_mode
  1556.    
  1557.     call init_snake
  1558.    
  1559.     call draw_walls
  1560.    
  1561.     call draw_art
  1562.    
  1563.     push es
  1564.     mov ah, 35h
  1565.     mov al, 09h
  1566.     int 21h
  1567.     mov word ptr old_09_code + 1, bx
  1568.     mov word ptr old_09_code + 3, es
  1569.     mov dx, offset m09_handler
  1570.     mov ah, 25h
  1571.     mov al, 09h
  1572.     int 21h
  1573.     pop es
  1574.    
  1575.     push es
  1576.     mov ah, 35h
  1577.     mov al, 1ch
  1578.     int 21h
  1579.     mov word ptr old_08_code + 1, bx
  1580.     mov word ptr old_08_code + 3, es
  1581.     mov dx, offset m08_handler
  1582.     mov ah, 25h
  1583.     mov al, 1ch
  1584.     int 21h
  1585.     pop es
  1586.    
  1587. main_loop:
  1588.     mov al, cur_teak
  1589.     cmp al, delay
  1590.     jge @@move
  1591.     jmp @@end
  1592.    
  1593. @@move:
  1594.     call move_snake
  1595.     call play_sounds
  1596.     mov cur_teak, 0h
  1597. @@end:
  1598.    
  1599.     mov al, ext_flag
  1600.     test al, al
  1601.     jz main_loop
  1602.     popa
  1603.    
  1604.     ;call final_music  
  1605.    
  1606.     call statistics
  1607.    
  1608.    
  1609.    
  1610.     push ds
  1611.     mov dx, word ptr old_08_code + 1
  1612.     mov ds, word ptr old_08_code + 3
  1613.     mov ah, 25h
  1614.     mov al, 1ch
  1615.     int 21h
  1616.     pop ds
  1617.    
  1618.     push ds
  1619.     mov dx, word ptr old_09_code + 1
  1620.     mov ds, word ptr old_09_code + 3
  1621.     mov ah, 25h
  1622.     mov al, 09h
  1623.     int 21h
  1624.     pop ds 
  1625.    
  1626.     mov ah, 00h
  1627.     int 16h
  1628.    
  1629.     xor ax, ax
  1630.     mov al, [old_vm]
  1631.     push ax
  1632.     mov al, [old_page]
  1633.     push ax
  1634.     call change_vm
  1635. exit:
  1636.    
  1637. ret
  1638.  
  1639.  
  1640. setting_flags       dw  0000h
  1641. art_count           db  01h
  1642.  
  1643. food_count          db  00h
  1644. snake_length        db  03h
  1645. snake_color         db  07h
  1646. snake_point_size    dw  0004h
  1647. cur_point_size      dw  ?
  1648. cur_color           db  ?
  1649. temp_size_x         dw  ?
  1650. temp_size_y         dw  ?
  1651. temp_point          db  ?
  1652. start_x             dw  ?
  1653. start_y             dw  ?
  1654. is_vertical_snake   db  00h
  1655. temp_line           dw  ?
  1656. temp_column         dw  ?
  1657.  
  1658. found_portal        db  0
  1659. found_elastic_wall  db  0
  1660. found_incr_food     db  0
  1661. found_dec_food      db  0      
  1662. found_next_step     db  0
  1663.  
  1664. portal_sound        dw  4559
  1665. elastic_wall_sound  dw  3620
  1666. inc_food_sound      dw  3044
  1667. dec_food_sound      dw  2416
  1668. next_step_sound     dw  3225
  1669.  
  1670.  
  1671. direct              db  4   ; 4  <-
  1672.                             ; 1 up
  1673.                             ; 2 ->
  1674.                             ; 3 down
  1675. direct_tail         db  0
  1676. head_x              db  ?
  1677. head_y              db  ?
  1678. tail_x              db  ?
  1679. tail_y              db  ?
  1680.  
  1681. ext_flag            db  0
  1682.  
  1683. old_vm              db  ?
  1684. old_page            db  ?
  1685. old_08_code         db  0eah, 00, 00, 00, 00
  1686. old_09_code         db  0eah, 00, 00, 00, 00
  1687. our_vm              db  13h
  1688. our_page            db  02h
  1689.  
  1690. for_inter           db  1   ; 0 - умерла
  1691.                             ; 1 - обрезала хвост
  1692.                             ; 2 - ничего не произошло
  1693.                            
  1694. help                db  "Snake", 0ah, 0dh,"Keys: /h | /l [length] | /a [artefacts count] | /i [0|1|2].", 0ah, 0dh, "Length get values from 2 to 15.", 0ah, 0dh, "Artefacts count get values from 1 to 30.", 0ah, 0dh, "Gray walls are corcrete walls, where snake die.", 0ah, 0dh, "Blue wall is elastic wall.", 0ah, 0dh, "Pink wall is portal.", 0ah, 0dh, "$"
  1695. error_msg           db  "Incorrect keys!$"
  1696.    
  1697. length_msg          db  "Lenght: $"
  1698. art_msg             db  "Artefacts: $"
  1699. msg                 db  ?
  1700. results_msg         db  "Results $"
  1701.  
  1702. ;размер экрана - 320*200
  1703. ;размер карты - 40*64
  1704. ; colors:   08h - бетонный пол
  1705. ;           0bh - упругая стена
  1706. ;           0dh - портал
  1707. ;           09h - артефакт. Увеличивает длину  
  1708. ;           0ch - артефакт. Убивает
  1709. ;           0eh - артефакт. Уменьшает длину
  1710. ;           04h - движение влево
  1711. ;           01h - движение вверх
  1712. ;           02h - движение вправо
  1713. ;           03h - движение вниз
  1714. ;           05h - пустая клетка
  1715.  
  1716.  
  1717. end start
Add Comment
Please, Sign In to add comment