' Swarmz 1.2 (anti-flicker version) ' Copyright (C) 2001, Allan Crossman. ' There seems to be some odd (compiler caused?) bug if I use ' for next loops while both queens and beasts = 1. ' Hence there are no for next loops... ' Looks best on a large monitor resolution. On low resolution, try a lower number of beasts. ' Constants and options; these can be fiddled with... k = 0.5 ' Constant to change all the critters' speeds waitlength = 0.00 ' How long to wait before going to the next tick ' (make this 0 if it's too slow) beasts = 750 ' Number of beasts queens = 15 ' Number of queens maxqueenspeed = k * 6 ' Queens' maximum speed in any direction maxbeastspeed = k * 12 ' Beasts' maximum speed in any direction queenattraction = k * 1.2 ' How much queen can accelerate towards beast each tick beastattraction = k * 2.5 ' How much beast can accelerate towards queen each tick queenjitters = k * 1.0 ' Queens' speed can have up to this value randomly added each tick beastjitters = k * 4.0 ' Beasts' speed can have up to this value randomly added each tick queenmargin = 100 ' If a queen is in the queenmargin it gets a boost away from the edge beastmargin = 50 ' If a beast is in the beastmargin it gets a boost away from the edge hardedge = 2 ' If any critter is beyond the hardedge it gets put back in the arena queenwallhatred = k * 0.5 ' Change in speed of queen when in queenmargin beastwallhatred = k * 2.0 ' Change in speed of beast when in beastmargin queenbouncespeed = k * 1.8 ' Speed queen is set to if it reaches the hardedge beastbouncespeed = k * 1.0 ' Speed beast is set to if it reaches the hardedge beastturnprob = 0.003 ' Chance of beast switching queen in any tick queenturnprob = 0.010 ' Chance of queen switching beast in any tick aloofprob = 0.003 ' Chance of queen switching to no attraction drawqueens = 1 ' Do we draw the queens? drawbeasts = 1 ' Do we draw the beasts? bigqueens = 0 ' Do we draw queens bigger? showmode = 0 ' Is an aloof queen shown in a different colour? persist = 0 ' Do the plots persist? (and not get cleared each tick) ' Housekeeping... disable done disable break hidecursor n = rnd(0 - timer) ' Screen Size... menubardepth = 20 top_left_x = 0 top_left_y = menubardepth width = screen width depth = screen height - menubardepth ' Zap the menu bar (fails under 1.7a) resize console 0, 0, screen width, screen height cls mbclr = init screen (0, 0, screen width, menubardepth) set screen to mbclr cls set screen to console copyrect 0, 0, screen width, menubardepth, 0, 0, screen width, menubardepth, 0, mbclr, 0 kill screen mbclr ' Set console to correct size... resize console top_left_x, top_left_y, top_left_x + width, top_left_y + depth cls set console title to "Swarmz - Press Q to quit..." ' Initialise some settings (and check that the user hasn't given bad values)... if hardedge < 2 then hardedge = 2 queens = int (queens) beasts = int (beasts) if queens < 1 then queens = 1 if beasts < 1 then beasts = 1 dim queen_pos(queens, 2) dim queen_speed(queens, 2) dim queen_allegiance(queens) i = 1 GenerateQueens: queen_speed(i, 1) = 0 queen_speed(i, 2) = 0 queen_allegiance(i) = int (rnd * beasts) queen_pos(i, 1) = rnd * width queen_pos(i, 2) = rnd * depth i = i + 1 if i <= queens then goto GenerateQueens dim beast_pos(beasts, 2) dim beast_speed(beasts, 2) dim allegiance(beasts) i = 1 GenerateBeasts: beast_speed(i, 1) = 0 beast_speed(i, 2) = 0 allegiance(i) = int (rnd * queens) + 1 if allegiance(i) > queens then allegiance(i) = queens beast_pos(i, 1) = rnd * width beast_pos(i, 2) = rnd * depth i = i + 1 if i <= beasts then goto GenerateBeasts set dport to 0 Main: starttime = timer gosub SetQueens gosub SetBeasts if inkey$ = "q" or inkey$ = "Q" or inkey$ = chr$(27) then end if button then end wait waitlength goto Main SetQueens: i = 1 Queen: ' Remove the old queen from the screen... if drawqueens <> 0 and persist = 0 then dport color 0, 0, 0 if bigqueens <> 0 then dplot (queen_pos(i, 1)), (queen_pos(i, 2)) dplot (queen_pos(i, 1)) + 1, (queen_pos(i, 2)) dplot (queen_pos(i, 1)) - 1, (queen_pos(i, 2)) dplot (queen_pos(i, 1)), (queen_pos(i, 2)) + 1 dplot (queen_pos(i, 1)), (queen_pos(i, 2)) - 1 else dplot (queen_pos(i, 1)), (queen_pos(i, 2)) end if end if ' Do queen attraction to beast if it's not in aloof mode... if queen_allegiance(i) <> 0 then n = rnd if beast_pos(queen_allegiance(i), 1) < queen_pos(i, 1) then queen_speed(i, 1) = queen_speed(i, 1) - (queenattraction * n) end if if beast_pos(queen_allegiance(i), 1) > queen_pos(i, 1) then queen_speed(i, 1) = queen_speed(i, 1) + (queenattraction * n) end if if beast_pos(queen_allegiance(i), 2) < queen_pos(i, 2) then queen_speed(i, 2) = queen_speed(i, 2) - (queenattraction * n) end if if beast_pos(queen_allegiance(i), 2) > queen_pos(i, 2) then queen_speed(i, 2) = queen_speed(i, 2) + (queenattraction * n) end if end if ' Do random queen speed alteration... queen_speed(i, 1) = queen_speed(i, 1) + ((rnd - 0.5) * 2 * queenjitters) queen_speed(i, 2) = queen_speed(i, 2) + ((rnd - 0.5) * 2 * queenjitters) ' Queen avoids walls... if queen_pos(i, 1) > width - queenmargin then queen_speed(i, 1) = queen_speed(i, 1) - queenwallhatred if queen_pos(i, 1) < queenmargin then queen_speed(i, 1) = queen_speed(i, 1) + queenwallhatred if queen_pos(i, 2) > depth - queenmargin then queen_speed(i, 2) = queen_speed(i, 2) - queenwallhatred if queen_pos(i, 2) < queenmargin then queen_speed(i, 2) = queen_speed(i, 2) + queenwallhatred ' Slow queen down if it's too fast... if queen_speed(i, 1) > maxqueenspeed then queen_speed(i, 1) = maxqueenspeed if queen_speed(i, 1) < maxqueenspeed * -1 then queen_speed(i, 1) = maxqueenspeed * -1 if queen_speed(i, 2) > maxqueenspeed then queen_speed(i, 2) = maxqueenspeed if queen_speed(i, 2) < maxqueenspeed * -1 then queen_speed(i, 2) = maxqueenspeed * -1 ' Update queen position... queen_pos(i, 1) = (queen_pos(i, 1) + queen_speed(i, 1)) queen_pos(i, 2) = (queen_pos(i, 2) + queen_speed(i, 2)) ' Put queen back in arena if necessary... if queen_pos(i, 1) > (width - hardedge) - 1 then queen_pos(i, 1) = (width - hardedge) - 1 queen_speed(i, 1) = queenbouncespeed * -1 end if if queen_pos(i, 1) < hardedge then queen_pos(i, 1) = hardedge queen_speed(i, 1) = queenbouncespeed end if if queen_pos(i, 2) > (depth - hardedge) - 1 then queen_pos(i, 2) = (depth - hardedge) - 1 queen_speed(i, 2) = queenbouncespeed * -1 end if if queen_pos(i, 2) < hardedge then queen_pos(i, 2) = hardedge queen_speed(i, 2) = queenbouncespeed end if ' See if queen changes allegiance / mode... if rnd < queenturnprob then queen_allegiance(i) = int (rnd * beasts) + 1 if queen_allegiance(i) > beasts then queen_allegiance(i) = beasts end if if rnd < aloofprob then queen_allegiance(i) = 0 end if ' Draw the new queen... if drawqueens <> 0 then if showmode <> 0 and queen_allegiance(i) = 0 then dport color 65535, 65535, 65535 else dport color 65535, 65535, 0 end if if bigqueens <> 0 then dplot (queen_pos(i, 1)), (queen_pos(i, 2)) dplot (queen_pos(i, 1)) + 1, (queen_pos(i, 2)) dplot (queen_pos(i, 1)) - 1, (queen_pos(i, 2)) dplot (queen_pos(i, 1)), (queen_pos(i, 2)) + 1 dplot (queen_pos(i, 1)), (queen_pos(i, 2)) - 1 else dplot (queen_pos(i, 1)), (queen_pos(i, 2)) end if end if i = i + 1 if i <= queens then goto Queen return SetBeasts: i = 1 Beast: ' Remove the old beast from the screen... if drawbeasts <> 0 and persist = 0 then dport color 0, 0, 0 dplot beast_pos(i, 1), beast_pos(i, 2) end if ' Do beast attraction to queen... n = rnd if queen_pos(allegiance(i), 1) < beast_pos(i, 1) then beast_speed(i, 1) = beast_speed(i, 1) - (beastattraction * n) end if if queen_pos(allegiance(i), 1) > beast_pos(i, 1) then beast_speed(i, 1) = beast_speed(i, 1) + (beastattraction * n) end if if queen_pos(allegiance(i), 2) < beast_pos(i, 2) then beast_speed(i, 2) = beast_speed(i, 2) - (beastattraction * n) end if if queen_pos(allegiance(i), 2) > beast_pos(i, 2) then beast_speed(i, 2) = beast_speed(i, 2) + (beastattraction * n) end if ' Do random beast speed alteration... beast_speed(i, 1) = beast_speed(i, 1) + ((rnd - 0.5) * 2 * beastjitters) beast_speed(i, 2) = beast_speed(i, 2) + ((rnd - 0.5) * 2 * beastjitters) ' Beast avoids walls... if beast_pos(i, 1) > width - beastmargin then beast_speed(i, 1) = beast_speed(i, 1) - beastwallhatred if beast_pos(i, 1) < beastmargin then beast_speed(i, 1) = beast_speed(i, 1) + beastwallhatred if beast_pos(i, 2) > depth - beastmargin then beast_speed(i, 2) = beast_speed(i, 2) - beastwallhatred if beast_pos(i, 2) < beastmargin then beast_speed(i, 2) = beast_speed(i, 2) + beastwallhatred ' Slow beast down if it's too fast... if beast_speed(i, 1) > maxbeastspeed then beast_speed(i, 1) = maxbeastspeed if beast_speed(i, 1) < maxbeastspeed * -1 then beast_speed(i, 1) = maxbeastspeed * -1 if beast_speed(i, 2) > maxbeastspeed then beast_speed(i, 2) = maxbeastspeed if beast_speed(i, 2) < maxbeastspeed * -1 then beast_speed(i, 2) = maxbeastspeed * -1 ' Update beast position... beast_pos(i, 1) = beast_pos(i, 1) + beast_speed(i, 1) beast_pos(i, 2) = beast_pos(i, 2) + beast_speed(i, 2) ' Put beast back in arena if necessary... if beast_pos(i, 1) > (width - hardedge) - 1 then beast_pos(i, 1) = (width - hardedge) - 1 beast_speed(i, 1) = beastbouncespeed * -1 end if if beast_pos(i, 1) < hardedge then beast_pos(i, 1) = hardedge beast_speed(i, 1) = beastbouncespeed end if if beast_pos(i, 2) > (depth - hardedge) - 1 then beast_pos(i, 2) = (depth - hardedge) - 1 beast_speed(i, 2) = beastbouncespeed * -1 end if if beast_pos(i, 2) < hardedge then beast_pos(i, 2) = hardedge beast_speed(i, 2) = beastbouncespeed end if ' See if beast changes queen allegiance... if rnd < beastturnprob then allegiance(i) = int (rnd * queens) + 1 if allegiance(i) > queens then allegiance(i) = queens end if ' Draw the new beast... if drawbeasts <> 0 then dport color 0, 65535, 0 dplot beast_pos(i, 1), beast_pos(i, 2) end if i = i + 1 if i <= beasts then goto Beast return