Imports System
Imports System.Drawing
Imports System.Collections
Imports System.IO
' Simple Herbivore.
' Strategy: This animal moves until it finds a food source and then stays there.
' This animal will run away from carnivores.
' This animal will fight agressive herbivores.
' Improvements have been made to attempt to counter the
' kittyreturns1 threat, but there is still a long way to go.
<Assembly: OrganismClass("VBAnimal.GrayH09")>
<Assembly: AuthorInformation("Nick Mosley", "graysqirl@mediaone.net")>
Namespace VBAnimal
< _
CarnivoreAttribute(False), _
MatureSize(26), _
AnimalSkin(AnimalSkinFamilyEnum.Inchworm), _
MarkingColor(KnownColor.Violet), _
MaximumEnergyPoints(10), _
EatingSpeedPoints(0), _
AttackDamagePoints(20), _
DefendDamagePoints(24), _
MaximumSpeedPoints(16), _
CamouflagePoints(0), _
EyesightPoints(30) _
> Public Class GrayH09 : Inherits Animal
Dim TargetPlant As PlantState = Nothing
'Dim DangerousAnimal As AnimalState = Nothing
Const cruisingSpeed As Integer = 5
Const runningSpeed As Integer = 10
Const fleeingSpeed As Integer = 16
Dim alternateRoute As Boolean = False
Protected Overrides Sub Initialize()
AddHandler Born, AddressOf Me.BornEvent
AddHandler Load, AddressOf Me.LoadEvent
AddHandler Idle, AddressOf Me.IdleEvent
AddHandler MoveCompleted, AddressOf Me.MoveCompletedEvent
AddHandler EatCompleted, AddressOf Me.EatCompletedEvent
AddHandler Teleported, AddressOf Me.TeleportedEvent
AddHandler ReproduceCompleted, AddressOf Me.ReproduceCompletedEvent
AddHandler DefendCompleted, AddressOf Me.DefendCompletedEvent
AddHandler Attacked, AddressOf Me.AttackedEvent
End Sub
Private Sub LoadEvent(ByVal sender As Object, ByVal e As LoadEventArgs)
If Not TargetPlant Is Nothing Then
TargetPlant = CType(LookFor(TargetPlant), PlantState)
If TargetPlant Is Nothing Then
WriteTrace("Target plant disappeared.")
End If
End If
End Sub
Private Sub IdleEvent(ByVal sender As Object, ByVal e As IdleEventArgs)
Try
If CanReproduce Then
BeginReproduction(Nothing)
End If
ScanForTargetPlant()
ScanForAttackers()
If CanEat Then
WriteTrace("Hungry.")
If Not IsEating Then
WriteTrace("Not eating: Have target plant?")
If Not TargetPlant Is Nothing Then
WriteTrace("Have target plant already.")
If TargetPlant.PercentInjured < 0.8 Or State.EnergyState = EnergyState.Deterioration Then
If WithinEatingRange(TargetPlant) Then
BeginEating(TargetPlant)
If IsMoving Then
WriteTrace("Was Moving: Stop while eating.")
StopMoving()
End If
Else
If Not IsMoving Then
WriteTrace("Not in Range of target plant: Move to it.")
BeginMoving(New MovementVector(TargetPlant.Position, runningSpeed))
Else
If alternateRoute Then
WriteTrace("Trying alternate route...")
Else
WriteTrace("Moving toward Plant...")
End If
End If
End If
Else
WriteTrace("TargetPlant is too injured to eat.")
ScanForTargetPlant()
End If
Else
WriteTrace("Don't have target plant.")
If Not ScanForTargetPlant() Then
If Not IsMoving Then
WriteTrace("No target plant found: Start Moving and Looking.")
BeginMoving(New MovementVector(New Point(OrganismRandom.Next(0, WorldWidth - 1), _
OrganismRandom.Next(0, WorldHeight - 1)), cruisingSpeed))
Else
WriteTrace("Moving and Looking...")
End If
End If
End If
Else
WriteTrace("Eating.")
If IsMoving Then
WriteTrace("Stop moving while eating.")
StopMoving()
End If
End If
Else
WriteTrace("Full: do nothing.")
If IsMoving Then
StopMoving()
End If
End If
Catch ex As Exception
WriteTrace("Error: " & ex.ToString)
End Try
End Sub
Private Function ScanForTargetPlant() As Boolean
Dim foundAnimals As ArrayList = Scan()
If foundAnimals.Count > 0 Then
Dim orgState As OrganismState
For Each orgState In foundAnimals
If TypeOf orgState Is PlantState Then
If orgState.PercentInjured < 0.8 Or State.EnergyState = EnergyState.Deterioration Then
TargetPlant = CType(orgState, PlantState)
BeginMoving(New MovementVector(orgState.Position, runningSpeed))
Return True
Else
WriteTrace("Plant too injured to eat.")
End If
End If
Next
End If
Return False
End Function
Private Function ScanForAttackers() As Boolean
Try
Dim foundAnimals As ArrayList = Scan()
If foundAnimals.Count > 0 Then
Dim orgState As OrganismState
For Each orgState In foundAnimals
If TypeOf orgState Is AnimalState Then
Dim IASa As IAnimalSpecies = orgState.Species
If IASa.IsCarnivore = True Then
If WithinAttackingRange(orgState) = True And State.PercentInjured >= 0.7 Then
WriteTrace("Carnivore in range: Flee")
'DangerousAnimal = orgState
Dim X As Integer = OrganismRandom.Next(0, WorldWidth - 1)
Dim Y As Integer = OrganismRandom.Next(0, WorldHeight - 1)
BeginMoving(New MovementVector(New Point(X, Y), fleeingSpeed))
Return True
End If
Else
If WithinAttackingRange(orgState) = True And State.EnergyState <= EnergyState.Normal And State.PercentInjured <= 0.5 And orgState.IsMature = True And IsMySpecies(orgState) = False And State.IsAlive = True Then
BeginAttacking(orgState)
End If
End If
End If
Next
End If
Return False
Catch ex As Exception
WriteTrace("Error: " & ex.ToString)
End Try
End Function
Private Sub AttackedEvent(ByVal sender As Object, ByVal e As AttackedEventArgs)
Try
If e.Attacker.IsAlive = True Then
If IsMySpecies(e.Attacker) = False Then
BeginDefending(e.Attacker)
Dim IASb As IAnimalSpecies = e.Attacker.Species
If IASb.IsCarnivore = False And WithinAttackingRange(e.Attacker) = True And State.EnergyState <= EnergyState.Normal And IsMySpecies(e.Attacker) = False And State.IsAlive = True Then
BeginAttacking(e.Attacker)
Else
If WithinAttackingRange(e.Attacker) = True And State.EnergyState <= EnergyState.Normal And State.PercentInjured <= 0.5 And IsMySpecies(e.Attacker) = False And State.IsAlive = True Then
BeginAttacking(e.Attacker)
End If
End If
End If
End If
Catch ex As Exception
WriteTrace("Error: " & ex.ToString)
End Try
End Sub
Private Sub BornEvent(ByVal sender As Object, ByVal e As BornEventArgs)
TargetPlant = Nothing
'DangerousAnimal = Nothing
End Sub
Private Sub MoveCompletedEvent(ByVal sender As Object, ByVal e As MoveCompletedEventArgs)
If e.Reason = ReasonForStop.Blocked Then
If Not TargetPlant Is Nothing Then
' If we get blocked going for a plant, try moving perpendicular to the
' direction we were moving for a while and then try again
WriteTrace("Blocked while moving toward plant: trying an alternate route.")
alternateRoute = True
Dim originalDestination As Point = e.MoveToAction.MovementVector.Destination
Dim originalVector As Vector = Vector.Subtract(originalDestination, Position)
Dim newVector As Vector = originalVector.Rotate(Math.PI / 4)
Dim unitVector As Vector = newVector.GetUnitVector()
Dim newPositionVector As Vector = unitVector.Scale(20)
Dim newPosition As Point = Vector.Add(Position, newPositionVector)
BeginMoving(New MovementVector(newPosition, cruisingSpeed))
End If
Else
If alternateRoute Then
WriteTrace("Alternate route Completed: Now try going for plant again.")
alternateRoute = False
End If
End If
End Sub
Private Sub DefendCompletedEvent(ByVal sender As Object, ByVal e As DefendCompletedEventArgs)
End Sub
Private Sub EatCompletedEvent(ByVal sender As Object, ByVal e As EatCompletedEventArgs)
If Not e.Successful Then
WriteTrace("Eating unsuccessful.")
End If
End Sub
Private Sub TeleportedEvent(ByVal sender As Object, ByVal e As TeleportedEventArgs)
End Sub
Private Sub ReproduceCompletedEvent(ByVal sender As Object, ByVal e As ReproduceCompletedEventArgs)
End Sub
' This gets called whenever your animal is being saved -- either the game is closing
' or you are being teleported. Store anything you want to remember when you are instantiated
' again in the stream.
Public Overrides Sub SerializeAnimal(ByVal m As MemoryStream)
Dim b As New BinaryWriter(m)
If Not TargetPlant Is Nothing Then
b.Write(True)
b.Write(TargetPlant.ID)
Else
b.Write(False)
End If
b.Write(alternateRoute)
End Sub
' This gets called when you are instantiated again after being saved. You get a chance
' to pull out any information that you put into the stream when you were saved
Public Overrides Sub DeserializeAnimal(ByVal m As MemoryStream)
Dim b As New BinaryReader(m)
If b.ReadBoolean() Then
TargetPlant = CType(RefreshState(b.ReadString()), PlantState)
End If
alternateRoute = b.ReadBoolean()
End Sub
End Class
End Namespace