home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 605a.lha / Worm / Worm.s < prev    next >
Text File  |  1992-01-12  |  16KB  |  527 lines

  1. ;   Worm, The Latest Version
  2. ;   © 1991/1992 Ducksoft
  3. ;   We apologize for the somewhat silly comments --
  4. ;   this was coded late at night.
  5.  
  6.             incdir  "include:
  7.             include "exec/exec_lib.i"
  8.             include "graphics/graphics_lib.i"
  9.             include "intuition/intuition_lib.i"
  10.             include "intuition/intuition.i" ;hate to do this, but...
  11.             include "libraries/dos_lib.i"
  12.  
  13.     ; Deal with the system: open Graphics, snog sprites 2-7 and die
  14.     ; gracefully if we can't get 'em all.
  15.  
  16. grabsprites move.l  4,a6
  17.             lea     gname,a1
  18.             moveq.l #0,d0
  19.             jsr     _LVOOpenLibrary(a6)
  20.             move.l  d0,a6
  21.             move.l  d0,GLib
  22.             moveq.l #2,d6
  23.             lea     ImgLst,a4
  24. .1          move.w  d6,d0
  25.             move.l  (a4)+,a0            ;List of sprite structs
  26.             jsr     _LVOGetSprite(a6)   ;Snog!
  27.             cmp.b   d0,d6               ;Did we get the one we wanted?
  28.             bne     ls.1                ;Nope. Die real quietly.
  29.             addq.l  #1,d6
  30.             cmp.b   #8,d6
  31.             bne.s   .1
  32.  
  33.     ; Ok, got my dirty li'l paws on the sprites. Now it's time to open up
  34.     ; my little window.
  35.  
  36.             move.l  4,a6
  37.             lea     dname,a1
  38.             moveq.l #0,d0
  39.             jsr     _LVOOpenLibrary(a6)
  40.             move.l  d0,DLib
  41.             lea     iname,a1
  42.             moveq.l #0,d0
  43.             jsr     _LVOOpenLibrary(a6)
  44.             move.l  d0,a6
  45.             move.l  d0,ILib
  46.             lea     windowdef,a0
  47.             jsr     _LVOOpenWindow(a6)
  48.             move.l  d0,WPtr
  49.             beq     closeint        ;Couldn't open window?
  50.             move.w  d0,Random       ;Good as any for random seed...
  51.             move.l  d0,a0
  52.             move.l  wd_WScreen(a0),a0
  53.             move.l  a0,Scrn         ;save window's screen
  54.             lea     sc_ViewPort(a0),a0
  55.             move.l  a0,Vyew         ;and screen's view
  56.     ; And now for brute force setting of the colors.
  57.     ; There's GOTTA be a better way, but until I find one...
  58.             move.l  GLib,a6
  59.             moveq.b #21,d0
  60.             moveq.b #5,d1
  61.             moveq.b #5,d2
  62.             moveq.b #13,d3
  63.             jsr     _LVOSetRGB4(a6)
  64.             moveq.b #25,d0
  65.             moveq.b #5,d1
  66.             moveq.b #5,d2
  67.             moveq.b #13,d3
  68.             jsr     setcol
  69.             moveq.b #29,d0
  70.             moveq.b #5,d1
  71.             moveq.b #5,d2
  72.             moveq.b #13,d3
  73.             jsr     setcol
  74.             moveq.b #22,d0
  75.             moveq.b #1,d1
  76.             moveq.b #1,d2
  77.             moveq.b #11,d3
  78.             jsr     setcol
  79.             moveq.b #26,d0
  80.             moveq.b #1,d1
  81.             moveq.b #1,d2
  82.             moveq.b #11,d3
  83.             jsr     setcol
  84.             moveq.b #30,d0
  85.             moveq.b #1,d1
  86.             moveq.b #1,d2
  87.             moveq.b #11,d3
  88.             jsr     setcol
  89.             moveq.b #23,d0
  90.             moveq.b #1,d1
  91.             moveq.b #1,d2
  92.             moveq.b #6,d3
  93.             jsr     setcol
  94.             moveq.b #27,d0
  95.             moveq.b #1,d1
  96.             moveq.b #1,d2
  97.             moveq.b #6,d3
  98.             jsr     setcol
  99.             moveq.b #31,d0
  100.             moveq.b #1,d1
  101.             moveq.b #1,d2
  102.             moveq.b #6,d3
  103.             jsr     setcol
  104.             move.b  #4,rndfu
  105.  
  106.     ; And now, at last, we can get on with the main loop.
  107. main
  108.             jsr     rnd                 ;Play with pseudo-random number
  109.             jsr     ckmouse             ;If too near mouse, turn away.
  110.             jsr     pushworm            ;Move worm ahead.
  111.             jsr     putworm             ;Actual sprite moving code
  112.             move.l  WPtr,a0             ;Hello? Is Bernie there?
  113.             move.l  wd_UserPort(a0),a0  ;Hello? Is Bernie there?
  114.             move.l  4,a6                ;Hello? Is Bernie there?
  115.             jsr     _LVOGetMsg(a6)      ;Hi, this is Bernie, any messages
  116.             tst.l   d0                  ;                         for me?
  117.             bne     gobyebye    ;we got a message, which musta been CLOSE.
  118.             move.l  GLib,a6
  119.             jsr     _LVOWaitTOF(a6)     ;Wait 'til next frame
  120.             jsr     _LVOWaitTOF(a6)     ;Blow off another frame
  121.             subq.b  #1,rndfu
  122.             bne.s   main
  123.             move.b  #5,rndfu
  124.             eor.b   #$FF,Random+1
  125.             bra.s   main                ;an' do it again
  126.  
  127.     ;Rnd: does things to the random number.
  128. rnd
  129.             move.b  Random,d0
  130.             move.b  Random+1,d1
  131.             rol.b   #1,d0
  132.             eor.b   d0,d1
  133.             ror.b   #1,d1
  134.             addq.b  #1,Random+1
  135.             add.b   Random+1,d1
  136.             bvc     .1
  137.             addq.b  #1,Random+1
  138. .1          move.b  d1,Random
  139.             rts
  140.  
  141.     ;Pythag: calculates distance between mouse pointer and worm's head.
  142.     ;Expects D0.w,D1.w=Px,Px; D2.w,D3.w=Wx,Wy; D4.b=vec;
  143.     ;returns dist in D5.L
  144. pythag
  145.             move.w  d2,-(a7)    ;Preserve old worm X
  146.             move.w  d3,-(a7)    ;Preserve old worm y
  147.             add.b   d4,d4
  148.             lea     xmov,a0
  149.             add.w   0(a0,d4.w),d2   ;Get worm's future X
  150.             add.w   32(a0,d4.w),d3  ;and y
  151.             sub.w   d0,d2       ;Find X distance
  152.             sub.w   d1,d3       ;Find Y distance
  153.             muls.w  d2,d2       ;Apply the theorem of Pythagoras
  154.             muls.w  d3,d3       ;...sort of...
  155.             add.l   d2,d3
  156.             bvc.s   .1          ;If overflow, then max out distance!
  157.             move.l  #$ffffffff,d3
  158. .1          move.l  d3,d5
  159.             move.w  d3,(a7)+    ;Pop worm X
  160.             move.w  d2,(a7)+    ;Pop worm Y
  161.             rts
  162.  
  163.     ;Ckmouse: checks for mouse proximity. If too close, turn away; if not,
  164.     ;         take a random turn.
  165. ckmouse
  166.             move.l  Scrn,a0
  167.             move.w  sc_MouseX(a0),d0    ;Get mouse XY
  168.             move.w  sc_MouseY(a0),d1
  169.             move.w  XTable,d2       ;Get worm XY
  170.             move.w  YTable,d3
  171.             add.w   d0,d0           ;Bong mouse XY into "proper" resolution
  172.             add.w   d1,d1
  173.             move.b  HeadVec,d4
  174.             bsr.s   pythag
  175.             cmp.l   #16384,d5       ;Is worm in "danger" from the mouse?
  176.             bge.s   RandMove        ;No - do a random move.
  177.             move.b  d4,d6
  178.             addq.b  #1,d4
  179.             and.b   #15,d4
  180.             bsr.s   pythag          ;Check result of a left turn
  181.             move.l  d5,d7           ;Save result
  182.             move.b  d6,d4
  183.             subq.b  #1,d4
  184.             and.b   #15,d4
  185.             bsr.s   pythag          ;Now check a right turn
  186.             cmp.l   d5,d7           ;So, which one will be closer to mouse?
  187.             blt.s   .1
  188.             addq.b  #1,d6           ;Left turn
  189.             bra.s   .2
  190. .1          subq.b  #1,d6           ;Right turn
  191. .2          and.b   #15,d6
  192.             move.b  d6,HeadVec      ;Save result and end
  193.             rts
  194.  
  195. RandMove    move.b  Random,d1
  196.             move.b  HeadVec,d0
  197.             and.b   #15,d1
  198.             cmp.b   #1,d1
  199.             bgt.s   .r1
  200.             addq.b  #1,d0   ;25% to turn each way, 50% to go straight.
  201.             jmp .end
  202. .r1         cmp.b   #3,d1
  203.             bgt.s   .end
  204.             subq.b  #1,d0
  205. .end        and.b   #15,d0  ;Mask off high nybble
  206.             move.b  d0,HeadVec
  207.             rts
  208.  
  209.     ;Pushworm: pushes X and Y table along, then moves worm ahead.
  210. pushworm
  211.             lea     GLib,a0
  212.             lea     GLib-2,a1
  213.             moveq.b #100,d0
  214. .loop       move.w  -(a1),-(a0)
  215.             dbra    d0,.loop
  216.             move.w  YTable+2,YTable
  217.             clr.w   d0
  218.             move.b  HeadVec,d0
  219.             add.b   d0,d0
  220.             move.w  XTable,d1
  221.             move.w  YTable,d2
  222.             lea     xmov,a0
  223.             add.w   0(a0,d0.w),d1
  224.             bpl.s   .1          ;Positive #?
  225.             move.w  #0,d1       ;No - keep on-screen
  226.             bsr.s   XBounce     ;And bounce.
  227. .1          cmp.w   #1220,d1     ;Greater than right edge?
  228.             ble.s   .2          ;Nope; do Y stuff.
  229.             move.w  #1220,d1     ;Yes - keep on-screen
  230.             bsr.s   XBounce
  231. .2          add.w   32(a0,d0.w),d2
  232.             bpl.s   .3          ;Positive #?
  233.             move.w  #0,d2       ;No - keep on-screen
  234.             bsr.s   YBounce     ;And bounce.
  235. .3          cmp.w   #740,d2     ;Greater than bottom edge?
  236.             ble.s   .4          ;Nope; plop values.
  237.             move.w  #740,d2     ;Yes - keep on-screen
  238.             bsr.s   YBounce
  239. .4          move.w  d1,XTable
  240.             move.w  d2,YTable
  241.             lsr.w   #1,d0
  242.             move.b  d0,HeadVec
  243.             rts
  244.  
  245.     ;Bounce routines. Used when worm reaches edge of screen...
  246. XBounce
  247.             lea     xboing,a1
  248.             lsr.b   #1,d0
  249.             move.b  0(a1,d0.w),d0
  250.             add.b   d0,d0
  251.             rts
  252. YBounce
  253.             lea     yboing,a1
  254.             lsr.b   #1,d0
  255.             move.b  0(a1,d0.w),d0
  256.             add.b   d0,d0
  257.             rts
  258.  
  259.     ;Putworm: does the actual job of moving the sprites
  260. putworm
  261.             moveq.b #0,d7   ;loop counter
  262.             moveq.l #0,d6   ;offset into tables
  263.             lea     XTable,a3
  264.             lea     ImgLst,a5
  265.             move.l  GLib,a6
  266. .loop       move.w  0(a3,d6),d0         ;Grab X value
  267.             move.w  102(a3,d6),d1       ;Grab Y value
  268.             lsr.w   #2,d0               ;Scale 'em down
  269.             lsr.w   #2,d1
  270.             move.l  (a5)+,a1            ;Point to SimpleSprite
  271.             move.l  #0,a0
  272.             jsr     _LVOMoveSprite(a6)  ;Do it!
  273.             addq.b  #1,d7
  274.             cmp.b   #6,d7               ;Loop stuff
  275.             beq     endput
  276.             add.w   #8,d6              ;Next table entrys
  277.             bra     .loop               ;More sprites to move.
  278. endput      rts
  279.  
  280. setcol      move.l  Vyew,a0
  281.             jsr     _LVOSetRGB4(a6)
  282.             rts
  283.  
  284.     ; Free up stuff and exit. You wouldn't -believe- how long it took to
  285.     ; find the moron mistake in setting up for that CloseLibrary. I was
  286.     ; passing a pointer to the "graphics.library" text instead of a ptr to
  287.     ; graphicsbase... Stupid, stupid. Hell, it's my first Amy code, I'm
  288.     ; allowed to make braindead mistakes like that. And I'll remember...
  289. gobyebye
  290.             move.l  ILib,a6
  291.             move.l  WPtr,a0
  292.             jsr     _LVOCloseWindow(a6)
  293. closeint    move.l  4,a6
  294.             move.l  ILib,a1
  295.             jsr     _LVOCloseLibrary(a6)
  296. losespr     move.l  GLib,a6             ;Get Graphics.lib again
  297.             moveq.l #7,d6               ;Free sprites in reverse order
  298. ls.1        move.b  d6,d0
  299.             jsr     _LVOFreeSprite(a6)
  300.             subq.l  #1,d6
  301.             cmp.b   #1,d6
  302.             bne.s   ls.1
  303.             move.l  4,a6
  304.             move.l  GLib,a1
  305.             jsr     _LVOCloseLibrary(a6)
  306.             rts
  307.  
  308.     section Data,data
  309.  
  310. XTable      ds.w    51
  311. YTable      ds.w    51
  312. GLib        ds.l    1
  313. ILib        ds.l    1
  314. DLib        ds.l    1
  315. WPtr        ds.l    1
  316. Scrn        ds.l    1
  317. Vyew        ds.l    1
  318. Random      ds.b    2
  319. rndfu       ds.b    1
  320. HeadVec     dc.b    5
  321.  
  322. ImgLst      dc.l    SS0,SS1,SS2,SS3,SS4,SS5
  323.  
  324. SS0             dc.l    head
  325.                 dc.w    $10
  326.                 dc.w    1
  327.                 dc.w    1
  328.                 dc.w    0
  329.  
  330. SS1             dc.l    body0
  331.                 dc.w    $10
  332.                 dc.w    1
  333.                 dc.w    1
  334.                 dc.w    0
  335.  
  336. SS2             dc.l    body1
  337.                 dc.w    $10
  338.                 dc.w    1
  339.                 dc.w    1
  340.                 dc.w    0
  341.  
  342. SS3             dc.l    body2
  343.                 dc.w    $10
  344.                 dc.w    1
  345.                 dc.w    1
  346.                 dc.w    0
  347.  
  348. SS4             dc.l    body3
  349.                 dc.w    $10
  350.                 dc.w    1
  351.                 dc.w    1
  352.                 dc.w    0
  353.  
  354. SS5             dc.l    body4
  355.                 dc.w    $10
  356.                 dc.w    1
  357.                 dc.w    1
  358.                 dc.w    0
  359.  
  360. xmov    dc.w    0,5,8,11,12,11,8,5,0,-5,-8,-11,-12,-11,-8,-5
  361. ymov    dc.w    12,11,8,5,0,-5,-8,-11,-12,-11,-8,-5,0,5,8,11
  362. xboing  dc.b    $0,$f,$e,$d,$c,$b,$a,$9,$8,$7,$6,$5,$4,$3,$2,$1
  363. yboing  dc.b    $8,$7,$6,$5,$4,$3,$2,$1,$0,$f,$e,$d,$c,$b,$a,$9
  364.  
  365. gname   dc.b "graphics.library",0,0   ;2nd 0 word-aligns.
  366. iname   dc.b "intuition.library",0
  367. dname   dc.b "dos.library",0
  368.  
  369. windowdef
  370.         dc.w    100,100     ;x,y
  371.         dc.w    120,10      ;width, height
  372.         dc.b    -1,-1       ;default pens
  373.         dc.l    $200        ;CLOSEWINDOW
  374.         dc.l    $e          ;WINDOWDRAG!WINDOWDEPTH!WINDOWCLOSE
  375.         dc.l    0,0         ;egad! no gads, no checkmark
  376.         dc.l    wintitle    ;->title
  377.         dc.l    0,0         ;no screen, no bitmap
  378.         dc.w    0,0,0,0     ;minsize. no wife, no horse, no moustache.
  379.         dc.w    $1          ;WBENCHSCREEN
  380. wintitle    dc.b    "Worm",0
  381.  
  382.     section Images,data_c
  383. head    dc.l  $00000000  ; Remember it's exactly $10 lines high.
  384.         dc.l  $000007c0
  385.         dc.l  $00001ff0
  386.         dc.l  $00003ff8
  387.         dc.l  $06C0793c
  388.         dc.l  $0FE4701c
  389.         dc.l  $0FE2f45e
  390.         dc.l  $1FF6e44e
  391.         dc.l  $9EF2e10e
  392.         dc.l  $4016fffe
  393.         dc.l  $A00Efffe
  394.         dc.l  $541C7ffc
  395.         dc.l  $6ABC7ffc
  396.         dc.l  $35F83ff8
  397.         dc.l  $1FF01ff0
  398.         dc.l  $07C007c0
  399.         dc.l  $00000000
  400. imagelen    equ *-head
  401.  
  402. body0   dc.l  $00000000
  403.         dc.l  $000007c0
  404.         dc.l  $05001af0
  405.         dc.l  $0E803178
  406.         dc.l  $1D0862fc
  407.         dc.l  $280457fc
  408.         dc.l  $100Aeffe
  409.         dc.l  $0006fffe
  410.         dc.l  $800Afffe
  411.         dc.l  $4016fffe
  412.         dc.l  $A02Efffe
  413.         dc.l  $555C7ffc
  414.         dc.l  $6ABC7ffc
  415.         dc.l  $35F83ff8
  416.         dc.l  $1FF01ff0
  417.         dc.l  $07C007c0
  418.         dc.l  $00000000
  419.  
  420. body1   dc.l  $00000000
  421.         dc.l  $000007c0
  422.         dc.l  $05001af0
  423.         dc.l  $0E803178
  424.         dc.l  $1D0862fc
  425.         dc.l  $280457fc
  426.         dc.l  $100Aeffe
  427.         dc.l  $0006fffe
  428.         dc.l  $800Afffe
  429.         dc.l  $4016fffe
  430.         dc.l  $A02Efffe
  431.         dc.l  $555C7ffc
  432.         dc.l  $6ABC7ffc
  433.         dc.l  $35F83ff8
  434.         dc.l  $1FF01ff0
  435.         dc.l  $07C007c0
  436.         dc.l  $00000000
  437.  
  438. body2   dc.l  $00000000
  439.         dc.l  $000007c0
  440.         dc.l  $05001af0
  441.         dc.l  $0E803178
  442.         dc.l  $1D0862fc
  443.         dc.l  $280457fc
  444.         dc.l  $100Aeffe
  445.         dc.l  $0006fffe
  446.         dc.l  $800Afffe
  447.         dc.l  $4016fffe
  448.         dc.l  $A02Efffe
  449.         dc.l  $555C7ffc
  450.         dc.l  $6ABC7ffc
  451.         dc.l  $35F83ff8
  452.         dc.l  $1FF01ff0
  453.         dc.l  $07C007c0
  454.         dc.l  $00000000
  455.  
  456. body3   dc.l  $00000000
  457.         dc.l  $000007c0
  458.         dc.l  $05001af0
  459.         dc.l  $0E803178
  460.         dc.l  $1D0862fc
  461.         dc.l  $280457fc
  462.         dc.l  $100Aeffe
  463.         dc.l  $0006fffe
  464.         dc.l  $800Afffe
  465.         dc.l  $4016fffe
  466.         dc.l  $A02Efffe
  467.         dc.l  $555C7ffc
  468.         dc.l  $6ABC7ffc
  469.         dc.l  $35F83ff8
  470.         dc.l  $1FF01ff0
  471.         dc.l  $07C007c0
  472.         dc.l  $00000000
  473.  
  474. body4   dc.l  $00000000
  475.         dc.l  $000007c0
  476.         dc.l  $05001af0
  477.         dc.l  $0E803178
  478.         dc.l  $1D0862fc
  479.         dc.l  $280457fc
  480.         dc.l  $100Aeffe
  481.         dc.l  $0006fffe
  482.         dc.l  $800Afffe
  483.         dc.l  $4016fffe
  484.         dc.l  $A02Efffe
  485.         dc.l  $555C7ffc
  486.         dc.l  $6ABC7ffc
  487.         dc.l  $35F83ff8
  488.         dc.l  $1FF01ff0
  489.         dc.l  $07C007c0
  490.         dc.l  $00000000
  491.  
  492. body5   dc.l  $00000000
  493.         dc.l  $000007c0
  494.         dc.l  $05001af0
  495.         dc.l  $0E803178
  496.         dc.l  $1D0862fc
  497.         dc.l  $280457fc
  498.         dc.l  $100Aeffe
  499.         dc.l  $0006fffe
  500.         dc.l  $800Afffe
  501.         dc.l  $4016fffe
  502.         dc.l  $A02Efffe
  503.         dc.l  $555C7ffc
  504.         dc.l  $6ABC7ffc
  505.         dc.l  $35F83ff8
  506.         dc.l  $1FF01ff0
  507.         dc.l  $07C007c0
  508.         dc.l  $00000000
  509.  
  510. body6   dc.l  $00000000
  511.         dc.l  $000007c0
  512.         dc.l  $05001af0
  513.         dc.l  $0E803178
  514.         dc.l  $1D0862fc
  515.         dc.l  $280457fc
  516.         dc.l  $100Aeffe
  517.         dc.l  $0006fffe
  518.         dc.l  $800Afffe
  519.         dc.l  $4016fffe
  520.         dc.l  $A02Efffe
  521.         dc.l  $555C7ffc
  522.         dc.l  $6ABC7ffc
  523.         dc.l  $35F83ff8
  524.         dc.l  $1FF01ff0
  525.         dc.l  $07C007c0
  526.         dc.l  $00000000
  527.