Person.ts 6.0 KB

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