Rûn Cookbook: Logic
Recipes for the questions makers ask most: numbers, chances, counters, timers and choices. Each recipe is complete and has been run in the engine. The world's recipes (doors, shops, enemies, scenes) are on Rûn Cookbook: The World.
1. A random number, and a branch on it
Roll a number from 0 to 50 and do something when it is under 10. In an event script:
[use]
set roll rand(51)
if roll < 10
say A low roll: {roll}.
else
say A high roll: {roll}.In a program:
number roll = floor(random(0, 51))
if (roll < 10) {
print("a low roll:", roll)
} else {
print("a high roll:", roll)
}a high roll: 23rand(51) and floor(random(0, 51)) both give every whole number from 0 to 50 the same chance. round(random(0, 50)) gives 0 and 50 half as often as the rest.
2. A percent chance
Something that happens one time in four:
number hits = 0
for (number i = 0; i < 1000; i = i + 1) {
if (random(100) < 25) hits = hits + 1
}
print(hits > 150 && hits < 350)onIn an event script: if rand(100) < 25.
3. A counter
[use]
add lever_pulls 1
if lever_pulls = 3
say Something clicks deep in the wall.
flag set wall_openedThe count is kept in the save. See Variables and Counters.
4. Something that happens once
A flag remembers that it happened. Ask whether it is still lowered, and raise it the first time:
[use]
if flag(cook_gift_given) = 0
flag set cook_gift_given
say Take this, friend.
else
say I have nothing more for you.5. A timer
Wait, then act. The section pauses; the game goes on.
[use]
say The fuse is lit.
wait 3
say Boom.A repeating timer: every runs the whole section again on a beat while the map is loaded. Arm it from the map's load section.
[load]
run cook_every tick
[tick]
every 5
add five_seconds 1A timer in a program counts the frame's seconds in a live program:
number left = 10
on update(number dt) {
if (left > 0) {
left = left - dt
if (left <= 0) World.FlagSet("cook_time_up")
}
}6. A dice roll
roll() rolls dice through the d20 rules and gives the total:
number total = roll("2d6+1")
print(total >= 3 && total <= 13)onIn an event script, require <attribute> <dc> : <section> is a hidden d20 roll plus the attribute's modifier.
7. One of several outcomes
text weather = pick("rain", "fog", "sun")
print(weather == "rain" || weather == "fog" || weather == "sun")on8. A weighted choice
Common, rare and very rare, as 70, 25 and 5 in 100:
Loot() {
number r = random(100)
if (r < 70) return "a coin"
if (r < 95) return "a gem"
return "a crown"
}
number gems = 0
for (number i = 0; i < 1000; i = i + 1) {
if (Loot() == "a gem") gems = gems + 1
}
print("about a quarter were gems:", gems > 150 && gems < 350)about a quarter were gems: on9. Keeping a number between limits
number hp = 95
hp = clamp(hp + 20, 0, 100)
print(hp)10010. Going round in a cycle
mod wraps a counter back to 0: four seasons, then the first again.
seasons = ["spring", "summer", "autumn", "winter"]
number turn = 6
print(seasons[mod(turn, 4)])autumn11. Building a sentence
number wolves = 3
text line = "You see " + wolves + " wolves"
if (wolves == 1) line = "You see a wolf"
print(line + ".")You see 3 wolves.12. A function shared by sections
Write the function once in the program, and call it from each section:
Greeting(text place) {
return "Welcome to " + place
}
on enter {
print(Greeting("the harbour"))
}
on exit {
print("Leaving. " + Greeting("the road"))
}Welcome to the harbour13. Leaving a loop early
There is no break. Put the loop in a function and return:
FirstEven(number from) {
for (number i = from; i < from + 10; i = i + 1) {
if (mod(i, 2) == 0) return i
}
return -1
}
print(FirstEven(7))8