FactoryClient.cpp 42 KB

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