Friday, July 22, 2011

A Million Grains of Sand

Okay, so we’ve played with creating items. So now lets say we wish to send the player on a quest to collect a million grains of sand for whatever silly reason the plot calls for.

A sand is a kind of thing. There are 10000 sand in the beach shore.

No, it looks good, but trying that will cause many many issues.

1. Inform understands ‘Take sand’ (1 grain at a time) and ‘Take All’ (runs through every object in the room and tries to take them all, which means printing over 10,000 take messages). Inform does not understand ‘Take 1000 sand’ because the ‘take’ action is defined as taking some thing, not taking some number and some thing, nor would ‘take all sand’ limit the taking action to just all the sand in a room.

2. That pesky 100 copies limit mentioned in the last post. It probably makes sense now with that ‘take all’ action reporting each individual take.

3. For the player to collect a million grains of sand where each is a copy of the sand kind, there must actually be a million of them at least at game start. This alone might choke the game.

So is there no hope other than making sand something insubstantial such as an XP number where when a monster is killed the player’s XP goes up so many points?

No, there is a solution, and it’s just to be less literal about things. The blank weapon kind from the last post is the basis for the solution. We can have sand objects, but they must be bundles of sand.

A sand-bundle is a kind of thing. A sand-bundle can be bagged. The printed name of a sand-bundle is “[if item described is bagged]sturdy bag of sand[otherwise]pile of sand[end if]”. Understand “sand” as a sand-bundle. Understand “bag/of” as a bagged sand-bundle. Understand “pile/of” as a not bagged sand-bundle. A sand-bundle has a number called sand.

This works quite well, we need only create enough sand-bundles to account for all the piles or bags of sand in the game. Each pile/bag of sand will contain as many grains as the sand number. A beach for instance could have multiple piles of sand placed in it with varying amounts of sand in each pile. In z-machine code the max/min of a number is about +/- 65,000, whereas in Glulx which is the modern version, the max/min of a number is +/- about 2 billion, which is more than enough to carry 1 million grains of sand in one bag.

Inform will in this case understand ‘take sand’ as taking an entire bag or pile of sand.

Of course this isn’t finished yet. I at least wouldn’t want the player carrying piles of sand, although multiple bags might be okay. There are two options for consolidating. The simplest is the assume the player has some method of carrying the sand along with everything else, similar to how RPGs don’t really track how the player is carrying 52 health potions and 1074 gold pieces. It’s just in the player’s inventory.

The more fun way is to make it where the first time the player finds a bag of sand, the player uses the bag for sand carrying. This can be simulated with a Carry Out rule for taking.

Carry out taking a sand-bundle:
If the player is carrying a bagged sand-bundle(called sandbag):
Increase the sand of sandbag by the sand of the noun;
Now the noun is off-stage;
say “You add the sand to your bag.”;
Rule succeeds;[this should prevent taking from carrying out further and actually taking the bag]
otherwise if the noun is bagged: [code for claiming it as a bag to carry sand in]
say “Ah, now you have a handy bag to carry sand in.”; [We don’t interfere here so the taking action continues, which should eventually report Taken: bag of sand.]
otherwise:
say “You can’t meaningfully carry that much loose sand.” instead; [this stop the action as a failure]


More on how actions process in a later post, but the idea here is that we want the taking action to add sand to the carried bag and ‘throw away’ the now empty bag, putting it out of play where it can be recycled, perhaps as a new pile somewhere.

This type of code can be done for anything that is numerous and is indeed the trick most video games use for this sort of item, which is why RPGs will drop piles/bags of gold, quivers of arrows, braces of throwing daggers, etc. In those games it is less because they have to create all their items before hand and more to simply keep the current total of all items down.

For more detailed information, chapter 14 of the Inform documentation, Numbers and Equations has some wonderful examples dealing with weights, value of money, liquids and various other numbers.

As an example, say we wish to put in the RPG staple, loads and loads of gold, silver and copper coins. We can use the sand-bundling concept, but we can model the numbers a bit differently.

Working form the basis that 40 copper = 1 silver and 40 silver = 1 gold

1. A coin number, when printing a description of the amount and type of coins in a pile, we can do some math to determine how many where 1 = copper, 40 = silver and 1600 = gold. In this method while the exact value is kept track of, the description math can be written to be a bit random in describing, where it might describe a pile of 2000 coins as 1 gold piece and 10 silver pieces or it might say 2000 copper pieces. Both equal 2000 coins.

2. A defined coin number as 1g39s39c which creates a complex number that automatically ‘makes change’, where 39c + 24c = 1s23c. This type of number is like defining Dollars as $1.99 and time as 1:59, the first number can be as high as we wish, but any following numbers are listed at their highest value. 1 minute after 3:59 will thus be 4:00, and 1c above 1g39s39c will be 2g

3. Simply make separate gold silver and copper coin numbers. In this way if the player is carrying 2000 copper (and 50 coins = 1 pound), the player might visit the money changer in town and pay the 3% change to have that copper converted to gold and silver. This is the realistic method where copper doesn't magically convert to silver, the player must get it converted somewhere or spend it. We can also write code to convert 50 coins into 1 pound of weight, so this will also make coins more of a realistic acting item if the player can only carry so much.

We also haven’t considered something else yet, which is how the player can do ‘drop 500 copper’ in case the player is carrying too much and wants to drop some heavy copper pieces without dropping the whole money bag. That is for another day though, it is only a problem if we make it a problem after all.

0 comments:

Post a Comment