Balancer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "Balancer.h"
  2. Balancer::Balancer() : levels(_FL_)
  3. {
  4. }
  5. Balancer::~Balancer()
  6. {
  7. }
  8. //Инициализировать объект
  9. bool Balancer::Create(MOPReader & reader)
  10. {
  11. levels.DelAll();
  12. level = reader.Long();
  13. maxLevel = reader.Long();
  14. minLevel = reader.Long();
  15. if(maxLevel <= minLevel)
  16. {
  17. maxLevel = minLevel + 1;
  18. LogicDebug("Invalidate min or max level, now is min level = %i, max level = %i", minLevel, maxLevel);
  19. }
  20. if(level < minLevel || level > maxLevel)
  21. {
  22. if(level < minLevel) level = minLevel;
  23. if(level > maxLevel) level = maxLevel;
  24. LogicDebug("Initial level out of range and corrected: %i", level);
  25. }
  26. long count = reader.Array();
  27. for(long i = 0; i < count; i++)
  28. {
  29. LevelTrigger & lt = levels[levels.Add()];
  30. lt.level = reader.Long();
  31. lt.incCounter.Init(reader);
  32. lt.decCounter.Init(reader);
  33. }
  34. Activate(reader.Bool());
  35. if(levels == 0)
  36. {
  37. LogicDebug("No thresholds...");
  38. }
  39. return true;
  40. }
  41. //Обработчик команд для объекта
  42. void Balancer::Command(const char * id, dword numParams, const char ** params)
  43. {
  44. if(!IsActive())
  45. {
  46. LogicDebug("Skip command %s, because object not actvie", id);
  47. return;
  48. }
  49. if(string::IsEqual(id, "inc"))
  50. {
  51. if(level < maxLevel)
  52. {
  53. level++;
  54. LogicDebug("Command <inc>, level = %i", level);
  55. CheckEvent(true);
  56. }else{
  57. LogicDebug("Command <inc>, level already is max (%i), skip events", level);
  58. }
  59. }else
  60. if(string::IsEqual(id, "dec"))
  61. {
  62. if(level > minLevel)
  63. {
  64. level--;
  65. LogicDebug("Command <dec>, level = %i", level);
  66. CheckEvent(false);
  67. }else{
  68. LogicDebug("Command <dec>, level already is min (%i), skip events", level);
  69. }
  70. }
  71. if(!numParams)
  72. {
  73. LogicDebug("Command <%s> - no param", id);
  74. return;
  75. }
  76. //
  77. char * stop;
  78. long lvl = strtol(params[0], &stop, 10);
  79. if(lvl < minLevel) lvl = minLevel;
  80. if(lvl > maxLevel) lvl = maxLevel;
  81. if(string::IsEqual(id, "set"))
  82. {
  83. level = lvl;
  84. LogicDebug("Command <set>, new level is %i", level);
  85. }else
  86. if(string::IsEqual(id, "change"))
  87. {
  88. bool isUp = (level < lvl);
  89. level = lvl ;
  90. LogicDebug("Command <change>, new level is %i", level);
  91. CheckEvent(isUp);
  92. }
  93. }
  94. //Инициализировать объект
  95. bool Balancer::EditMode_Create(MOPReader & reader)
  96. {
  97. return Create(reader);
  98. }
  99. //Обновить параметры
  100. bool Balancer::EditMode_Update(MOPReader & reader)
  101. {
  102. levels.DelAll();
  103. return Create(reader);
  104. }
  105. //Получить размеры описывающего ящика
  106. void Balancer::EditMode_GetSelectBox(Vector & min, Vector & max)
  107. {
  108. min = max = 0.0f;
  109. }
  110. void Balancer::CheckEvent(bool isUp)
  111. {
  112. for(long i = 0; i < levels; i++)
  113. {
  114. if(levels[i].level == level)
  115. {
  116. if(isUp)
  117. {
  118. LogicDebug("Activate increase event for level %i", level);
  119. levels[i].incCounter.Activate(Mission(), false);
  120. }else{
  121. LogicDebug("Activate decrease event for level %i", level);
  122. levels[i].decCounter.Activate(Mission(), false);
  123. }
  124. }
  125. }
  126. }
  127. const char * Balancer::comment =
  128. "Object for some events at some count\n"
  129. " \n"
  130. "Commands list:\n"
  131. "----------------------------------------\n"
  132. " Increase counter value\n"
  133. "----------------------------------------\n"
  134. " command: inc\n"
  135. " \n"
  136. "----------------------------------------\n"
  137. " Decrease counter value\n"
  138. "----------------------------------------\n"
  139. " command: dec\n"
  140. " \n"
  141. "----------------------------------------\n"
  142. " Set counter value without events\n"
  143. "----------------------------------------\n"
  144. " command: set\n"
  145. " parm: number value\n"
  146. " \n"
  147. "----------------------------------------\n"
  148. " Change counter value with events\n"
  149. "----------------------------------------\n"
  150. " command: change\n"
  151. " parm: number value\n"
  152. " \n"
  153. " ";
  154. MOP_BEGINLISTCG(Balancer, "Balancer", '1.00', 0x0fffffff, Balancer::comment, "Logic")
  155. MOP_LONGEX("Initial level", 0, 0, 1000000)
  156. MOP_LONGEX("Max level", 10, 0, 1000000)
  157. MOP_LONGEX("Min level", 0, 0, 1000000)
  158. MOP_ARRAYBEG("Level thresholds", 1, 1000)
  159. MOP_LONGC("Threshold", 5, "Level threshold")
  160. MOP_MISSIONTRIGGER("inc ")
  161. MOP_MISSIONTRIGGER("dec ")
  162. MOP_ARRAYEND
  163. MOP_BOOLC("Active", true, "Active trigger in start mission time")
  164. MOP_ENDLIST(Balancer)