FactoryClient.cpp 41 KB

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