Trigger.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "Trigger.h"
  2. #include "Gegenstand.h"
  3. VarPointer::VarPointer( const char *name, Variable *var )
  4. {
  5. this->name = name;
  6. this->var = var;
  7. ref = 1;
  8. }
  9. VarPointer::~VarPointer()
  10. {
  11. if( var )
  12. var->release();
  13. }
  14. Text VarPointer::getName() const
  15. {
  16. return name;
  17. }
  18. void VarPointer::setVariable( Variable *var )
  19. {
  20. if( this->var )
  21. this->var->release();
  22. this->var = var;
  23. }
  24. Variable *VarPointer::getVariable() const
  25. {
  26. return var ? var->getThis() : 0;
  27. }
  28. Variable *VarPointer::zVariable() const
  29. {
  30. return var;
  31. }
  32. VarPointer::operator Variable *( ) const
  33. {
  34. return var;
  35. }
  36. VarPointer *VarPointer::getThis()
  37. {
  38. ref++;
  39. return this;
  40. }
  41. VarPointer *VarPointer::release()
  42. {
  43. if( !--ref )
  44. delete this;
  45. return 0;
  46. }
  47. LocalMemory::LocalMemory()
  48. {
  49. ref = 1;
  50. }
  51. LocalMemory::~LocalMemory()
  52. {}
  53. void LocalMemory::setVar( const char *name, Variable *var )
  54. {
  55. for( auto v = vars.getIterator(); v; v++ )
  56. {
  57. if( v->getName().istGleich( name ) )
  58. {
  59. v->setVariable( var );
  60. return;
  61. }
  62. }
  63. vars.add( new VarPointer( name, var ) );
  64. }
  65. Variable *LocalMemory::getVariable( const char *name )
  66. {
  67. for( auto v = vars.getIterator(); v; v++ )
  68. {
  69. if( v->getName().istGleich( name ) )
  70. return v->getVariable();
  71. }
  72. return 0;
  73. }
  74. Variable *LocalMemory::zVariable( const char *name )
  75. {
  76. for( auto v = vars.getIterator(); v; v++ )
  77. {
  78. if( v->getName().istGleich( name ) )
  79. return v->zVariable();
  80. }
  81. return 0;
  82. }
  83. LocalMemory *LocalMemory::getThis()
  84. {
  85. ref++;
  86. return this;
  87. }
  88. LocalMemory *LocalMemory::release()
  89. {
  90. if( !--ref )
  91. delete this;
  92. return 0;
  93. }
  94. ProgramCounter::ProgramCounter()
  95. {
  96. current.add( 0 );
  97. depth = 0;
  98. ref = 1;
  99. }
  100. ProgramCounter::~ProgramCounter()
  101. {}
  102. void ProgramCounter::stepInto()
  103. {
  104. depth++;
  105. if( current.getEintragAnzahl() <= depth )
  106. current.add( 0 );
  107. }
  108. void ProgramCounter::count()
  109. {
  110. current.set( current.get( depth ) + 1, depth );
  111. while( depth + 1 < current.getEintragAnzahl() )
  112. current.remove( depth + 1 );
  113. }
  114. void ProgramCounter::stepOut()
  115. {
  116. depth--;
  117. }
  118. int ProgramCounter::currentPosition() const
  119. {
  120. return current.get( depth );
  121. }
  122. ProgramCounter *ProgramCounter::getThis()
  123. {
  124. ref++;
  125. return this;
  126. }
  127. ProgramCounter *ProgramCounter::release()
  128. {
  129. if( !--ref )
  130. delete this;
  131. return 0;
  132. }
  133. Bedingung::Bedingung( Aktion *expression )
  134. {
  135. this->expression = expression;
  136. ref = 1;
  137. }
  138. Bedingung::~Bedingung()
  139. {
  140. if( expression )
  141. expression->release();
  142. }
  143. void Bedingung::setExpression( Aktion *expr )
  144. {
  145. if( expression )
  146. expression->release();
  147. expression = expr;
  148. }
  149. bool Bedingung::check( Spiel *zSpiel, Ereignis *zEreignis )
  150. {
  151. if( !expression )
  152. return 1;
  153. double wait;
  154. ProgramCounter c;
  155. LocalMemory m;
  156. while( !expression->runNext( zSpiel, zEreignis, &m, &c, wait ) );
  157. Variable *var = m.zVariable( "__return__" );
  158. if( !var )
  159. return 0;
  160. switch( var->getVariableTyp() )
  161. {
  162. case NICHTS:
  163. return 0;
  164. case INTEGER:
  165. return ( (Integer *)var )->getValue() != 0;
  166. case BOOLEAN:
  167. return ( (Boolean *)var )->getValue();
  168. case STRING:
  169. return ( (String *)var )->getValue().getLength();
  170. case FLOAT:
  171. return ( (Float *)var )->getValue() != 0;
  172. case TASTE:
  173. return ( (Integer *)var )->getValue() != 0;
  174. case GEGENSTAND_TYP:
  175. return ( (GegenstandTypVar *)var )->getValue() != KEIN_GEGENSTAND;
  176. default:
  177. return 1;
  178. }
  179. }
  180. Bedingung *Bedingung::getThis()
  181. {
  182. ref++;
  183. return this;
  184. }
  185. Bedingung *Bedingung::release()
  186. {
  187. if( !--ref )
  188. delete this;
  189. return 0;
  190. }
  191. Trigger::Trigger( int id, const char *name, int ereignisAnzahl, EreignisTyp *ereignisse, RCArray< Bedingung > *bedingungen, RCArray< Aktion > *aktionen )
  192. : Variable( TRIGGER )
  193. {
  194. this->id = id;
  195. this->name = name;
  196. this->ereignisAnzahl = ereignisAnzahl;
  197. this->ereignisse = ereignisse;
  198. this->bedingungen = bedingungen;
  199. this->aktionen = aktionen;
  200. }
  201. Trigger::~Trigger()
  202. {
  203. delete[]ereignisse;
  204. bedingungen->release();
  205. aktionen->release();
  206. }
  207. bool Trigger::hatEreignis( EreignisTyp typ ) const
  208. {
  209. for( int i = 0; i < ereignisAnzahl; i++ )
  210. {
  211. if( ereignisse[ i ] == typ )
  212. return 1;
  213. }
  214. return 0;
  215. }
  216. int Trigger::getAktionAnzahl() const
  217. {
  218. return aktionen->getEintragAnzahl();
  219. }
  220. Aktion *Trigger::zAktion( int index ) const
  221. {
  222. return aktionen->z( index );
  223. }
  224. Aktion *Trigger::getAktion( int index ) const
  225. {
  226. return aktionen->get( index );
  227. }
  228. // return: 0, falls die bedingungen nicht erfüllt sind
  229. TriggerRun *Trigger::runTrigger( Ereignis *e, Spiel *zSpiel )
  230. {
  231. return new TriggerRun( (Trigger *)getThis(), e, zSpiel );
  232. }
  233. int Trigger::getId() const
  234. {
  235. return id;
  236. }
  237. TriggerRun::TriggerRun( Trigger *trig, Ereignis *e, Spiel *zSpiel )
  238. {
  239. trigger = trig;
  240. ereignis = e;
  241. this->zSpiel = zSpiel;
  242. waitCount = 0;
  243. ref = 1;
  244. }
  245. TriggerRun::~TriggerRun()
  246. {
  247. trigger->release();
  248. ereignis->release();
  249. }
  250. // gibt 0 zurück, wenn der Auslöser vollständig durchgelaufen ist
  251. bool TriggerRun::runNext( double t )
  252. {
  253. if( waitCount > 0 )
  254. waitCount -= t;
  255. else
  256. {
  257. int current = counter.currentPosition();
  258. if( current >= trigger->getAktionAnzahl() )
  259. return 0;
  260. Aktion *ak = trigger->zAktion( current );
  261. if( !ak || ak->runNext( zSpiel, ereignis, &localMem, &counter, waitCount ) )
  262. counter.count();
  263. if( counter.currentPosition() >= trigger->getAktionAnzahl() )
  264. return 0;
  265. }
  266. return 1;
  267. }
  268. Trigger *TriggerRun::getTrigger() const
  269. {
  270. return (Trigger *)trigger->getThis();
  271. }
  272. TriggerRun *TriggerRun::getThis()
  273. {
  274. ref++;
  275. return this;
  276. }
  277. TriggerRun *TriggerRun::release()
  278. {
  279. if( !--ref )
  280. delete this;
  281. return 0;
  282. }