Someone last week asked about how to simulate a random dump–like pouring a bag of coins onto a table. I thought about it because I considered it in the past but never figured out a way to do it. Cattlesquat proposed an idea based on drawing random cards, but I discovered an even simpler way, and I tested it in a sample module. Here is how it works. (If everybody already knows this or there is a trivial way I’ve missed, forgive me for being the fool.)
Suppose we have a coin and a coin prototype. I want to toss it on a spot but have land somewhere nearby and different each time. In the coin’s prototype, I add the “sendto” trait. I decided to send to a region on a target map because this (and send to pixel location) gives me the advanced option to offset from the base point. It’s then just a matter of generating random numbers to put in those offsets. But how to do that?
(As an aside, it would be really nice if the bean shell tool had a random() function that provided random integers. It would open the door for some simple AI in solo games among other things like my coin toss.)
So again, how do I do that? The way I did is is to add a Layer trait. In fact, I made two. I called them “randomX” and “randomY” and set to always active. The layer trait has a command to get a random level, so I added 10 null image levels, and I implemented only one command in it–randomize. I did not expose a right-click command, and I made the key code a string-- “randomizeX” in one and “randomizeY” in the other. Each time I call randomizeX, it stores a random number from 1-10 in the property randomX_Level (and the same for randomizeY).
Next, I just needed to use those random numbers in my sendto offsets. I needed a trigger trait that looked like this:
randomizeX !get a random x from the X layer trait
randomizeY
send
The send command sends the coin to region “Start” and offset by “drift”, a local property that I can set to taste. My pieces were 48 pixels a side, so I randomly set drift to 23, but I can change it widen or narrow the toss range. In the advanced section, it became these bean shell lines: {drift} times {randomX_Level - 1} and {drift} times {randomY_Level - 1}. (I subtract 1 to get the multiplier down to 0-9 from 1-10, but that doesn’t matter much.) And that’s it.
So that brings me to my second thing. I made a very simple module to demonstrate this plus something else where “tossed” pieces line themselves neatly without much work by me. It occurred that wouldn’t it be nice if there was a dedicated area in the Vassal module library for non-game-playing modules whose only purpose was to teach how to use a feature or two. All the complications would be stripped out, so no more looking at Twilight to figure out how a deck works. The admins would have to set this up, but I think it would be great.