Bladeren bron

Add Follow Action
Fix bug with "Instead of" actions losing their essence

Reddo 5 jaren geleden
bovenliggende
commit
17cb62e307

+ 9 - 7
app/Elements/Modules/HyperlinkHandler.ts

@@ -46,16 +46,18 @@ module Elements.HyperlinkHandler {
 
     export async function hyperlinkObject (thing? : any) {
         resetAvailableActions();
-        if (thing instanceof Thing && thing != WorldState.player && thing.isVisibleTo(WorldState.player)) {
+        if (thing instanceof Thing && thing != WorldState.player) {
             await HyperlinkingRulebook.execute({noun: thing});
 
-            currentActionTarget.nodeValue = thing.getPrintedName() + ": ";
+            if (availableActions.length > 0) {
+                currentActionTarget.nodeValue = thing.getPrintedName() + ": ";
 
-            for (let i = 0, value = availableActions[i]; value != undefined; value = availableActions[++i]) {
-                let link = createLink(value);
-                link.classList.add("columnLink");
-                Controls.KeyHandler.applyCode(link, Controls.KeyHandler.getSecondKeyCode());
-                linkedActionsTab.appendChild(link);
+                for (let i = 0, value = availableActions[i]; value != undefined; value = availableActions[++i]) {
+                    let link = createLink(value);
+                    link.classList.add("columnLink");
+                    Controls.KeyHandler.applyCode(link, Controls.KeyHandler.getSecondKeyCode());
+                    linkedActionsTab.appendChild(link);
+                }
             }
         }
     }

+ 3 - 0
app/World/Classes/Action.ts

@@ -55,9 +55,12 @@ class Action {
             return;
         } else if(result instanceof Action) {
             console.debug(Rulebook.getIndentation() + "[ACTION] Instead of...");
+            let originalNouns = this.nouns;
             await result.execute();
             this.say.add(result.say);
             this.nouns = result.nouns;
+            // Reset to initial state
+            this.nouns = originalNouns;
             return;
         }
 

+ 60 - 0
app/World/Classes/Action/ActionFollow.ts

@@ -0,0 +1,60 @@
+/// <reference path="../Action.ts" />
+/// <reference path="../Rule.ts" />
+/// <reference path="../Rulebook.ts" />
+class ActionFollow extends Action {
+    public static check = new Rulebook<ActionFollow>("Check Follow");
+    public static carry = new Rulebook<ActionFollow>("Carry out Follow");
+
+    public constructor (actor : Thing, ...nouns : Array<any>) {
+        super(actor, ...nouns);
+        this.requiresNoun = true;
+        this.requiresVisibility = false;
+    }
+
+    public getCommandText () {
+        return "follow " + this.getNoun(0).getPrintedName();
+    }
+
+    public static isCloseEnough (actor : Person, stalked : Thing) {
+        let cRoom = <RoomRandom> actor.getRoom();
+        let tRoom = <RoomRandom> stalked.getRoom();
+        let distance = tRoom.getDistanceTo(cRoom);
+
+        if (distance == 1 || distance < (actor.getStat(Attributes.Intelligence)/2)) {
+            return true;
+        }
+        return false;
+    }
+
+    public checkFollowClose = ActionFollow.check.createAndAddRule({
+        name : "Follow - Is the stalked close enough?",
+        code : (rulebook) => {
+            let action = <ActionFollow> rulebook.noun;
+            let actor = <Person> action.actor;
+            let stalked = action.getNoun(0);
+            let cRoom = <RoomRandom> actor.getRoom();
+            let tRoom = <RoomRandom> stalked.getRoom();
+
+            if (ActionFollow.isCloseEnough(actor, stalked)) {
+                let direction = cRoom.getAStarBestDirectionTo(tRoom);
+                return new ActionGo(actor, direction);
+            } else {
+                action.stop();
+            }
+        }
+    })
+}
+
+Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
+    {
+        name : "Hyperlink - Follow",
+        firstPriority : Rule.PRIORITY_HIGHEST,
+        code : (rulebook : RulebookRunner<Thing>) => {
+            let thing = <Thing> rulebook.noun;
+
+            if (thing instanceof Person && thing.getRoom() != WorldState.player.getRoom() && ActionFollow.isCloseEnough(WorldState.player, thing)) {
+                Elements.HyperlinkHandler.addAvailableAction("Follow", new ActionFollow(WorldState.player, thing));
+            }
+        }
+    }
+));

+ 1 - 1
app/World/EveryTurn.ts

@@ -16,7 +16,7 @@ module EveryTurn {
 
             let people = <Array<Person>> Thing.InsideRoomRelation.getAnyRightType(Person).filter(isAIAvailable);
             for (let i = 0; i < people.length; i++) {
-                let action = await people[i].AI.execute(people[i]);
+                let action = await people[i].AI.execute();
                 let person = people[i];
                 let visible = people[i].isVisibleTo(WorldState.player);
 

+ 1 - 0
app/World/TurnSequence.ts

@@ -99,6 +99,7 @@ module TurnSequence {
             await Elements.RememberedHandler.updateMap();
 
             let playerAction = <Action> rulebook.noun;
+            console.log(playerAction);
             if (playerAction) {
                 await Elements.HyperlinkHandler.hyperlinkObject(playerAction.getNoun(0));
             } else {