Saturday, August 23, 2008

Adventures in F# - A port of the simplest Herbivore

Following the past of my previous post, this time I present a barebones stripped-down version of the Herbivore that ships with the Terrarium SDK. The code is will equip you with a compilable herbivore, which however does nothing but sit in your terrarium. Just like in the previous post, I did not strip out some of the inline comments, which may be helpful to some of us.

So here comes the code ...
namespace CBHerbivore

#light
#r "E:\\Development\\my fsharp\\Terrarium\\Terrarium\\OrganismBase.dll"

open System;;
open System.Drawing;;
open System.Collections;;
open System.IO;;
open OrganismBase;;



//Sample Herbivore
//The following Assembly attributes must be applied to each
//Organism Assembly


[<assembly: OrganismClass("CBHerbivore.CBHerbivore")>]
//The class that derives from Animal
[<assembly: AuthorInformation("Christian Bitter", "x@y.z")>]
//It's an herbivore
[<CarnivoreAttribute(false)>]

//This value must be between 24 and 48, 24 means faster
//reproduction
//while 48 would give more defense and attack power
//Make it smaller for reproduction

[<MatureSize(26)>]

//AnimalSkin = AnimalSkinFamilyEnum.Beetle, you can be a Beetle
//an Ant, a Scorpion, an Inchworm, or a Spider
//MarkingColor = KnownColor.Red, you can choose to mark your
//creature with a color.
//This does not affect appearance in the game.

[<AnimalSkin(AnimalSkinFamily.Beetle)>]
[<MarkingColor(KnownColor.Red)>]

//You get 100 points to distribute among these attributes to define
//what your organism can do.
//Choose them based on the strategy your organism will use.
//This organism hides and has good eyesight to find plants.

[<MaximumEnergyPoints(0)>]

//Don't need to increase this as it just sits next to plants

[<EatingSpeedPoints(0)>] //Ditto
[<AttackDamagePoints(0)>] //Doesn't ever attack
[<DefendDamagePoints(0)>]
//This attribute changes the skin of our herbivore,
//which is now going to be a beetle.
//Point Based Attributes
//Doesn't even defend

[<MaximumSpeedPoints(0)>] //Doesn't need to move quickly
[<CamouflagePoints(50)>] //Try to remain hidden
[<EyesightPoints(50)>] //Need this to find plants better


type CBHerbivore () =  
  inherit Animal() 
  override sh.Initialize() = () 
  override sh.SerializeAnimal (m:MemoryStream) = () 
  override sh.DeserializeAnimal (m:MemoryStream) = ()

As you may see, the main difference between this herbivore and the previous plant, is the base type our herbivore type needs to implement, and some attributes declared on our animal. In this case the abstract Animal class, declares 3 abstract members (Initialize, SerializeAnimal, DesirializeAnimal), which need to be implemented by our herbivor. Attributes control some of the properties of our herbivore, such as its moving speed, etc. 

Again, if you compile this type and introduce it to your terrarium, do not be surprised, to see, that the animal is stiff and not moving like an animal during hibernation. This is due to not implemented event handlers. These event handlers are responsible for your animal's interaction with its environment.

No comments: