°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
-- NEW CONTINENT GENERATION ALGORITHM   John Cartan 3/9/94
--
-- The old algorithm produced land (1) and sea (2).  This new version
-- produces mountains (1), land (2), and sea (3) and is divided into
-- three distinct phases.  In the first phase, which lasts for about
-- the first 500 spark assignments, the land grows like water around
-- the original 20 spark mountains.  During this phase, if a selected
-- spark is mountainous, there's only a 45% chance (5/11 as opposed
-- to 5/6 in the old algorithm) that neighboring positions will be
-- mountainous; if the selected spark is land, ALL its neighbors will
-- be land.
--
-- At the beginning of phase two, 40 sea sparks are tossed into the map.
-- During this phase, if a selected spark is land OR mountain, there's
-- an 83% (5/6) chance that neighboring positions will be LAND; if the
-- selected spark is sea, ALL its neighbors will be sea.  Note that
-- mountains are only formed phase one, and sea is only formed in phase
-- two.
--
-- Phase THREE is a cutoff in which no more land is formed and all
-- unassigned positions are set to sea.  This is designed to
-- counteract the tendency for several continents to grow together
-- into a single world-spanning supercontient.  Phase three should kick
-- in after about half of the sparks are assigned and would thus cut
-- generation time in half.
--
-- The modification to the original algorithm is shown in UPPER CASE
-- and occurs mostly in the "spread" handler (see below).  As you can
-- see, the change is quite simple.  This stack allows you to compare
-- both algorithms over time.  We may need to adjust the probabilities
-- and phase transition points.  Also, it may not be worthwhile to toss
-- out extra sea sparks at the opening of phase two.
°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

on mouseUp
  global map,sparks,PHASE
  if number of cards>1 then
    answer "Please Clear Stack First!" with "Oops"
    exit mouseUp
  end if
  put 26 into word 4 of field "Card Count"
  
  initMap
  set brush to 4
  choose brush tool
  
  PUT 1 INTO PHASE
  scatter 20,1 -- 20 MOUNTAINS
  
  newframe
  repeat until sparks is empty
    put random(number of lines of sparks) into p
    put line p of sparks into q
    delete line p of sparks
    put item 1 of q into x
    put item 2 of q into y
    spread x,y
  end repeat
  choose browse tool
end mouseUp

on initMap
  global map,sparks,count
  put empty into sparks
  put 0 into count
  put "0" into map
  repeat 7 times
    put map after map
  end repeat
  put char 1 to 97 of map & return into map
  repeat 6 times
    put map after map
  end repeat
  put line 1 to 57 of map into map
end initMap

on scatter num,which
  global map,sparks
  SET PATTERN TO ITEM WHICH OF "12,20,14"
  put 1 into j
  repeat until j>=num
    put random(53)+4 into y
    put random(93)+4 into x
    put char x of line y of map into m
    if m<>0 then next repeat
    click at x*5,y*5
    put which into char x of line y of map
    put x&","&y&return after sparks
    add 1 to j
  end repeat
end scatter

on spread x,y
  global map,sparks,count,PHASE
  put char x of line y of map into m
  repeat with j=-1 to 1
    repeat with k=-1 to 1
      if j=0 and k=0 then next repeat
      put x-j into x1
      put y-k into y1
      if x1<5 or x1>97 or y1<5 or y1>57 then next repeat
      put char x1 of line y1 of map into n
      if n<>0 then next repeat
      put m into r
      
      IF PHASE=1 THEN
        IF R=1 THEN PUT (RANDOM(11) DIV 6)+1 INTO R
        PUT R INTO CHAR X1 OF LINE Y1 OF MAP
        SET PATTERN TO ITEM R OF "12,20"
      ELSE
        IF R<3 THEN PUT (RANDOM(6) DIV 6)+2 INTO R
        PUT R INTO CHAR X1 OF LINE Y1 OF MAP
        SET PATTERN TO ITEM R OF "12,20,14"
      END IF
      
      click at x1*5,y1*5
      put x1&","&y1&return after sparks
      add 1 to count
      
      IF COUNT=500 THEN
        PUT 2 INTO PHASE
        SCATTER 40,3
      END IF
      
      IF COUNT=2500 THEN
        PUT 3 INTO PHASE
        SET PATTERN TO 14
        REPEAT WITH Y=5 TO 57
          REPEAT WITH X=5 TO 97
            PUT CHAR X OF LINE Y OF MAP INTO M
            IF M=0 THEN
              PUT 3 INTO CHAR X OF LINE Y OF MAP
              CLICK AT X*5,Y*5
              ADD 1 TO COUNT
            END IF
          END REPEAT
        END REPEAT
        PUT EMPTY INTO SPARKS
      END IF
      
      if count mod 100 = 0 then newframe
    end repeat
  end repeat
end spread

on newframe
  if word 2 of field "Card Count" is "50" then exit newframe
  domenu "Copy Card"
  domenu "Paste Card"
  add 1 to word 2 of field "Card Count"
end newframe