Created
October 29, 2017 01:26
-
-
Save Inviz/6e0d5ffa33981e92d5d0399e05e24704 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Petri = function() { | |
this.output = this.name; | |
} | |
Petri.compile = function(functions, name) { | |
var struct = {} | |
var size = 0; | |
var allArgs = functions.map(function(fn) { | |
return fn.toString().match(/\(\s*(.*?)\s*\)/)[0].split(/\s*,\s*/).map(function(arg, index) { | |
if (struct[arg] == null) | |
struct[arg] = size++ | |
return arg; | |
}) | |
}) | |
var invocations = functions.map(function(fn, index) { | |
var args = allArgs[index]; | |
var attribute = struct[args[0]] | |
var prefix = 'data[index * ' + size + ' + '; | |
var suffix = ']'; | |
return prefix + struct[args[0]] + suffix + ' = ' + fn.name + '(' + args.map(function(arg) { | |
return prefix + struct[arg] + suffix | |
}).join(', ') + ')' | |
}); | |
return new Function( | |
functions.join('\n') + '\n' | |
+ 'return (function ' + name + ' (data, index) {' | |
+ invocations.join(';\n') | |
+ '})')() | |
} | |
Game = {} | |
Game.Creature = [ | |
function hunger(hunger, step) { | |
if (step % 10) | |
return hunger + 1; | |
}, | |
// multiple transitions | |
function fear_calming_down(fear) { | |
return fear - fear / 100 | |
}, | |
function fear_from_damage(fear, damage) { | |
if (damage) | |
return fear + damage; | |
}, | |
function fear_from_loud_noises(fear, noise) { | |
if (noise > 50) | |
return fear + noise / 100; | |
}, | |
// exclusive sources of change | |
function room_temperature(temperature, zone, area) { | |
if (zone) | |
return temperature + (zone.temperature - temperature) / 10 | |
else | |
return temperature + (area.temperature - temperature) / 10 | |
}, | |
function action_got_hungry(action, hunger) { | |
var priority = hunger; | |
if (hunger > 50 && action % 1000 < priority) { | |
return Game.Action.get_food.index * 1000 + priority | |
} | |
}, | |
function action_got_fearful(action, fear) { | |
var priority = fear; | |
if (hunger > 50 && action % 1000 < priority) { | |
return Game.Action.get_safe.index * 1000 + hunger | |
} | |
} | |
], | |
function consume_food(hunger, Food) { | |
return hunger + Food.nutrition | |
}, | |
function Food(zone) { | |
return zone.food | |
}, | |
function Food(food) { | |
return food | |
}, | |
Game.Building = { | |
} | |
Petri.compile(Game.Creature, 'Creature') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment