Showing posts with label caos. Show all posts
Showing posts with label caos. Show all posts

Saturday, 10 May 2025

Mating behaviour in C3 critters

Mating in C3 critters is a special state which is typically controlled by sex drive (OV21) increasing past a threshold (when they are old enough), and by a flag being set - although food is considered more important.

The normal logic flow in the timer script

 Typically, mating is switched on by OV00 (State) being 6, and it is an intermediate step in the normal logic flow in the timer of most C3 critters - which typically follows this pattern:

  1. increase age
  2. decrease energy
  3. if old enough, increase sex drive
  4. drown in water - lose energy rapidly
  5. if low on energy (sometimes also OR if too old) flag for death
  6. if hungry flag to get food
  7. if sex drive high, flag to mate
  8. obstacle checking - sometimes also water-shyness
  9. if flagged for death go to die subroutine
  10. if flagged to get food, get food
  11. if flagged to mate, mate
  12. if flagged to roam, roam

The logic flow in the timer script being arranged around two phases - first flagging, then checking the flags - is an example of a behaviour tree. This has the benefits of avoiding conflicts in behaviour, creating a predictable flow, allowing for lifelike complexity (flagging for multiple behaviours allows the system to choose between them with DOIF logic), while being performance friendly.

Mating strategies

Interestingly, some critters have male and female types (OV06 being 0 or 1) and find an opposite sex mate to impregnate (reusing some code from the hunting food behaviour, as in the hedgehog and the dragonfly), and some simply reproduce parthenogenetically (without needing to find or hunt a mate) when their reproductive drive is past a certain threshold (and they have enough energy), such as the hoppity.  Additionally, even some critters that find mates and get pregnant only use pregnancy as a binary state (pregnant or not) rather than having a gestation period.

Reproduction Step-by-Step: a Hedgehog in Love

Let’s walk through how a hedgehog goes from carefree youth to proud parent:

1. Maturity and Sex Drive

  • The hedgehog ages via the ov01 counter.

  • Once it passes age 1000 and enters adulthood (ov05 = 2), it starts building sex drive (ov20).

  • This happens gradually, simulating the onset of reproductive maturity.

Fun detail: The "youth" hedgehog is actually removed and replaced by an adult version—a trick to avoid bounding box errors.

2. Seeking a Mate

  • When sex drive (ov20) exceeds 200, the hedgehog changes state to "Find Mate" (ov00 = 2).

  • It searches nearby using genus and species markers (ov47, ov48, ov49) for a compatible mate of the opposite sex.

3. Mating Process

  • If a suitable mate is found, the state changes to "Mate" (ov00 = 6). If a mate is not found, the hedgehog returns to roaming behaviour. An error in this logic, fixed in the Norn Woodland Fix Patch, is that ov16 should not be set to null twice before calling the find subroutine, and that hedgehogs are curiously undiscerning of their target's gender when seeking a mate.

  • The mate subroutine verifies gender compatibility and, if successful, sets the pregnancy counter (ov70 = 1) for females.

  • The sex drive (ov20) is reset to 0 after mating.

4. Pregnancy and Birth

  • The pregnancy counter (ov70) increases over time.

  • When it exceeds 10, the hedgehog enters "Give Birth" state (ov00 = 7).

  • The layg subroutine handles the birthing process, spawning new baby hoglets.

Once their part in the mating ritual is finished, the parent hedgehogs return to roaming (ov00 = 0), ready to begin the cycle again.

Key Variables for Reproduction

  • ov06 – Gender: 0 = female, 1 = male

  • ov00 – State:

    • 0 = Roam

    • 2 = Find Mate

    • 6 = Mate

    • 7 = Give Birth

    • 99 = Die

  • ov20 – Sex drive level (increases with age and plays a role in determining when a hedgehog seeks a mate).

  • ov70 – Pregnancy counter: When it’s greater than 0, the hedgehog is pregnant.

  • ov01 – Age counter: When the hedgehog reaches a certain age (1000), the script starts checking for mating behaviour.

  • ov16 – Target (food or mate): Used to track the hedgehog’s current mate.

  • ov05 – Life stage (1 = youth, 2 = adult).

  • ov02 – Energy: Used for survival but also affects mating behaviour. If low, the hedgehog seeks food.

  • ov73 – Hunger threshold: If energy drops below this, the hedgehog seeks food.

