Person.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /// <reference path="../Thing.ts" />
  2. /// <reference path="../AI.ts" />
  3. /// <reference path="Person/Attribute.ts" />
  4. /// <reference path="Person/Skill.ts" />
  5. /// <reference path="../Container/Corpse.ts" />
  6. /**
  7. * A person is a thing that happens to be "alive" in some significant way, not necessarily human.
  8. * A robot is a person.
  9. * A dog is a person.
  10. *
  11. * Fuck your standards.
  12. */
  13. class Person extends Thing implements AttributeBearer, SkillBearer {
  14. public AI = new AI({actor: this});
  15. public animated = true;
  16. public soreness = 0;
  17. public lastHealthUpdate = 0;
  18. public stamina = 10;
  19. public lastStaminaUpdate = 0;
  20. public staminaPerTurn = 1;
  21. public static MAX_STAMINA = 10;
  22. public static STRENGTH_SORENESS_MULTIPLIER = 4;
  23. public constructor (options : ThingOptions) {
  24. super(options);
  25. this.addGetAlterations((person : Person) => {
  26. // Let's not do this for NPCs. It can break patches.
  27. if (person.isPlayer()) {
  28. return {
  29. Stats: this.attributeValue,
  30. Skills: this.skillValue
  31. }
  32. }
  33. });
  34. // We *should* have the bodypart thing here, but there are issues with Humanoids and that...
  35. // this.addGetAlterations((person : Person) => {
  36. //
  37. // });
  38. this.addSetAlterations((person : Person, changes) => {
  39. // Let's not do this for NPCs. It can break patches.
  40. if (person.isPlayer()) {
  41. if (changes.Stats != undefined) {
  42. for (let name in changes.Stats) {
  43. let attr = Attribute.getAttribute(name);
  44. if (attr != undefined) {
  45. this.setStat(attr, changes.Stats[name]);
  46. }
  47. }
  48. }
  49. if (changes.Skills != undefined) {
  50. for (let name in changes.Skills) {
  51. let attr = Skill.getSkill(name);
  52. if (attr != undefined) {
  53. this.setSkill(attr, changes.Skills[name]);
  54. }
  55. }
  56. }
  57. }
  58. });
  59. }
  60. public changeHealth (n : number) {
  61. let bodyparts = <Array<Bodypart>> this.getParts(Bodypart);
  62. for (let i = 0; i < bodyparts.length; i++) {
  63. bodyparts[i].changeSoreness(n);
  64. }
  65. this.updateHealth();
  66. }
  67. /**
  68. * Returns health as a number from 0 to 10.
  69. */
  70. public getHealthOnScale () {
  71. return Math.round(
  72. ((this.getHealth() * 10) / (this.getStat(Attributes.Strength) * 2))
  73. );
  74. }
  75. /**
  76. * Returns current health.
  77. * Important = force update, otherwise use sufficiently accurate current value. to prevent looping too much.
  78. * @param {boolean} important
  79. * @returns {number}
  80. */
  81. public getHealth (important? : boolean) {
  82. if (important === true || this.lastHealthUpdate != WorldState.getCurrentTurn()) {
  83. this.updateHealth();
  84. }
  85. return this.soreness / (this.getStat(Attributes.Strength) * Person.STRENGTH_SORENESS_MULTIPLIER)
  86. }
  87. /**
  88. * Lazy updates allow us to count Health/Stamina for NPCs without overloading the Every Turn rulebook.
  89. */
  90. public updateHealth () {
  91. let health = 0;
  92. let bodyparts = this.getParts(Bodypart);
  93. for (let i = 0; i < bodyparts.length; i++) {
  94. health += bodyparts[i].getWeightedSoreness();
  95. }
  96. this.soreness = health;
  97. this.lastHealthUpdate = WorldState.getCurrentTurn();
  98. }
  99. public changeStamina (n : number) {
  100. this.updateStamina();
  101. this.stamina += n;
  102. if (this.stamina > Person.MAX_STAMINA) {
  103. this.stamina = Person.MAX_STAMINA;
  104. } else if (this.stamina < 0) {
  105. this.stamina = 0;
  106. }
  107. }
  108. /**
  109. * Returns stamina as a number from 0 to 10.
  110. */
  111. public getStaminaOnScale () {
  112. return Math.round(
  113. ((this.stamina * 10) / Person.MAX_STAMINA)
  114. );
  115. }
  116. public updateStamina () {
  117. var nTurns = WorldState.getCurrentTurn() - this.lastStaminaUpdate;
  118. this.stamina += this.staminaPerTurn * nTurns;
  119. if (this.stamina > Person.MAX_STAMINA) {
  120. this.stamina = Person.MAX_STAMINA;
  121. }
  122. }
  123. public isPlayer () {
  124. return (<any> this) == WorldState.player;
  125. }
  126. protected attributeValue : {[id : string] : number} = {};
  127. protected skillValue : {[id : string] : number} = {};
  128. public getStat (stat : Attribute) {
  129. if (this.attributeValue[stat.id] == undefined) {
  130. this.attributeValue[stat.id] = stat.defaultValue;
  131. }
  132. return this.attributeValue[stat.id];
  133. }
  134. public setStat (stat : Attribute, value : number) {
  135. this.attributeValue[stat.id] = value;
  136. }
  137. public getSkill (stat : Skill) {
  138. if (this.skillValue[stat.id] == undefined) {
  139. this.skillValue[stat.id] = stat.defaultValue;
  140. }
  141. return this.skillValue[stat.id];
  142. }
  143. public setSkill (stat : Skill, value : number) {
  144. this.skillValue[stat.id] = value;
  145. }
  146. public die () {
  147. let corpse = new Corpse({
  148. name : this.name + "'s corpse",
  149. unique : false,
  150. description : new Say("The lifeless body of ", this, ". May ", new SayHeSheIt(this), " rest in peace.")
  151. });
  152. corpse.put(...Thing.CarryRelation.getRight(this));
  153. corpse.put(...Thing.WearRelation.getRight(this));
  154. this.getRoom().place(corpse);
  155. OutOfPlay.removeFromPlay(this);
  156. }
  157. }