agents-flow
Version:
Framework for multi-agent interactive fiction videogame.
3 lines (2 loc) • 30.3 kB
JavaScript
var t=require("npc-emotional"),e=require("first-order-logic"),n=require("data-structs-n-algos");function i(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function r(t,e,n){return e&&i(t.prototype,e),n&&i(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function o(t,e){return o=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},o(t,e)}function s(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,i=new Array(e);n<e;n++)i[n]=t[n];return i}function a(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return s(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?s(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var i=0;return function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var c,u=/*#__PURE__*/function(){function t(t){this._choiceIndex=void 0,this._choiceIndex=t}return t.void=function(){return new t(-1)},r(t,[{key:"choiceIndex",get:function(){return this._choiceIndex}},{key:"isVoid",get:function(){return this._choiceIndex<0}}]),t}();function l(t){return 1===t.length?t[0]:t[function(t){var e=t+1;return Math.floor(Math.random()*e)}(t.length-1)]}!function(t){t[t.Low=-1]="Low",t[t.None=0]="None",t[t.High=1]="High"}(c||(c={}));var h,f=/*#__PURE__*/function(){function t(t,e,n){if(void 0===n&&(n=null),this._content=void 0,this._effect=void 0,this._sentence=void 0,null==t||""===t.trim())throw new Error("Content cannot be empty.");if(null==e)throw new Error("Effect cannot be null.");this._content=t,this._effect=e,this._sentence=n}return r(t,[{key:"content",get:function(){return this._content}},{key:"effect",get:function(){return this._effect}},{key:"sentence",get:function(){return this._sentence}},{key:"hasSentence",get:function(){return null!==this._sentence}}]),t}(),_=/*#__PURE__*/function(){function e(e,n,i){if(void 0===n&&(n=null),void 0===i&&(i=null),this._text=void 0,this._effect=void 0,this._sentence=void 0,null==e)throw new Error("Text function cannot be null.");this._text=e,this._effect=null==n?function(e){return t.Effect.null()}:n,this._sentence=i}return r(e,[{key:"text",get:function(){return this._text}},{key:"effect",get:function(){return this._effect}},{key:"sentence",get:function(){return this._sentence}}]),e}(),d=/*#__PURE__*/function(){function e(t,e){if(void 0===e&&(e=null),this._alternatives=void 0,this._performerRole=void 0,this._targetRole=void 0,this._isIntimate=void 0,null==t||""===t.trim())throw new Error("Performer role cannot be null.");if(null!=e&&""===e.trim())throw new Error("If defining target role, it can't be empty.");this._performerRole=t,this._targetRole=e,this._isIntimate=!1,this._alternatives=[]}var n=e.prototype;return n.allAlternatives=function(t){return this._alternatives.map(function(e){return e.text(t)})},n.withAlternative=function(t,e,n){if(void 0===e&&(e=null),void 0===n&&(n=null),null==t)throw new Error("Alternative cannnot be null");return this._alternatives.push(new _(t,e,n)),this},n.intimate=function(){this._isIntimate=!0},n.action=function(t,n,i){void 0===i&&(i=-1);var r=this.chooseAlternative(t,n,i),o=r.text(t);if(o.includes(":")){var s=o.split(":");return new f(t.get(e.uncorcheted(s[0])).say(t.assignRoles(s[1])),r.effect(t),null==r.sentence?null:r.sentence(t))}return new f(t.assignRoles(o),r.effect(t),null==r.sentence?null:r.sentence(t))},n.chooseAlternative=function(t,e,n){return 1===this._alternatives.length?this._alternatives[0]:n>=0?this._alternatives[n]:this.performerHasMeaningfulDesires(t,e)?this.calculateAlternativeBasedOnDesires(t,e):this.calculateAlternativeBasedOnEmotions(t)},n.performerHasMeaningfulDesires=function(t,e){var n=t.get(this._performerRole).Desires;return n.any&&n.anyInvolved(e.all)},n.calculateAlternativeBasedOnDesires=function(e,n){for(var i,r=this,o=-1e6,s=e.get(this._performerRole),c=this._alternatives[0],u=o,l=function(){var a=i.value,l=o,h=a.effect(e);h.direction===t.EffectDirection.Neutral?l=s.Desires.heuristicTotal(n):(f=n.copy(),_=new t.Situation(f.get(s.Name),f.get(e.get(h.target).Name),r._isIntimate?h.isThirdPerson?[f.get(e.get(r._targetRole).Name)]:[]:f.all.filter(function(t){return t.Name!==e.get(h.target).Name&&t.Name!==s.Name})),t.apply(h,_,r._isIntimate),l=s.Desires.heuristicTotal(f)),l>u&&(c=a,u=l)},h=a(this._alternatives);!(i=h()).done;){var f,_;l()}return c},n.calculateAlternativeBasedOnEmotions=function(t){var e=null==this._targetRole?this.calculateEffectDirection(t.get(this._performerRole).Happiness):this.calculateEffectDirectionWithRelation(t.get(this._performerRole).Relations.get(t.get(this._targetRole).Name),t.get(this._performerRole).Happiness),n=this._alternatives.filter(function(n){return n.effect(t).direction===e});return 0===n.length&&(n=this._alternatives),l(n)},n.calculateEffectDirection=function(e){return e.isHappy?t.EffectDirection.Positive:e.isUnhappy?t.EffectDirection.Negative:t.EffectDirection.Neutral},n.calculateEffectDirectionWithRelation=function(e,n){if(e.isPonderatedGood&&n.isHappy)return t.EffectDirection.Positive;if(e.isPonderatedBad&&n.isUnhappy)return t.EffectDirection.Negative;if(e.isPonderatedNeutral&&n.isNeutral)return t.EffectDirection.Neutral;if(e.isPonderatedNeutral&&n.isHappy)return t.EffectDirection.Positive;if(e.isPonderatedNeutral&&n.isUnhappy)return t.EffectDirection.Negative;if(e.isPonderatedGood&&n.isNeutral)return t.EffectDirection.Positive;if(e.isPonderatedBad&&n.isNeutral)return t.EffectDirection.Negative;if(e.isPonderatedGood&&n.isUnhappy)return t.EffectDirection.Neutral;if(e.isPonderatedBad&&n.isHappy)return t.EffectDirection.Neutral;throw new Error("Not existing option.")},e.uncorcheted=function(t){return t.replace("[","").replace("]","")},r(e,[{key:"performerRole",get:function(){return this._performerRole}},{key:"targetRole",get:function(){return this._targetRole}},{key:"isIntimate",get:function(){return this._isIntimate}},{key:"isMultialternative",get:function(){return this._alternatives.length>1}}]),e}(),p=/*#__PURE__*/function(){function t(t,e){if(void 0===e&&(e=[]),this._main=void 0,this._secondarys=void 0,null==t||""===t.trim())throw new Error("Main cannot be null.");if(null==e)throw new Error("Secondarys cannot be null.");this._main=t,this._secondarys=new Set(e)}return r(t,[{key:"main",get:function(){return this._main}},{key:"secondarys",get:function(){return Array.from(this._secondarys.values())}},{key:"all",get:function(){return[this._main].concat(this.secondarys)}}]),t}(),g=/*#__PURE__*/function(){function t(){this._roles=void 0,this._roles=new Map}var e=t.prototype;return e.match=function(t,e){return this.validateRole(t),this.validateAgent(e),this._roles.set(t,e),this},e.get=function(t){if(this.validateRole(t),!this._roles.has(t))throw new Error("Specified role doesn't exist");return this._roles.get(t)},e.has=function(t){return this.validateRole(t),this._roles.has(t)},e.matched=function(t){return this.validateAgent(t),this.agents.some(function(e){return e.Name===t.Name})},e.assignRoles=function(t){for(var e,n=t,i=a(this._roles.keys());!(e=i()).done;){var r=e.value;n=n.replace("["+r+"]",this._roles.get(r).Name)}return n},e.equals=function(t){if(null==t)return!1;if(this._roles.size!==t._roles.size)return!1;for(var e,n=a(this._roles.keys());!(e=n()).done;){var i=e.value;if(!t._roles.has(i))return!1;if(this._roles.get(i).Name!==t._roles.get(i).Name)return!1}return!0},e.toString=function(){var t=this;return this.roleNames.map(function(e){return t._roles.get(e).Name+" as "+e}).join(",")},e.validateRole=function(t){if(null==t||""===t.trim())throw new Error("Role cannot be empty.")},e.validateAgent=function(t){if(null==t)throw new Error("Agent cannot be null.")},r(t,[{key:"roleNames",get:function(){return Array.from(this._roles.keys())}},{key:"agents",get:function(){return Array.from(this._roles.values())}}]),t}(),v=/*#__PURE__*/function(){function t(){this._content=void 0,this._ender=void 0,this._choices=void 0,this._reactions=void 0,this._content=[],this._choices=null,this._reactions=null}return t.prototype.append=function(t){return null!=t&&""!==t.trim()&&this._content.push(t),this},t.fromContent=function(e,n){if(void 0===n&&(n=!1),null==e||""===e.trim())throw new Error("Content cannot be empty.");var i=new t;return i._content=[e],i._ender=n,i._choices=null,i},t.fromChoices=function(e,n){if(void 0===n&&(n=!1),null==e||0===e.length)throw new Error("Choices cannot be empty.");var i=new t;return i._ender=n,i._choices=new m(e),i},t.fromReactions=function(e,n){if(void 0===n&&(n=!1),null==e)throw new Error("Reactions cannot be null.");var i=new t;return i._ender=n,i._reactions=e,i},r(t,[{key:"content",get:function(){return this._content}},{key:"isEnder",get:function(){return this._ender}},{key:"hasChoices",get:function(){return null!==this._choices}},{key:"choices",get:function(){return this._choices}},{key:"reactions",get:function(){return this._reactions}},{key:"hasReactions",get:function(){return null!=this._reactions}}]),t}(),m=/*#__PURE__*/function(){function t(t){if(this._items=void 0,null==t||0===t.length)throw new Error("Items cannot be empty.");this._items=t}return t.prototype.isValidItem=function(t){return t>=0&&t<this._items.length},r(t,[{key:"count",get:function(){return this._items.length}},{key:"items",get:function(){return this._items}}]),t}(),y=/*#__PURE__*/function(){function t(t,n,i,r,o,s,a,c,u){if(void 0===u&&(u=null),this._name=void 0,this._aspect=void 0,this._likes=void 0,this._relations=void 0,this._happiness=void 0,this._personality=void 0,this._isHuman=void 0,this._characteristics=void 0,this._desires=void 0,this._onTurnPassed=void 0,this._logic=void 0,this._isActive=void 0,null==t||""===t.trim())throw new Error("Name cannot be empty.");if(null==n)throw new Error("Aspect cannot be null.");if(null==i)throw new Error("Relations cannot be null.");if(null==r)throw new Error("Happiness cannot be null.");if(null==o)throw new Error("Personality cannot be null.");if(null==s)throw new Error("Likes cannot be null.");if(null==a)throw new Error("Characteristics cannot be null.");this._name=t,this._aspect=n,this._relations=i,this._happiness=r,this._personality=o,this._likes=s,this._characteristics=new k(t,a),this._desires=new I,this._isHuman=c,this._isActive=!0,this._onTurnPassed=null==u?function(t,e){return""}:u,this._logic=new e.LogicAgent,this._logic.population.add(this.Individual)}var n=t.prototype;return n.say=function(t){if(null==t)throw new Error("Content cannot be null.");return this.Name+": - "+t},n.activate=function(){this._isActive=!0},n.deactivate=function(){this._isActive=!1},n.humanize=function(){this._isHuman=!0},n.dehumanize=function(){this._isHuman=!1},n.copy=function(){return new t(this._name,this._aspect.copy(),this._relations.copy(),this._happiness.copy(),this._personality.copy(),this._likes.copy(),this._characteristics.table.elements.map(function(t){return t.function.name}),this._isHuman)},n.passTurn=function(t){return this._onTurnPassed(t,this)},r(t,[{key:"Name",get:function(){return this._name}},{key:"Aspect",get:function(){return this._aspect}},{key:"Likes",get:function(){return this._likes}},{key:"Relations",get:function(){return this._relations}},{key:"Happiness",get:function(){return this._happiness}},{key:"Personality",get:function(){return this._personality}},{key:"IsHuman",get:function(){return this._isHuman}},{key:"Characteristics",get:function(){return this._characteristics}},{key:"Desires",get:function(){return this._desires}},{key:"IsActive",get:function(){return this._isActive}},{key:"OnTurnPassed",get:function(){return this._onTurnPassed}},{key:"Individual",get:function(){return new e.Individual(this.Name.replace(" ",""))}},{key:"logic",get:function(){return this._logic}}]),t}(),w=/*#__PURE__*/function(){function t(t){if(this._crowd=void 0,null==t)throw new Error("Agents cannot be null.");this._crowd=new Map;for(var e,n=a(t);!(e=n()).done;){var i=e.value;this._crowd.set(i.Name,i)}}var e=t.prototype;return e.get=function(t){return this._crowd.get(t)},e.copy=function(){return new t(this.all.map(function(t){return t.copy()}))},r(t,[{key:"all",get:function(){return Array.from(this._crowd.values())}}]),t}(),b=/*#__PURE__*/function(){function t(t,e){if(this._involved=void 0,this._heuristic=void 0,null==t)throw new Error("Heuristic cannot be null.");if(null==e)throw new Error("Involved agents cannot be null.");this._heuristic=t,this._involved=new Set(e)}return t.prototype.anyInvolved=function(t){var e=this;return t.some(function(t){return e._involved.has(t.Name)})},r(t,[{key:"heuristic",get:function(){return this._heuristic}}]),t}(),I=/*#__PURE__*/function(){function t(){this._desires=void 0,this._desires=[]}var e=t.prototype;return e.append=function(t){return null!=t&&this._desires.push(t),this},e.clear=function(){this._desires=[]},e.anyInvolved=function(t){return this._desires.some(function(e){return e.anyInvolved(t)})},e.heuristicTotal=function(t){return this._desires.reduce(function(e,n){return e+n.heuristic(t)},0)},r(t,[{key:"any",get:function(){return this._desires.length>0}}]),t}(),E=/*#__PURE__*/function(){function t(t){if(this._agents=void 0,this._availableAgents=void 0,null==t)throw new Error("Agents cannot be null.");this._agents=t,this._availableAgents=[]}var e=t.prototype;return e.allExcept=function(t){return this._agents.filter(function(e){return e.Name!==t.Name})},e.popRandomAgent=function(){0===this._availableAgents.length&&(this._availableAgents=[].concat(this._agents));for(var t,e=[],n=a(this._availableAgents);!(t=n()).done;)for(var i=t.value,r=0;r<i.Personality.introvertyExtroverty;r++)e.push(i);var o=l(e);return this._availableAgents=this._availableAgents.filter(function(t){return t.Name!==o.Name}),o},r(t,[{key:"all",get:function(){return this._agents}},{key:"count",get:function(){return this._agents.length}}]),t}(),k=/*#__PURE__*/function(){function t(t,n){if(void 0===n&&(n=[]),this._name=void 0,this._table=void 0,null==t||""===t.trim())throw new Error("Name cannot be empty.");this._name=t,this._table=new e.TruthTable;for(var i,r=a(n);!(i=r()).done;){var o=i.value;null!=t&&""!==t.trim()&&this._table.add(e.Sentence.build(o,this._name))}}return t.prototype.is=function(t){if(null==t||""===t.trim())throw new Error("Characteristic cannot be empty.");return this._table.exists(e.Sentence.build(t,this._name))},r(t,[{key:"table",get:function(){return this._table}}]),t}(),N=/*#__PURE__*/function(){function t(t){if(this._name=void 0,this._connections=void 0,this._agents=void 0,null==t||""===t.trim())throw new Error("Location cannot be null.");this._name=t,this._connections=new Map,this._agents=new Map}var e=t.prototype;return e.append=function(t){if(null!=t)for(var e,n=a(t);!(e=n()).done;){var i=e.value;this._agents.set(i.Name,i)}},e.remove=function(t){null!=t&&this._agents.delete(t.Name)},e.has=function(t){return null!=t&&this._agents.has(t.Name)},e.connectWith=function(t){null!=t&&this._connections.set(t.name,t)},e.isConnected=function(t){return null!=t&&this._connections.has(t.name)},t.join=function(t,e){t.connectWith(e),e.connectWith(t)},r(t,[{key:"name",get:function(){return this._name}},{key:"connections",get:function(){return Array.from(this._connections.values())}},{key:"agents",get:function(){return Array.from(this._agents.values())}}]),t}(),A=/*#__PURE__*/function(){function t(t){if(this._locations=void 0,this._ubications=void 0,null==t)throw new Error("Locations cannot be null.");this._locations=new Map,this._ubications=new Map;for(var e,n=a(t);!(e=n()).done;){var i=e.value;this._locations.set(i.name,i)}}var e=t.prototype;return e.getLocation=function(t){return null!=t&&""!==t.trim()&&this._locations.has(t)?this._locations.get(t):null},e.getUbication=function(t){return null!=t&&this._ubications.has(t.Name)?this._locations.get(this._ubications.get(t.Name)):null},e.move=function(t,e){if(null==t||null==e||!this._locations.has(e.name))return!1;if(!this._ubications.has(t.Name))return this._ubications.set(t.Name,e.name),e.append([t]),!0;var n=this.getUbication(t);return!!n.has(t)&&!!n.isConnected(e)&&(this._ubications.set(t.Name,e.name),n.remove(t),e.append([t]),!0)},e.areInTheSameLocation=function(t,e){return!(null==t||null==e||!this._ubications.has(t.Name)||!this._ubications.has(e.Name))&&this._ubications.get(t.Name)===this._ubications.get(e.Name)},t}();exports.Timing=void 0,(h=exports.Timing||(exports.Timing={}))[h.Single=0]="Single",h[h.GlobalSingle=1]="GlobalSingle",h[h.Repeteable=2]="Repeteable";var x=/*#__PURE__*/function(){function t(t){if(this._phrases=void 0,null==t)throw new Error("Phrases cannot be null.");this._phrases=new n.Queue(t)}return t.prototype.deliver=function(t){if(!this.isFinished)return this._phrases.dequeue();throw new Error("No phrases to deliver.")},r(t,[{key:"isFinished",get:function(){return this._phrases.isEmpty()}}]),t}(),R=/*#__PURE__*/function(){function t(t,e,n,i,r){if(this._name=void 0,this._description=void 0,this._preconditions=void 0,this._postconditions=void 0,this._timing=void 0,this._isIntimate=void 0,null==t||""===t.trim())throw new Error("Name cannot be empty.");if(null==e||""===e.trim())throw new Error("Description cannot be empty.");if(null==i)throw new Error("Preconditions cannot be null.");if(null==r)throw new Error("Postconditions cannot be null.");this._name=t,this._description=e,this._timing=n,this._preconditions=i,this._postconditions=r,this._isIntimate=!1}var e=t.prototype;return e.equals=function(t){return null!=t&&this._name===t.name},e.globallyEquals=function(t){return null!=t&&this._name===t.name},e.getPermutations=function(t,e){var n=[],i=this.rolesDescriptor;if(e.length<i.secondarys.length)return n;for(var r,o=a(this.calculateAgentCombinations(e,i.secondarys.length));!(r=o()).done;){var s=r.value;if(0!==s.length)for(var c,u=a(s);!(c=u()).done;){var l=c.value,h=(new g).match(i.main,t);h.match(i.secondarys[0],l);var f=i.secondarys.filter(function(t){return!h.has(t)}),_=s.filter(function(t){return!h.matched(t)});this.fillRemainingPermutations(h,f,_),n.push(h)}else n.push((new g).match(i.main,t))}return n},e.calculateAgentCombinations=function(t,e){var n=t.map(function(t){return[t]}),i=[];i.push([]);for(var r,o=a(n);!(r=o()).done;)for(var s=r.value,c=i.length,u=0;u<c;u++)i[u].length>=e||i.push(i[u].concat(s));return i.filter(function(t){return t.length===e})},e.fillRemainingPermutations=function(t,e,n){if(0!==e.length)for(var i,r=a(n);!(i=r()).done;){t.match(e[0],i.value);var o=e.filter(function(e){return!t.has(e)}),s=n.filter(function(e){return!t.matched(e)});this.fillRemainingPermutations(t,o,s)}},r(t,[{key:"name",get:function(){return this._name}},{key:"description",get:function(){return this._description}},{key:"preconditions",get:function(){return this._preconditions}},{key:"postconditions",get:function(){return this._postconditions}},{key:"isIntimate",get:function(){return this._isIntimate}},{key:"timing",get:function(){return this._timing}}]),t}(),P=/*#__PURE__*/function(t){var n,i;function s(n,i,r,o,s,a,c){var u;if(void 0===s&&(s=exports.Timing.Single),void 0===a&&(a=function(t,e,n){return!0}),void 0===c&&(c=function(t,n){return e.TruthTable.empty}),(u=t.call(this,n,i,s,a,c)||this)._phrases=void 0,u._rolesDescriptor=void 0,null==r)throw new Error("Roles descriptor cannot be null.");if(null==o||0===o.length)throw new Error("Interaction must have almost one phrase.");return u._rolesDescriptor=r,u._phrases=o,u}return i=t,(n=s).prototype=Object.create(i.prototype),n.prototype.constructor=n,o(n,i),s.prototype.intimate=function(){this._isIntimate=!0;for(var t,e=a(this._phrases);!(t=e()).done;)t.value.intimate();return this},r(s,[{key:"deliverer",get:function(){return new x(this._phrases)}},{key:"rolesDescriptor",get:function(){return this._rolesDescriptor}}]),s}(R),S=/*#__PURE__*/function(){function n(n,i,r,o){if(this._deliverer=void 0,this._effect=void 0,this._performerRole=void 0,this._targetRole=void 0,this._actualPhrase=void 0,this._onGoingPostconditions=void 0,this._roles=void 0,this._map=void 0,this._table=void 0,this._interaction=void 0,null==n)throw new Error("Interaction cannot be null.");if(null==i)throw new Error("Roles cannot be null.");if(null==r)throw new Error("Map cannot be null.");if(null==o)throw new Error("Postconditions table cannot be null.");this._interaction=n,this._roles=i,this._map=r,this._table=o,this._effect=t.Effect.null(),this._performerRole=null,this._targetRole=null,this._onGoingPostconditions=new e.TruthTable}var i=n.prototype;return i.performStep=function(t){var e=this;return this.initializeDeliverer(),this._effect.isNull?t.isVoid?(this._actualPhrase=this._deliverer.deliver(this._roles),this.forcePhraseRelations(this._actualPhrase.performerRole,this._actualPhrase.targetRole),this._roles.get(this._actualPhrase.performerRole).IsHuman&&this._actualPhrase.isMultialternative?v.fromChoices(this._actualPhrase.allAlternatives(this._roles).map(function(t){return e._roles.assignRoles(t)})):this.executePhraseAction(t)):this.executePhraseAction(t):this.calculateAgentsReactions()},i.initializeDeliverer=function(){null==this._deliverer&&(this._deliverer=this._interaction.deliverer)},i.forcePhraseRelations=function(t,e){null!=e&&""!==e.trim()&&(this._roles.get(t).Relations.get(this._roles.get(e).Name),this._roles.get(e).Relations.get(this._roles.get(t).Name))},i.executePhraseAction=function(t){var e=this._actualPhrase.action(this._roles,new w(this.location.agents),t.choiceIndex);return this._effect=e.effect,this._performerRole=this._actualPhrase.performerRole,this._targetRole=this._actualPhrase.targetRole,e.hasSentence&&!this._onGoingPostconditions.exists(e.sentence)&&this._onGoingPostconditions.add(e.sentence),v.fromContent(e.content,this.isInteractionEnded)},i.calculateAgentsReactions=function(){var e=this,n=new t.Reactions;if(this._effect.direction!=t.EffectDirection.Neutral){var i=this._effect.isTargeted?new t.Situation(this._roles.get(this._performerRole),this._roles.get(this._effect.target),this._interaction.isIntimate?this._effect.isThirdPerson?[this._roles.get(this._targetRole)]:[]:this.location.agents.filter(function(t){return t.Name!==e._roles.get(e._effect.target).Name&&t.Name!==e._roles.get(e._performerRole).Name})):new t.Situation(this._roles.get(this._performerRole),null,this.location.agents.filter(function(t){return t.Name!==e._roles.get(e._performerRole).Name}));n.append(t.apply(this._effect,i,this._interaction.isIntimate))}return this._effect=t.Effect.null(),v.fromReactions(n,this.isInteractionEnded)},i.equals=function(t){return this._interaction.equals(t.interaction)&&this._roles.equals(t.roles)},i.toString=function(){return this._roles.assignRoles(this._interaction.description)},r(n,[{key:"interaction",get:function(){return this._interaction}},{key:"roles",get:function(){return this._roles}},{key:"map",get:function(){return this._map}},{key:"isInteractionEnded",get:function(){return this._deliverer.isFinished&&this._effect.isNull}},{key:"location",get:function(){return this._map.getUbication(this._roles.get(this._interaction.rolesDescriptor.main))}},{key:"postconditions",get:function(){return this._onGoingPostconditions.join(this._interaction.postconditions(this._table,this._roles,this._map)),this._onGoingPostconditions}}]),n}(),D=/*#__PURE__*/function(){function t(){this._executions=void 0,this._executions=[]}var e=t.prototype;return e.add=function(t,e){if(null==t)throw new Error("Agent cannot be null.");if(null==e)throw new Error("On going interaction cannot be null.");this._executions.push({agent:t,ongoing:e})},e.hasExecutedInteraction=function(t){return this._executions.some(function(e){return e.ongoing.interaction.equals(t)})},e.hasExecutedOnGoingInteraction=function(t){return this._executions.some(function(e){return e.ongoing.equals(t)})},t}(),C=/*#__PURE__*/function(){function t(){this._conditions=void 0,this._conditions=[]}var e=t.prototype;return e.with=function(t){if(null==t)throw new Error("Condition cannot be null");return this._conditions.push(t),this},e.allMet=function(t){return this._conditions.every(function(e){return e(t)})},t}(),T=/*#__PURE__*/function(){function t(){this._scenarios=void 0,this._lastScenario=void 0,this._scenarios=[],this._lastScenario=null}return t.prototype.add=function(t){if(null==t)throw new Error("Scenario cannot be null.");this._scenarios.push(t)},r(t,[{key:"currentScenario",get:function(){var t=this._scenarios.filter(function(t){return!t.isFinished});return 0===t.length?null:(null===this._lastScenario?this._lastScenario=t[0]:this._lastScenario.name!==t[0].name&&t[0].isInheritor&&t[0].postconditions.join(this._lastScenario.postconditions),t[0])}}]),t}(),H=/*#__PURE__*/function(){function t(t,n,i,r,o,s){if(void 0===s&&(s=null),this._interactions=void 0,this._finishingConditions=void 0,this._globalPostconditions=void 0,this._onTurnPassed=void 0,this._currentInteraction=void 0,this._selectableInteractions=void 0,this._interactor=void 0,this._name=void 0,this._map=void 0,this._agents=void 0,this._historic=void 0,this._isFinished=void 0,this._turn=void 0,this._isInheritor=void 0,null==t||""===t.trim())throw new Error("Name cannot be empty.");if(null==n)throw new Error("Map cannot be null.");if(null==i)throw new Error("Agents cannot be null.");if(null==r)throw new Error("Interactions cannot be null.");if(null==o)throw new Error("FinishingConditions cannot be null.");this._name=t,this._map=n,this._agents=i,this._interactions=r,this._finishingConditions=o,this._onTurnPassed=null==s?function(t,e){return""}:s,this._isFinished=!1,this._selectableInteractions=[],this._interactor=null,this._historic=new D,this._globalPostconditions=new e.TruthTable,this._turn=0,this._isInheritor=!1}var n=t.prototype;return n.inheritor=function(){return this._isInheritor=!0,this},n.performStep=function(t){return this.thereIsCurrentInteraction?this.performCurrentInteractionStep(t):t.isVoid?this.performIAInteraction():this.performHumanInteraction(t.choiceIndex)},n.performIAInteraction=function(){if(this._finishingConditions.allMet(this))return this._isFinished=!0,v.fromContent("0");var t=this.getNextAgentWithInteractions();null==t.agent&&(t=this.getNextAgentWithInteractionsAll());var e=t.agent,n=t.possibleInteractions;return null==e?(this._isFinished=!0,v.fromContent("1")):e.IsHuman?(this._interactor=e,this._selectableInteractions=n,1===this._selectableInteractions.length?(this._currentInteraction=this._selectableInteractions[0],this._historic.add(e,this._currentInteraction),this.performCurrentInteractionStep(u.void())):v.fromChoices(this._selectableInteractions.map(function(t){return t.toString()}))):(this._currentInteraction=l(n),this._historic.add(e,this._currentInteraction),this.performCurrentInteractionStep(u.void()))},n.getNextAgentWithInteractions=function(){for(var t=null,e=null,n=0;null==t&&n<=this._agents.count;){var i=this._agents.popRandomAgent();n++,(e=this.calculateAllAvailableInteractions(i,this._agents.allExcept(i))).length>0&&(t=i)}return{agent:t,possibleInteractions:e}},n.getNextAgentWithInteractionsAll=function(){for(var t,e=null,n=null,i=a(this._agents.all);!(t=i()).done;){var r=t.value;if((n=this.calculateAllAvailableInteractions(r,this._agents.allExcept(r))).length>0){e=r;break}}return{agent:e,possibleInteractions:n}},n.calculateAllAvailableInteractions=function(t,e){for(var n,i=[],r=a(this._interactions);!(n=r()).done;){var o=n.value;if(o.timing!==exports.Timing.GlobalSingle||!this._historic.hasExecutedInteraction(o))for(var s,c=a(o.getPermutations(t,e));!(s=c()).done;){var u=s.value;if(o.preconditions(this._globalPostconditions,u,this._map)){var l=new S(o,u,this._map,this._globalPostconditions);o.timing!==exports.Timing.Repeteable&&this._historic.hasExecutedOnGoingInteraction(l)||i.push(l)}}}return i},n.performCurrentInteractionStep=function(t){var n=this._currentInteraction.performStep(t);if(n.isEnder){var i=this._currentInteraction.postconditions;if(this._globalPostconditions.join(i))for(var r,o=a(this._currentInteraction.interaction.isIntimate?this._currentInteraction.roles.agents:this._currentInteraction.location.agents);!(r=o()).done;){var s=r.value;s.logic.rules.elements.length>0&&e.modusPonens(s.logic.rules,s.logic.population,s.logic.table,i.elements)}this._currentInteraction=null;for(var c,u=a(this.passTurn());!(c=u()).done;)n.append(c.value)}return n},n.performHumanInteraction=function(t){return this._currentInteraction=this._selectableInteractions[t],this._historic.add(this._interactor,this._currentInteraction),this.performCurrentInteractionStep(u.void())},n.passTurn=function(){var t=this;this._turn++;for(var e,n=[this._onTurnPassed(this._turn,this)],i=a(this._agents.all.map(function(e){return e.OnTurnPassed(t._turn,e)}));!(e=i()).done;)n.push(e.value);return n},r(t,[{key:"thereIsCurrentInteraction",get:function(){return null!=this._currentInteraction}},{key:"name",get:function(){return this._name}},{key:"map",get:function(){return this._map}},{key:"agents",get:function(){return this._agents}},{key:"interactions",get:function(){return this._interactions}},{key:"historic",get:function(){return this._historic}},{key:"isFinished",get:function(){return this._isFinished}},{key:"turn",get:function(){return this._turn}},{key:"isInheritor",get:function(){return this._isInheritor}},{key:"postconditions",get:function(){return this._globalPostconditions}}]),t}();exports.Agent=y,exports.Agents=E,exports.Alternative=_,exports.BaseInteraction=R,exports.Characteristics=k,exports.Choices=m,exports.Crowd=w,exports.Deliverer=x,exports.Desire=b,exports.Desires=I,exports.FinishingConditions=C,exports.HistoricInteractions=D,exports.Input=u,exports.Interaction=P,exports.Location=N,exports.MapStructure=A,exports.OnGoingInteraction=S,exports.Phrase=d,exports.PhraseResult=f,exports.Roles=g,exports.RolesDescriptor=p,exports.Scenario=H,exports.ScenarioEndAllConditionsMet="0",exports.ScenarioEndNoInteractions="1",exports.Step=v,exports.World=T;
//# sourceMappingURL=index.js.map