Visualizing the Process:

  • Roam (ov00 = 0) → Find Mate (ov00 = 2) → Mate (ov00 = 6) → Pregnancy (ov70 1-10) → Give Birth (ov00 = 7) → Roam (ov00 = 0)

This flow ensures that hedgehogs mature, seek mates, reproduce, and care for their young, all while adhering to a life cycle that mirrors natural behaviours. Note that this is laser-focused on only the reproductive cycle, and seeking food takes priority in the hedgehog's mind - it cannot search for a mate until it has first checked if it is hungry.

 Bugs 

One particularly odd case is the dragonfly, whose mating behaviour appears to be a bit of a bug—literally and figuratively. While it starts the process of finding a mate using the same targeting logic as other critters, it sometimes treats them as prey instead of a partner—approaching the intended mate only to eat them rather than complete the mating process. This quirky behaviour likely stems from overlapping use of the ov16 target variable, the fact that dragonflies hunt their own whole genus, the fact that dragonflies are more likely to be in contact with energy-draining water bodies, and a bug in state transitions, causing the dragonfly’s natural instincts to blur in a way that's more predatory than romantic. This bug is fixed in the Steam and GOG.com editions of Creatures Exodus.

Conclusion: Added realism and depth

While the mating logic in Creatures 3 operates entirely behind the scenes, it plays a fun and crucial role in making the ecosystem feel alive, real and self-sustaining. It’s not something players typically see directly—but it quietly governs when critters reproduce, how they prioritise needs, and how population dynamics shift over time, such as when a norn develops a taste for hedgehogs.

At the heart of this system is a behaviour tree structure: a two-phase loop where the critter first flags possible actions based on internal variables (like hunger or sex drive), and then acts based on priority. This design avoids conflicting behaviours, keeps things running efficiently, and allows complex decision-making to emerge from relatively simple rules.

This kind of background scripting, although largely hidden from view, adds realism and complexity to your creatures’ world—creating behaviours that feel organic, even when driven by simple variables and checking a list of possible states.

Sunday, 4 May 2025

Hunger and Hunting in C3 Critters

Eating in C3 critters is a special state which allows them to keep living, and is a multi-step process beginning with low energy triggering the search, searching for food, finding food and hunting for food.

The normal logic flow in the timer script

Typically, getting food is switched on by OV00 (State) being 1, and it is the first step in the normal logic flow in the timer of most C3 critters - which typically follows this pattern:

  1. increase age
  2. decrease energy
  3. if old enough, increase sex drive
  4. drown in water - lose energy rapidly
  5. if low on energy (sometimes also OR if too old) flag for death
  6. if hungry flag to get food
  7. if sex drive high, flag to mate
  8. obstacle checking - sometimes also water-shyness
  9. if flagged for death go to die subroutine
  10. if flagged to get food, get food
  11. if flagged to mate, mate
  12. if flagged to roam, roam 

The logic flow in the timer script being arranged around two phases - first flagging, then checking the flags - is an example of a behaviour tree. This has the benefits of avoiding conflicts in behaviour, creating a predictable flow, allowing for lifelike complexity (flagging for multiple behaviours allows the system to choose between them with DOIF logic), while being performance friendly.

The gfod Subroutine: Your Critter’s Inner Forager

When it’s time to eat, the gfod (get food) subroutine takes over. It’s more than a simple “go to food” instruction—gfod is a nest of smaller routines: find, hunt, vect, anim, and move, all working together to give your critter a believable foraging pattern, while avoiding the common coding error of repeating yourself.

Let’s walk through the full food-finding journey:

Step 0.5: The Critter Gets Hungry

