home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Imitate_Li336321162001.psc / AntEater.bas < prev    next >
Encoding:
BASIC Source File  |  2001-11-06  |  8.8 KB  |  307 lines

  1. Attribute VB_Name = "AntEater"
  2. 'the number of ants to introduce the eaters at
  3. Public Const IntroduceEaterAt = 20
  4.  
  5. 'changed values increase/decrease by the mutation rate
  6. Public Const MutationRateEater = 3
  7.  
  8. 'the amount of food to look for per tick
  9. Public Const FoodRateEater = 1
  10.  
  11. 'the amount of ticks a Ant can survive (max)
  12. Public Const KillAtTickEater = 2000
  13.  
  14. Public Const MaxLifeSpanEater = 5000
  15.  
  16. 'area to search for food
  17. Public Const InitialSpeedEater = 10
  18.  
  19. 'maximum area to search for food
  20. Public Const MaxSpeedEater = 15
  21.  
  22. 'the amount of food needed before reproduction
  23. '(recommended to be 1/3 KillAtTickEater)
  24. Public Const PropegateLevelEater = 50
  25.  
  26. 'this prevend clutter and helps keep the program speed at a
  27. 'reasonable level
  28. Public Const MinPropLevelEater = 50
  29.  
  30. 'the maximum amount of eaters on the screen at any one time.
  31. Const MaxEaters = 100
  32.  
  33. 'store data on the ant eaters
  34. Public Eaters() As CreatureType
  35. Public NumOfEaters As Integer
  36.  
  37. Public Sub StartingEater()
  38. 'This is only activated during load. It sets the default values of
  39. 'the Startingeater eater
  40.  
  41. NumOfEaters = 1
  42. ReDim Preserve Eaters(NumOfEaters)
  43.  
  44. With Eaters(NumOfEaters)
  45.     .Direction = GetRndInt(Left, Up)
  46.     .FoodLevel = 0
  47.     .FoodToSplit = GetRndInt(MinPropLevel, PropegateLevelEater)
  48.     .KillLevel = GetRndInt(KillAtTickEater, MaxLifeSpanEater)
  49.     .Speed = GetRndInt(MaxSpeedEater, InitialSpeedEater)
  50.     .TickNum = 0
  51.     Do
  52.         'keep searching for a free space until one is found
  53.         .XPos = GetRndInt(0, GridSize)
  54.         .YPos = GetRndInt(0, GridSize)
  55.     Loop Until Grid(.XPos, .YPos) = HereEmpty
  56. End With
  57. End Sub
  58.  
  59. Public Sub KillEater(EaterNum As Integer)
  60. 'this reduces the number of Eaters by one.
  61.  
  62. Dim Counter As Integer
  63.  
  64. 'a dead Eater becomes food /(empty)
  65. Grid(Eaters(EaterNum).XPos, Eaters(EaterNum).YPos) = Food
  66. Call frmLife.DrawDot(Eaters(EaterNum).XPos, Eaters(EaterNum).YPos, vbWhite)
  67. Call frmLife.DrawDot(Eaters(EaterNum).XPos, Eaters(EaterNum).YPos, vbGreen)
  68.  
  69. 're-calculate averages
  70. TotalLife = TotalLife - Eaters(EaterNum).KillLevel
  71. TotalSpeed = TotalSpeed - Eaters(EaterNum).Speed
  72. TotalProp = TotalProp - Eaters(EaterNum).FoodToSplit
  73.  
  74. 'kill Eater
  75. For Counter = EaterNum To (NumOfEaters - 1)
  76.     Eaters(Counter) = Eaters(Counter + 1)
  77. Next Counter
  78.  
  79. 'reduce the Eater number
  80. NumOfEaters = NumOfEaters - 1
  81. ReDim Preserve Eaters(NumOfEaters)
  82.  
  83. 'show stats
  84. TotalDead = TotalDead + 1
  85. 'frmLife.lblDeath.Caption = "Death Rate : " & Format((TotalDead / Generations) * 100, "0.0") & "%"
  86. End Sub
  87.  
  88. Public Sub MoveEater(EaterNum As Integer)
  89. 'move the Eater in the direction it is meEater to (if it can) and
  90. 'change the direction.
  91.  
  92. Const X = 0
  93. Const Y = 1
  94.  
  95. Dim Target(2) As Integer
  96. Dim Got(2) As Integer
  97. Dim Speed As Integer
  98. Dim MyX As Integer
  99. Dim MyY As Integer
  100. Dim GotAntNum As Integer
  101. Dim Turn As Integer
  102.  
  103. 'check to see if changes are needed to be made to the Eater
  104. 'and the Eater has been removed, then exit
  105. If Not CheckEater(EaterNum) Then
  106.     Exit Sub
  107. End If
  108.  
  109. 'randomize
  110.  
  111. Got(X) = Eaters(EaterNum).XPos
  112. Got(Y) = Eaters(EaterNum).YPos
  113. Speed = Eaters(EaterNum).Speed
  114.  
  115. MyX = Got(X)
  116. MyY = Got(Y)
  117.  
  118. 'look for several ants per turn (governed by foodrate)
  119. If frmLife.SearchForFood(MyX, MyY, Speed, Eaters(EaterNum).Direction, 1) Then
  120.     'found food
  121.     Target(X) = MyX
  122.     Target(Y) = MyY
  123.     
  124.     'move Eater to new position
  125.     Grid(Eaters(EaterNum).XPos, Eaters(EaterNum).YPos) = HereEmpty
  126.     
  127.     'draw a blank dot on the old position
  128.     Call frmLife.DrawDot(Got(X), Got(Y), vbWhite)
  129.     
  130.     'if the new position contains food, then eat it
  131.     If Grid(Target(X), Target(Y)) = WorldDetails.Ant Then
  132.         Eaters(EaterNum).FoodLevel = Eaters(EaterNum).FoodLevel + 1
  133.         
  134.         'reduce the ant population
  135.         Call KillAnt(GetAntNumber(MyX, MyY))
  136.     End If
  137.     
  138.     'move the ant eater to where the ant was
  139.     Eaters(EaterNum).XPos = MyX
  140.     Eaters(EaterNum).YPos = MyY
  141.     
  142.     Grid(MyX, MyY) = Eater
  143.     Call frmLife.DrawDot(MyX, MyY, vbCyan)
  144.     
  145.     'reduce the eaters speed
  146.     If Eaters(EaterNum).Speed > 1 Then
  147.         Eaters(EaterNum).Speed = Eaters(EaterNum).Speed - 1 'CheckRange(1, MaxSpeedEater, Eaters(EaterNum).Speed - 1)
  148.     End If
  149. Else
  150.     'just move the eater to a new position
  151.     MyX = Got(X)
  152.     MyY = Got(Y)
  153.     
  154.     'project co-ordinates to new target (in the given direction)
  155.     Select Case Eaters(EaterNum).Direction
  156.     Case Up
  157.         MyY = Got(Y) - Speed
  158.         If MyY < 0 Then
  159.             MyY = (GridSize + 1) + MyY
  160.         End If
  161.     Case Right
  162.         MyX = (Got(X) + Speed) Mod (GridSize + 1)
  163.     Case Down
  164.         MyY = (Got(Y) + Speed) Mod (GridSize + 1)
  165.     Case Left
  166.         MyX = Got(X) - Speed
  167.         If MyX < 0 Then
  168.             MyX = (GridSize + 1) + MyX
  169.         End If
  170.     End Select
  171.     
  172.     'copy over the old position
  173.     Call frmLife.DrawDot(Got(X), Got(Y), vbWhite)
  174.     Grid(Got(X), Got(Y)) = HereEmpty
  175.     
  176.     'increase the eaters speed
  177.     If Eaters(EaterNum).Speed < MaxSpeedEater Then
  178.         Eaters(EaterNum).Speed = Eaters(EaterNum).Speed + 1 'CheckRange(1, MaxSpeedEater, Eaters(EaterNum).Speed + 1)
  179.     End If
  180.     
  181.     'move to the mew position
  182.     Grid(MyX, MyY) = Eater
  183.     Eaters(EaterNum).XPos = MyX
  184.     Eaters(EaterNum).YPos = MyY
  185.     Call frmLife.DrawDot(MyX, MyY, vbCyan)
  186.  
  187.     'change the direction
  188.     Eaters(EaterNum).Direction = GetRndInt(Up, Left) '((Left - Up + 1) * Rnd + Up)
  189. End If
  190.     
  191. DoEvents
  192. Eaters(EaterNum).TickNum = Eaters(EaterNum).TickNum + 1
  193. End Sub
  194.  
  195. Public Function CheckEater(Num As Integer) As Boolean
  196. 'This will check the stats of the selected Eater and make the
  197. 'appropiate changes.
  198. Static test As Integer
  199.  
  200. CheckEater = True
  201.  
  202. If (Eaters(Num).FoodLevel >= Eaters(Num).FoodToSplit) Then 'And (Eaters(Num).FoodToSplit >= MinPropLevelEater)
  203.     'MutateEater Eater
  204.     Call MutateEater(Num)
  205. End If
  206.  
  207. If (Eaters(Num).TickNum >= Eaters(Num).KillLevel) Or (Eaters(Num).Speed = 0) Then
  208.     'delete the Eater and report that the Eater has been removed
  209.     Call KillEater(Num)
  210.     CheckEater = False
  211. End If
  212. End Function
  213.  
  214. Private Sub MutateEater(Num As Integer)
  215. 'This sub will MutateEater all the values of a Eater withing the
  216. 'mutation rate.
  217.  
  218. Dim NewVal As Integer
  219. Dim Upperbound As Integer
  220. Dim Lowerbound As Integer
  221. Dim X As Integer
  222. Dim Y As Integer
  223. Dim Being As CreatureType
  224.  
  225. If (NumOfEaters >= MaxEaters) Then
  226.     'if population pressure is too high, increase life span
  227.     Eaters(Num).FoodLevel = 0
  228.     Eaters(Num).KillLevel = Eaters(Num).KillLevel + FoodRateEater
  229.     Exit Sub
  230. End If
  231.  
  232. Being = Eaters(Num)
  233.  
  234. Upperbound = MutationRateEater
  235. Lowerbound = -MutationRateEater
  236.  
  237. 'evolve Eater with new settings and "age" the original Eater
  238. With Being
  239.     .FoodLevel = 0
  240.     .TickNum = 0
  241.     .Direction = ((Left - Up + 1) * Rnd + Up)
  242.     Upperbound = Upperbound * (GridSize / 15)
  243.     Lowerbound = Lowerbound * (GridSize / 15)
  244.     .FoodToSplit = .FoodToSplit + ((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
  245.     If (.FoodToSplit < MinPropLevelEater) Then
  246.         .FoodToSplit = MinPropLevelEater
  247.     End If
  248.     
  249.     Upperbound = MutationRateEater * (GridSize / 4)
  250.     Lowerbound = MutationRateEater * (GridSize / 4)
  251.     .KillLevel = .KillLevel + ((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
  252.     If .KillLevel > MaxLifeSpanEater Then
  253.         .KillLevel = MaxLifeSpanEater
  254.     End If
  255.     
  256.     Upperbound = MutationRateEater
  257.     Lowerbound = -MutationRateEater
  258.     .Speed = .Speed + ((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
  259.     If .Speed > MaxSpeedEater Then
  260.         .Speed = MaxSpeedEater
  261.     End If
  262. End With
  263.  
  264. 'the old Eater produces slower and searches smaller, dies faster
  265. Eaters(Num).FoodLevel = 0
  266. Eaters(Num).FoodToSplit = Eaters(Num).FoodToSplit + 1
  267. Eaters(Num).Speed = Eaters(Num).Speed - 1
  268. Eaters(Num).KillLevel = Eaters(Num).KillLevel - 1
  269. Eaters(Num).Direction = ((Left - Up + 1) * Rnd + Up)
  270.  
  271. 'New Eater details
  272. X = Eaters(EaterNum).XPos
  273. Y = Eaters(EaterNum).YPos
  274. If Not GetEmptyPos(X, Y) Then
  275.     'if there is no available space for a new Eater, then don't
  276.     'create one
  277.     Eaters(Num).FoodLevel = 0
  278.     Eaters(Num).KillLevel = Eaters(Num).KillLevel + FoodRateEater
  279.     Exit Sub
  280. End If
  281.  
  282. 'create a new Eater
  283. NumOfEaters = NumOfEaters + 1
  284. ReDim Preserve Eaters(NumOfEaters)
  285. Eaters(NumOfEaters) = Being
  286. Eaters(NumOfEaters).XPos = X
  287. Eaters(NumOfEaters).YPos = Y
  288. Grid(X, Y) = Eater
  289. Call frmLife.DrawDot(X, Y, vbBlack)
  290. End Sub
  291.  
  292. Public Function GetAntNumber(XCo As Integer, YCo As Integer) As Integer
  293. 'this returns the ant's number in a given set of co-ordinates
  294.  
  295. Dim GotNum As Integer
  296. Dim Counter As Integer
  297.  
  298. For Counter = 1 To NumOfAnts
  299.     If (Ants(Counter).XPos = XCo) And (Ants(Counter).YPos = YCo) Then
  300.         GotNum = Counter
  301.         Exit For
  302.     End If
  303. Next Counter
  304.  
  305. GetAntNumber = GotNum
  306. End Function
  307.