Quellcode durchsuchen

Autmoatically throttle AI in the event of the game becoming too slow. Should never trigger outside of stress testing, though.

Reddo vor 5 Jahren
Ursprung
Commit
c200472d21
1 geänderte Dateien mit 30 neuen und 4 gelöschten Zeilen
  1. 30 4
      app/World/EveryTurn.ts

+ 30 - 4
app/World/EveryTurn.ts

@@ -1,21 +1,45 @@
 /// <reference path="./Classes/Rulebook.ts" />
 /// <reference path="./Classes/Rule.ts" />
 /// <reference path="Classes/Things/Person.ts" />
+/// <reference path="TurnSequence.ts" />
 module EveryTurn {
     export var EveryTurn = new Rulebook("Every Turn");
+    export var lastTurnTime = 10;
+    export var lastPercentage = 1;
+    export var minPercentage = 0.05;
+    export var maxTurnDuration = 100;
+    export var maxTurnsDead = 4;
 
     export var RunAIRule = EveryTurn.createAndAddRule({
         name : "Run NPC AI Rule",
         code : async function () {
+            let allPeople = <Array<Person>> Thing.InsideRoomRelation.getAnyRightType(Person);
+            let change;
+            if (lastTurnTime > maxTurnDuration) {
+                change = -0.1 * (lastTurnTime / maxTurnDuration);
+            } else {
+                change = +0.1 * (maxTurnDuration / lastTurnTime);
+            }
+            lastPercentage += change;
+            lastPercentage = lastPercentage < minPercentage ? minPercentage : lastPercentage > 1 ? 1 : lastPercentage;
             function isAIAvailable (person : Person) {
                 return (person != WorldState.player
-                    && ((person.getRoom() instanceof RoomRandom
-                        && (<RoomRandom> person.getRoom()).placed))
-                            && (person.lastActionTurn < WorldState.getCurrentTurn()));
+                    && (
+                        (person.getRoom() instanceof RoomRandom
+                        && (<RoomRandom>person.getRoom()).placed)
+                    )
+                    && (person.lastActionTurn < WorldState.getCurrentTurn())
+                    && (
+                        person.lastActionTurn < (WorldState.getCurrentTurn() - maxTurnsDead) || Math.random() <= lastPercentage ||
+                        person.getRoom() == WorldState.player.getRoom() || WorldState.player.getRoom().getConnectedRooms().includes(person.getRoom())
+                    )
+                );
             }
 
 
-            let people = <Array<Person>> Thing.InsideRoomRelation.getAnyRightType(Person).filter(isAIAvailable);
+            let people = allPeople.filter(isAIAvailable);
+
+            let t0 = performance.now();
             for (let i = 0; i < people.length; i++) {
                 let person = people[i];
                 if (person.getHealthOnScale() > 0) {
@@ -33,6 +57,8 @@ module EveryTurn {
                     }
                 }
             }
+            let t1 = performance.now();
+            lastTurnTime = t1 - t0;
         }
     });