Every time the timer runs, the critter’s energy (OV02) drops by 1. If it falls below the hunger threshold (typically stored in another variable, OV73), the critter flags itself as needing food—and gfod is called into action.

Step 1: gfod — Search Phase Begins

This is where the critter starts searching for food:

  • Temporary variables (VA47, VA48, VA49) define what kind of food it’s looking for—based on family, genus, and species. A 0 means “any”.

  • If there’s already a food target saved in OV16, the critter jumps straight to hunting it with hunt.

  • Otherwise, it calls the find subroutine to scout for the nearest edible object.

Bonus: Some critters, like hedgehogs, have varied diets—gfod can be expanded to look for secondary food types if the favourite isn’t found. (Just be careful they don’t accidentally target themselves if the critter is looking for its own genus in general.)

Step 2: find — Locating the Nearest Snack

In this subroutine, the critter:

  • First sets up a temporary variable to hold a massive number (VA99 = 99999999) and then changes that to be the shortest distance found from a target food item (VA58).

  • Scans nearby objects using esee, filtering by the desired food types.

  • Calculates the squared distance to each object with the Pythagorean theorem.

  • If something closer is found, it updates the new, closer target (VA58) and sets OV16 accordingly.

  • If nothing is found, the critter gives up—for now—and sets the target to null before changing its direction.

Step 3: hunt — Move to Target and Eat

Once a target is locked in, the critter enters hunting mode:

  • It checks that the target (OV16) is still valid and hasn't disappeared.

  • It figures out the direction to move by comparing its own coordinates with the target’s, adjusting movement vectors (OV10 and OV11) accordingly.

  • When it reaches the target—touching it—the critter switches its state to the next one in the main logic's behaviour tree and tells the food it’s been eaten (mesg writ OV16 12) and adjusts its own energy. (targ ownr subv ov02 ov72)

Ants behave a bit differently here: instead of eating on the spot, they pick up the food (OV18 tracks the carried food) and head back to the nest, with a message (mesg writ OV16 4) sent to the food to signal collection.

Step 4. Return to roaming

The critter reuses a little roaming behaviour after it has been hungry, got food, found food, and hunted food successfully to randomly change direction and start moving - and the behaviour tree will continue on with other activities.

Conclusion: A Background Mechanic With Quiet Complexity

While critters eating is a simple background function in Creatures 3, it's a surprisingly intricate part of what keeps critters behaving believably. From hunger detection to food targeting and pursuit, the eating system is built with clear logic, modular structure, and enough flexibility to support varied diets, differences between critters, and environmental challenges.

It’s not flashy, and it rarely calls attention to itself if it's done well—but it quietly supports the broader simulation and creates a vibrant world for creatures to live in. By using a behaviour tree model, flag-based decisions, and reusable subroutines, the developers created a system for critters that’s both efficient and extensible.

Understanding how critters eat offers a window into how even the “invisible” mechanics in Creatures 3 are carefully crafted. It’s a reminder that beneath the surface of every lifelike action is a system designed to keep the world moving.

Moving on

Wednesday, 4 April 2018

How to make a simple remover COB for a Creatures 2 official object

Download Slink's Creatures 2 scriptorium and unzip it.  

Then open the "c2scrp.htm" file in your web browser and you'll get a list that looks a little like this: with sprite files, names, class numbers.


The Scriptorium browser that's available with the Creatures 2 CAOS Tool is more comprehensive, as it comes directly from your copy of the game, particularly for more complex objects, like the Vibraphone, but for a quick reference to sprites and class numbers, Slink's Creatures 2 Scriptorium is where it's at.





Open up Bobcob and choose the autoscript function:


Choose preferences

Tick Source type: Creatures 2, and output of Remover.  Then use the C2 Scriptorium list from before to choose the correct class number, then click Build:


It will then create a new script for you.

inst
enum 2 13 12
kill targ
next
scrx 2 13 12 1
scrx 2 13 12 4
scrx 2 13 12 5
scrx 2 13 12 6
endm

Note that this script will remove all the purple balls from the world:

inst
enum 2 13 12
kill targ
next

and all the associated scripts:

