1
1

Person.ts 6.2 KB

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