1
1

EveryTurn.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /// <reference path="./Classes/Rulebook.ts" />
  2. /// <reference path="./Classes/Rule.ts" />
  3. /// <reference path="Classes/Things/Person.ts" />
  4. /// <reference path="TurnSequence.ts" />
  5. module EveryTurn {
  6. export var EveryTurn = new Rulebook("Every Turn");
  7. export var lastTurnTime = 10;
  8. export var lastPercentage = 1;
  9. export var minPercentage = 0.05;
  10. export var maxTurnDuration = 100;
  11. export var maxTurnsDead = 4;
  12. export var RunAIRule = EveryTurn.createAndAddRule({
  13. name : "Run NPC AI Rule",
  14. code : async function () {
  15. let allPeople = <Array<Person>> Thing.InsideRoomRelation.getAnyRightType(Person);
  16. let change;
  17. if (lastTurnTime > maxTurnDuration) {
  18. change = -0.1 * (lastTurnTime / maxTurnDuration);
  19. } else {
  20. change = +0.1 * (maxTurnDuration / lastTurnTime);
  21. }
  22. lastPercentage += change;
  23. lastPercentage = lastPercentage < minPercentage ? minPercentage : lastPercentage > 1 ? 1 : lastPercentage;
  24. function isAIAvailable (person : Person) {
  25. return (person != WorldState.player
  26. && (
  27. (person.getRoom() instanceof RoomRandom
  28. && (<RoomRandom>person.getRoom()).placed)
  29. )
  30. && (person.lastActionTurn < WorldState.getCurrentTurn())
  31. && (
  32. person.lastActionTurn < (WorldState.getCurrentTurn() - maxTurnsDead) || Math.random() <= lastPercentage ||
  33. person.getRoom() == WorldState.player.getRoom() || WorldState.player.getRoom().getConnectedRooms().includes(person.getRoom())
  34. )
  35. );
  36. }
  37. let people = allPeople.filter(isAIAvailable);
  38. let t0 = performance.now();
  39. for (let i = 0; i < people.length; i++) {
  40. let person = people[i];
  41. if (person.getHealthOnScale() > 0) {
  42. let action = await people[i].AI.execute();
  43. let visible = people[i].isVisibleTo(WorldState.player); // Was the actor visible BEFORE doing the action?
  44. if (action == undefined) action = new ActionWait(person);
  45. let printValue: Say = await action.execute();
  46. if (
  47. (
  48. visible ||
  49. person.isVisibleTo(WorldState.player)
  50. ) && printValue != undefined) {
  51. Elements.CurrentTurnHandler.printAsContent(printValue);
  52. }
  53. }
  54. }
  55. let t1 = performance.now();
  56. lastTurnTime = t1 - t0;
  57. }
  58. });
  59. export var incrementTurnCounterRule = EveryTurn.createAndAddRule({
  60. firstPriority: Rule.PRIORITY_LOWEST,
  61. priority: Rule.PRIORITY_LOWEST,
  62. name: "Increment Turn Counter",
  63. code: function () {
  64. WorldState.incrementWorldTurn();
  65. }
  66. });
  67. }