scrx 2 13 12 1
scrx 2 13 12 4
scrx 2 13 12 5
scrx 2 13 12 6

before quitting:

endm

To have a script that simply removes the balls while leaving their scripts intact, you would use the code:

inst
enum 2 13 12
kill targ
next
endm

Move the script from the "Remover Script" section to the "Install Scripts" section, and remove the script from the remover section.

This will allow you to inject it as if it was any other COB.

Final touches


In the Picture tab, use Slink's C2 Scriptorium to find the correct sprite file name from your game and import the image - this can help prevent crashes.  The sprite for the purple ball is dbal.s16.


Now, the little * in the name of the COB means that the file needs to be saved, so I'll go ahead and do that.  The amount being -1 means there are infinite amounts of purple ball remover.  

Download

If you just want to get rid of those purple balls right this second, download the fruits of this tutorial.

Moving on

It's possible to stack remover scripts together in one script so that you can remove more things at once, for example to write a script removing the purple ball (2 13 12) and the green tennis ball (2 13 5) AND all their scripts, you would use the following:

inst
enum 2 13 12
kill targ
next
enum 2 13 5
kill targ
next
scrx 2 13 12 1
scrx 2 13 12 4
scrx 2 13 12 5
scrx 2 13 12 6
scrx 2 13 5 1
scrx 2 13 5 2
scrx 2 13 5 3
scrx 2 13 5 4
scrx 2 13 5 5
scrx 2 13 5 6
endm

Note that the green tennis ball has more scripts associated with it than the purple one does.  I would not recommend creating a 'Remove All' remover COB like this because the inst command makes things happen in 1/10th of a second - so putting too many things in one remove script might make the game crash.

Another use for a simple remover script like this:

inst
enum 2 13 12
kill targ
next
scrx 2 13 12 1
scrx 2 13 12 4
scrx 2 13 12 5
scrx 2 13 12 6
endm

would be to put it in an installation script for a new object using the same class number - so that the purple ball is removed before a new object is created.  

Thursday, 14 January 2016

More Plants in C3 and DS

So we're going to look at creating more Pinky Plants to decorate our worlds.

First things first, open the starplant.cos file in your Creatures 3 bootstrap.

As you can see, it's a fairly simple plant.  Five of them (reps 5) get moved to random locations in the Norn Terrarium, (mvto rand 1740 2740 674) and then if they're not fully grown (scrp 2 4 5 9), they grow one step, at a rate of between 1 growth step per second and 1 growth step every 4 seconds.  (tick rand 20 80) If they're pushed, they give the stimulus 62, which is... reached peak of starch smell.  Perhaps this should be changed to 84, which is played with a friendly plant.  

But, moving on, we're only going to concentrate on the installation script.  We will assume that our players will not have a modified starplant.cos file, and thus all the other scripts can be found in our players' games already.  The first thing we're going to do is get rid of the reps 5 .. repe loop.  It is essential that we have the ATTR and the BHVR the same as the original starplants.

inst
reps 5
    new: simp 2 4 5 "starplant" 10 0 100
    attr 192
*act1
    bhvr 1
    tick rand 20 80
    mvto rand 1740 2740 674
    elas 0
repe

Now for the MVTO and RAND sections, (mvto rand 1740 2740 674) which govern where the starplants grow.  In a normal MVTO command, the first number is the X-dimension, lengthways across the floor, and the second is how high.  This has three numbers because of the RAND command.  In this case, it should be read as 'move to a random x-location between 1740 and 2740, and always at the 674 y-location'.  The Ctrl-Shift-X keyboard shortcut in Docking Station can be used to make the fingertip of the hand point at what XY location it's at.  MVTO moves the top left corner of the agent to that position, even if the player can't see it.  You can see that my hand is in line with the beehives, etc. to give the rest of the pinky plant space to exist if the top leftmost part of the pinky plant is created there.  If you take the time to put the attr value through Amaikokonut's ATTR Calculator, you'll notice that it collides with floors and suffers physics, so the plant will simply fall to the ground, in a lot of locations that should all be valid.


