Resources

Friday, May 21, 2010

Creating a food object

Today, we'll be creating a food object, a decorated cake.

You will need:
  • These resources (put the wav file in the C3 or DS Sounds subdirectory, and the c16 file in the Images one.)
  • An open text editor (anything from emacs to notepad is fine, but don't use things like word, they add binary garbage to the file that will confuse the engine.)
  • Some co-ordinates for where you want the object to first appear (the previous tutorial details how to get these.)

The install script injects your object in the world, or an instance thereof, and defines some properties about it. Be careful, when you've set the behaviour, just an install script won't work, you need to have appropriate action scripts for the behaviour. We'll get to behaviour in a minute. First, here's the code in full. Note that all numeric values are positive integers (whole numbers) unless specified otherwise.

new: simp 2 11 29501 "tutorial_cake" 1 0 2000
attr 195
bhvr 48
elas 5
accg 3
aero 10
fric 50
perm 60
mvto 2412 9086

The first line determines this is an install script for a simple object, that it is a food (2 11) with the unique identifier 29501, that its sprite is "tutorial_cake", which has 1 image, and that it is using the first image. The type and unique identifier, or classifier, tell an objects what scripts to use. If two objects use the same classifier, and both have an eat script, there will be a conflict, and errors. The last number on this line determines where the object  sits, depth-wise, in 2.5 dimensional space. It is usally OK to leave this at 2000, if you find your object is hidden, or hiding other objects, you can try experimenting with different values.

The second line determines the attributes of an object (what can carry it, invisibilty etc.) and the third line determines behaviour (what can be done to it by creatures and the hand.) attr 195 means creatures and the hand can carry this object, that it can be affected by collisions, and that it can be affected by other types of physics. bhvr 48 means this object can be picked up and eaten by creatures. This means the object will require an eat script, but there is no need for a pick up script, unless you want something special to happen when a creature picks up the object. Appropriate attr and bhvr numbers can sometimes be difficult to determine by hand, so there is this handy calculator that runs on Windows.

The next few lines determine how physics affects the object. elas is elasticity, and determines how much an object will bounce when hitting a surface. accg is acceleration due to gravity, the only property here which accepts a float or fractional number. There is little reason to change the value. aero is air resistance, and its value should be somewhat proportionate to the size of an object. fric is friction, and determines deceleration when the object is touching a surface. Finally, perm is permeability, and determines which doors or openings an object can pass through, according to that door or opening's own permeability.
 The last line determines where the object will be placed. Change 2412 9086 to your co-ordinates.

Now it's time to write the eat script!

scrp 2 11 29501 12
snde "tuea"
stim writ from 79 1
stim writ from 77 1
stim writ from  0.5
kill ownr
endm

The first line tells this us this is a script for objects of the specifier classifier. 12 is the number for eat scripts.

The second line just plays a sound. Don't forget to enclose the sound name in quotes, and never include the filename.

The third line makes our object do what food is supposed to do, provide nutrients to a creature. What those nutrients are depends on a creature's genome. 79 is the stimulus for eating food, and 1 is the amount. The amount can be an integer, or a decimal value such as 0.75. 77 is the stimulus for eating seeds, and usually provides starch.

The fourth line kills the object after it has been eaten, and the last line just tells the engine that the script is over.

Finally we will write a remove script. These are pretty simple.

enum 2 11 29501 kill targ next
scrx 2 11 29501 12

Any code in between enum classifier and next will perform an action on all objects which have that classifier. scrx removes the specified classifier specific script, especially useful for dealing with clashes.

Save the text file as "tutorial_cake.cos" somewhere that is accessible.

All that's left is to test the object. We won't be compiling an agent in this lesson, so you should think about how to inject the object. A good program to use is the CAOS Tool from the CDN, which also serves as an IDE (development environment) if you wish to abandon standard text editors.

If all has gone well, you will have coded a fully functioning agent, but if you are having problems, don't hesitate to leave a comment!

No comments:

Post a Comment