FactoryClient.cpp 41 KB

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