// Code to load actions: function decodeRuleEffectFromXML(actionElement) { if (actionElement.tagName=="gameComplete") { return {action : actionElement.tagName}; } else if (actionElement.tagName=="addBehavior") { var priority = parseInt(actionElement.attributes.getNamedItem("priority").nodeValue); var behaviorName = actionElement.textContent; var id = actionElement.attributes.getNamedItem("id"); if (id!=null) id = id.nodeValue; return {action : actionElement.tagName, behaviorName : behaviorName, priority : priority, id : id}; } else if (actionElement.tagName=="removeBehavior") { var id = actionElement.attributes.getNamedItem("id").nodeValue; return {action : actionElement.tagName, id: id}; } else if (actionElement.tagName=="teleport") { var xstr = eval(actionElement.attributes.getNamedItem("x").nodeValue); var ystr = eval(actionElement.attributes.getNamedItem("y").nodeValue); var mapstr = actionElement.attributes.getNamedItem("map"); if (mapstr!=null) mapstr = eval(mapstr.nodeValue); return {action : actionElement.tagName, x : xstr, y : ystr, map : mapstr}; } else if (actionElement.tagName=="goTo") { var xstr = eval(actionElement.attributes.getNamedItem("x").nodeValue); var ystr = eval(actionElement.attributes.getNamedItem("y").nodeValue); var mapstr = actionElement.attributes.getNamedItem("map"); if (mapstr!=null) mapstr = eval(mapstr.nodeValue); return {action : actionElement.tagName, x : xstr, y : ystr, map : mapstr}; } else if (actionElement.tagName=="die") { return {action : actionElement.tagName}; } else if (actionElement.tagName=="activateLever") { var xstr = eval(actionElement.attributes.getNamedItem("x").nodeValue); var ystr = eval(actionElement.attributes.getNamedItem("y").nodeValue); var mapstr = actionElement.attributes.getNamedItem("map"); if (mapstr!=null) mapstr = eval(mapstr.nodeValue); return {action : actionElement.tagName, x : xstr, y : ystr, map : mapstr}; } else if (actionElement.tagName=="openDoor") { var doorstr = eval(actionElement.attributes.getNamedItem("door").nodeValue); return {action : actionElement.tagName, door : doorstr}; } else if (actionElement.tagName=="message") { return {action : actionElement.tagName, text : actionElement.attributes.getNamedItem("text").nodeValue}; } else if (actionElement.tagName=="talk") { var subActions = []; for(var j = 0;j0) { conditionElement = conditionElement[0]; for(var j = 0;j0) { thenElement = thenElement[0]; for(var j = 0;j0) { elseElement = elseElement[0]; for(var j = 0;j=action.subactions.length) { character.sendAction({type:ACTION_TALK, performative:{type:action.text, target:otherCharacter}}, map, game); return true; } return undefined; } else { character.sendAction({type:ACTION_TALK, performative:{type:action.text, target:otherCharacter}}, map, game); return true; } } if (action.action=="pendingTalk") { var pt = null; for(var i = 0;i=0) { var item = otherCharacter.inventory[found]; otherCharacter.inventory.splice(found,1); character.inventory.push(item); return true; } return false; } if (action.action=="give") { if (action.inventory!=null) { for(var i = 0;i=price) { if (otherCharacter.inventory.length=price) { var item = eval(action.newItem); var price = action.newItem.value; if (otherCharacter.gold>=price) { if (otherCharacter.inventory.length=action.cycles) { // reset the action: action.startTime = undefined; return true; } return undefined; } if (action.action=="playSound") { var s = game.loadAudioFile(action.sound); if (s.duration) s.currentTime=0; s.play(); return true; } if (action.action=="stopSound") { var s = game.loadAudioFile(action.sound); s.stop(); return true; } if (action.action=="if") { if (action.conditionResult == undefined) { var action2 = action.conditionActions[action.conditionIP]; var retVal = executeRuleEffect(action2, character, map, game, otherCharacter); // when "executeRuleEffect" returns "undefined", it means that the action has not completed execution, so we have to keep it: if (retVal!=undefined) action.conditionIP++; if (action.conditionIP>=action.conditionActions.length) action.conditionResult = retVal; return undefined; } else if (action.conditionResult == true) { var action2 = action.thenActions[action.thenIP]; var retVal = executeRuleEffect(action2, character, map, game, otherCharacter); // when "executeRuleEffect" returns "undefined", it means that the action has not completed execution, so we have to keep it: if (retVal!=undefined) action.thenIP++; if (action.thenIP>=action.thenActions.length) { // reset the action: action.conditionResult = undefined; action.conditionIP = 0; action.thenIP = 0; action.elseIP = 0; return retVal; } return undefined; } else { var action2 = action.elseActions[action.elseIP]; var retVal = executeRuleEffect(action2, character, map, game, otherCharacter); // when "executeRuleEffect" returns "undefined", it means that the action has not completed execution, so we have to keep it: if (retVal!=undefined) action.elseIP++; if (action.elseIP>=action.elseActions.length) { // reset the action: action.conditionResult = undefined; action.conditionIP = 0; action.thenIP = 0; action.elseIP = 0; return retVal; } return undefined; } return true; } return false; } function initAIRules(object) { object.onStart = []; object.onEnd = []; object.storyState = {}; object.storyStateRules = []; object.eventRules = {}; object.timerEventRules = []; object.storyStateLastCycleUpdated = -1; object.storyStateRulesLastCycleChecked = -1; object.actionsToExecute = []; // when rules get triggered, they push their actions into this queue } function parseAIRules(object, xml) { // story state rules: var storystateElements = xml.getElementsByTagName("storyStateRule"); for(var i = 0;i