creatures caves welcome, guest
downloads   gallery   dev   community   creatchi   forum   mycaves
ccsf | links | advice | chat | polls | resources | post

Basic Sprite Randomization   Development   Papriko | 5/27/2013  log in to like post  4

updated5/27/2013
An overview how to use multiple sprite sets to make an agent appear more random.

Alright, let's jump right into it. Imagine you made a cool object, like a fruit or a toy and you made multiple sprite sets, but you can not decide which you want to use. In the end you come to the conclusion that you'd like to use all of them, but how? Do you want to make an own agent for each of them which all do the same in the end?

Naw, that is no solution. The answer is: randomized sprite sets! When you follow a few rules you can make one single agent use several sprite sets! And the best thing: it costs you barely any effort, it's just like 3 extra lines of code in the beginning!
In fact do many agents already do that. Norn Eggs for example use exactly that system. It's always the same boring egg agent, it just uses a random sprite system. The eggs are a good example. Throughout the article I will use the eggs as practical examples. The files you need are the eggs.c16 and the DS cosfile "Norn Egg layer.cos". Keep them open in the background. I'll expect you to and write so as if you'd see them in front of you.

I said it is pretty easy, but you do need some preparation and follow a few rules:

1. All sprite sets you want to use should be in the same file. Look at the eggs.c16. It contains all possible egg sprites. The isn't a bunch of files, like blank_egg.c16 or checkerboard_egg.c16, just one single eggs.c16.
In theory you could work with multiple files too, but that makes things overly complicated. Stick to 1 file, it is way easier to code and ingame you won't see any difference.

2. All the sprite sets should have the same number of frames. Norn eggs for example have a fixed count of 8. Freshly laid, tiny bit grown, middle growth, full size, 3 sprites for the "about to hatch" animation and one broken frame.
Make sure that all sprite sets got the same number of frames. It does not need to be 8, just the same within the same agent. Let's say your agent uses 5 frames, then make sure that ALL sprite sets have exactly 5 frames.
Be aware of your frame-count, you'll need it later!

3. All frames should be in a fixed order. When you look at the eggs.c16 you'll notice that the eggs are always in their exact growing order.
Your sprites need to have an order too. Which order is up to you, just make sure that you use the same for every sprite set.

4. Make sure that there are no annoying other things in the way. When you make a food object with random sprites, but a vendor with a fixed sprite for example, put the vendor at the end of the sprite file! When it is in the beginning or somewhere in the middle, it will mess up everything. When you have different objects and all of them use multiple sets, then the easiest thing you can do is to split them into multiple files, like random_vendor.c16 and random_food.c16.

5. Be aware of how many sets you want to use. When your code is set up for 6 sets, but your sprite file only contains 4, it will cause a horrible mess of errors.
The other way round is less fatal, but still sucks. When your code is just meant to use 4 sets, but you provide 6, then the last 2 will be "cut off" and never used ingame. You made the effort to have 6 different awesome looks, but then one third of them is never seen.
Not funny, right? So always know how many sets you want to use.
I said the eggs.c16 was a great example, but actually it breaks this single rule. The file provides 17 (if I did not count wrong) sprite sets, but the code is only meant to handle 11. That means the last 6 (the C1 eggs) get cut off and are not seen ingame. Let's just pretend they'd really not there, to keep things easy.

I think now it is time to look at the injection line for an egg. I took it straight from the Norn Egg layer.cos that DS provides. The code may seem pretty complicated, but that is because it messes with PRAY files and agent chunks and catalogues and what not. Don't worry about that, you don't need it. Let's rather focus on one tiny line. My text editor says it is line 392:
new: simp 3 4 1 "eggs" 8 va60 0

This is the line which creates a new egg. I guess I don't need to explain new: simp. You probably made some agents before.
You might notice one oddity, though. Instead of a normal starting frame, it uses a variable: va60
Now you might ask "what is va60"? Well, the answer can be found some lines further up. My text editor says it is in the lines 335 and 336:
setv va60 rand 0 10
mulv va60 8

This is the whole trick. The rand command picks a random whole number between 0 and 10 inclusive. This is a range of 11 different numbers you might get (0 counts too). Remember what I said? C3/DS uses a set of 11 different eggs. Coincidence? No! This is exactly the spot where it gets determined.
It's because this is the exact formula of the starting image for using random sprite sets:

choose a random number between 0 and (number of possible sprite sets minus 1)
multiply it by the number of frames in one set


This works always. You can calculate it for every number possible. We can try it out if you want! Let's say as random number it picks 0. 0 * 8 = 0. What is frame number 0 in the eggs.c16? The mini stage of the slightly blue-ish egg.
Aaaand what if it chooses..... 4? 4 * 8 = 32. Frame number 32 iiiiissss.... The mini stage of the cow-pattern egg.
Extreme case: it does pick 10 as random number. 10 * 8 = 80. You see? Even in the most extreme case it still picks a valid sprite, in this case the mini form of the checkerboard egg.

Let's go further and try to connect these 2 things, the code and that formula. We have 11 different egg sets we want to choose from. It says number of sets minus 1.
11 - 1 = 10
So, pick a random number from 0 to 10
setv va60 rand 0 10
And then? Ah yes, I said the eggs have a fixed number of 8 frames per set. So multiply the random number by 8
mulv va60 8

Exactly what the file says! And look ingame, it works. Muco produces random eggs, using all 11 sets.
And do you know what? You just need to adjust those two little values, the 10 and the 8, according to your sprite file and it will work for any sprite selection! Regardless if it is an agent from the injector, an agent produced by a vendor or even a part of a more complex agent (new: comp and new: vhcl)!
This code is awesomely short and spices up almost every agent. Just try it out, you'll love it! As long as you follow the 5 rules and the formula, nothing can go wrong.

An additional note: when you use a permanent ov variable instead of a va, then the agent's even able to remember which "identity" it has. When you combine this with a few doifs, you could for example change what chemicals a fruit contains. Perhaps that even could be made genetic...
If you do it and if yes how and what stays up to you of course

A second note: I think modifying existing code is easier than writing your entirely your own. You could try to modify the Norn Egg layer.cos to use the C1 eggs too as a first test of your knowledge.
Don't forget to edit the DS creatureBreeding.cos too, so norns also lay them.

 
 
Papriko | 4/2/2014  log in to like post

Belated note: when the agent just uses 1 frame for the random part, you can spare that mulv line. No need to multiply by 1.
 
Papriko | 6/16/2013  log in to like post  2

Use an OV variable which is set along with the color and then ask for that in the eat script.
 
Malkin | 5/31/2013  log in to like post

Thanks! Let's say you'd tinted Charles the Cheese a few different colours - how would you change the eat code to accommodate the new colours? Or, as the POSE is "Relative to any index specified by BASE", would it have to change at all?
 
Papriko | 5/28/2013  log in to like post  2

@ Malkin: when they start out the same, you can either copy the shared frame(s) multiple times or you use the random code later. So instead of using it for new: simp, va60 goes into the gall command:

The CAOS Doc wrote:
GALL (command) sprite_file (string) first_image (integer)
Changes the gallery (sprite file) used by an agent. This works for simple and compound agents (using the current PART). The current POSE is kept the same in both galleries.


Then you just have to re-adjust your code for the poses, since then pose 0 changes to the first individual frame rather than the first overall frame. It's a bit of messing, I'd rather go for the copypasta method.

Also, lol @ grendel_man
 
Mioonktoo | 5/27/2013  log in to like post

I actually did what Grendel_Man said on accident when making the radish basket for C1! Had me stumped for a good few hours until I figured out where the problem was. [nlaugh]
 
Karias | 5/27/2013  log in to like post

Grendel_Man, that was waay too funny!
 
Spykkie | 5/27/2013  log in to like post

omg grendel_man XD
 
RisenAngel | 5/27/2013  log in to like post  6

Yo dawg, I heard you like vendors, so I made a vendor that makes vendors so you can vend while you vend.
 
generalflame | 5/27/2013  log in to like post  2

VENDORCEPTION
 
Spykkie | 5/27/2013  log in to like post

Feddlefew, I think you're giving reality a headache now..XD

prev | 1 | 2 | next

downloads
cobs
adoptions
creaturelink
metarooms
breeds
 
gallery
art
wallpaper
screenshots
graphics
promos
sprites
dev
hack shack
script reservations
dev resources
active projects
dev forum
 
community
links
advice
chat
polls
resources
creatchi
 
forum
bookmarks
general
news
help
development
strangeo
survivor
mycaves
log in
register
lost pw
1 online
Pimmix
creatures caves is your #1 resource for the creatures artificial life game series: creatures, creatures 2, creatures 3, docking station, and the upcoming creatures family.

contact    help    privacy policy    terms & conditions    rules    donate    wiki