PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/SDK/Skeletons/CSHerbivore.cs

#
C# | 190 lines | 120 code | 15 blank | 55 comment | 18 complexity | ecadf3894c310b1b8411bb956e47d03b MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.IO;
  5. using OrganismBase;
  6. // Sample Herbivore
  7. // Strategy: This animal moves until it finds a food source and then stays there
  8. // The following Assembly attributes must be applied to each Organism Assembly
  9. [assembly: OrganismClass("CSharpCreatures.SimpleHerbivore")] // The class that derives from Animal
  10. [assembly: AuthorInformation("Your Name", "someone@microsoft.com")] // Provide Author Information
  11. // Author Name, Email
  12. namespace CSharpCreatures
  13. {
  14. // It's an herbivore
  15. [CarnivoreAttribute(false)]
  16. // This value must be between 24 and 48, 24 means faster reproduction
  17. // while 48 would give more defense and attack power
  18. // Make it smaller for reproduction
  19. [MatureSize(26)]
  20. // AnimalSkin = AnimalSkinFamilyEnum.Beetle, you can be a Beetle
  21. // an Ant, a Scorpion, an Inchworm, or a Spider
  22. // MarkingColor = KnownColor.Red, you can choose to mark your
  23. // creature with a color. This does not affect appearance in
  24. // the game.
  25. [AnimalSkin(AnimalSkinFamily.Beetle)]
  26. [MarkingColor(KnownColor.Red)]
  27. // Point Based Attributes
  28. // You get 100 points to distribute among these attributes to define
  29. // what your organism can do. Choose them based on the strategy your organism
  30. // will use. This organism hides and has good eyesight to find plants.
  31. [MaximumEnergyPoints(0)] // Don't need to increase this as it just sits next to plants
  32. [EatingSpeedPoints(0)] // Ditto
  33. [AttackDamagePoints(0)] // Doesn't ever attack
  34. [DefendDamagePoints(0)] // Doesn't even defend
  35. [MaximumSpeedPoints(0)] // Doesn't need to move quickly
  36. [CamouflagePoints(50)] // Try to remain hidden
  37. [EyesightPoints(50)] // Need this to find plants better
  38. public class SimpleHerbivore : Animal
  39. {
  40. PlantState targetPlant = null; // The current plant we're going after
  41. // Set up any event handlers that we want when first initialized
  42. protected override void Initialize()
  43. {
  44. Load += new LoadEventHandler(LoadEvent);
  45. Idle += new IdleEventHandler(IdleEvent);
  46. }
  47. // First event fired on an organism each turn
  48. void LoadEvent(object sender, LoadEventArgs e)
  49. {
  50. try
  51. {
  52. if(targetPlant != null)
  53. {
  54. // See if our target plant still exists (it may have died)
  55. // LookFor returns null if it isn't found
  56. targetPlant = (PlantState) LookFor(targetPlant);
  57. }
  58. }
  59. catch(Exception exc)
  60. {
  61. // WriteTrace is useful in debugging creatures
  62. WriteTrace(exc.ToString());
  63. }
  64. }
  65. // Fired after all other events are fired during a turn
  66. void IdleEvent(object sender, IdleEventArgs e)
  67. {
  68. try
  69. {
  70. // Our Creature will reproduce as often as possible so
  71. // every turn it checks CanReproduce to know when it
  72. // is capable. If so we call BeginReproduction with
  73. // a null Dna parameter to being reproduction.
  74. if(CanReproduce)
  75. BeginReproduction(null);
  76. // Check to see if we are capable of eating
  77. // If we are then try to eat or find food,
  78. // else we'll just stop moving.
  79. if(CanEat && !IsEating)
  80. {
  81. // If we have a Target Plant we can try
  82. // to either eat it, or move towards it.
  83. // else we'll move to a random vector
  84. // in search of a plant.
  85. if(targetPlant != null)
  86. {
  87. // If we are within range start eating
  88. // and stop moving. Else we'll try
  89. // to move towards the plant.
  90. if(WithinEatingRange(targetPlant))
  91. {
  92. BeginEating(targetPlant);
  93. if(IsMoving)
  94. StopMoving();
  95. }
  96. else
  97. {
  98. if(!IsMoving)
  99. BeginMoving(new MovementVector(targetPlant.Position, 2));
  100. }
  101. }
  102. else
  103. {
  104. // We'll try try to find a target plant
  105. // If we don't find one we'll move to
  106. // a random vector
  107. if(!ScanForTargetPlant())
  108. {
  109. if(!IsMoving)
  110. {
  111. int RandomX= OrganismRandom.Next(0, WorldWidth - 1);
  112. int RandomY= OrganismRandom.Next(0, WorldHeight - 1);
  113. BeginMoving(new MovementVector(new Point(RandomX,RandomY), 2));
  114. }
  115. }
  116. }
  117. }
  118. else
  119. {
  120. // Since we are Full or we are already eating
  121. // We should stop moving.
  122. if(IsMoving)
  123. StopMoving();
  124. }
  125. }
  126. catch(Exception exc)
  127. {
  128. WriteTrace(exc.ToString());
  129. }
  130. }
  131. // Looks for target plants, and starts moving towards the first one it finds
  132. bool ScanForTargetPlant()
  133. {
  134. try
  135. {
  136. // Find all Plants/Animals in range
  137. ArrayList foundAnimals = Scan();
  138. // If we found some Plants/Animals lets try
  139. // to weed out the plants.
  140. if(foundAnimals.Count > 0)
  141. {
  142. foreach(OrganismState organismState in foundAnimals)
  143. {
  144. // If we found a plant, set it as our target
  145. // then begin moving towards it. Tell the
  146. // caller we have a target.
  147. if(organismState is PlantState)
  148. {
  149. targetPlant = (PlantState) organismState;
  150. BeginMoving(new MovementVector(organismState.Position, 2));
  151. return true;
  152. }
  153. }
  154. }
  155. }
  156. catch(Exception exc)
  157. {
  158. WriteTrace(exc.ToString());
  159. }
  160. // Tell the caller we couldn't find a target
  161. return false;
  162. }
  163. // This gets called whenever your animal is being saved -- either the game is closing
  164. // or you are being teleported. Store anything you want to remember when you are
  165. // instantiated again in the stream.
  166. public override void SerializeAnimal(MemoryStream m)
  167. {
  168. }
  169. // This gets called when you are instantiated again after being saved. You get a
  170. // chance to pull out any information that you put into the stream when you were saved
  171. public override void DeserializeAnimal(MemoryStream m)
  172. {
  173. }
  174. }
  175. }