home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 4_2005-2006.ISO / data / Zips / AntieLife_1976492262006.psc / AntieLife / LifeForm.bas < prev   
BASIC Source File  |  2006-02-25  |  15KB  |  443 lines

  1. Attribute VB_Name = "LifeForm"
  2. Option Explicit
  3.  
  4. Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
  5.  
  6. Type Creature
  7.     'attributes for life forms...
  8.     Alive As Boolean 'Initialize phenotype when Alive gets set to True.
  9.     
  10.     Chromosome As String 'Genes are stored here.
  11.     activeGene As Long 'Gene number waiting to be processed next.
  12.     geneProcessAge As Long 'Earliest age at which next gene may be processed.
  13.         
  14.     Age As Long 'Count of times this life form has been processed.
  15.     MaturityAge As Long 'Minimum age to produce offspring.
  16.     MatingAge As Long 'Minimum age to mate.
  17.     CloningAge As Long 'Minimum age to self reproduce.
  18.     CertainDeathAge As Long 'Absolute maximum life-span.
  19.     
  20.     Health As Single 'Meant to be an indiator of wellness in the range of 0="critical" to 1="thriving".
  21.     Speed As Single 'Speed of motion expected
  22.     Velocity As Single 'Speed of motion in effect
  23.     Red As Integer 'Display this level of Red. (See Note.)
  24.     Green As Integer 'Display this level of Green. (See Note.)
  25.     Blue As Integer 'Display this level of Blue. (See Note.)
  26.     'Note... Adjusted automatically before use to ensure visibility.
  27.     RGB As Long 'Stores actual calculated DisplayColor.
  28.     
  29.     Width As Single 'Pen width, or thinckness of creature.
  30.     Orientation As Single
  31.     minMates As Integer 'Minimum number of mates needed to reproduce.  The default of 0 allows self reproduction.
  32.     
  33.     NextX As Single
  34.     NextY As Single
  35.     
  36.     NearSenseAngle As Single
  37.     FarSenseAngle As Single
  38.     
  39.     'if speed is zero, nothing from here down is used.
  40.     length As Integer 'Additional length to be acquired before moving to NextX, NextY.
  41.     Energy As Double 'Energy available for use at this time.
  42.     Hungry As Double 'Below this level of energy, life form is hungry.
  43.     EnergyToMate As Double 'Mimimum Energy Needed Before Mating.
  44.     EnergyToClone As Double  'Mimimum Energy Needed Before Self Reproduction.
  45.     redEnergy As Double 'Energy from Red food, not yet ready for use.
  46.     greenEnergy As Double 'Energy from Green food, not yet ready for use.
  47.     blueEnergy As Double 'Energy from Blue food, not yet ready for use.
  48.     CloneCountDown As Integer  ' off = 0.  Clone now = 1. Higher numbers = counter.
  49.     mateRed As Integer 'Will see this level of Red as a potential mate.
  50.     mateGreen As Integer 'Will see this level of Green as a potential mate.
  51.     mateBlue As Integer 'Will see this level of Blue as a potential mate.
  52.     foodRed As Integer 'Will see this level of Red as potential food.
  53.     foodGreen As Integer 'Will see this level of Green as potential food.
  54.     foodBlue As Integer 'Will see this level of Blue as potential food.
  55. End Type
  56.  
  57. 'GridSizeX and GridSizeY define the dimensions of the grid on which the life forms live.
  58. Public Const GridSizeX = 700
  59. Public Const GridSizeY = 600
  60.  
  61. 'used in positioning the display...
  62. Public LowerX As Single
  63. Public RangeX As Single
  64. Public UpperX As Single
  65. Public LowerY As Single
  66. Public RangeY As Single
  67. Public UpperY As Single
  68. Public OffsetX As Single
  69. Public OffsetY As Single
  70. Public mulX As Double
  71. Public mulY As Double
  72.  
  73. Public ZoomFactor As Single
  74.  
  75. Public Grid(GridSizeX - 1, GridSizeY - 1) As Creature
  76. Public ListXY As String
  77. Public lifeCount As Long
  78. Public PlantCount As Long
  79. Public AntCount As Long
  80.  
  81. Private itemPosition As Long
  82.  
  83. Function setBackgroundColor()
  84.     Dim R As Integer
  85.     Dim G As Integer
  86.     Dim b As Integer
  87.     Dim bColor As Long
  88.     Dim tColor As Long 'Label text color
  89.     R = IIf(formLife.mChkInvertRed.Checked, 255, 0)
  90.     G = IIf(formLife.mChkInvertGreen.Checked, 255, 0)
  91.     b = IIf(formLife.mChkInvertBlue.Checked, 255, 0)
  92.     bColor = RGB(R, G, b)
  93.     'PictureBox background...
  94.     formLife.picLife.BackColor = bColor
  95.     'Form and label colors...
  96.     tColor = RGB(255 - R, Abs(192 - G), Abs(192 - b))
  97.     formLife.BackColor = bColor
  98.     formLife.lblAntCountLbl.BackColor = bColor
  99.     formLife.lblPlantCountLbl.BackColor = bColor
  100.     formLife.lblAntCount.BackColor = bColor
  101.     formLife.lblPlantCount.BackColor = bColor
  102.     formLife.lblAntCountLbl.ForeColor = tColor
  103.     formLife.lblPlantCountLbl.ForeColor = tColor
  104.     formLife.lblAntCount.ForeColor = tColor
  105.     formLife.lblPlantCount.ForeColor = tColor
  106. End Function
  107.  
  108. Function DisplayColor(X As Single, Y As Single) As Long
  109.     Dim R As Integer
  110.     Dim G As Integer
  111.     Dim b As Integer
  112.     With Grid(X, Y)
  113.         R = .Red
  114.         G = .Green
  115.         b = .Blue
  116.     End With
  117.     If (R = G) And (G = b) Then
  118.         R = R + 32
  119.         G = G + 32
  120.         b = b + 32
  121.     ElseIf (R < G) And (R < b) Then
  122.         G = G + 48
  123.         b = b + 48
  124.     ElseIf (G < R) And (G < b) Then
  125.         R = R + 48
  126.         b = b + 48
  127.     ElseIf (b < R) And (b < G) Then
  128.         R = R + 48
  129.         G = G + 48
  130.     ElseIf (R > G) And (R > b) Then
  131.         R = R + 64
  132.     ElseIf (G > R) And (G > b) Then
  133.         G = G + 64
  134.     ElseIf (b > R) And (b > G) Then
  135.         b = b + 64
  136.     End If
  137.     'Note: RGB values may be higher than 255. That's why Abs() is used here...
  138.     If formLife.mChkInvertRed.Checked Then R = Abs(255 - R)
  139.     If formLife.mChkInvertGreen.Checked Then G = Abs(255 - G)
  140.     If formLife.mChkInvertBlue.Checked Then b = Abs(255 - b)
  141.     DisplayColor = RGB(R, G, b)
  142. End Function
  143.  
  144. Sub AddItemXY(X As Single, Y As Single)
  145.     Static entry As String
  146.     entry = makeEntryXY(X, Y)
  147.     If InStr(ListXY, entry) Then Exit Sub
  148.     ListXY = ListXY + entry
  149.     lifeCount = lifeCount + 1
  150.     If Grid(X, Y).Speed <= 0 Then
  151.         PlantCount = PlantCount + 1
  152.     Else
  153.         AntCount = AntCount + 1
  154.     End If
  155. End Sub
  156.  
  157. Function makeEntryXY(X As Single, Y As Single)
  158.     makeEntryXY = "[" + Hex(inGridX(CLng(X))) + "," + Hex(inGridY(CLng(Y))) + "]"
  159. End Function
  160.  
  161. Function RemoveItemEntry(entry) As Boolean
  162.     Static i As Long
  163.     If entry = "" Then
  164.         RemoveItemEntry = False
  165.         Exit Function
  166.     End If
  167.     i = InStr(ListXY, entry)
  168.     If i = 0 Then
  169.         RemoveItemEntry = False
  170.         Exit Function
  171.     End If
  172.     If itemPosition > i Then itemPosition = itemPosition - Len(entry)
  173.     ListXY = Left(ListXY, i - 1) + Mid(ListXY, i + Len(entry))
  174.     lifeCount = lifeCount - 1
  175.     RemoveItemEntry = True
  176.     If itemPosition > 1 Then itemPosition = itemPosition - 1
  177. End Function
  178.  
  179. Sub RemoveItemXY(X As Single, Y As Single)
  180.     If RemoveItemEntry(makeEntryXY(X, Y)) Then
  181.         If Grid(X, Y).Speed <= 0 Then
  182.             PlantCount = PlantCount - 1
  183.         Else
  184.             AntCount = AntCount - 1
  185.         End If
  186.     End If
  187. End Sub
  188.  
  189. Sub GetNextItemXY(ByRef X As Single, ByRef Y As Single)
  190.     Static ii As Long
  191.     Static entry
  192.     If ListXY < "[" Then Exit Sub
  193.     itemPosition = InStr(itemPosition + 1, ListXY, "[")
  194.     If itemPosition = 0 Then itemPosition = 1 'Return to first item
  195.     ii = InStr(itemPosition, ListXY, "]")
  196.     entry = Mid(ListXY, itemPosition, ii - itemPosition + 1)
  197.     ii = InStr(entry, ",")
  198.     X = Val("&h" + Mid(entry, 2))
  199.     Y = Val("&h" + Mid(entry, ii + 1))
  200. End Sub
  201.  
  202. Sub ResetItemXY(X As Single, Y As Single)
  203.     Dim tmp As Creature
  204.     Grid(X, Y) = tmp
  205. End Sub
  206.  
  207. Sub reBirthItemXY(X As Single, Y As Single)
  208.     Dim baby As Creature
  209.     baby = Grid(X, Y)
  210.     BirthItemXY X, Y
  211.     With Grid(X, Y)
  212.         .Energy = baby.Energy
  213.         .redEnergy = baby.redEnergy
  214.         .greenEnergy = baby.greenEnergy
  215.         .blueEnergy = baby.blueEnergy
  216.         .Orientation = baby.Orientation
  217.     End With
  218. End Sub
  219. Sub BirthItemXY(X As Single, Y As Single)
  220.     Dim Genes As String
  221.     Dim baby As Creature
  222.     Genes = Grid(X, Y).Chromosome
  223.     Grid(X, Y) = baby
  224.     With Grid(X, Y)
  225.         .Chromosome = Genes
  226.         .Alive = True
  227.         .Health = 1 'Thriving
  228.         'Set to default phenotype...
  229.         .Red = 77
  230.         .Green = 99
  231.         .Blue = 82
  232.         .mateRed = 77
  233.         .mateGreen = 99
  234.         .mateBlue = 82
  235.         .foodRed = 77
  236.         .foodGreen = 99
  237.         .foodBlue = 82
  238.         .CertainDeathAge = 23
  239.         .MatingAge = 0
  240.         .CloningAge = 0
  241.         .MaturityAge = 0
  242.         .Hungry = 0
  243.         .Orientation = Rnd * pi * 2 'random facing direction
  244.         .activeGene = 0 'Reset to default active gene.
  245.         'Process a few genes normally (if available) before birth...
  246.         processNextGeneXY X, Y
  247.         processNextGeneXY X, Y
  248.         processNextGeneXY X, Y
  249.         processNextGeneXY X, Y
  250.         processNextGeneXY X, Y
  251.         'Make sure item will display...
  252.         If .Width < 0.1 Then .Width = 1
  253.         .NextX = X + Cos(.Orientation) * .Width
  254.         .NextY = Y + Sin(.Orientation) * .Width
  255.         If .CertainDeathAge < 1 Then .CertainDeathAge = 1
  256.         If .Width > 33 Then .Width = 33
  257.         .RGB = DisplayColor(X, Y)
  258.     End With
  259.     AddItemXY X, Y 'Add coordinates of newborn to list
  260. End Sub
  261.  
  262. Function biteItemXY(X As Single, Y As Single, Optional biteCount As Single = 1) As Boolean
  263.     ' take a bite out of grid(x,y) and return a boolean indicating whether or not .health is still greater than 0.
  264.     Dim sTmp As Single
  265.     With Grid(X, Y)
  266.         'note: biteCount could also simulate mouth size.
  267.         sTmp = 1 / (2 + .Width + .length) * biteCount
  268.         .Health = .Health - sTmp
  269.         biteItemXY = (.Health > 0)
  270.     End With
  271. End Function
  272.  
  273. Function healItemXY(X As Single, Y As Single) As Boolean
  274.     ' Restore some health to grid(x,y) and return a boolean indicating whether or not .health has reached or exceded 1.
  275.     Dim sTmp As Single
  276.     If Grid(X, Y).Health >= 1 Then
  277.         healItemXY = True
  278.         Exit Function
  279.     Else
  280.         With Grid(X, Y)
  281.             sTmp = 1 / (3 + .Width + .length)
  282.             If sTmp >= .Energy / 2 Then sTmp = .Energy / 2 + 0.1
  283.             .Health = .Health + sTmp
  284.             .Energy = .Energy - sTmp 'Energy used to heal.
  285.             healItemXY = (.Health >= 1)
  286.         End With
  287.     End If
  288. End Function
  289.  
  290. Function getNextGeneValueXY(X As Single, Y As Single)
  291.     Dim t() As Byte
  292.     With Grid(X, Y)
  293.         If .activeGene < 1 Then .activeGene = 1
  294.         If .activeGene > Len(.Chromosome) Then
  295.             getNextGeneValueXY = 0
  296.             Exit Function
  297.         End If
  298.         t() = Mid(.Chromosome, .activeGene, 1)
  299.         getNextGeneValueXY = PickGeneValue(t(0), t(1))
  300.         .activeGene = .activeGene + 1
  301.         'On Error GoTo 0
  302.     End With
  303.     Exit Function
  304. End Function
  305.  
  306. Sub processNextGeneXY(X As Single, Y As Single)
  307.     Dim value As Integer
  308.     Dim mode As Integer
  309.     With Grid(X, Y)
  310.         If .Age < .geneProcessAge Then Exit Sub
  311.         If .activeGene > Len(.Chromosome) - 2 Then Exit Sub
  312.         mode = getNextGeneValueXY(X, Y)
  313. '        If mode > 32 Then mode = mode - 32 'Don't waste genes.
  314.         value = getNextGeneValueXY(X, Y)
  315.         Select Case mode
  316.         Case 1
  317.             .Red = Abs(.Red + value)
  318.         Case 2
  319.             .mateRed = Abs(.mateRed + value)
  320.         Case 3
  321.             .Green = Abs(.Green + value)
  322.         Case 4
  323.             .mateGreen = Abs(.mateGreen + value)
  324.         Case 5
  325.             .Blue = Abs(.Blue + value)
  326.         Case 6
  327.             .mateBlue = Abs(.mateBlue + value)
  328.         Case 7
  329.             .foodRed = Abs(.foodRed + value)
  330.         Case 8
  331.             .foodGreen = Abs(.foodGreen + value)
  332.         Case 9
  333.             .foodBlue = Abs(.foodBlue + value)
  334.         Case 10
  335.             .Speed = .Speed + 0.01 * value
  336.         Case 11
  337.             .MaturityAge = .MaturityAge + value
  338.         Case 12
  339.             .Hungry = .Hungry + value
  340.         Case 13
  341.             .CertainDeathAge = .CertainDeathAge + value * 3
  342.         Case 14
  343.             .CertainDeathAge = .CertainDeathAge + value
  344.             .Speed = .Speed + Log(value + 1) 'alternate animal gene
  345.         Case 15
  346.             .CloningAge = .CloningAge + value
  347.         Case 16
  348.             .MatingAge = .MatingAge + value
  349.         Case 17
  350.             .length = greaterOf(.length / 2 + value, .length + 1)
  351.         Case 18
  352.             .minMates = 1
  353.         Case 19
  354.             .activeGene = .activeGene + value
  355.         Case 20
  356.             .geneProcessAge = .Age + value + 2
  357.         Case 21
  358.             .NearSenseAngle = .NearSenseAngle + value / 8 - 1
  359.         Case 22
  360.             .FarSenseAngle = .FarSenseAngle + value / 8 - 1
  361.         Case 23
  362.             .Red = Abs(.Red + value * 2 - 16)
  363.         Case 24
  364.             .mateRed = Abs(.mateRed + value * 2 - 16)
  365.         Case 25
  366.             .Green = Abs(.Green + value * 2 - 16)
  367.         Case 26
  368.             .mateGreen = Abs(.mateGreen + value * 2 - 16)
  369.         Case 27
  370.             .Blue = Abs(.Blue + value * 2 - 16)
  371.         Case 28
  372.             .mateBlue = Abs(.mateBlue + value * 2 - 16)
  373.         Case 29
  374.             .foodRed = Abs(.foodRed + value * 2 - 16)
  375.         Case 30
  376.             .foodGreen = Abs(.foodGreen + value * 2 - 16)
  377.         Case 31
  378.             .foodBlue = Abs(.foodBlue + value * 2 - 16)
  379.         Case 32
  380.             .Width = .Width + 1
  381.         Case 33
  382.             .Red = Abs(.Red - value)
  383.         Case 34
  384.             .mateRed = Abs(.mateRed - value)
  385.         Case 35
  386.             .Green = Abs(.Green - value)
  387.         Case 36
  388.             .mateGreen = Abs(.mateGreen - value)
  389.         Case 37
  390.             .Blue = Abs(.Blue - value)
  391.         Case 38
  392.             .mateBlue = Abs(.mateBlue - value)
  393.         Case 39
  394.             .foodRed = Abs(.foodRed - value)
  395.         Case 40
  396.             .foodGreen = Abs(.foodGreen - value)
  397.         Case 41
  398.             .foodBlue = Abs(.foodBlue - value)
  399.         End Select
  400.         'On Error GoTo 0
  401.     End With
  402.     Exit Sub
  403. End Sub
  404.  
  405. Function PickGeneValue(gM, gP)  'Decides which of two alleles to use.
  406.     PickGeneValue = IIf(gM And &H80, IIf(gP And &H80, (gM Or gP) And &H7F, gM And &H7F), IIf(gP And &H80, gP And &H7F, (gM And gP) And &H7F))
  407. End Function
  408.  
  409. Function greaterOf(A, b)
  410.     greaterOf = IIf(A > b, A, b)
  411. End Function
  412.  
  413. Function lesserOf(A, b)
  414.     lesserOf = IIf(A < b, A, b)
  415. End Function
  416.  
  417. Sub firstLife(Optional ByVal number As Long = 1)      '** NEVER use Variant
  418.     Dim baby As Creature
  419.     Dim X As Single
  420.     Dim Y As Single
  421.     Dim i As Long           '** NEVER use Variant
  422.     Dim ii As Long          '** NEVER use Variant
  423.     Dim t() As Byte
  424.     Dim s As String
  425.     Dim ct As String
  426.     Dim errorCount As Long
  427.     
  428.     ReDim t(3)
  429.     t(0) = 10 'Speed adjust mode... to produce an animal.
  430.     t(1) = 10
  431.     t(2) = 3 'Adjustment value
  432.     t(3) = 3
  433.     ct = Left(t(), 1)
  434.     
  435.     If number < 0 Then
  436.         baby.Chromosome = t()
  437.     End If
  438.     If (formLife.mChkQuickStart.Checked Xor breakRules) Then
  439.         'add some random genes...
  440.         s = Chr(Int(Rnd * 7lueDim errorCount As Long
  441.    errorCount As Long
  442.    errorCount As Long
  443.    of (y As Crel