When choosing where to put your new starplants, it's a good idea to have a notion of how big the starplant is going to grow.  Open starplant.c16 in SpriteBuilder or similar and check the dimensions of the image.  The starplant will grow to a maximum size of 64 pixels wide and 80 pixels high.


If you want precision placement as opposed to letting it fall to the floor, then it will be harder. In the grendel jungle's lift shaft, there is less space to fall about aimlessly, but it still can be done.
So, from the above, we can create a new cos file and have it contain the following:
 
inst
    new: simp 2 4 5 "starplant" 10 0 100
    attr 192
*act1
    bhvr 1
    tick rand 20 80
    mvto 1336 2457
    elas 0

Installation scripts, unlike other scripts, don't necessarily have to end with endm.

Save it into your C3 bootstrap (I called mine extrastarplant.cos) and then use the caos command line to inject it into your world.

You can keep adding different starplants at different locations in the same cos file - it has distinct possibilities for enhancing your worlds through plants.

Wednesday, 5 August 2015

Creature-Friendly Principles - Consistency and Readiness

A Bear of Very Little Brain and Friends at the New York Public Library

Courtesy of Flickr users Tony and Wayne.

Norns, Grendels, and Ettins are bears of very little brain.  Even since the earliest days, the classification system has helped creatures experience their world.  To a creature, all toys are the same, and all food the same.

This has a few implications.

Creatures learn what they can do with any given object genus (i.e. they learn that they can eat food or push toys, once they come across enough edible foodstuffs or pushable toys to learn that this is possible).

Creatures also learn what to expect from genuses.    They expect food to satisfy their hunger.  They expect fire to be painful.  They expect toys to be fun.  They can't tell if an item is 'ready' for them or not.

When deciding what your object should do, it is helpful to look at the official scriptoriums (scriptoria?) of the worlds.   Keep in mind that several of the games were built by a team, so some genuses won't be fully consistent.   The Creatures Development Standards for C3/DS are a good place to start, as well.  You're looking for what actions a creature can take on an object, as well as what chemicals and stimuli result from that.

If you want to make it so that items seem to be ever-ready for the creatures, that can be accomplished by not designing your objects so that they're 'out of commission' for a long time, or by making them invisible for the period of time when they aren't ready for action.  That way, creatures won't learn that pushing food doesn't always work, and try other actions instead. 

For example, a DOIF command can be used with the POSE command to make an apple blossom invisible by keeping its attributes invisible until the timer script runs through all the growth poses. At this point, in C3/DS, smells are added too, to make the object perceptible through the winding corridors of the Ark.  Similarly, a mature herb can be installed as being visible, and when it is eaten, toggled to invisible until the herb is mature again.  A lot of concepts from C3/DS coding are retrofittable to C2 or C1 coding, if appropriate commands exist in the earlier versions. 

Saturday, 4 July 2015

Moving Items Into Your World With TMVT

A short tutorial on how to move new agents into your world by checking their location first with TMVT.

To find a good position for something to appear in the world, use the key command Ctrl-Shift-X. (Docking Station or Docked worlds only, but it will still be valid with C3 standalone.) This will give you two numbers that appear next to your hand in the world. Hover the fingertip over the place you want your agent to appear, and write down the two numbers. These are your coordinates.

mvto will make the top left point of a target agent appear exactly at the coordinates you chose.

mvsf will try to make an item appear in the general vicinity of the coordinates, at a slightly different spot that it considers 'safe'.

So what if you want to use mvto, but you want to make sure that the game won't crash if the spot isn't suitable?

You use a doif block with tmvt.

tmvt is a command that tests if the target can move to the given location and still lie validly within the room system. tmvt returns 1 if it can, 0 if it can't.

*If the test move works
doif tmvt X Y eq 1
*actually move the object to where I want to
mvto X Y
*If the test move doesn't work
else
*move the item safely to the same location
mvsf X Y
*End the doif block
endi


You can see that you can extend this code to try to place your object in multiple locations, if need be.

