UNPKG

agents-flow

Version:

Framework for multi-agent interactive fiction videogame.

3 lines (2 loc) 29.9 kB
import{EffectDirection as t,Effect as e,Situation as n,apply as i,Reactions as r}from"npc-emotional";import{Sentence as o,LogicAgent as s,Individual as a,TruthTable as u,modusPonens as c}from"first-order-logic";import{Queue as l}from"data-structs-n-algos";function h(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 f(t,e,n){return e&&h(t.prototype,e),n&&h(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function _(t,e){return _=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},_(t,e)}function d(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 v(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 d(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)?d(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 g,p=/*#__PURE__*/function(){function t(t){this._choiceIndex=void 0,this._choiceIndex=t}return t.void=function(){return new t(-1)},f(t,[{key:"choiceIndex",get:function(){return this._choiceIndex}},{key:"isVoid",get:function(){return this._choiceIndex<0}}]),t}();function m(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"}(g||(g={}));var y,w=/*#__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 f(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}(),b=/*#__PURE__*/function(){function t(t,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==t)throw new Error("Text function cannot be null.");this._text=t,this._effect=null==n?function(t){return e.null()}:n,this._sentence=i}return f(t,[{key:"text",get:function(){return this._text}},{key:"effect",get:function(){return this._effect}},{key:"sentence",get:function(){return this._sentence}}]),t}(),I=/*#__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 r=e.prototype;return r.allAlternatives=function(t){return this._alternatives.map(function(e){return e.text(t)})},r.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 b(t,e,n)),this},r.intimate=function(){this._isIntimate=!0},r.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 w(t.get(e.uncorcheted(s[0])).say(t.assignRoles(s[1])),r.effect(t),null==r.sentence?null:r.sentence(t))}return new w(t.assignRoles(o),r.effect(t),null==r.sentence?null:r.sentence(t))},r.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)},r.performerHasMeaningfulDesires=function(t,e){var n=t.get(this._performerRole).Desires;return n.any&&n.anyInvolved(e.all)},r.calculateAlternativeBasedOnDesires=function(e,r){for(var o,s=this,a=-1e6,u=e.get(this._performerRole),c=this._alternatives[0],l=a,h=function(){var h=o.value,f=a,v=h.effect(e);v.direction===t.Neutral?f=u.Desires.heuristicTotal(r):(_=r.copy(),d=new n(_.get(u.Name),_.get(e.get(v.target).Name),s._isIntimate?v.isThirdPerson?[_.get(e.get(s._targetRole).Name)]:[]:_.all.filter(function(t){return t.Name!==e.get(v.target).Name&&t.Name!==u.Name})),i(v,d,s._isIntimate),f=u.Desires.heuristicTotal(_)),f>l&&(c=h,l=f)},f=v(this._alternatives);!(o=f()).done;){var _,d;h()}return c},r.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),m(n)},r.calculateEffectDirection=function(e){return e.isHappy?t.Positive:e.isUnhappy?t.Negative:t.Neutral},r.calculateEffectDirectionWithRelation=function(e,n){if(e.isPonderatedGood&&n.isHappy)return t.Positive;if(e.isPonderatedBad&&n.isUnhappy)return t.Negative;if(e.isPonderatedNeutral&&n.isNeutral)return t.Neutral;if(e.isPonderatedNeutral&&n.isHappy)return t.Positive;if(e.isPonderatedNeutral&&n.isUnhappy)return t.Negative;if(e.isPonderatedGood&&n.isNeutral)return t.Positive;if(e.isPonderatedBad&&n.isNeutral)return t.Negative;if(e.isPonderatedGood&&n.isUnhappy)return t.Neutral;if(e.isPonderatedBad&&n.isHappy)return t.Neutral;throw new Error("Not existing option.")},e.uncorcheted=function(t){return t.replace("[","").replace("]","")},f(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}(),k=/*#__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 f(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}(),E=/*#__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=v(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=v(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.")},f(t,[{key:"roleNames",get:function(){return Array.from(this._roles.keys())}},{key:"agents",get:function(){return Array.from(this._roles.values())}}]),t}(),N=/*#__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 A(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},f(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}(),A=/*#__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},f(t,[{key:"count",get:function(){return this._items.length}},{key:"items",get:function(){return this._items}}]),t}(),P=/*#__PURE__*/function(){function t(t,e,n,i,r,o,a,u,c){if(void 0===c&&(c=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==e)throw new Error("Aspect cannot be null.");if(null==n)throw new Error("Relations cannot be null.");if(null==i)throw new Error("Happiness cannot be null.");if(null==r)throw new Error("Personality cannot be null.");if(null==o)throw new Error("Likes cannot be null.");if(null==a)throw new Error("Characteristics cannot be null.");this._name=t,this._aspect=e,this._relations=n,this._happiness=i,this._personality=r,this._likes=o,this._characteristics=new H(t,a),this._desires=new S,this._isHuman=u,this._isActive=!0,this._onTurnPassed=null==c?function(t,e){return""}:c,this._logic=new s,this._logic.population.add(this.Individual)}var e=t.prototype;return e.say=function(t){if(null==t)throw new Error("Content cannot be null.");return this.Name+": - "+t},e.activate=function(){this._isActive=!0},e.deactivate=function(){this._isActive=!1},e.humanize=function(){this._isHuman=!0},e.dehumanize=function(){this._isHuman=!1},e.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)},e.passTurn=function(t){return this._onTurnPassed(t,this)},f(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 a(this.Name.replace(" ",""))}},{key:"logic",get:function(){return this._logic}}]),t}(),R=/*#__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=v(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()}))},f(t,[{key:"all",get:function(){return Array.from(this._crowd.values())}}]),t}(),x=/*#__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)})},f(t,[{key:"heuristic",get:function(){return this._heuristic}}]),t}(),S=/*#__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)},f(t,[{key:"any",get:function(){return this._desires.length>0}}]),t}(),C=/*#__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=v(this._availableAgents);!(t=n()).done;)for(var i=t.value,r=0;r<i.Personality.introvertyExtroverty;r++)e.push(i);var o=m(e);return this._availableAgents=this._availableAgents.filter(function(t){return t.Name!==o.Name}),o},f(t,[{key:"all",get:function(){return this._agents}},{key:"count",get:function(){return this._agents.length}}]),t}(),H=/*#__PURE__*/function(){function t(t,e){if(void 0===e&&(e=[]),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 u;for(var n,i=v(e);!(n=i()).done;){var r=n.value;null!=t&&""!==t.trim()&&this._table.add(o.build(r,this._name))}}return t.prototype.is=function(t){if(null==t||""===t.trim())throw new Error("Characteristic cannot be empty.");return this._table.exists(o.build(t,this._name))},f(t,[{key:"table",get:function(){return this._table}}]),t}(),D=/*#__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=v(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)},f(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}(),T=/*#__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=v(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}();!function(t){t[t.Single=0]="Single",t[t.GlobalSingle=1]="GlobalSingle",t[t.Repeteable=2]="Repeteable"}(y||(y={}));var M=/*#__PURE__*/function(){function t(t){if(this._phrases=void 0,null==t)throw new Error("Phrases cannot be null.");this._phrases=new l(t)}return t.prototype.deliver=function(t){if(!this.isFinished)return this._phrases.dequeue();throw new Error("No phrases to deliver.")},f(t,[{key:"isFinished",get:function(){return this._phrases.isEmpty()}}]),t}(),O=/*#__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=v(this.calculateAgentCombinations(e,i.secondarys.length));!(r=o()).done;){var s=r.value;if(0!==s.length)for(var a,u=v(s);!(a=u()).done;){var c=a.value,l=(new E).match(i.main,t);l.match(i.secondarys[0],c);var h=i.secondarys.filter(function(t){return!l.has(t)}),f=s.filter(function(t){return!l.matched(t)});this.fillRemainingPermutations(l,h,f),n.push(l)}else n.push((new E).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=v(n);!(r=o()).done;)for(var s=r.value,a=i.length,u=0;u<a;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=v(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)}},f(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}(),G=/*#__PURE__*/function(t){var e,n;function i(e,n,i,r,o,s,a){var c;if(void 0===o&&(o=y.Single),void 0===s&&(s=function(t,e,n){return!0}),void 0===a&&(a=function(t,e){return u.empty}),(c=t.call(this,e,n,o,s,a)||this)._phrases=void 0,c._rolesDescriptor=void 0,null==i)throw new Error("Roles descriptor cannot be null.");if(null==r||0===r.length)throw new Error("Interaction must have almost one phrase.");return c._rolesDescriptor=i,c._phrases=r,c}return n=t,(e=i).prototype=Object.create(n.prototype),e.prototype.constructor=e,_(e,n),i.prototype.intimate=function(){this._isIntimate=!0;for(var t,e=v(this._phrases);!(t=e()).done;)t.value.intimate();return this},f(i,[{key:"deliverer",get:function(){return new M(this._phrases)}},{key:"rolesDescriptor",get:function(){return this._rolesDescriptor}}]),i}(O),j=/*#__PURE__*/function(){function o(t,n,i,r){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==t)throw new Error("Interaction cannot be null.");if(null==n)throw new Error("Roles cannot be null.");if(null==i)throw new Error("Map cannot be null.");if(null==r)throw new Error("Postconditions table cannot be null.");this._interaction=t,this._roles=n,this._map=i,this._table=r,this._effect=e.null(),this._performerRole=null,this._targetRole=null,this._onGoingPostconditions=new u}var s=o.prototype;return s.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?N.fromChoices(this._actualPhrase.allAlternatives(this._roles).map(function(t){return e._roles.assignRoles(t)})):this.executePhraseAction(t)):this.executePhraseAction(t):this.calculateAgentsReactions()},s.initializeDeliverer=function(){null==this._deliverer&&(this._deliverer=this._interaction.deliverer)},s.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))},s.executePhraseAction=function(t){var e=this._actualPhrase.action(this._roles,new R(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),N.fromContent(e.content,this.isInteractionEnded)},s.calculateAgentsReactions=function(){var o=this,s=new r;if(this._effect.direction!=t.Neutral){var a=this._effect.isTargeted?new n(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!==o._roles.get(o._effect.target).Name&&t.Name!==o._roles.get(o._performerRole).Name})):new n(this._roles.get(this._performerRole),null,this.location.agents.filter(function(t){return t.Name!==o._roles.get(o._performerRole).Name}));s.append(i(this._effect,a,this._interaction.isIntimate))}return this._effect=e.null(),N.fromReactions(s,this.isInteractionEnded)},s.equals=function(t){return this._interaction.equals(t.interaction)&&this._roles.equals(t.roles)},s.toString=function(){return this._roles.assignRoles(this._interaction.description)},f(o,[{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}}]),o}(),F=/*#__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}(),q=/*#__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}(),W=/*#__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)},f(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}(),L="0",U="1",B=/*#__PURE__*/function(){function t(t,e,n,i,r,o){if(void 0===o&&(o=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==e)throw new Error("Map cannot be null.");if(null==n)throw new Error("Agents cannot be null.");if(null==i)throw new Error("Interactions cannot be null.");if(null==r)throw new Error("FinishingConditions cannot be null.");this._name=t,this._map=e,this._agents=n,this._interactions=i,this._finishingConditions=r,this._onTurnPassed=null==o?function(t,e){return""}:o,this._isFinished=!1,this._selectableInteractions=[],this._interactor=null,this._historic=new F,this._globalPostconditions=new u,this._turn=0,this._isInheritor=!1}var e=t.prototype;return e.inheritor=function(){return this._isInheritor=!0,this},e.performStep=function(t){return this.thereIsCurrentInteraction?this.performCurrentInteractionStep(t):t.isVoid?this.performIAInteraction():this.performHumanInteraction(t.choiceIndex)},e.performIAInteraction=function(){if(this._finishingConditions.allMet(this))return this._isFinished=!0,N.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,N.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(p.void())):N.fromChoices(this._selectableInteractions.map(function(t){return t.toString()}))):(this._currentInteraction=m(n),this._historic.add(e,this._currentInteraction),this.performCurrentInteractionStep(p.void()))},e.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}},e.getNextAgentWithInteractionsAll=function(){for(var t,e=null,n=null,i=v(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}},e.calculateAllAvailableInteractions=function(t,e){for(var n,i=[],r=v(this._interactions);!(n=r()).done;){var o=n.value;if(o.timing!==y.GlobalSingle||!this._historic.hasExecutedInteraction(o))for(var s,a=v(o.getPermutations(t,e));!(s=a()).done;){var u=s.value;if(o.preconditions(this._globalPostconditions,u,this._map)){var c=new j(o,u,this._map,this._globalPostconditions);o.timing!==y.Repeteable&&this._historic.hasExecutedOnGoingInteraction(c)||i.push(c)}}}return i},e.performCurrentInteractionStep=function(t){var e=this._currentInteraction.performStep(t);if(e.isEnder){var n=this._currentInteraction.postconditions;if(this._globalPostconditions.join(n))for(var i,r=v(this._currentInteraction.interaction.isIntimate?this._currentInteraction.roles.agents:this._currentInteraction.location.agents);!(i=r()).done;){var o=i.value;o.logic.rules.elements.length>0&&c(o.logic.rules,o.logic.population,o.logic.table,n.elements)}this._currentInteraction=null;for(var s,a=v(this.passTurn());!(s=a()).done;)e.append(s.value)}return e},e.performHumanInteraction=function(t){return this._currentInteraction=this._selectableInteractions[t],this._historic.add(this._interactor,this._currentInteraction),this.performCurrentInteractionStep(p.void())},e.passTurn=function(){var t=this;this._turn++;for(var e,n=[this._onTurnPassed(this._turn,this)],i=v(this._agents.all.map(function(e){return e.OnTurnPassed(t._turn,e)}));!(e=i()).done;)n.push(e.value);return n},f(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}();export{P as Agent,C as Agents,b as Alternative,O as BaseInteraction,H as Characteristics,A as Choices,R as Crowd,M as Deliverer,x as Desire,S as Desires,q as FinishingConditions,F as HistoricInteractions,p as Input,G as Interaction,D as Location,T as MapStructure,j as OnGoingInteraction,I as Phrase,w as PhraseResult,E as Roles,k as RolesDescriptor,B as Scenario,L as ScenarioEndAllConditionsMet,U as ScenarioEndNoInteractions,N as Step,y as Timing,W as World}; //# sourceMappingURL=index.mjs.map