home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / SKY.ZIP / SUBDIV.ASM < prev    next >
Assembly Source File  |  1996-02-14  |  5KB  |  230 lines

  1. ;----------------------------------------------------------------------
  2. ;  Subdivide(Map:Pointer;Start,Side:word): Fractal plasma routine.
  3. ;
  4. ;  Map points to a 65536-byte array initialised to all 255's.
  5. ;  Start is the coords of the TL corner (Y,X in 8.8 format)
  6. ;  Length is the side length of the square
  7. ;
  8. ;  Smooth(Map:Pointer)  Smooths a map
  9. ;
  10. ; This code is Copyright (C) 1996 Mark Mackey. Use, modification,
  11. ; and redistribution of this code is freely permitted, provided that
  12. ; the original author is acknowledged.
  13. ;
  14. ;----------------------------------------------------------------------
  15.  
  16. .model small,pascal
  17. .386
  18.  
  19. EXTRN Randseed:DWORD
  20. .CODE
  21.  
  22. @Random   MACRO
  23. ; Returns a random number in ax
  24. ; Kills ax,dx,flags
  25.         mov            eax,[randseed]
  26.         mov            edx,000041a7h
  27.         mul            edx
  28.         and            eax,07FFFFFFFh
  29.         mov            [randseed],eax
  30.         shr            eax,8
  31. endm
  32.  
  33. @ScaledRandom   MACRO scale
  34. ; Returns a random number in dx between -scale and scale
  35. ; Kills ax,dx,flags
  36.         @Random
  37.         mov    dl,scale
  38.         xor    dh,dh
  39.         shl    dx,2
  40.         imul   dx
  41. endm
  42.  
  43. @AddRandom    MACRO
  44. ; Input: dx = raw value cl=size of random displacement
  45. ; Output al = clipped final value
  46. ; Modifies dx,ax
  47.     push  dx                ; average of tl and tr corners of square
  48.     @ScaledRandom cl
  49.     pop   ax
  50.     add   ax,dx        ; add random displacement
  51.     jg    $+5        ; jump to NotTooLow if greater than zero
  52.     mov   ax,1
  53. ;NotTooLow
  54.     cmp   ax,0feh
  55.     jbe   $+4        ; jump to NotTooHigh if less than 255
  56.     mov   al,0feh
  57. ;NotTooHigh
  58. endm
  59.  
  60.     PUBLIC Subdivide,Smooth
  61. Subdivide proc near Pascal Map:FAR PTR, Start:WORD, Side: WORD
  62.  
  63.     pusha
  64.     les   di,[Map]
  65.     cmp   di,0
  66.     jne   @Finished        ;need di=0 (ie Map is whole segment)
  67.  
  68.     @Random            ;initialise RNG
  69.  
  70.         mov   cx,[Side]
  71.         shr   cx,1              ;cl holds Side/2
  72.         mov   ax,[Start]        ;ax holds map x,y position: assumes width=256
  73.  
  74.         mov   di,ax
  75.     mov   dl,es:[di]        ;dl holds value at tl corner
  76.     mov   bx,ax
  77.     add   bl,cl
  78.     mov   di,bx
  79.     add   bl,cl             ;ax=tl corner,di=top centre, bx=tr corner
  80.  
  81.         cmp   byte ptr es:[di],0FFh
  82.     jne   @TopOK
  83.         xor   dh,dh
  84.         add   dl,es:[bx]
  85.         adc   dh,0
  86.     shr   dx,1        ; average of tl and tr corners of square
  87.     @AddRandom
  88.     mov   es:[di],al        ; midpoint of top of square done!
  89. @TopOK:
  90.  
  91.         mov   dl,es:[bx]
  92.         add   bh,cl
  93.         mov   di,bx
  94.         add   bh,cl             ;di=right centre, bx=br corner
  95.  
  96.         cmp   byte ptr es:[di],0FFh
  97.         jnz   @RightOK
  98.         xor   dh,dh
  99.         add   dl,es:[bx]
  100.         adc   dh,0
  101.     shr   dx,1
  102.     @AddRandom
  103.     mov   es:[di],al
  104. @RightOK:
  105.  
  106.         mov   dl,es:[bx]
  107.         sub   bl,cl
  108.         mov   di,bx
  109.         sub   bl,cl             ;di=bottom centre, bx=bl corner
  110.  
  111.         cmp   byte ptr es:[di],0FFh
  112.         jnz   @BottomOK
  113.         xor   dh,dh
  114.         add   dl,es:[bx]
  115.         adc   dh,0
  116.     shr   dx,1
  117.     @AddRandom
  118.     mov   es:[di],al
  119. @BottomOK:
  120.  
  121.         mov   dl,es:[bx]
  122.         sub   bh,cl
  123.         mov   di,bx
  124.     sub   bh,cl             ;di=left centre, bx=tl corner
  125.  
  126.         cmp   byte ptr es:[di],0FFh
  127.         jnz   @LeftOK
  128.         xor   dh,dh
  129.         add   dl,es:[bx]
  130.         adc   dh,0
  131.     shr   dx,1
  132.     @AddRandom
  133.     mov   es:[di],al
  134. @LeftOK:
  135.  
  136.     xor   dh,dh        ; Now do centre of square
  137.     mov   dl,es:[bx]        ; tl corner
  138.     add   bl,cl
  139.     add   bl,cl
  140.     add   dl,es:[bx]        ; tr corner
  141.     adc   dh,0
  142.     add   bh,cl
  143.     add   bh,cl
  144.     add   dl,es:[bx]        ; br corner
  145.     adc   dh,0
  146.     sub   bl,cl
  147.     sub   bl,cl
  148.     add   dl,es:[bx]    ; bl corner
  149.     adc   dh,0
  150.     shr   dx,2              ; divide by 4
  151.     sub   bh,cl
  152.     add   bl,cl             ; bx points to centre of square
  153.     @AddRandom
  154.     mov   es:[bx],al
  155. @MiddleOK:
  156.         cmp   cl,1
  157.         jbe   @Finished
  158.         mov   ch,0
  159.         push  es
  160.         push  0000h
  161.         push  bx                ;centrepoint: br square
  162.         push  cx
  163.     call  Subdivide
  164.  
  165.         push  es
  166.         push  0000h
  167.         sub   bl,cl             ;bl square
  168.         push  bx
  169.         push  cx
  170.     call  Subdivide
  171.  
  172.         push  es
  173.         push  0000h
  174.         sub   bh,cl             ;tl square
  175.         push  bx
  176.         push  cx
  177.     call  Subdivide
  178.  
  179.         push  es
  180.         push  0000h
  181.         add   bl,cl             ;tr square
  182.         push  bx
  183.         push  cx
  184.     call  Subdivide
  185.  
  186. @Finished:
  187.         popa
  188.     ret
  189. Subdivide     endp
  190.  
  191. Smooth proc near Pascal MapToSmooth:FAR PTR,Shift:WORD
  192.  
  193.         les   si,[MapToSmooth]
  194.         xor   bx,bx
  195. @Loop:
  196.         mov   al,es:[bx]
  197.         xor   ah,ah
  198.         add   al,es:[bx+1]
  199.         adc   ah,0
  200.         add   al,es:[bx-1]
  201.         adc   ah,0
  202.         add   al,es:[bx+256]
  203.         adc   ah,0
  204.         shr   ax,2
  205.         mov   es:[bx],al
  206.         add   bx,1
  207.         jnc   @Loop
  208.  
  209.         xor   bx,bx
  210.         mov   dx,[Shift]
  211. @Loop2:
  212.         movzx   ax,es:[bx]
  213.     add   ax,dx
  214.     cmp   ax,0
  215.     jge   @NotNeg
  216.     mov   ax,0
  217.     jmp   @OK
  218. @NotNeg:
  219.     cmp   ax,0feh
  220.     jle   @OK
  221.     mov   al,0feh
  222. @OK:
  223.         mov   es:[bx],al
  224.         add   bx,1
  225.     jnc   @Loop2
  226.     ret
  227. Smooth endp
  228.  
  229. end
  230.