I like to first try to move my objects into a C3 location, and then if that isn't valid, move it to a valid DS position - thus incorporating a rough C3/DS check into the positioning of the agent. 'If the player isn't running C3, or a C3-docked world, then they must be running Docking Station!'

There are many, many other ways of running that C3/DS check and positioning the agent appropriately, but this is the one I use.

This can also be used in the push script for vendors to make sure that the vended item is made in a valid place in the world.

Monday, 22 June 2015

Getting Your C3/DS CAOS Language Guide

How to get a full listing of the CAOS commands acceptable to your C3/DS engine.

If you don't have access to the CAOS Tool, there is still a way to get documentation about all of the CAOS commands you need to start developing. This file acts as a dictionary of all the CAOS commands that are being used in your game, and is very useful when developing.

The information can be presented in two ways - an alphabetical listing of the commands, or a categorical listing. Both formats are useful so it is worth getting both. The alphabetical listing is useful for a quick-glance reference, and the categorical listing is useful when you're thinking about what each of the commands do, in relation to similar commands.

Open up the CAOS command line in-game CTRL+SHIFT+C and type in the following (all on one line):

file oope 1 "CAOS alphabetical.html" 0 dbg: html 0 file oclo


After you have done this you should find a file called "CAOS alphabetical.html" has been created in your journal folder.

To create the categorical version, type the following:

file oope 1 "CAOS categorical.html" 0 dbg: html 1 file oclo


This creates the file "CAOS categorical.html" in the same place.

If you are running Creatures Exodus it is a little harder to find the file you need.

  On Macintosh, look in your ~/Library/Preferences/Creatures Labs/Docking Station/data/Journal folder.

If you're on a PC, you'll want to look in My Documents for an analogous Journal folder.

If you are running Docking Station on Linux, you'll need to display hidden files, and then look for the Journal folder in ~/.dockingstation/Journal.

Saturday, 6 June 2015

Gnarler Water-Shyness

The following code is in the Gnarler timer script, which helps keep the Gnarler out of the Ettin Desert's pond by using the water CA. It isn't held in a subroutine.

**keep away from bodies of water
*Do this if you're in a room.
doif room targ ne -1
*Do this if you smell any water at all.
doif prop room targ 5 ge 0.00001
*Look for the room next to this one that smells less like water.
setv va00 lorp room targ 5 0
*Go towards the centre of the room that smells less like water.
setv va01 torx va00
*If the less-smelly room is on your left and you were going right, turn around.
doif va01 lt 0 and ov10 gt 0
negv ov10
*or else, if the less-smelly room is on your right and you were going left, turn around.
elif va01 gt 0 and ov10 lt 0
negv ov10
*end the turning around doif
endi
*end the 'smelling water' doif
endi
*end the 'I'm in a room' doif
endi


AquaShee's Crabs use a reversed version of this to help them seek out water sources.

Wednesday, 6 May 2015

A Hearing Vendor

The Mini Empathic Vendor has a script which, when the main empathic vendor has its hearing turned on, causes it to vend food in response to educated norns complaining about hunger, and causes the three food vendors in the norn meso (justanut, lemon and carrot pods) to vend food if the mini empathic vendor is not close enough.

The advantage of using the catalogue files in this way is that it will work for norns speaking English or any other language - as long as they are educated, the vendor will be able to hear them. 

From "empathic vendor.cos", in the DS bootstrap:

** The Vendor's Ear
scrp 2 23 3 126
**this whole script will not fire unless ov00 is 1.  The ear is set as 'on' by default, but it can be set as 'off' by clicking the relevant button on the main empathic vendor.
    doif ov00 = 1
        lock
        sets va00 _p1_
        seta va01 _p2_
        inst
        doif va01 <> null
            targ ownr
            rnge 1000
** Check to see if you can actually see the speaker, if not.. get one of the pods to make some food!
            doif seee ownr va01 = 1
                setv va99 1
            endi
** Get the hunger sentences from the catalogues.
** Protein
**this sets va10 as being the first entry in the 'creature drives' catalogue that can be 'read' by the ear.  This is 'hungry for protein'.
            sets va10 read "Creature Drives" 1
