1. 1 : /**
  2. 2 : * Base class for representing a goal evaluator in context of Goal-driven agent design.
  3. 3 : *
  4. 4 : * @author {@link https://github.com/Mugen87|Mugen87}
  5. 5 : */
  6. 6 : class GoalEvaluator {
  7. 7 :
  8. 8 : /**
  9. 9 : * Constructs a new goal evaluator.
  10. 10 : *
  11. 11 : * @param {Number} characterBias - Can be used to adjust the preferences of agents.
  12. 12 : */
  13. 13 : constructor( characterBias = 1 ) {
  14. 14 :
  15. 15 : /**
  16. 16 : * Can be used to adjust the preferences of agents. When the desirability score
  17. 17 : * for a goal has been evaluated, it is multiplied by this value.
  18. 18 : * @type {Number}
  19. 19 : * @default 1
  20. 20 : */
  21. 21 : this.characterBias = characterBias;
  22. 22 :
  23. 23 : }
  24. 24 :
  25. 25 : /**
  26. 26 : * Calculates the desirability. It's a score between 0 and 1 representing the desirability
  27. 27 : * of a goal. This goal is considered as a top level strategy of the agent like *Explore* or
  28. 28 : * *AttackTarget*. Must be implemented by all concrete goal evaluators.
  29. 29 : *
  30. 30 : * @param {GameEntity} owner - The owner of this goal evaluator.
  31. 31 : * @return {Number} The desirability.
  32. 32 : */
  33. 33 : calculateDesirability( /* owner */ ) {
  34. 34 :
  35. 35 : return 0;
  36. 36 :
  37. 37 : }
  38. 38 :
  39. 39 : /**
  40. 40 : * Executed if this goal evaluator produces the highest desirability. Must be implemented
  41. 41 : * by all concrete goal evaluators.
  42. 42 : *
  43. 43 : * @param {GameEntity} owner - The owner of this goal evaluator.
  44. 44 : */
  45. 45 : setGoal( /* owner */ ) {}
  46. 46 :
  47. 47 : /**
  48. 48 : * Transforms this instance into a JSON object.
  49. 49 : *
  50. 50 : * @return {Object} The JSON object.
  51. 51 : */
  52. 52 : toJSON() {
  53. 53 :
  54. 54 : return {
  55. 55 : type: this.constructor.name,
  56. 56 : characterBias: this.characterBias
  57. 57 : };
  58. 58 :
  59. 59 : }
  60. 60 :
  61. 61 : /**
  62. 62 : * Restores this instance from the given JSON object.
  63. 63 : *
  64. 64 : * @param {Object} json - The JSON object.
  65. 65 : * @return {GoalEvaluator} A reference to this goal evaluator.
  66. 66 : */
  67. 67 : fromJSON( json ) {
  68. 68 :
  69. 69 : this.characterBias = json.characterBias;
  70. 70 :
  71. 71 : return this;
  72. 72 :
  73. 73 : }
  74. 74 :
  75. 75 : }
  76. 76 :
  77. 77 :
  78. 78 :
  79. 79 : export { GoalEvaluator };