Minigames.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. zSchrift->setSchriftSize( 12 );
  215. suchFilter->setSize( zSchrift->getTextBreite( suchFilter->zText() ), 20 );
  216. suchName = initTextFeld( 20 + suchFilter->getBreite(), 10, 200, 20, zSchrift, TextFeld::Style::TextFeld, "" );
  217. suchen = initKnopf( 230 + suchFilter->getBreite(), 10, 100, 20, zSchrift, Knopf::Style::Sichtbar, "Suchen" );
  218. gefiltert = 0;
  219. zNachLoginFenster->addMember( this );
  220. dg = 0;
  221. mgl = 0;
  222. ref = 1;
  223. new MGSuchen( getThis() );
  224. }
  225. // Destruktor
  226. MiniGames::~MiniGames()
  227. {
  228. if( schrift )
  229. schrift->release();
  230. rahmen->release();
  231. suchName->release();
  232. suchFilter->release();
  233. suchen->release();
  234. laden->release();
  235. games->release();
  236. if( mgl )
  237. mgl->release();
  238. }
  239. // nicht constant
  240. void MiniGames::setSichtbar( bool sicht )
  241. {
  242. begPos = pos;
  243. begGröße = gr;
  244. animation |= ( sicht ? 0x1 : 0x2 );
  245. rend = 1;
  246. }
  247. void MiniGames::addMiniGame( MiniGame *mg )
  248. {
  249. games->add( mg );
  250. if( gefiltert )
  251. filter();
  252. else
  253. {
  254. int i = games->getEintragAnzahl() - 1;
  255. games->z( i )->setPosition( 10 + 10 * ( i % 3 ) + 250 * ( i % 3 ), 50 + 10 * ( i / 3 ) + 100 * ( i / 3 ) );
  256. }
  257. }
  258. void MiniGames::setAktuell( bool aktuell, int dg )
  259. {
  260. this->aktuell = aktuell;
  261. if( aktuell )
  262. new MGSuchen( getThis() );
  263. if( !this->dg )
  264. this->dg = dg;
  265. if( !aktuell && !updateH->hat( dg ) )
  266. {
  267. nachLogin->zNachrichtenListe()->addNachricht( new SpielUpdateNachricht( schrift, new Text( "Update" ), new Text( "Die minigames müssen aktualisiert werden." ), dg,
  268. []()
  269. {
  270. if( nachLogin && nachLogin->zMGFenster() )
  271. nachLogin->zMGFenster()->setAktuell( 1 );
  272. } ) );
  273. }
  274. }
  275. void MiniGames::filter()
  276. {
  277. Text filter = suchName->zText()->getText();
  278. bool notF = 0;
  279. if( !filter.getLength() )
  280. notF = 1;
  281. int anz = games->getEintragAnzahl();
  282. bool *fertig = new bool[ anz ];
  283. for( int i = 0; i < anz; i++ )
  284. fertig[ i ] = 0;
  285. for( int i = 0; i < anz; i++ )
  286. {
  287. int pos = -1;
  288. int p = -1;
  289. for( int j = 0; j < anz; j++ )
  290. {
  291. if( ( notF || ( games->z( j )->zName()->hat( filter ) && ( pos == -1 || games->z( j )->zName()->positionVon( filter ) < pos ) ) ) && !fertig[ j ] )
  292. {
  293. p = j;
  294. pos = games->z( j )->zName()->positionVon( filter );
  295. games->z( j )->setSichtbar( 1 );
  296. }
  297. }
  298. if( p < 0 )
  299. break;
  300. fertig[ p ] = 1;
  301. games->z( p )->setPosition( 10 + 10 * ( i % 3 ) + 250 * ( i % 3 ), 50 + 10 * ( i / 3 ) + 100 * ( i / 3 ) );
  302. }
  303. for( int i = 0; i < anz; i++ )
  304. {
  305. if( !fertig[ i ] )
  306. games->z( i )->setSichtbar( 0 );
  307. }
  308. delete[] fertig;
  309. }
  310. void MiniGames::doMausEreignis( MausEreignis &me )
  311. {
  312. if( laden->istSichtbar() || !sichtbar )
  313. return;
  314. me.mx -= pos.x;
  315. me.my -= pos.y;
  316. if( alpha2 )
  317. {
  318. suchName->doMausEreignis( me );
  319. bool vera = me.verarbeitet;
  320. suchen->doMausEreignis( me );
  321. if( !vera && me.verarbeitet && me.id == ME_RLinks )
  322. filter();
  323. int anz = games->getEintragAnzahl();
  324. for( int i = 0; i < anz; i++ )
  325. {
  326. bool vera = me.verarbeitet;
  327. games->z( i )->doMausEreignis( me );
  328. if( !vera && me.verarbeitet && me.id == ME_RLinks )
  329. { // spiel starten
  330. laden->setSichtbar( 1 );
  331. if( mgl )
  332. mgl = mgl->release();
  333. mgl = new MGLaden( games->z( i )->zName()->getText() );
  334. }
  335. }
  336. }
  337. if( mgl && mgl->zGame() )
  338. mgl->zGame()->doMausEreignis( me );
  339. me.mx += pos.x;
  340. me.my += pos.y;
  341. }
  342. void MiniGames::doTastaturEreignis( TastaturEreignis &te )
  343. {
  344. if( laden->istSichtbar() || !sichtbar )
  345. return;
  346. if( alpha2 )
  347. {
  348. bool vera = te.verarbeitet;
  349. suchName->doTastaturEreignis( te );
  350. if( !vera && te.verarbeitet && te.taste == T_Enter && te.id == TE_Release )
  351. filter();
  352. }
  353. if( mgl && mgl->zGame() )
  354. mgl->zGame()->doTastaturEreignis( te );
  355. }
  356. bool MiniGames::tick( double z )
  357. {
  358. if( laden->istSichtbar() && mgl && mgl->fertig() )
  359. {
  360. if( !mgl->zGame() )
  361. {
  362. mgl = mgl->release();
  363. nachLogin->zNachrichtenListe()->addNachricht( new Text( "Fehler" ), new Text( "Das Minigame konnte nicht geladen werden." ), new Text( "Ok" ) );
  364. }
  365. else
  366. {
  367. mgl->zGame()->setSchriftZ( schrift->getThis() );
  368. mgl->zGame()->setBildschirmZ( hauptScreen->getThis() );
  369. }
  370. laden->setSichtbar( 0 );
  371. }
  372. if( mgl && mgl->zGame() && !alpha2 )
  373. {
  374. if( sichtbar && !animation )
  375. rend |= mgl->zGame()->tick( z );
  376. if( mgl->zGame()->istEnde() )
  377. mgl = mgl->release();
  378. }
  379. rend |= laden->tick( z );
  380. if( alpha2 )
  381. {
  382. rend |= suchName->tick( z );
  383. rend |= suchen->tick( z );
  384. int anz = games->getEintragAnzahl();
  385. for( int i = 0; i < anz; i++ )
  386. rend |= games->z( i )->tick( z );
  387. }
  388. tickVal += z * 150;
  389. int val = (int)tickVal;
  390. if( val < 1 )
  391. {
  392. bool ret = rend;
  393. rend = 0;
  394. return ret;
  395. }
  396. tickVal -= val;
  397. if( ( animation | 0x1 ) == animation ) // Einblenden
  398. {
  399. if( prozent1 != 100 )
  400. {
  401. prozent1 += val;
  402. if( prozent1 >= 100 )
  403. prozent1 = 100;
  404. pos = begPos + (Punkt)( ( ( Vec2< double > )( pos2 - begPos ) / 100.0 ) * prozent1 );
  405. gr = begGröße + (Punkt)( ( ( Vec2< double > )( größe2 - begGröße ) / 100.0 ) * prozent1 );
  406. }
  407. else if( alpha != 255 )
  408. {
  409. alpha += val * 2;
  410. if( alpha >= 255 || ( animation | 0x2 ) == animation )
  411. {
  412. alpha = 255;
  413. animation &= ~0x1;
  414. sichtbar = 1;
  415. prozent1 = 0;
  416. }
  417. }
  418. rend = 1;
  419. }
  420. if( ( animation | 0x2 ) == animation ) // ausblenden
  421. {
  422. if( alpha != 0 )
  423. {
  424. alpha -= val * 2;
  425. if( alpha < 0 )
  426. alpha = 0;
  427. }
  428. else
  429. {
  430. prozent2 += val;
  431. if( prozent2 > 100 )
  432. prozent2 = 100;
  433. pos = begPos + (Punkt)( ( ( Vec2< double > )( pos1 - begPos ) / 100.0 ) * prozent2 );
  434. gr = begGröße + (Punkt)( ( ( Vec2< double > )( größe1 - begGröße ) / 100.0 ) * prozent2 );
  435. if( prozent2 == 100 )
  436. {
  437. prozent2 = 0;
  438. animation &= ~0x2;
  439. sichtbar = 0;
  440. }
  441. }
  442. rend = 1;
  443. }
  444. if( mgl && alpha2 )
  445. {
  446. alpha2 -= val;
  447. if( alpha2 < 0 )
  448. alpha2 = 0;
  449. rend = 1;
  450. }
  451. if( !mgl && alpha2 != 255 )
  452. {
  453. alpha2 += val;
  454. if( alpha2 > 255 )
  455. alpha2 = 255;
  456. rend = 1;
  457. }
  458. bool ret = rend;
  459. rend = 0;
  460. return ret;
  461. }
  462. void MiniGames::render( Bild &zRObj )
  463. {
  464. if( pos == pos1 )
  465. return;
  466. rahmen->setPosition( pos );
  467. rahmen->setSize( gr );
  468. rahmen->render( zRObj );
  469. if( !zRObj.setDrawOptions( pos.x + 1, pos.y + 1, gr.x - 2, gr.y - 2 ) )
  470. return;
  471. int rbr = rahmen->getRBreite();
  472. zRObj.setAlpha( (unsigned char)alpha );
  473. zRObj.setAlpha( (unsigned char)alpha2 );
  474. suchFilter->render( zRObj );
  475. suchName->render( zRObj );
  476. suchen->render( zRObj );
  477. int anz = games->getEintragAnzahl();
  478. for( int i = 0; i < anz; i++ )
  479. games->z( i )->render( zRObj );
  480. zRObj.releaseAlpha();
  481. laden->render( zRObj );
  482. if( mgl && mgl->fertig() && mgl->zGame() )
  483. mgl->zGame()->render( zRObj );
  484. zRObj.releaseAlpha();
  485. zRObj.releaseDrawOptions();
  486. }
  487. // constant
  488. bool MiniGames::istAnimiert() const
  489. {
  490. return animation != 0;
  491. }
  492. bool MiniGames::istSichtbar() const
  493. {
  494. return sichtbar || prozent1 != 0;
  495. }
  496. // Reference Counting
  497. MiniGames *MiniGames::getThis()
  498. {
  499. ref++;
  500. return this;
  501. }
  502. MiniGames *MiniGames::release()
  503. {
  504. ref--;
  505. if( !ref )
  506. delete this;
  507. return 0;
  508. }