FactoryClient.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. #include "FactoryClient.h"
  2. #include <Bild.h>
  3. #include <Datei.h>
  4. #include <DateiSystem.h>
  5. #include <M3Datei.h>
  6. #include <Textur.h>
  7. #include "Globals.h"
  8. #include "Load.h"
  9. using namespace Network;
  10. using namespace Framework;
  11. void createDefaultCube(Bildschirm* zScreen)
  12. {
  13. Model3DData* data = zScreen->zGraphicsApi()->createModel("cube");
  14. data->setAmbientFactor(0.f);
  15. data->setDiffusFactor(1.f);
  16. data->setSpecularFactor(0.f);
  17. float size = 1;
  18. float left, right, top, bottom;
  19. // Calculate the screen coordinates of the left side of the bitmap.
  20. right = (float)((-size / 2.0));
  21. // Calculate the screen coordinates of the right side of the bitmap.
  22. left = right + (float)size;
  23. // Calculate the screen coordinates of the top of the bitmap.
  24. top = (float)(size / 2.0);
  25. // Calculate the screen coordinates of the bottom of the bitmap.
  26. bottom = top - (float)size;
  27. float front = -size / 2;
  28. float back = front + size;
  29. Vertex3D* vertecies = new Vertex3D[24];
  30. for (int i = 0; i < 24; i++)
  31. vertecies[i].knochenId = 0;
  32. // front side
  33. vertecies[0].pos = Vec3<float>(left, front, top);
  34. vertecies[0].tPos = Vec2<float>(0.f, 0.f);
  35. vertecies[1].pos = Vec3<float>(right, front, top);
  36. vertecies[1].tPos = Vec2<float>(1.f, 0.f);
  37. vertecies[2].pos = Vec3<float>(left, front, bottom);
  38. vertecies[2].tPos = Vec2<float>(0.f, 1.f);
  39. vertecies[3].pos = Vec3<float>(right, front, bottom);
  40. vertecies[3].tPos = Vec2<float>(1.f, 1.f);
  41. // back side
  42. vertecies[4].pos = Vec3<float>(right, back, top);
  43. vertecies[4].tPos = Vec2<float>(0.0f, 0.0f);
  44. vertecies[5].pos = Vec3<float>(left, back, top);
  45. vertecies[5].tPos = Vec2<float>(1.0f, 0.0f);
  46. vertecies[6].pos = Vec3<float>(right, back, bottom);
  47. vertecies[6].tPos = Vec2<float>(0.0f, 1.0f);
  48. vertecies[7].pos = Vec3<float>(left, back, bottom);
  49. vertecies[7].tPos = Vec2<float>(1.0f, 1.0f);
  50. // left side
  51. vertecies[8].pos = Vec3<float>(left, back, top);
  52. vertecies[8].tPos = Vec2<float>(0.f, 0.f);
  53. vertecies[9].pos = Vec3<float>(left, front, top);
  54. vertecies[9].tPos = Vec2<float>(1.f, 0.f);
  55. vertecies[10].pos = Vec3<float>(left, back, bottom);
  56. vertecies[10].tPos = Vec2<float>(0.f, 1.f);
  57. vertecies[11].pos = Vec3<float>(left, front, bottom);
  58. vertecies[11].tPos = Vec2<float>(1.f, 1.f);
  59. // right side
  60. vertecies[12].pos = Vec3<float>(right, front, top);
  61. vertecies[12].tPos = Vec2<float>(0.0f, 0.0f);
  62. vertecies[13].pos = Vec3<float>(right, back, top);
  63. vertecies[13].tPos = Vec2<float>(1.0f, 0.0f);
  64. vertecies[14].pos = Vec3<float>(right, front, bottom);
  65. vertecies[14].tPos = Vec2<float>(0.0f, 1.0f);
  66. vertecies[15].pos = Vec3<float>(right, back, bottom);
  67. vertecies[15].tPos = Vec2<float>(1.0f, 1.0f);
  68. // top side
  69. vertecies[16].pos = Vec3<float>(left, back, top);
  70. vertecies[16].tPos = Vec2<float>(0.f, 0.f);
  71. vertecies[17].pos = Vec3<float>(right, back, top);
  72. vertecies[17].tPos = Vec2<float>(1.f, 0.f);
  73. vertecies[18].pos = Vec3<float>(left, front, top);
  74. vertecies[18].tPos = Vec2<float>(0.f, 1.f);
  75. vertecies[19].pos = Vec3<float>(right, front, top);
  76. vertecies[19].tPos = Vec2<float>(1.f, 1.f);
  77. // botom side
  78. vertecies[20].pos = Vec3<float>(left, front, bottom);
  79. vertecies[20].tPos = Vec2<float>(0.0f, 0.0f);
  80. vertecies[21].pos = Vec3<float>(right, front, bottom);
  81. vertecies[21].tPos = Vec2<float>(1.0f, 0.0f);
  82. vertecies[22].pos = Vec3<float>(left, back, bottom);
  83. vertecies[22].tPos = Vec2<float>(0.0f, 1.0f);
  84. vertecies[23].pos = Vec3<float>(right, back, bottom);
  85. vertecies[23].tPos = Vec2<float>(1.0f, 1.0f);
  86. data->setVertecies(vertecies, 24);
  87. // the order of the polygons has to be NORTH (front), EAST (left), SOUTH
  88. // (back), WEST (right), TOP, BOTTOM according to the Area definition
  89. // front side
  90. Polygon3D* p
  91. = new Polygon3D(); // looking from (0,0,0) to (1,0,0) to see this side
  92. p->indexAnz = 6;
  93. p->indexList = new int[p->indexAnz];
  94. p->indexList[0] = 0;
  95. p->indexList[1] = 1;
  96. p->indexList[2] = 2;
  97. p->indexList[3] = 1;
  98. p->indexList[4] = 3;
  99. p->indexList[5] = 2;
  100. data->addPolygon(p);
  101. // left side
  102. p = new Polygon3D(); // looking from (0,0,0) to (0,1,0) to see this side
  103. p->indexAnz = 6;
  104. p->indexList = new int[p->indexAnz];
  105. p->indexList[0] = 0 + 8;
  106. p->indexList[1] = 1 + 8;
  107. p->indexList[2] = 2 + 8;
  108. p->indexList[3] = 1 + 8;
  109. p->indexList[4] = 3 + 8;
  110. p->indexList[5] = 2 + 8;
  111. data->addPolygon(p);
  112. // back side
  113. p = new Polygon3D(); // looking from (0,0,0) to (-1,0,0) to see this side
  114. p->indexAnz = 6;
  115. p->indexList = new int[p->indexAnz];
  116. p->indexList[0] = 0 + 4;
  117. p->indexList[1] = 1 + 4;
  118. p->indexList[2] = 2 + 4;
  119. p->indexList[3] = 1 + 4;
  120. p->indexList[4] = 3 + 4;
  121. p->indexList[5] = 2 + 4;
  122. data->addPolygon(p);
  123. // right side
  124. p = new Polygon3D(); // looking from (0,0,0) to (0,-1,0) to see this side
  125. p->indexAnz = 6;
  126. p->indexList = new int[p->indexAnz];
  127. p->indexList[0] = 0 + 12;
  128. p->indexList[1] = 1 + 12;
  129. p->indexList[2] = 2 + 12;
  130. p->indexList[3] = 1 + 12;
  131. p->indexList[4] = 3 + 12;
  132. p->indexList[5] = 2 + 12;
  133. data->addPolygon(p);
  134. // top side
  135. p = new Polygon3D(); // looking from (0,0,0) to (0,0,-1) to see this side
  136. p->indexAnz = 6;
  137. p->indexList = new int[p->indexAnz];
  138. p->indexList[0] = 0 + 16;
  139. p->indexList[1] = 1 + 16;
  140. p->indexList[2] = 2 + 16;
  141. p->indexList[3] = 1 + 16;
  142. p->indexList[4] = 3 + 16;
  143. p->indexList[5] = 2 + 16;
  144. data->addPolygon(p);
  145. // botom side
  146. p = new Polygon3D(); // looking from (0,0,0) to (0,0,1) to see this side
  147. p->indexAnz = 6;
  148. p->indexList = new int[p->indexAnz];
  149. p->indexList[0] = 0 + 20;
  150. p->indexList[1] = 1 + 20;
  151. p->indexList[2] = 2 + 20;
  152. p->indexList[3] = 1 + 20;
  153. p->indexList[4] = 3 + 20;
  154. p->indexList[5] = 2 + 20;
  155. data->addPolygon(p);
  156. data->calculateNormals();
  157. data->release();
  158. }
  159. void createPlayer(Bildschirm* zScreen)
  160. {
  161. Framework::Model3DData* data
  162. = window->zBildschirm()->zGraphicsApi()->createModel("player");
  163. data->setAmbientFactor(0.8f);
  164. data->setDiffusFactor(0.1f);
  165. data->setSpecularFactor(0.1f);
  166. float size = 0.8f;
  167. float left, right, top, bottom;
  168. // Calculate the screen coordinates of the left side of the bitmap.
  169. left = (float)((size / 2.0) * -1);
  170. // Calculate the screen coordinates of the right side of the bitmap.
  171. right = left + (float)size;
  172. // Calculate the screen coordinates of the top of the bitmap.
  173. top = (float)(size / 2.0);
  174. // Calculate the screen coordinates of the bottom of the bitmap.
  175. bottom = top - (float)size;
  176. float front = -1.5f / 2;
  177. float back = front + 1.5f;
  178. Vertex3D* vertecies = new Vertex3D[24];
  179. for (int i = 0; i < 24; i++)
  180. vertecies[i].knochenId = 0;
  181. vertecies[0].pos = Vec3<float>(left, top, front);
  182. vertecies[0].tPos = Vec2<float>(0.f, 0.f);
  183. vertecies[1].pos = Vec3<float>(right, top, front);
  184. vertecies[1].tPos = Vec2<float>(1.f, 0.f);
  185. vertecies[2].pos = Vec3<float>(left, bottom, front);
  186. vertecies[2].tPos = Vec2<float>(0.f, 1.f);
  187. vertecies[3].pos = Vec3<float>(right, bottom, front);
  188. vertecies[3].tPos = Vec2<float>(1.f, 1.f);
  189. vertecies[4].pos = Vec3<float>(left, top, back);
  190. vertecies[4].tPos = Vec2<float>(0.0f, 0.0f);
  191. vertecies[5].pos = Vec3<float>(right, top, back);
  192. vertecies[5].tPos = Vec2<float>(1.0f, 0.0f);
  193. vertecies[6].pos = Vec3<float>(left, bottom, back);
  194. vertecies[6].tPos = Vec2<float>(0.0f, 1.0f);
  195. vertecies[7].pos = Vec3<float>(right, bottom, back);
  196. vertecies[7].tPos = Vec2<float>(1.0f, 1.0f);
  197. vertecies[8].pos = Vec3<float>(left, top, front);
  198. vertecies[8].tPos = Vec2<float>(1.f, 0.f);
  199. vertecies[9].pos = Vec3<float>(right, top, front);
  200. vertecies[9].tPos = Vec2<float>(0.f, 0.f);
  201. vertecies[10].pos = Vec3<float>(left, bottom, front);
  202. vertecies[10].tPos = Vec2<float>(1.f, 1.f);
  203. vertecies[11].pos = Vec3<float>(right, bottom, front);
  204. vertecies[11].tPos = Vec2<float>(0.f, 1.f);
  205. vertecies[12].pos = Vec3<float>(left, top, back);
  206. vertecies[12].tPos = Vec2<float>(0.0f, 0.0f);
  207. vertecies[13].pos = Vec3<float>(right, top, back);
  208. vertecies[13].tPos = Vec2<float>(1.0f, 0.0f);
  209. vertecies[14].pos = Vec3<float>(left, bottom, back);
  210. vertecies[14].tPos = Vec2<float>(0.0f, 1.0f);
  211. vertecies[15].pos = Vec3<float>(right, bottom, back);
  212. vertecies[15].tPos = Vec2<float>(1.0f, 1.0f);
  213. vertecies[16].pos = Vec3<float>(left, top, front);
  214. vertecies[16].tPos = Vec2<float>(0.f, 1.f);
  215. vertecies[17].pos = Vec3<float>(right, top, front);
  216. vertecies[17].tPos = Vec2<float>(1.f, 1.f);
  217. vertecies[18].pos = Vec3<float>(left, bottom, front);
  218. vertecies[18].tPos = Vec2<float>(0.f, 0.f);
  219. vertecies[19].pos = Vec3<float>(right, bottom, front);
  220. vertecies[19].tPos = Vec2<float>(1.f, 0.f);
  221. vertecies[20].pos = Vec3<float>(left, top, back);
  222. vertecies[20].tPos = Vec2<float>(0.0f, 0.0f);
  223. vertecies[21].pos = Vec3<float>(right, top, back);
  224. vertecies[21].tPos = Vec2<float>(1.0f, 0.0f);
  225. vertecies[22].pos = Vec3<float>(left, bottom, back);
  226. vertecies[22].tPos = Vec2<float>(0.0f, 1.0f);
  227. vertecies[23].pos = Vec3<float>(right, bottom, back);
  228. vertecies[23].tPos = Vec2<float>(1.0f, 1.0f);
  229. data->setVertecies(vertecies, 24);
  230. // the order of the polygons has to be NORTH, EAST, SOUTH, WEST, TOP, BOTTOM
  231. // according to the Area definition down side
  232. Polygon3D* p = new Polygon3D();
  233. p->indexAnz = 6;
  234. p->indexList = new int[p->indexAnz];
  235. p->indexList[0] = 6 + 16;
  236. p->indexList[1] = 2 + 16;
  237. p->indexList[2] = 3 + 16;
  238. p->indexList[3] = 6 + 16;
  239. p->indexList[4] = 3 + 16;
  240. p->indexList[5] = 7 + 16;
  241. data->addPolygon(p);
  242. // right side
  243. p = new Polygon3D();
  244. p->indexAnz = 6;
  245. p->indexList = new int[p->indexAnz];
  246. p->indexList[0] = 1 + 8;
  247. p->indexList[1] = 7 + 8;
  248. p->indexList[2] = 3 + 8;
  249. p->indexList[3] = 1 + 8;
  250. p->indexList[4] = 5 + 8;
  251. p->indexList[5] = 7 + 8;
  252. data->addPolygon(p);
  253. // top side
  254. p = new Polygon3D();
  255. p->indexAnz = 6;
  256. p->indexList = new int[p->indexAnz];
  257. p->indexList[0] = 4 + 16;
  258. p->indexList[1] = 1 + 16;
  259. p->indexList[2] = 0 + 16;
  260. p->indexList[3] = 4 + 16;
  261. p->indexList[4] = 5 + 16;
  262. p->indexList[5] = 1 + 16;
  263. data->addPolygon(p);
  264. // left side
  265. p = new Polygon3D();
  266. p->indexAnz = 6;
  267. p->indexList = new int[p->indexAnz];
  268. p->indexList[0] = 0 + 8;
  269. p->indexList[1] = 2 + 8;
  270. p->indexList[2] = 6 + 8;
  271. p->indexList[3] = 0 + 8;
  272. p->indexList[4] = 6 + 8;
  273. p->indexList[5] = 4 + 8;
  274. data->addPolygon(p);
  275. // back side
  276. p = new Polygon3D();
  277. p->indexAnz = 6;
  278. p->indexList = new int[p->indexAnz];
  279. p->indexList[0] = 4;
  280. p->indexList[1] = 6;
  281. p->indexList[2] = 7;
  282. p->indexList[3] = 4;
  283. p->indexList[4] = 7;
  284. p->indexList[5] = 5;
  285. data->addPolygon(p);
  286. // front side
  287. p = new Polygon3D();
  288. p->indexAnz = 6;
  289. p->indexList = new int[p->indexAnz];
  290. p->indexList[0] = 0;
  291. p->indexList[1] = 3;
  292. p->indexList[2] = 2;
  293. p->indexList[3] = 0;
  294. p->indexList[4] = 1;
  295. p->indexList[5] = 3;
  296. data->addPolygon(p);
  297. data->calculateNormals();
  298. data->release();
  299. }
  300. void createGrass(Bildschirm* zScreen)
  301. {
  302. Model3DData* data = zScreen->zGraphicsApi()->createModel("grass");
  303. data->setAmbientFactor(0.f);
  304. data->setDiffusFactor(1.f);
  305. data->setSpecularFactor(0.f);
  306. float size = 1;
  307. float left, right, top, bottom;
  308. // Calculate the screen coordinates of the left side of the bitmap.
  309. right = (float)((-size / 2.0));
  310. // Calculate the screen coordinates of the right side of the bitmap.
  311. left = right + (float)size;
  312. // Calculate the screen coordinates of the top of the bitmap.
  313. top = (float)(size / 2.0);
  314. // Calculate the screen coordinates of the bottom of the bitmap.
  315. bottom = top - (float)size;
  316. float front = -size / 2;
  317. float back = front + size;
  318. Vertex3D* vertecies = new Vertex3D[32];
  319. for (int i = 0; i < 32; i++)
  320. vertecies[i].knochenId = 0;
  321. // first y plane
  322. vertecies[0].pos = Vec3<float>(left, front + 0.2f, top);
  323. vertecies[0].tPos = Vec2<float>(0.01f, 0.01f);
  324. vertecies[1].pos = Vec3<float>(right, front + 0.2f, top);
  325. vertecies[1].tPos = Vec2<float>(0.99f, 0.01f);
  326. vertecies[2].pos = Vec3<float>(left, front + 0.2f, bottom);
  327. vertecies[2].tPos = Vec2<float>(0.01f, 0.99f);
  328. vertecies[3].pos = Vec3<float>(right, front + 0.2f, bottom);
  329. vertecies[3].tPos = Vec2<float>(0.99f, 0.99f);
  330. // second y plane
  331. vertecies[4].pos = Vec3<float>(left, front + 0.4f, top);
  332. vertecies[4].tPos = Vec2<float>(0.01f, 0.01f);
  333. vertecies[5].pos = Vec3<float>(right, front + 0.4f, top);
  334. vertecies[5].tPos = Vec2<float>(0.99f, 0.01f);
  335. vertecies[6].pos = Vec3<float>(left, front + 0.4f, bottom);
  336. vertecies[6].tPos = Vec2<float>(0.01f, 0.99f);
  337. vertecies[7].pos = Vec3<float>(right, front + 0.4f, bottom);
  338. vertecies[7].tPos = Vec2<float>(0.99f, 0.99f);
  339. // third y plane
  340. vertecies[8].pos = Vec3<float>(left, front + 0.6f, top);
  341. vertecies[8].tPos = Vec2<float>(0.01f, 0.01f);
  342. vertecies[9].pos = Vec3<float>(right, front + 0.6f, top);
  343. vertecies[9].tPos = Vec2<float>(0.99f, 0.01f);
  344. vertecies[10].pos = Vec3<float>(left, front + 0.6f, bottom);
  345. vertecies[10].tPos = Vec2<float>(0.01f, 0.99f);
  346. vertecies[11].pos = Vec3<float>(right, front + 0.6f, bottom);
  347. vertecies[11].tPos = Vec2<float>(0.99f, 0.99f);
  348. // forth y plane
  349. vertecies[12].pos = Vec3<float>(left, front + 0.8f, top);
  350. vertecies[12].tPos = Vec2<float>(0.01f, 0.01f);
  351. vertecies[13].pos = Vec3<float>(right, front + 0.8f, top);
  352. vertecies[13].tPos = Vec2<float>(0.99f, 0.01f);
  353. vertecies[14].pos = Vec3<float>(left, front + 0.8f, bottom);
  354. vertecies[14].tPos = Vec2<float>(0.01f, 0.99f);
  355. vertecies[15].pos = Vec3<float>(right, front + 0.8f, bottom);
  356. vertecies[15].tPos = Vec2<float>(0.99f, 0.99f);
  357. // first x plane
  358. vertecies[16].pos = Vec3<float>(right + 0.2f, front, top);
  359. vertecies[16].tPos = Vec2<float>(0.01f, 0.01f);
  360. vertecies[17].pos = Vec3<float>(right + 0.2f, back, top);
  361. vertecies[17].tPos = Vec2<float>(0.99f, 0.01f);
  362. vertecies[18].pos = Vec3<float>(right + 0.2f, front, bottom);
  363. vertecies[18].tPos = Vec2<float>(0.01f, 0.99f);
  364. vertecies[19].pos = Vec3<float>(right + 0.2f, back, bottom);
  365. vertecies[19].tPos = Vec2<float>(0.99f, 0.99f);
  366. // second x plane
  367. vertecies[20].pos = Vec3<float>(right + 0.4f, front, top);
  368. vertecies[20].tPos = Vec2<float>(0.01f, 0.01f);
  369. vertecies[21].pos = Vec3<float>(right + 0.4f, back, top);
  370. vertecies[21].tPos = Vec2<float>(0.99f, 0.01f);
  371. vertecies[22].pos = Vec3<float>(right + 0.4f, front, bottom);
  372. vertecies[22].tPos = Vec2<float>(0.01f, 0.99f);
  373. vertecies[23].pos = Vec3<float>(right + 0.4f, back, bottom);
  374. vertecies[23].tPos = Vec2<float>(0.99f, 0.99f);
  375. // third x plane
  376. vertecies[24].pos = Vec3<float>(right + 0.6f, front, top);
  377. vertecies[24].tPos = Vec2<float>(0.01f, 0.01f);
  378. vertecies[25].pos = Vec3<float>(right + 0.6f, back, top);
  379. vertecies[25].tPos = Vec2<float>(0.99f, 0.01f);
  380. vertecies[26].pos = Vec3<float>(right + 0.6f, front, bottom);
  381. vertecies[26].tPos = Vec2<float>(0.01f, 0.99f);
  382. vertecies[27].pos = Vec3<float>(right + 0.6f, back, bottom);
  383. vertecies[27].tPos = Vec2<float>(0.99f, 0.99f);
  384. // forth x plane
  385. vertecies[28].pos = Vec3<float>(right + 0.8f, front, top);
  386. vertecies[28].tPos = Vec2<float>(0.01f, 0.01f);
  387. vertecies[29].pos = Vec3<float>(right + 0.8f, back, top);
  388. vertecies[29].tPos = Vec2<float>(0.99f, 0.01f);
  389. vertecies[30].pos = Vec3<float>(right + 0.8f, front, bottom);
  390. vertecies[30].tPos = Vec2<float>(0.01f, 0.99f);
  391. vertecies[31].pos = Vec3<float>(right + 0.8f, back, bottom);
  392. vertecies[31].tPos = Vec2<float>(0.99f, 0.99f);
  393. for (int i = 0; i < 16; i++)
  394. {
  395. vertecies[i].normal = Vec3<float>(0, 1, 0);
  396. }
  397. for (int i = 16; i < 32; i++)
  398. {
  399. vertecies[i].normal = Vec3<float>(1, 0, 0);
  400. }
  401. data->setVertecies(vertecies, 32);
  402. Polygon3D* p = new Polygon3D();
  403. p->indexAnz = 6 * 8;
  404. p->indexList = new int[p->indexAnz];
  405. for (int i = 0; i < 8; i++)
  406. {
  407. p->indexList[i * 6 + 0] = 0 + i * 4;
  408. p->indexList[i * 6 + 1] = 1 + i * 4;
  409. p->indexList[i * 6 + 2] = 2 + i * 4;
  410. p->indexList[i * 6 + 3] = 1 + i * 4;
  411. p->indexList[i * 6 + 4] = 3 + i * 4;
  412. p->indexList[i * 6 + 5] = 2 + i * 4;
  413. }
  414. data->addPolygon(p);
  415. // data->calculateNormals();
  416. data->release();
  417. }
  418. void createFluidCube(Bildschirm* zScreen)
  419. {
  420. Model3DData* data = zScreen->zGraphicsApi()->createModel("fluid");
  421. data->setAmbientFactor(0.f);
  422. data->setDiffusFactor(1.f);
  423. data->setSpecularFactor(0.f);
  424. float size = 1;
  425. float left, right, top, bottom;
  426. // Calculate the screen coordinates of the left side of the bitmap.
  427. right = (float)((-size / 2.0));
  428. // Calculate the screen coordinates of the right side of the bitmap.
  429. left = right + (float)size;
  430. // Calculate the screen coordinates of the top of the bitmap.
  431. top = (float)(size / 2.0);
  432. // Calculate the screen coordinates of the bottom of the bitmap.
  433. bottom = top - (float)size;
  434. float front = -size / 2;
  435. float back = front + size;
  436. Vertex3D* vertecies = new Vertex3D[24];
  437. for (int i = 0; i < 24; i++)
  438. vertecies[i].knochenId = 0;
  439. // front side
  440. vertecies[0].pos = Vec3<float>(left, front, top);
  441. vertecies[0].tPos = Vec2<float>(0.f, 0.f);
  442. vertecies[1].pos = Vec3<float>(right, front, top);
  443. vertecies[1].tPos = Vec2<float>(1.f, 0.f);
  444. vertecies[2].pos = Vec3<float>(left, front, bottom);
  445. vertecies[2].tPos = Vec2<float>(0.f, 1.f);
  446. vertecies[3].pos = Vec3<float>(right, front, bottom);
  447. vertecies[3].tPos = Vec2<float>(1.f, 1.f);
  448. // back side
  449. vertecies[4].pos = Vec3<float>(right, back, top);
  450. vertecies[4].tPos = Vec2<float>(0.0f, 0.0f);
  451. vertecies[5].pos = Vec3<float>(left, back, top);
  452. vertecies[5].tPos = Vec2<float>(1.0f, 0.0f);
  453. vertecies[6].pos = Vec3<float>(right, back, bottom);
  454. vertecies[6].tPos = Vec2<float>(0.0f, 1.0f);
  455. vertecies[7].pos = Vec3<float>(left, back, bottom);
  456. vertecies[7].tPos = Vec2<float>(1.0f, 1.0f);
  457. // left side
  458. vertecies[8].pos = Vec3<float>(left, back, top);
  459. vertecies[8].tPos = Vec2<float>(0.f, 0.f);
  460. vertecies[9].pos = Vec3<float>(left, front, top);
  461. vertecies[9].tPos = Vec2<float>(1.f, 0.f);
  462. vertecies[10].pos = Vec3<float>(left, back, bottom);
  463. vertecies[10].tPos = Vec2<float>(0.f, 1.f);
  464. vertecies[11].pos = Vec3<float>(left, front, bottom);
  465. vertecies[11].tPos = Vec2<float>(1.f, 1.f);
  466. // right side
  467. vertecies[12].pos = Vec3<float>(right, front, top);
  468. vertecies[12].tPos = Vec2<float>(0.0f, 0.0f);
  469. vertecies[13].pos = Vec3<float>(right, back, top);
  470. vertecies[13].tPos = Vec2<float>(1.0f, 0.0f);
  471. vertecies[14].pos = Vec3<float>(right, front, bottom);
  472. vertecies[14].tPos = Vec2<float>(0.0f, 1.0f);
  473. vertecies[15].pos = Vec3<float>(right, back, bottom);
  474. vertecies[15].tPos = Vec2<float>(1.0f, 1.0f);
  475. // top side
  476. vertecies[16].pos = Vec3<float>(left, back, top);
  477. vertecies[16].tPos = Vec2<float>(0.f, 0.f);
  478. vertecies[17].pos = Vec3<float>(right, back, top);
  479. vertecies[17].tPos = Vec2<float>(1.f, 0.f);
  480. vertecies[18].pos = Vec3<float>(left, front, top);
  481. vertecies[18].tPos = Vec2<float>(0.f, 1.f);
  482. vertecies[19].pos = Vec3<float>(right, front, top);
  483. vertecies[19].tPos = Vec2<float>(1.f, 1.f);
  484. // botom side
  485. vertecies[20].pos = Vec3<float>(left, front, bottom);
  486. vertecies[20].tPos = Vec2<float>(0.0f, 0.0f);
  487. vertecies[21].pos = Vec3<float>(right, front, bottom);
  488. vertecies[21].tPos = Vec2<float>(1.0f, 0.0f);
  489. vertecies[22].pos = Vec3<float>(left, back, bottom);
  490. vertecies[22].tPos = Vec2<float>(0.0f, 1.0f);
  491. vertecies[23].pos = Vec3<float>(right, back, bottom);
  492. vertecies[23].tPos = Vec2<float>(1.0f, 1.0f);
  493. data->setVertecies(vertecies, 24);
  494. // the order of the polygons has to be NORTH (front), EAST (left), SOUTH
  495. // (back), WEST (right), TOP, BOTTOM according to the Area definition front
  496. // side
  497. Polygon3D* p
  498. = new Polygon3D(); // looking from (0,0,0) to (1,0,0) to see this side
  499. p->indexAnz = 6;
  500. p->indexList = new int[p->indexAnz];
  501. p->indexList[0] = 0;
  502. p->indexList[1] = 1;
  503. p->indexList[2] = 2;
  504. p->indexList[3] = 1;
  505. p->indexList[4] = 3;
  506. p->indexList[5] = 2;
  507. data->addPolygon(p);
  508. // left side
  509. p = new Polygon3D(); // looking from (0,0,0) to (0,1,0) to see this side
  510. p->indexAnz = 6;
  511. p->indexList = new int[p->indexAnz];
  512. p->indexList[0] = 0 + 8;
  513. p->indexList[1] = 1 + 8;
  514. p->indexList[2] = 2 + 8;
  515. p->indexList[3] = 1 + 8;
  516. p->indexList[4] = 3 + 8;
  517. p->indexList[5] = 2 + 8;
  518. data->addPolygon(p);
  519. // back side
  520. p = new Polygon3D(); // looking from (0,0,0) to (-1,0,0) to see this side
  521. p->indexAnz = 6;
  522. p->indexList = new int[p->indexAnz];
  523. p->indexList[0] = 0 + 4;
  524. p->indexList[1] = 1 + 4;
  525. p->indexList[2] = 2 + 4;
  526. p->indexList[3] = 1 + 4;
  527. p->indexList[4] = 3 + 4;
  528. p->indexList[5] = 2 + 4;
  529. data->addPolygon(p);
  530. // right side
  531. p = new Polygon3D(); // looking from (0,0,0) to (0,-1,0) to see this side
  532. p->indexAnz = 6;
  533. p->indexList = new int[p->indexAnz];
  534. p->indexList[0] = 0 + 12;
  535. p->indexList[1] = 1 + 12;
  536. p->indexList[2] = 2 + 12;
  537. p->indexList[3] = 1 + 12;
  538. p->indexList[4] = 3 + 12;
  539. p->indexList[5] = 2 + 12;
  540. data->addPolygon(p);
  541. // top side
  542. p = new Polygon3D(); // looking from (0,0,0) to (0,0,-1) to see this side
  543. p->indexAnz = 6;
  544. p->indexList = new int[p->indexAnz];
  545. p->indexList[0] = 0 + 16;
  546. p->indexList[1] = 1 + 16;
  547. p->indexList[2] = 2 + 16;
  548. p->indexList[3] = 1 + 16;
  549. p->indexList[4] = 3 + 16;
  550. p->indexList[5] = 2 + 16;
  551. data->addPolygon(p);
  552. // botom side
  553. p = new Polygon3D(); // looking from (0,0,0) to (0,0,1) to see this side
  554. p->indexAnz = 6;
  555. p->indexList = new int[p->indexAnz];
  556. p->indexList[0] = 0 + 20;
  557. p->indexList[1] = 1 + 20;
  558. p->indexList[2] = 2 + 20;
  559. p->indexList[3] = 1 + 20;
  560. p->indexList[4] = 3 + 20;
  561. p->indexList[5] = 2 + 20;
  562. data->addPolygon(p);
  563. data->calculateNormals();
  564. data->release();
  565. }
  566. void createModels(Bildschirm* zScreen)
  567. {
  568. createDefaultCube(zScreen);
  569. createPlayer(zScreen);
  570. createGrass(zScreen);
  571. createFluidCube(zScreen);
  572. }
  573. struct FileInfo
  574. {
  575. Text path;
  576. __int64 size;
  577. int year;
  578. int month;
  579. int day;
  580. int seconds;
  581. };
  582. FactoryClient::FactoryClient()
  583. {
  584. client = 0;
  585. background = 0;
  586. foreground = 0;
  587. backgroundReader = 0;
  588. foregroundReader = 0;
  589. bgReaderUsage = 0;
  590. fgReaderUsage = 0;
  591. }
  592. FactoryClient::~FactoryClient()
  593. {
  594. if (client) disconnect();
  595. }
  596. void FactoryClient::loadServerInfo()
  597. {
  598. LoadMenu* loadMenu = (LoadMenu*)(Menu*)menuRegister->get("load");
  599. Text resourcePath = "data/resources/";
  600. resourcePath.append() << client->getServerIp() << "/"
  601. << client->getServerPort() << "/";
  602. Text tmp = resourcePath + "metainfo.dat";
  603. Datei metaInfo(tmp);
  604. Trie<FileInfo*> metaInfos;
  605. Array<FileInfo*> metaInfoList;
  606. Array<FileInfo*> downloadOrder;
  607. if (metaInfo.existiert())
  608. {
  609. loadMenu->beginNextStage(
  610. "reading resource metadata", (int)metaInfo.getSize());
  611. metaInfo.open(Datei::Style::lesen);
  612. while (!metaInfo.istEnde())
  613. {
  614. short len = 0;
  615. metaInfo.lese((char*)&len, 2);
  616. char* path = new char[len + 1];
  617. metaInfo.lese(path, len);
  618. path[len] = 0;
  619. FileInfo* fi = new FileInfo();
  620. fi->path = Text(path);
  621. fi->size = 0;
  622. metaInfo.lese((char*)&fi->year, 4);
  623. metaInfo.lese((char*)&fi->month, 4);
  624. metaInfo.lese((char*)&fi->day, 4);
  625. metaInfo.lese((char*)&fi->seconds, 4);
  626. metaInfos.set(path, len, fi);
  627. delete[] path;
  628. metaInfoList.add(fi);
  629. loadMenu->stageProgress(18 + len);
  630. }
  631. metaInfo.close();
  632. }
  633. short len = 0;
  634. foreground->getNachricht((char*)&len, 2);
  635. while (len)
  636. {
  637. char* path = new char[len + 1];
  638. foreground->getNachricht(path, len);
  639. path[len] = 0;
  640. FileInfo* fi = metaInfos.get(path, len);
  641. int year;
  642. int month;
  643. int day;
  644. int seconds;
  645. foreground->getNachricht((char*)&year, 4);
  646. foreground->getNachricht((char*)&month, 4);
  647. foreground->getNachricht((char*)&day, 4);
  648. foreground->getNachricht((char*)&seconds, 4);
  649. if (!fi || fi->year != year || fi->month != month || fi->day != day
  650. || fi->seconds != seconds)
  651. {
  652. if (!fi)
  653. {
  654. fi = new FileInfo();
  655. fi->path = Text(path);
  656. metaInfos.set(path, len, fi);
  657. metaInfoList.add(fi);
  658. }
  659. fi->year = year;
  660. fi->month = month;
  661. fi->day = day;
  662. fi->seconds = seconds;
  663. foreground->sende("\1", 1);
  664. foreground->getNachricht((char*)&fi->size, 8);
  665. downloadOrder.add(fi);
  666. }
  667. else
  668. {
  669. foreground->sende("\0", 1);
  670. }
  671. delete[] path;
  672. foreground->getNachricht((char*)&len, 2);
  673. }
  674. loadMenu->allProgress(1);
  675. char* buffer = new char[4096];
  676. loadMenu->beginNextStage(
  677. "download updated resouces", downloadOrder.getEintragAnzahl());
  678. for (FileInfo* info : downloadOrder)
  679. {
  680. loadMenu->beginNextStep(info->path, (int)info->size);
  681. tmp = resourcePath + info->path;
  682. Datei file(tmp);
  683. if (!file.existiert())
  684. {
  685. file.erstellen();
  686. }
  687. file.open(Datei::Style::schreiben);
  688. __int64 remaining = info->size;
  689. while (remaining > 0)
  690. {
  691. int toRead = remaining > 4096 ? 4096 : (int)remaining;
  692. foreground->getNachricht(buffer, toRead);
  693. file.schreibe(buffer, toRead);
  694. remaining -= toRead;
  695. loadMenu->stepProgress(toRead);
  696. }
  697. file.close();
  698. loadMenu->stageProgress(1);
  699. }
  700. delete[] buffer;
  701. loadMenu->allProgress(1);
  702. loadMenu->beginNextStage(
  703. "writing resource metadata", metaInfoList.getEintragAnzahl());
  704. if (!metaInfo.existiert())
  705. {
  706. metaInfo.erstellen();
  707. }
  708. metaInfo.open(Datei::Style::schreiben);
  709. for (FileInfo* info : metaInfoList)
  710. {
  711. short len = (short)info->path.getLength();
  712. metaInfo.schreibe((char*)&len, 2);
  713. metaInfo.schreibe(info->path.getText(), len);
  714. metaInfo.schreibe((char*)&info->year, 4);
  715. metaInfo.schreibe((char*)&info->month, 4);
  716. metaInfo.schreibe((char*)&info->day, 4);
  717. metaInfo.schreibe((char*)&info->seconds, 4);
  718. loadMenu->stageProgress(1);
  719. }
  720. metaInfo.close();
  721. loadMenu->allProgress(1);
  722. loadMenu->beginNextStage(
  723. "loading resources", metaInfoList.getEintragAnzahl());
  724. for (FileInfo* info : metaInfoList)
  725. {
  726. if (info->path.endsWith(".ltdb"))
  727. {
  728. LTDBDatei dat;
  729. dat.setDatei(new Text(resourcePath + info->path));
  730. dat.leseDaten(0);
  731. loadMenu->beginNextStep(info->path, dat.getBildAnzahl());
  732. for (Text* name : *dat.zBildListe())
  733. {
  734. Bild* b = dat.laden(0, new Text(*name));
  735. uiFactory.initParam.bildschirm->zGraphicsApi()
  736. ->createOrGetTextur(info->path + "/" + *name, b)
  737. ->release();
  738. loadMenu->stepProgress(1);
  739. }
  740. }
  741. else if (info->path.endsWith(".m3"))
  742. {
  743. M3Datei dat(resourcePath + info->path);
  744. dat.leseDaten();
  745. loadMenu->beginNextStep(info->path, dat.getModelAnzahl());
  746. for (int i = 0; i < dat.getModelAnzahl(); i++)
  747. {
  748. Model3DData* d = dat.ladeModel(dat.zModelName(i)->getText(),
  749. uiFactory.initParam.bildschirm->zGraphicsApi(),
  750. info->path + "/" + *dat.zModelName(i));
  751. d->release();
  752. loadMenu->stepProgress(1);
  753. }
  754. }
  755. loadMenu->stageProgress(1);
  756. }
  757. loadMenu->allProgress(1);
  758. createModels(uiFactory.initParam.bildschirm);
  759. loadMenu->allProgress(1);
  760. std::cout << "downloading server type information\n";
  761. // receive type information
  762. for (int i = 0; i < blockTypeCount; i++)
  763. blockTypes[i]->release();
  764. delete[] blockTypes;
  765. for (int i = 0; i < itemTypeCount; i++)
  766. itemTypes[i]->release();
  767. delete[] itemTypes;
  768. for (int i = 0; i < entityTypeCount; i++)
  769. entityTypes[i]->release();
  770. delete[] entityTypes;
  771. foregroundReader->lese((char*)&blockTypeCount, 4);
  772. blockTypes = new BlockType*[blockTypeCount];
  773. for (int i = 0; i < blockTypeCount; i++)
  774. {
  775. int id;
  776. foregroundReader->lese((char*)&id, 4);
  777. bool needsInstance;
  778. foregroundReader->lese((char*)&needsInstance, 1);
  779. bool needsSubscription;
  780. foregroundReader->lese((char*)&needsSubscription, 1);
  781. bool fluid;
  782. foregroundReader->lese((char*)&fluid, 1);
  783. char maxFlowDistance = 0;
  784. if (fluid) foregroundReader->lese((char*)&maxFlowDistance, 1);
  785. int maxHp;
  786. foregroundReader->lese((char*)&maxHp, 4);
  787. blockTypes[i] = new BlockType(id,
  788. needsInstance,
  789. ModelInfo(foregroundReader),
  790. maxHp,
  791. needsSubscription,
  792. fluid,
  793. maxFlowDistance);
  794. }
  795. loadMenu->allProgress(1);
  796. foregroundReader->lese((char*)&itemTypeCount, 4);
  797. itemTypes = new ItemType*[itemTypeCount];
  798. for (int i = 0; i < itemTypeCount; i++)
  799. {
  800. int id;
  801. foregroundReader->lese((char*)&id, 4);
  802. char len;
  803. foregroundReader->lese((char*)&len, 1);
  804. char* name = new char[len + 1];
  805. foregroundReader->lese(name, len);
  806. name[len] = 0;
  807. short tlen;
  808. foregroundReader->lese((char*)&tlen, 2);
  809. char* tooltipUIML = new char[tlen + 1];
  810. foregroundReader->lese(tooltipUIML, tlen);
  811. tooltipUIML[tlen] = 0;
  812. itemTypes[i] = new ItemType(
  813. id, ModelInfo(foregroundReader), Text(name), Text(tooltipUIML));
  814. delete[] name;
  815. delete[] tooltipUIML;
  816. }
  817. loadMenu->allProgress(1);
  818. foregroundReader->lese((char*)&entityTypeCount, 4);
  819. entityTypes = new EntityType*[entityTypeCount];
  820. for (int i = 0; i < entityTypeCount; i++)
  821. {
  822. int id;
  823. foregroundReader->lese((char*)&id, 4);
  824. entityTypes[i] = new EntityType(id, ModelInfo(foregroundReader));
  825. }
  826. loadMenu->allProgress(1);
  827. loadMenu->beginNextStage("rendering item icons", itemTypeCount);
  828. Kam3D* kam = new Kam3D();
  829. Welt3D* w = new Welt3D();
  830. w->addDiffuseLight(DiffuseLight{
  831. Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f)});
  832. kam->setWelt(w);
  833. kam->setBildschirmPosition(0, 0);
  834. kam->setBildschirmSize(50, 50);
  835. kam->setPosition(Vec3<float>(0, 0, 0));
  836. kam->setRotation(
  837. {(float)PI / 2.f, 0.f, std::atan2(0.f, -1.f) + (float)PI / 2});
  838. Bild* b = new Bild();
  839. b->neuBild(50, 50, 0);
  840. for (int i = 0; i < itemTypeCount; i++)
  841. {
  842. Model3D* mdl = new Model3D();
  843. Model3DData* data = itemTypes[i]->getItemModel();
  844. if (data)
  845. {
  846. Vec3<float> min = data->getMinPos();
  847. Vec3<float> max = data->getMaxPos();
  848. float maxX = MAX(
  849. MAX(MAX(abs(min.x), abs(max.x)), MAX(abs(min.y), abs(max.y))),
  850. MAX(abs(min.z), abs(max.z)));
  851. kam->setPosition(Vec3<float>(maxX * 3.5f, 0.f, 0.f));
  852. }
  853. mdl->setModelDaten(data);
  854. mdl->setModelTextur(itemTypes[i]->getItemTextur());
  855. mdl->setPosition(Vec3<float>(0.f, 0.f, 0.f));
  856. mdl->setDrehung(0.25f, 0.25f, 0.55f);
  857. mdl->setAmbientFactor(0.8f);
  858. mdl->setDiffusFactor(0.1f);
  859. mdl->setSpecularFactor(0.1f);
  860. mdl->setSize(itemTypes[i]->getSize());
  861. w->addZeichnung(mdl);
  862. w->tick(0);
  863. window->zBildschirm()->lock();
  864. DX11Textur* t = (DX11Textur*)window->zBildschirm()
  865. ->zGraphicsApi()
  866. ->createOrGetTextur(
  867. Text("rendered/items/") + itemTypes[i]->getId(),
  868. dynamic_cast<Bild*>(b->getThis()));
  869. window->zBildschirm()->zGraphicsApi()->renderKamera(kam, t);
  870. Bild* result = new Bild();
  871. t->copyToImage(result);
  872. itemTypes[i]->setBild(result);
  873. t->release();
  874. window->zBildschirm()->unlock();
  875. w->removeZeichnung(mdl);
  876. loadMenu->stageProgress(1);
  877. }
  878. b->release();
  879. kam->release();
  880. loadMenu->allProgress(1);
  881. }
  882. bool FactoryClient::connect(Text ip, unsigned short sslPort)
  883. {
  884. if (client) disconnect();
  885. client = new SSLKlient();
  886. if (!client->verbinde(sslPort, ip)) return false;
  887. char c;
  888. while (client->hatNachricht(1))
  889. client->getNachricht(&c, 1);
  890. this->ip = ip;
  891. return 1;
  892. }
  893. int FactoryClient::ping()
  894. {
  895. ZeitMesser zm;
  896. zm.messungStart();
  897. if (!client->sende("\3", 1)) return -1;
  898. char c;
  899. client->getNachricht(&c, 1);
  900. zm.messungEnde();
  901. return (int)(zm.getSekunden() * 1000);
  902. }
  903. int FactoryClient::status(Framework::Text name, Framework::Text secret)
  904. {
  905. if (!client->sende("\4", 1)) return 404;
  906. char c;
  907. client->getNachricht(&c, 1);
  908. if (c == 1)
  909. {
  910. char len = (char)name.getLength();
  911. client->sende(&len, 1);
  912. client->sende(name, len);
  913. short sLen = (short)secret.getLength();
  914. client->sende((char*)&sLen, 2);
  915. client->sende(secret, sLen);
  916. char res;
  917. client->getNachricht(&res, 1);
  918. if (res == 1) return 200;
  919. if (res == 0) return 403;
  920. }
  921. return 404;
  922. }
  923. int FactoryClient::join(
  924. Framework::Text name, Framework::Text& secret, unsigned short port)
  925. {
  926. client->sende("\1", 1);
  927. char len = (char)name.getLength();
  928. client->sende(&len, 1);
  929. client->sende(name, len);
  930. short sLen = (short)secret.getLength();
  931. client->sende((char*)&sLen, 2);
  932. client->sende(secret, sLen);
  933. char res;
  934. client->getNachricht(&res, 1);
  935. if (res == 1 || res == 2)
  936. {
  937. if (res == 2)
  938. {
  939. client->getNachricht((char*)&sLen, 2);
  940. char* buffer = new char[sLen + 1];
  941. client->getNachricht(buffer, sLen);
  942. buffer[sLen] = 0;
  943. secret = buffer;
  944. delete[] buffer;
  945. }
  946. short keyLen;
  947. client->getNachricht((char*)&keyLen, 2);
  948. char* key = new char[keyLen];
  949. client->getNachricht(key, keyLen);
  950. foreground = new Klient();
  951. if (!foreground->verbinde(port, ip))
  952. {
  953. delete[] key;
  954. return false;
  955. }
  956. if (!foreground->sende((char*)&keyLen, 2))
  957. {
  958. delete[] key;
  959. return false;
  960. }
  961. if (!foreground->sende(key, keyLen))
  962. {
  963. delete[] key;
  964. return false;
  965. }
  966. background = new Klient();
  967. if (!background->verbinde(port, ip))
  968. {
  969. delete[] key;
  970. foreground->release();
  971. foreground = 0;
  972. background->release();
  973. background = 0;
  974. return false;
  975. }
  976. if (!background->sende((char*)&keyLen, 2))
  977. {
  978. delete[] key;
  979. foreground->release();
  980. foreground = 0;
  981. background->release();
  982. background = 0;
  983. return false;
  984. }
  985. if (!background->sende(key, keyLen))
  986. {
  987. delete[] key;
  988. foreground->release();
  989. foreground = 0;
  990. background->release();
  991. background = 0;
  992. return false;
  993. }
  994. delete[] key;
  995. bool bg = 0;
  996. if (!foreground->sende((char*)&bg, 1))
  997. {
  998. delete[] key;
  999. return 201;
  1000. }
  1001. foregroundReader = new NetworkReader(foreground);
  1002. bg = 1;
  1003. if (!background->sende((char*)&bg, 1)) return 201;
  1004. backgroundReader = new NetworkReader(background);
  1005. char res;
  1006. foregroundReader->lese(&res, 1);
  1007. if (res != 1) return 403;
  1008. backgroundReader->lese(&res, 1);
  1009. if (res != 1) return 403;
  1010. client->trenne();
  1011. loadServerInfo();
  1012. return 200;
  1013. }
  1014. if (res == 0) return 403;
  1015. return 500;
  1016. }
  1017. void FactoryClient::disconnect()
  1018. {
  1019. if (client)
  1020. {
  1021. NetworkReader* fgReader = foregroundReader;
  1022. NetworkReader* bgReader = backgroundReader;
  1023. backgroundReader = 0;
  1024. foregroundReader = 0;
  1025. if (foreground) foreground->trenne();
  1026. if (background) background->trenne();
  1027. while (fgReaderUsage > 0 || bgReaderUsage > 0)
  1028. Sleep(100);
  1029. delete fgReader;
  1030. delete bgReader;
  1031. client->release();
  1032. client = 0;
  1033. if (foreground) foreground->release();
  1034. foreground = 0;
  1035. if (background) background->release();
  1036. background = 0;
  1037. }
  1038. }
  1039. NetworkReader* FactoryClient::getNextForegroundMessage()
  1040. {
  1041. fgReaderUsage++;
  1042. if (!foreground) return 0;
  1043. if (!foreground->isConnected()) return 0;
  1044. if (!foreground->hatNachricht(0)) return 0;
  1045. return foregroundReader;
  1046. }
  1047. NetworkReader* FactoryClient::getNextBackgroundMessage()
  1048. {
  1049. bgReaderUsage++;
  1050. if (!background) return 0;
  1051. if (!background->isConnected()) return 0;
  1052. if (!background->hatNachricht(0)) return 0;
  1053. return backgroundReader;
  1054. }
  1055. void FactoryClient::endMessageReading(bool bg)
  1056. {
  1057. if (bg)
  1058. bgReaderUsage--;
  1059. else
  1060. fgReaderUsage--;
  1061. }
  1062. void FactoryClient::sendPlayerAction(const char* data, unsigned short length)
  1063. {
  1064. if (!foreground) return;
  1065. cs.lock();
  1066. length += 1;
  1067. foreground->sende((char*)&length, 2);
  1068. char msgId = 2;
  1069. foreground->sende(&msgId, 1);
  1070. foreground->sende((char*)data, length - 1);
  1071. cs.unlock();
  1072. }
  1073. void FactoryClient::sendPlayerMovement(MovementFrame& frame)
  1074. {
  1075. if (!foreground) return;
  1076. cs.lock();
  1077. short length = 38;
  1078. foreground->sende((char*)&length, 2);
  1079. char msgId = 2; // player message
  1080. foreground->sende(&msgId, 1);
  1081. foreground->sende(&msgId, 1); // set movement
  1082. foreground->sende((char*)&frame.direction.x, 4);
  1083. foreground->sende((char*)&frame.direction.y, 4);
  1084. foreground->sende((char*)&frame.direction.z, 4);
  1085. foreground->sende((char*)&frame.targetPosition.x, 4);
  1086. foreground->sende((char*)&frame.targetPosition.y, 4);
  1087. foreground->sende((char*)&frame.targetPosition.z, 4);
  1088. foreground->sende((char*)&frame.movementFlags, 4);
  1089. foreground->sende((char*)&frame.duration, 8);
  1090. cs.unlock();
  1091. }
  1092. void FactoryClient::entityAPIRequest(
  1093. int entityId, const char* message, unsigned short length)
  1094. {
  1095. if (!foreground) return;
  1096. cs.lock();
  1097. length += 5;
  1098. foreground->sende((char*)&length, 2);
  1099. char msgId = 3;
  1100. foreground->sende(&msgId, 1);
  1101. foreground->sende((char*)&entityId, 4);
  1102. foreground->sende(message, length - 5);
  1103. cs.unlock();
  1104. }
  1105. void FactoryClient::blockAPIRequest(
  1106. Vec3<int> pos, const char* message, unsigned short length)
  1107. {
  1108. if (!foreground) return;
  1109. cs.lock();
  1110. length += 14;
  1111. foreground->sende((char*)&length, 2);
  1112. char msgId = 1;
  1113. foreground->sende(&msgId, 1);
  1114. foreground->sende(&msgId, 1);
  1115. foreground->sende((char*)&pos.x, 4);
  1116. foreground->sende((char*)&pos.y, 4);
  1117. foreground->sende((char*)&pos.z, 4);
  1118. foreground->sende(message, length - 14);
  1119. cs.unlock();
  1120. }
  1121. void FactoryClient::componentAPIRequest(
  1122. int* address, int addrLen, const char* message, unsigned short length)
  1123. {
  1124. if (!foreground) return;
  1125. cs.lock();
  1126. short sum = length + addrLen * 4 + (addrLen > 2 ? 3 : 2);
  1127. foreground->sende((char*)&sum, 2);
  1128. if (addrLen > 2)
  1129. {
  1130. char msgId = 7; // dimension request
  1131. foreground->sende(&msgId, 1);
  1132. foreground->sende((char*)&address[0], 4); // dimension id
  1133. msgId = 1; // block request
  1134. foreground->sende(&msgId, 1);
  1135. foreground->sende((char*)&address[1], 4); // position x
  1136. foreground->sende((char*)&address[2], 4); // position y
  1137. foreground->sende((char*)&address[3], 4); // position z
  1138. msgId = 2; // component request
  1139. foreground->sende(&msgId, 1);
  1140. foreground->sende((char*)&address[4], 4); // component index
  1141. }
  1142. else
  1143. {
  1144. char msgId = 3; // entity request
  1145. foreground->sende(&msgId, 1);
  1146. foreground->sende((char*)&address[0], 4); // entity id
  1147. msgId = 2; // component request
  1148. foreground->sende(&msgId, 1);
  1149. foreground->sende((char*)&address[1], 4); // component index
  1150. }
  1151. foreground->sende(message, length);
  1152. cs.unlock();
  1153. }
  1154. void FactoryClient::blockAPIRequest(
  1155. int dimensionId, Vec3<int> pos, const char* message, unsigned short length)
  1156. {
  1157. if (!foreground) return;
  1158. cs.lock();
  1159. length += 18;
  1160. foreground->sende((char*)&length, 2);
  1161. char msgId = 7;
  1162. foreground->sende(&msgId, 1);
  1163. foreground->sende((char*)&dimensionId, 4);
  1164. msgId = 1;
  1165. foreground->sende(&msgId, 1);
  1166. foreground->sende((char*)&pos.x, 4);
  1167. foreground->sende((char*)&pos.y, 4);
  1168. foreground->sende((char*)&pos.z, 4);
  1169. foreground->sende(message, length - 18);
  1170. cs.unlock();
  1171. }
  1172. void FactoryClient::chunkAPIRequest(
  1173. Punkt center, const char* message, unsigned short length)
  1174. {
  1175. if (!foreground) return;
  1176. length += 10;
  1177. cs.lock();
  1178. foreground->sende((char*)&length, 2);
  1179. char type = 1;
  1180. foreground->sende(&type, 1);
  1181. type = 0;
  1182. foreground->sende(&type, 1);
  1183. foreground->sende((char*)&center.x, 4);
  1184. foreground->sende((char*)&center.y, 4);
  1185. foreground->sende(message, length - 10);
  1186. cs.unlock();
  1187. }
  1188. void FactoryClient::dimensionAPIRequest(
  1189. const char* message, unsigned short length)
  1190. {
  1191. if (!foreground) return;
  1192. length += 1;
  1193. cs.lock();
  1194. foreground->sende((char*)&length, 2);
  1195. char type = 1;
  1196. foreground->sende(&type, 1);
  1197. foreground->sende(message, length - 1);
  1198. cs.unlock();
  1199. }
  1200. void FactoryClient::inventoryAPIRequest(
  1201. Framework::Either<int, Framework::VecN<int, 4>> target,
  1202. const char* message,
  1203. unsigned short length)
  1204. {
  1205. if (!foreground) return;
  1206. cs.lock();
  1207. length += target.isA() ? 6 : 18;
  1208. foreground->sende((char*)&length, 2);
  1209. char msgId = 4;
  1210. foreground->sende(&msgId, 1);
  1211. bool isEntity = target.isA();
  1212. foreground->sende((char*)&isEntity, 1);
  1213. if (target.isA())
  1214. {
  1215. int id = target.getA();
  1216. foreground->sende((char*)&id, 4);
  1217. }
  1218. else
  1219. {
  1220. for (int i = 0; i < 4; i++)
  1221. {
  1222. int v = target.getB()[i];
  1223. foreground->sende((char*)&v, 4);
  1224. }
  1225. }
  1226. foreground->sende(message, length - (target.isA() ? 6 : 18));
  1227. cs.unlock();
  1228. }
  1229. void FactoryClient::uiRequest(
  1230. Framework::Text dialogId, const char* message, unsigned short length)
  1231. {
  1232. if (!foreground) return;
  1233. short nameLen = (short)dialogId.getLength();
  1234. length += nameLen + 3;
  1235. foreground->sende((char*)&length, 2);
  1236. cs.lock();
  1237. char msgId = 8;
  1238. foreground->sende(&msgId, 1);
  1239. foreground->sende((char*)&nameLen, 2);
  1240. foreground->sende(dialogId, nameLen);
  1241. foreground->sende(message, length - nameLen - 3);
  1242. cs.unlock();
  1243. }
  1244. void FactoryClient::craftingUIMLRequest(int itemTypeId)
  1245. {
  1246. if (!foreground) return;
  1247. cs.lock();
  1248. short length = 5;
  1249. foreground->sende((char*)&length, 2);
  1250. char msgId = 5;
  1251. foreground->sende(&msgId, 1);
  1252. foreground->sende((char*)&itemTypeId, 4);
  1253. cs.unlock();
  1254. }
  1255. void FactoryClient::sendChatMessage(Framework::Text message)
  1256. {
  1257. if (!background) return;
  1258. cs.lock();
  1259. short length = message.getLength() + 4;
  1260. background->sende((char*)&length, 2);
  1261. char msgId = 6;
  1262. background->sende(&msgId, 1);
  1263. msgId = 0;
  1264. background->sende(&msgId, 1);
  1265. length = message.getLength();
  1266. background->sende((char*)&length, 2);
  1267. background->sende(message.getText(), message.getLength());
  1268. cs.unlock();
  1269. }
  1270. void FactoryClient::chatAPIRequest(const char* data, unsigned short length)
  1271. {
  1272. if (!background) return;
  1273. cs.lock();
  1274. short totalLength = length + 1;
  1275. background->sende((char*)&totalLength, 2);
  1276. char msgId = 6;
  1277. background->sende(&msgId, 1);
  1278. background->sende(data, length);
  1279. cs.unlock();
  1280. }
  1281. bool FactoryClient::isConnected()
  1282. {
  1283. return foreground && background && foreground->isConnected()
  1284. && background->isConnected();
  1285. }
  1286. void FactoryClient::leaveGame()
  1287. {
  1288. cs.lock();
  1289. disconnect();
  1290. cs.unlock();
  1291. }