** Starch
**this sets va11 as being the second entry in the 'creature drives' catalogue that can be 'read' by the ear.  This is 'hungry for starch'.
            sets va11 read "Creature Drives" 2
** Fat
**this sets va12 as being the first entry in the 'creature drives' catalogue that can be 'read' by the ear.  This is 'hungry for fat'.
            sets va12 read "Creature Drives" 3

** Check Sentence to see if it wants Fat (Food)
**do if the string-in-string search, in lower case, is nearby (va00) and has expressed a need for fat
            doif sins lowa va00 1 lowa va12 <> -1
                part 1
                frat 2
                anim [4 5 6 5 255]
                snde "empv"
**if you're too far away
                doif va99 = 0
** Tell the Carrot Pod to make a Carrot
                    inst
                    enum 2 23 4
                        doif targ <> null
                            mesg writ targ 0
                        endi
                    next
                    wait 15
                    targ ownr
                    part 1
                    frat 5
                    anim [1 2 3 2 255]
                    stop
**or if you're close
                elif va99 = 1
** Tell yourself to vend Peaking Pie
                    mesg writ ownr 1003
                    stop
                endi
            endi
** Check Sentence to see if it wants Protein (Fruit)
            doif sins lowa va00 1 lowa va10 <> -1
                part 1
                frat 2
                anim [4 5 6 5 255]
                doif va99 = 0
** Tell the Lemon Pod to make a Lemon
                    inst
                    enum 2 23 6
                        doif targ <> null
                            mesg writ targ 0
                        endi
                    next
                    wait 15
                    targ ownr
                    part 1
                    frat 5
                    anim [1 2 3 2 255]
                    stop
                elif va99 = 1
** Tell yourself to vend Yarn Fruit

                    mesg writ ownr 1002
                    stop
                endi
            endi
** Check Sentence to see if it wants Starch (Seeds)
            doif sins lowa va00 1 lowa va11 <> -1
                part 1
                frat 2
                anim [4 5 6 5 255]
                doif va99 = 0
** Tell the Nut Pod to make a Nut
                    inst
                    enum 2 23 5
                        doif targ <> null
                            mesg writ targ 0
                        endi
                    next
                    wait 15
                    targ ownr
                    part 1
                    frat 5
                    anim [1 2 3 2 255]
                    stop
                elif va99 = 1
** Tell yourself to vend Star Seeds
                    mesg writ ownr 1001
                    stop
                endi
            endi

        endi
    endi
endm

Friday, 24 April 2015

Meerk Toy



***create the Meerk Toy
*Make the rest of the instructions run in one tick
*(happens automatically for injection scripts with the CAOS Tool though,
*this is just a good habit to get into).
*Commands that stop the script running all in one tick include: slow, over, wait and endm
inst
*create one under the Norn garden
*Create a simple object with a classifier of family=2, genus=21 and species=1000.
*Family 2, Genus=21 shows that this is a toy object.
*The sprite file to use is called 'meerk', and you should use all the images starting at 0.
*Put the finished creation at image plane 5000.
new: simp 2 21 1000 "Meerk" 0 0 5000
*Give it attributes of 199 - (gravity, boundaries, activateable, pickupable)
attr 199
*hand can click on it - activate 1
*When the hand clicks on it that will launch an activate 1 event
clac 0
*can be picked up, hit, activate1 (push) and 2 (pull)
bhvr 43
*Acceleration due to gravity = 3
accg 3
*Not very elastic ... when it hits the floor 0 percent of its velocity remains
elas 0
*High friction ... will not slide down slopes.
fric 100
*Use image number 59
pose 59
*Define a point on the object where it will be picked up from (pickup handle).
*If you look in the sprite file you'll see that each frame is 72x56 pixels.
*Therefore the point (40,30) is slightly right and down of the centre of this sprite.
puhl -1 40 30
*if a test move to the norn terrarium is safe, move the toy to the norn terrarium.
doif tmvt 1850 950 eq 1
mvto 1850 950
*if not, move it safely to a random spot in the bottom of the DS norn meso.
*(rand chooses an X position of between 748 and 1550, but the height is fixed at 9547).
else mvsf rand 748 1550 9547
*ends the doif statement
endi
*make the camera move to show you where the toy is.
cmrt 0


