Minigames.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #include "MiniGames.h"
  2. #include <Punkt.h>
  3. #include <Rahmen.h>
  4. #include "../../Global/Variablen.h"
  5. #include <Datei.h>
  6. #include "../../Global/Initialisierung.h"
  7. #include <InitDatei.h>
  8. #include <KSGTDatei.h>
  9. typedef MiniGameV *( *GetMiniGame )( );
  10. // Inhalt der MGLaden Klasse aus MiniGames.h
  11. // Konstruktor
  12. MGSuchen::MGSuchen( MiniGames *mGames )
  13. {
  14. this->mGames = mGames;
  15. start();
  16. }
  17. // Destruktor
  18. MGSuchen::~MGSuchen()
  19. {
  20. mGames->release();
  21. }
  22. // nicht constant
  23. void MGSuchen::thread()
  24. {
  25. KSGTDatei *dgt = new KSGTDatei( "data/dg.ksgt" );
  26. dgt->laden();
  27. bool ak = 0;
  28. int dgId = infoClient->getDateiGruppeIdVonPfad( "data/Minigames" );
  29. for( int i = 0; i < dgt->getZeilenAnzahl(); i++ )
  30. {
  31. if( dgt->zFeld( i, 0 ) && TextZuInt( dgt->zFeld( i, 0 )->getText(), 10 ) == dgId )
  32. {
  33. int lv = dgt->zFeld( i, 2 ) ? TextZuInt( dgt->zFeld( i, 2 )->getText(), 10 ) : 0;
  34. int ov = infoClient->getDateiGruppeVersion( dgId );
  35. if( lv == ov )
  36. ak = 1;
  37. break;
  38. }
  39. }
  40. dgt->release();
  41. if( !ak )
  42. {
  43. mGames->setAktuell( 0, dgId );
  44. delete this;
  45. return;
  46. }
  47. Datei *d = new Datei();
  48. d->setDatei( "data/Minigames" );
  49. if( !d->existiert() )
  50. DateiPfadErstellen( "data/MiniGames/" );
  51. RCArray< Text > *list = d->getDateiListe();
  52. if( list )
  53. {
  54. for( int i = 0; i < list->getEintragAnzahl(); i++ )
  55. {
  56. MiniGame *mg = new MiniGame( list->z( i )->getText() );
  57. if( !mg->istOk() )
  58. {
  59. mg->release();
  60. continue;
  61. }
  62. mGames->addMiniGame( mg );
  63. }
  64. list->release();
  65. }
  66. d->release();
  67. delete this;
  68. }
  69. // Inhalt der MGLaden Klasse aus MiniGameV.h
  70. // Konstruktor
  71. MGLaden::MGLaden( char *name )
  72. {
  73. this->name = new Text( name );
  74. game = 0;
  75. ref = 1;
  76. start();
  77. }
  78. // Destruktor
  79. MGLaden::~MGLaden()
  80. {
  81. if( game )
  82. {
  83. game->release();
  84. dllDateien->releaseDLL( name->getText() );
  85. }
  86. name->release();
  87. }
  88. // nicht constant
  89. void MGLaden::thread()
  90. {
  91. Text *pfad = new Text( "data/Minigames/" );
  92. pfad->append( name->getText() );
  93. if( !DateiExistiert( pfad->getThis() ) )
  94. {
  95. pfad->release();
  96. run = 0;
  97. return;
  98. }
  99. pfad->append( "/mg.ini" );
  100. if( !DateiExistiert( pfad->getThis() ) )
  101. {
  102. pfad->release();
  103. run = 0;
  104. return;
  105. }
  106. InitDatei *mgIni = new InitDatei( pfad );
  107. if( !mgIni->laden() )
  108. {
  109. mgIni->release();
  110. run = 0;
  111. return;
  112. }
  113. if( !mgIni->wertExistiert( "DllPfad" ) )
  114. {
  115. mgIni->release();
  116. run = 0;
  117. return;
  118. }
  119. Text *dllPfad = new Text( "data/Minigames/" );
  120. dllPfad->append( name->getText() );
  121. dllPfad->append( "/" );
  122. dllPfad->append( mgIni->zWert( "DllPfad" )->getText() );
  123. mgIni->release();
  124. if( !DateiExistiert( dllPfad->getThis() ) )
  125. {
  126. dllPfad->release();
  127. run = 0;
  128. return;
  129. }
  130. HMODULE dll = dllDateien->ladeDLL( name->getText(), dllPfad->getText() );
  131. dllPfad->release();
  132. if( !dll )
  133. {
  134. run = 0;
  135. return;
  136. }
  137. GetMiniGame getMiniGame = (GetMiniGame)GetProcAddress( dll, "GetMiniGame" );
  138. if( !getMiniGame )
  139. {
  140. dllDateien->releaseDLL( name->getText() );
  141. run = 0;
  142. return;
  143. }
  144. game = getMiniGame();
  145. if( !game )
  146. {
  147. dllDateien->releaseDLL( name->getText() );
  148. run = 0;
  149. return;
  150. }
  151. if( !minigameClient )
  152. minigameClient = mainClient->createMinigameServerClient();
  153. if( minigameClient )
  154. game->setMinigameClientZ( minigameClient->getThis() );
  155. if( !game->laden() )
  156. {
  157. game = game->release();
  158. dllDateien->releaseDLL( name->getText() );
  159. }
  160. run = 0;
  161. }
  162. // constant
  163. bool MGLaden::fertig() const
  164. {
  165. return !isRunning();
  166. }
  167. MiniGameV *MGLaden::zGame() const
  168. {
  169. return game;
  170. }
  171. // Reference Counting
  172. MGLaden *MGLaden::getThis()
  173. {
  174. ref++;
  175. return this;
  176. }
  177. MGLaden *MGLaden::release()
  178. {
  179. ref--;
  180. if( !ref )
  181. delete this;
  182. return 0;
  183. }
  184. // Inhalt der MiniGames Klasse aus MiniGames.h
  185. // Konstruktor
  186. MiniGames::MiniGames( Schrift *zSchrift, Fenster *zNachLoginFenster, int x )
  187. : Zeichnung()
  188. {
  189. schrift = zSchrift->getThis();
  190. bildschirmGröße = BildschirmGröße();
  191. pos = Punkt( x, 67 );
  192. gr = Punkt( 102, 32 );
  193. rahmen = new LRahmen();
  194. rahmen->setFarbe( 0xFFFFFFFF );
  195. rahmen->setSize( 102, 32 );
  196. alpha = 0;
  197. alpha2 = 0;
  198. animation = 0;
  199. sichtbar = 0;
  200. tickVal = 0;
  201. prozent1 = 0;
  202. prozent2 = 0;
  203. begPos = Punkt( 0, 0 );
  204. begGröße = Punkt( 0, 0 );
  205. größe1 = Punkt( 102, 32 );
  206. pos1 = Punkt( x, 67 );
  207. größe2 = Punkt( 800, 500 );
  208. pos2 = bildschirmGröße / 2 - größe2 / 2;
  209. laden = (Animation2D*)ladeAnimation->dublizieren();
  210. laden->setSichtbar( 0 );
  211. laden->setPosition( 375, 225 );
  212. games = new RCArray< MiniGame >();
  213. suchFilter = initTextFeld( 10, 10, 100, 20, zSchrift, TextFeld::Style::Text | TextFeld::Style::VCenter, "Suchfilter:" );
  214. TextRenderer tr( zSchrift->getThis() );
  215. tr.setSchriftSize( 12 );
  216. suchFilter->setSize( tr.getTextBreite( suchFilter->zText()->getText() ), 20 );
  217. suchName = initTextFeld( 20 + suchFilter->getBreite(), 10, 200, 20, zSchrift, TextFeld::Style::TextFeld, "" );
  218. suchen = initKnopf( 230 + suchFilter->getBreite(), 10, 100, 20, zSchrift, Knopf::Style::Sichtbar, "Suchen" );
  219. gefiltert = 0;
  220. zNachLoginFenster->addMember( this );
  221. dg = 0;
  222. mgl = 0;
  223. ref = 1;
  224. new MGSuchen( getThis() );
  225. }
  226. // Destruktor
  227. MiniGames::~MiniGames()
  228. {
  229. if( schrift )
  230. schrift->release();
  231. rahmen->release();
  232. suchName->release();
  233. suchFilter->release();
  234. suchen->release();
  235. laden->release();
  236. games->release();
  237. if( mgl )
  238. mgl->release();
  239. }
  240. // nicht constant
  241. void MiniGames::setSichtbar( bool sicht )
  242. {
  243. begPos = pos;
  244. begGröße = gr;
  245. animation |= ( sicht ? 0x1 : 0x2 );
  246. rend = 1;
  247. }
  248. void MiniGames::addMiniGame( MiniGame *mg )
  249. {
  250. games->add( mg );
  251. if( gefiltert )
  252. filter();
  253. else
  254. {
  255. int i = games->getEintragAnzahl() - 1;
  256. games->z( i )->setPosition( 10 + 10 * ( i % 3 ) + 250 * ( i % 3 ), 50 + 10 * ( i / 3 ) + 100 * ( i / 3 ) );
  257. }
  258. }
  259. void MiniGames::setAktuell( bool aktuell, int dg )
  260. {
  261. this->aktuell = aktuell;
  262. if( aktuell )
  263. new MGSuchen( getThis() );
  264. if( !this->dg )
  265. this->dg = dg;
  266. if( !aktuell && !updateH->hat( dg ) )
  267. {
  268. nachLogin->zNachrichtenListe()->addNachricht( new SpielUpdateNachricht( schrift, new Text( "Update" ), new Text( "Die minigames müssen aktualisiert werden." ), dg,
  269. []()
  270. {
  271. if( nachLogin && nachLogin->zMGFenster() )
  272. nachLogin->zMGFenster()->setAktuell( 1 );
  273. } ) );
  274. }
  275. }
  276. void MiniGames::filter()
  277. {
  278. Text filter = suchName->zText()->getText();
  279. bool notF = 0;
  280. if( !filter.getLength() )
  281. notF = 1;
  282. int anz = games->getEintragAnzahl();
  283. bool *fertig = new bool[ anz ];
  284. for( int i = 0; i < anz; i++ )
  285. fertig[ i ] = 0;
  286. for( int i = 0; i < anz; i++ )
  287. {
  288. int pos = -1;
  289. int p = -1;
  290. for( int j = 0; j < anz; j++ )
  291. {
  292. if( ( notF || ( games->z( j )->zName()->hat( filter ) && ( pos == -1 || games->z( j )->zName()->positionVon( filter ) < pos ) ) ) && !fertig[ j ] )
  293. {
  294. p = j;
  295. pos = games->z( j )->zName()->positionVon( filter );
  296. games->z( j )->setSichtbar( 1 );
  297. }
  298. }
  299. if( p < 0 )
  300. break;
  301. fertig[ p ] = 1;
  302. games->z( p )->setPosition( 10 + 10 * ( i % 3 ) + 250 * ( i % 3 ), 50 + 10 * ( i / 3 ) + 100 * ( i / 3 ) );
  303. }
  304. for( int i = 0; i < anz; i++ )
  305. {
  306. if( !fertig[ i ] )
  307. games->z( i )->setSichtbar( 0 );
  308. }
  309. delete[] fertig;
  310. }
  311. void MiniGames::doMausEreignis( MausEreignis &me )
  312. {
  313. if( laden->istSichtbar() || !sichtbar )
  314. return;
  315. me.mx -= pos.x;
  316. me.my -= pos.y;
  317. if( alpha2 )
  318. {
  319. suchName->doMausEreignis( me );
  320. bool vera = me.verarbeitet;
  321. suchen->doMausEreignis( me );
  322. if( !vera && me.verarbeitet && me.id == ME_RLinks )
  323. filter();
  324. int anz = games->getEintragAnzahl();
  325. for( int i = 0; i < anz; i++ )
  326. {
  327. bool vera = me.verarbeitet;
  328. games->z( i )->doMausEreignis( me );
  329. if( !vera && me.verarbeitet && me.id == ME_RLinks )
  330. { // spiel starten
  331. laden->setSichtbar( 1 );
  332. if( mgl )
  333. mgl = mgl->release();
  334. mgl = new MGLaden( games->z( i )->zName()->getText() );
  335. }
  336. }
  337. }
  338. if( mgl && mgl->zGame() )
  339. mgl->zGame()->doMausEreignis( me );
  340. me.mx += pos.x;
  341. me.my += pos.y;
  342. }
  343. void MiniGames::doTastaturEreignis( TastaturEreignis &te )
  344. {
  345. if( laden->istSichtbar() || !sichtbar )
  346. return;
  347. if( alpha2 )
  348. {
  349. bool vera = te.verarbeitet;
  350. suchName->doTastaturEreignis( te );
  351. if( !vera && te.verarbeitet && te.taste == T_Enter && te.id == TE_Release )
  352. filter();
  353. }
  354. if( mgl && mgl->zGame() )
  355. mgl->zGame()->doTastaturEreignis( te );
  356. }
  357. bool MiniGames::tick( double z )
  358. {
  359. if( laden->istSichtbar() && mgl && mgl->fertig() )
  360. {
  361. if( !mgl->zGame() )
  362. {
  363. mgl = mgl->release();
  364. nachLogin->zNachrichtenListe()->addNachricht( new Text( "Fehler" ), new Text( "Das Minigame konnte nicht geladen werden." ), new Text( "Ok" ) );
  365. }
  366. else
  367. {
  368. mgl->zGame()->setSchriftZ( schrift->getThis() );
  369. mgl->zGame()->setBildschirmZ( hauptScreen->getThis() );
  370. }
  371. laden->setSichtbar( 0 );
  372. }
  373. if( mgl && mgl->zGame() && !alpha2 )
  374. {
  375. if( sichtbar && !animation )
  376. rend |= mgl->zGame()->tick( z );
  377. if( mgl->zGame()->istEnde() )
  378. mgl = mgl->release();
  379. }
  380. rend |= laden->tick( z );
  381. if( alpha2 )
  382. {
  383. rend |= suchName->tick( z );
  384. rend |= suchen->tick( z );
  385. int anz = games->getEintragAnzahl();
  386. for( int i = 0; i < anz; i++ )
  387. rend |= games->z( i )->tick( z );
  388. }
  389. tickVal += z * 150;
  390. int val = (int)tickVal;
  391. if( val < 1 )
  392. {
  393. bool ret = rend;
  394. rend = 0;
  395. return ret;
  396. }
  397. tickVal -= val;
  398. if( ( animation | 0x1 ) == animation ) // Einblenden
  399. {
  400. if( prozent1 != 100 )
  401. {
  402. prozent1 += val;
  403. if( prozent1 >= 100 )
  404. prozent1 = 100;
  405. pos = begPos + (Punkt)( ( ( Vec2< double > )( pos2 - begPos ) / 100.0 ) * prozent1 );
  406. gr = begGröße + (Punkt)( ( ( Vec2< double > )( größe2 - begGröße ) / 100.0 ) * prozent1 );
  407. }
  408. else if( alpha != 255 )
  409. {
  410. alpha += val * 2;
  411. if( alpha >= 255 || ( animation | 0x2 ) == animation )
  412. {
  413. alpha = 255;
  414. animation &= ~0x1;
  415. sichtbar = 1;
  416. prozent1 = 0;
  417. }
  418. }
  419. rend = 1;
  420. }
  421. if( ( animation | 0x2 ) == animation ) // ausblenden
  422. {
  423. if( alpha != 0 )
  424. {
  425. alpha -= val * 2;
  426. if( alpha < 0 )
  427. alpha = 0;
  428. }
  429. else
  430. {
  431. prozent2 += val;
  432. if( prozent2 > 100 )
  433. prozent2 = 100;
  434. pos = begPos + (Punkt)( ( ( Vec2< double > )( pos1 - begPos ) / 100.0 ) * prozent2 );
  435. gr = begGröße + (Punkt)( ( ( Vec2< double > )( größe1 - begGröße ) / 100.0 ) * prozent2 );
  436. if( prozent2 == 100 )
  437. {
  438. prozent2 = 0;
  439. animation &= ~0x2;
  440. sichtbar = 0;
  441. }
  442. }
  443. rend = 1;
  444. }
  445. if( mgl && alpha2 )
  446. {
  447. alpha2 -= val;
  448. if( alpha2 < 0 )
  449. alpha2 = 0;
  450. rend = 1;
  451. }
  452. if( !mgl && alpha2 != 255 )
  453. {
  454. alpha2 += val;
  455. if( alpha2 > 255 )
  456. alpha2 = 255;
  457. rend = 1;
  458. }
  459. bool ret = rend;
  460. rend = 0;
  461. return ret;
  462. }
  463. void MiniGames::render( Bild &zRObj )
  464. {
  465. if( pos == pos1 )
  466. return;
  467. rahmen->setPosition( pos );
  468. rahmen->setSize( gr );
  469. rahmen->render( zRObj );
  470. if( !zRObj.setDrawOptions( pos.x + 1, pos.y + 1, gr.x - 2, gr.y - 2 ) )
  471. return;
  472. int rbr = rahmen->getRBreite();
  473. zRObj.setAlpha( (unsigned char)alpha );
  474. zRObj.setAlpha( (unsigned char)alpha2 );
  475. suchFilter->render( zRObj );
  476. suchName->render( zRObj );
  477. suchen->render( zRObj );
  478. int anz = games->getEintragAnzahl();
  479. for( int i = 0; i < anz; i++ )
  480. games->z( i )->render( zRObj );
  481. zRObj.releaseAlpha();
  482. laden->render( zRObj );
  483. if( mgl && mgl->fertig() && mgl->zGame() )
  484. mgl->zGame()->render( zRObj );
  485. zRObj.releaseAlpha();
  486. zRObj.releaseDrawOptions();
  487. }
  488. // constant
  489. bool MiniGames::istAnimiert() const
  490. {
  491. return animation != 0;
  492. }
  493. bool MiniGames::istSichtbar() const
  494. {
  495. return sichtbar || prozent1 != 0;
  496. }
  497. // Reference Counting
  498. MiniGames *MiniGames::getThis()
  499. {
  500. ref++;
  501. return this;
  502. }
  503. MiniGames *MiniGames::release()
  504. {
  505. ref--;
  506. if( !ref )
  507. delete this;
  508. return 0;
  509. }