**activate 1 (push) event
*This marks this chunk as being an activate 1 event script for
*any object that has the classifier 2 21 1000. This will be stored in the scriptorium.
scrp 2 21 1000 1
*Next time the hand clicks on this object it will launch an activate 2 event.
clac 1
*Give a stimulus to whatever activated (this will be ignored if the FROM object isn't a creature).
*Giving a stimulus lets the creature learn lessons from this.
*Stim 97 is the number you need to use for a toy, as it will reduce boredom in most cases.
stim writ from 97 1
*animate! Use the frames specified, in order.
*The 255 at the end means 'loop this sequence forever'.
anim [94 95 96 97 98 98 97 96 95 94 255]
*Play a sounds effect from the object - Mb_2 happens to be a sound effect from the music box
*but you can use any sound file you like!
sndc "mb_2"
*wait for a while (around 6-7 seconds). This allows the music to finish playing
*before the toy changes pose (in the next line)
wait 130
*Go back to the sitting down pose
pose 59
*End of the activate 1 event script.
endm

**activate 2 (pull) event
*This marks this chunk as being an activate 2 event script for any object that has the classifier
*2 21 1000. This will be stored in the scriptorium.
scrp 2 21 1000 2
*Same stimulus as the activate 1 event
stim writ from 97 1
*different animation than before, the last number in the anim is the 'sit' pose.
anim [22 23 24 25 26 27 28 29 30 31 32 33 59]
*Give the object some velocity in an upward direction.
setv vely -15
*Play a sound effect
sndc "ct_1"
*Next time the hand clicks on this object it will trigger an activate 1 event
clac 0
*End of the activate 2 event script
endm

**hit event
*This marks this chunk as being a hit event script for any object that has the classifier 2 21 1000.
*This will be stored in the scriptorium.
scrp 2 21 1000 3
*Same stim as before
stim writ from 97 1
*Play a sound effect, a metallic-sounding hit effect.
sndc "hit_"
*Animate
anim [70 71 72 73 74 75 76 77 78]
*Do not carry on with the rest of this script until the animation has finished playing.
*In this case when the object reaches pose 78 the script can carry on.
over
*Wait a little while (about half a second)
wait 10
*Change pose back to sitting on the floor
pose 59
*End of the hit script
endm

**pickup event
*This marks this chunk as being a pickup event script for any object
*that has the classifier 2 21 1000. This will be stored in the scriptorium.
scrp 2 21 1000 4
*'I have gotten' stimulus
stim writ from 18 1
*Change pose
pose 0
*End of the pickup script
endm

**drop event
*This marks this chunk as being a drop event script for any object
*that has the classifier 2 21 1000. This will be stored in the scriptorium.
scrp 2 21 1000 5
*'I have dropped' stimulus
stim writ from 19 1
*Change pose
pose 59
*End of the drop script
endm

*A remove script part is a very good habit to get into.
*It allows you to easily remove all objects and their scripts with the press of a button.
****REMOVAL
*This is a special command that is only used by the CAOS Tool.
*It marks the area below as being the remove script.
*This section will only be injected by selecting the 'inject remove script' from the CAOS Tool.
rscr
*Enumerate across all the objects that have a classifier of 2 21 1000
*(in case you've forgotten this is the Meerk toy we've just been making!)
enum 2 21 1000
*Kill it
kill targ
*Chose the next meerk toy
next
*Remove the activate 1 script for object 2 21 1000
scrx 2 21 1000 1
*Remove the activate 2 script
scrx 2 21 1000 2
*Remove the hit script
scrx 2 21 1000 3
*Remove the pickup script
scrx 2 21 1000 4
*Remove the drop script
scrx 2 21 1000 5