FactoryClient.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. #include "FactoryClient.h"
  2. #include <File.h>
  3. #include <FileSystem.h>
  4. #include <Image.h>
  5. #include <M3File.h>
  6. #include <RenderThread.h>
  7. #include <Texture.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. Image* b = new Image();
  701. b->newImage(50, 50, 0);
  702. for (int i = 0; i < itemTypeCount; i++)
  703. {
  704. Model3D* mdl = new Model3D();
  705. Model3DData* data = itemTypes[i]->getItemModel();
  706. if (data)
  707. {
  708. Vec3<float> min = data->getMinPos();
  709. Vec3<float> max = data->getMaxPos();
  710. float maxX = MAX(
  711. MAX(MAX(abs(min.x), abs(max.x)), MAX(abs(min.y), abs(max.y))),
  712. MAX(abs(min.z), abs(max.z)));
  713. kam->setPosition(Vec3<float>(maxX * 3.5f, 0.f, 0.f));
  714. }
  715. mdl->setModelData(data);
  716. mdl->setModelTextur(itemTypes[i]->getItemTextur());
  717. mdl->setPosition(Vec3<float>(0.f, 0.f, 0.f));
  718. mdl->setRotation(0.25f, 0.25f, 0.55f);
  719. mdl->setAmbientFactor(0.8f);
  720. mdl->setDiffusFactor(0.1f);
  721. mdl->setSpecularFactor(0.1f);
  722. mdl->setSize(itemTypes[i]->getSize());
  723. w->addDrawable(mdl);
  724. w->tick(0);
  725. DX11Texture* t = (DX11Texture*)window->zScreen()
  726. ->zGraphicsApi()
  727. ->createOrGetTexture(Text("rendered/items/")
  728. + itemTypes[i]->getId(),
  729. dynamic_cast<Image*>(b->getThis()));
  730. renderThread->writeLock().lock();
  731. window->zScreen()->zGraphicsApi()->renderKamera(kam, t);
  732. renderThread->writeLock().unlock();
  733. Image* result = new Image();
  734. t->copyToImage(result);
  735. itemTypes[i]->setBild(result);
  736. t->release();
  737. w->removeDrawable(mdl);
  738. loadMenu->stageProgress(1);
  739. }
  740. b->release();
  741. kam->release();
  742. loadMenu->allProgress(1);
  743. }
  744. bool FactoryClient::connect(Text ip, unsigned short sslPort)
  745. {
  746. if (client) disconnect();
  747. client = new SSLClient();
  748. if (!client->connect(sslPort, ip)) return false;
  749. char c;
  750. while (client->hasMessage(1))
  751. client->getMessage(&c, 1);
  752. this->ip = ip;
  753. return 1;
  754. }
  755. int FactoryClient::ping()
  756. {
  757. Timer zm;
  758. zm.measureStart();
  759. if (!client->send("\3", 1)) return -1;
  760. char c;
  761. client->getMessage(&c, 1);
  762. zm.measureEnd();
  763. return (int)(zm.getSekunden() * 1000);
  764. }
  765. int FactoryClient::status(Framework::Text name, Framework::Text secret)
  766. {
  767. if (!client->send("\4", 1)) return 404;
  768. char c;
  769. client->getMessage(&c, 1);
  770. if (c == 1)
  771. {
  772. char len = (char)name.getLength();
  773. client->send(&len, 1);
  774. client->send(name, len);
  775. short sLen = (short)secret.getLength();
  776. client->send((char*)&sLen, 2);
  777. client->send(secret, sLen);
  778. char res;
  779. client->getMessage(&res, 1);
  780. if (res == 1) return 200;
  781. if (res == 0) return 403;
  782. }
  783. return 404;
  784. }
  785. int FactoryClient::join(
  786. Framework::Text name, Framework::Text& secret, unsigned short port)
  787. {
  788. client->send("\1", 1);
  789. char len = (char)name.getLength();
  790. client->send(&len, 1);
  791. client->send(name, len);
  792. short sLen = (short)secret.getLength();
  793. client->send((char*)&sLen, 2);
  794. client->send(secret, sLen);
  795. char res;
  796. client->getMessage(&res, 1);
  797. if (res == 1 || res == 2)
  798. {
  799. if (res == 2)
  800. {
  801. client->getMessage((char*)&sLen, 2);
  802. char* buffer = new char[sLen + 1];
  803. client->getMessage(buffer, sLen);
  804. buffer[sLen] = 0;
  805. secret = buffer;
  806. delete[] buffer;
  807. }
  808. short keyLen;
  809. client->getMessage((char*)&keyLen, 2);
  810. char* key = new char[keyLen];
  811. client->getMessage(key, keyLen);
  812. foreground = new Client();
  813. if (!foreground->connect(port, ip))
  814. {
  815. delete[] key;
  816. return false;
  817. }
  818. if (!foreground->send((char*)&keyLen, 2))
  819. {
  820. delete[] key;
  821. return false;
  822. }
  823. if (!foreground->send(key, keyLen))
  824. {
  825. delete[] key;
  826. return false;
  827. }
  828. background = new Client();
  829. if (!background->connect(port, ip))
  830. {
  831. delete[] key;
  832. foreground->release();
  833. foreground = 0;
  834. background->release();
  835. background = 0;
  836. return false;
  837. }
  838. if (!background->send((char*)&keyLen, 2))
  839. {
  840. delete[] key;
  841. foreground->release();
  842. foreground = 0;
  843. background->release();
  844. background = 0;
  845. return false;
  846. }
  847. if (!background->send(key, keyLen))
  848. {
  849. delete[] key;
  850. foreground->release();
  851. foreground = 0;
  852. background->release();
  853. background = 0;
  854. return false;
  855. }
  856. delete[] key;
  857. bool bg = 0;
  858. if (!foreground->send((char*)&bg, 1))
  859. {
  860. delete[] key;
  861. return 201;
  862. }
  863. foregroundReader = new NetworkReader(foreground);
  864. bg = 1;
  865. if (!background->send((char*)&bg, 1)) return 201;
  866. backgroundReader = new NetworkReader(background);
  867. char res;
  868. foregroundReader->read(&res, 1);
  869. if (res != 1) return 403;
  870. backgroundReader->read(&res, 1);
  871. if (res != 1) return 403;
  872. client->disconnect();
  873. loadServerInfo();
  874. return 200;
  875. }
  876. if (res == 0) return 403;
  877. return 500;
  878. }
  879. void FactoryClient::disconnect()
  880. {
  881. if (client)
  882. {
  883. NetworkReader* fgReader = foregroundReader;
  884. NetworkReader* bgReader = backgroundReader;
  885. backgroundReader = 0;
  886. foregroundReader = 0;
  887. if (foreground) foreground->disconnect();
  888. if (background) background->disconnect();
  889. while (fgReaderUsage > 0 || bgReaderUsage > 0)
  890. Sleep(100);
  891. delete fgReader;
  892. delete bgReader;
  893. client->release();
  894. client = 0;
  895. if (foreground) foreground->release();
  896. foreground = 0;
  897. if (background) background->release();
  898. background = 0;
  899. }
  900. }
  901. NetworkReader* FactoryClient::getNextForegroundMessage()
  902. {
  903. fgReaderUsage++;
  904. if (!foreground) return 0;
  905. if (!foreground->isConnected()) return 0;
  906. if (!foreground->hasMessage(0)) return 0;
  907. return foregroundReader;
  908. }
  909. NetworkReader* FactoryClient::getNextBackgroundMessage()
  910. {
  911. bgReaderUsage++;
  912. if (!background) return 0;
  913. if (!background->isConnected()) return 0;
  914. if (!background->hasMessage(0)) return 0;
  915. return backgroundReader;
  916. }
  917. void FactoryClient::endMessageReading(bool bg)
  918. {
  919. if (bg)
  920. bgReaderUsage--;
  921. else
  922. fgReaderUsage--;
  923. }
  924. void FactoryClient::sendPlayerAction(const char* data, unsigned short length)
  925. {
  926. if (!foreground) return;
  927. cs.lock();
  928. length += 1;
  929. foreground->send((char*)&length, 2);
  930. char msgId = 2;
  931. foreground->send(&msgId, 1);
  932. foreground->send((char*)data, length - 1);
  933. cs.unlock();
  934. }
  935. void FactoryClient::sendPlayerFaceDirection(Vec3<float> dir)
  936. {
  937. if (!foreground) return;
  938. cs.lock();
  939. short length = 14;
  940. foreground->send((char*)&length, 2);
  941. char msgId = 2; // player message
  942. foreground->send(&msgId, 1);
  943. foreground->send(&msgId, 1); // set face direction
  944. foreground->send((char*)&dir.x, 4);
  945. foreground->send((char*)&dir.y, 4);
  946. foreground->send((char*)&dir.z, 4);
  947. cs.unlock();
  948. }
  949. void FactoryClient::sendPlayerMovement(int flags)
  950. {
  951. if (!foreground) return;
  952. cs.lock();
  953. short length = 6;
  954. foreground->send((char*)&length, 2);
  955. char msgId = 2; // player message
  956. foreground->send(&msgId, 1);
  957. msgId = 10; // set movement
  958. foreground->send(&msgId, 1);
  959. foreground->send((char*)&flags, 4);
  960. cs.unlock();
  961. }
  962. void FactoryClient::entityAPIRequest(
  963. int entityId, const char* message, unsigned short length)
  964. {
  965. if (!foreground) return;
  966. cs.lock();
  967. length += 5;
  968. foreground->send((char*)&length, 2);
  969. char msgId = 3;
  970. foreground->send(&msgId, 1);
  971. foreground->send((char*)&entityId, 4);
  972. foreground->send(message, length - 5);
  973. cs.unlock();
  974. }
  975. void FactoryClient::blockAPIRequest(
  976. Vec3<int> pos, const char* message, unsigned short length)
  977. {
  978. if (!foreground) return;
  979. cs.lock();
  980. length += 14;
  981. foreground->send((char*)&length, 2);
  982. char msgId = 1;
  983. foreground->send(&msgId, 1);
  984. foreground->send(&msgId, 1);
  985. foreground->send((char*)&pos.x, 4);
  986. foreground->send((char*)&pos.y, 4);
  987. foreground->send((char*)&pos.z, 4);
  988. foreground->send(message, length - 14);
  989. cs.unlock();
  990. }
  991. void FactoryClient::componentAPIRequest(
  992. int* address, int addrLen, const char* message, unsigned short length)
  993. {
  994. if (!foreground) return;
  995. cs.lock();
  996. short sum = length + addrLen * 4 + (addrLen > 2 ? 3 : 2);
  997. foreground->send((char*)&sum, 2);
  998. if (addrLen > 2)
  999. {
  1000. char msgId = 7; // dimension request
  1001. foreground->send(&msgId, 1);
  1002. foreground->send((char*)&address[0], 4); // dimension id
  1003. msgId = 1; // block request
  1004. foreground->send(&msgId, 1);
  1005. foreground->send((char*)&address[1], 4); // position x
  1006. foreground->send((char*)&address[2], 4); // position y
  1007. foreground->send((char*)&address[3], 4); // position z
  1008. msgId = 2; // component request
  1009. foreground->send(&msgId, 1);
  1010. foreground->send((char*)&address[4], 4); // component index
  1011. }
  1012. else
  1013. {
  1014. char msgId = 3; // entity request
  1015. foreground->send(&msgId, 1);
  1016. foreground->send((char*)&address[0], 4); // entity id
  1017. msgId = 2; // component request
  1018. foreground->send(&msgId, 1);
  1019. foreground->send((char*)&address[1], 4); // component index
  1020. }
  1021. foreground->send(message, length);
  1022. cs.unlock();
  1023. }
  1024. void FactoryClient::blockAPIRequest(
  1025. int dimensionId, Vec3<int> pos, const char* message, unsigned short length)
  1026. {
  1027. if (!foreground) return;
  1028. cs.lock();
  1029. length += 18;
  1030. foreground->send((char*)&length, 2);
  1031. char msgId = 7;
  1032. foreground->send(&msgId, 1);
  1033. foreground->send((char*)&dimensionId, 4);
  1034. msgId = 1;
  1035. foreground->send(&msgId, 1);
  1036. foreground->send((char*)&pos.x, 4);
  1037. foreground->send((char*)&pos.y, 4);
  1038. foreground->send((char*)&pos.z, 4);
  1039. foreground->send(message, length - 18);
  1040. cs.unlock();
  1041. }
  1042. void FactoryClient::chunkAPIRequest(
  1043. Point center, const char* message, unsigned short length)
  1044. {
  1045. if (!foreground) return;
  1046. length += 10;
  1047. cs.lock();
  1048. foreground->send((char*)&length, 2);
  1049. char type = 1;
  1050. foreground->send(&type, 1);
  1051. type = 0;
  1052. foreground->send(&type, 1);
  1053. foreground->send((char*)&center.x, 4);
  1054. foreground->send((char*)&center.y, 4);
  1055. foreground->send(message, length - 10);
  1056. cs.unlock();
  1057. }
  1058. void FactoryClient::dimensionAPIRequest(
  1059. const char* message, unsigned short length)
  1060. {
  1061. if (!foreground) return;
  1062. length += 1;
  1063. cs.lock();
  1064. foreground->send((char*)&length, 2);
  1065. char type = 1;
  1066. foreground->send(&type, 1);
  1067. foreground->send(message, length - 1);
  1068. cs.unlock();
  1069. }
  1070. void FactoryClient::inventoryAPIRequest(
  1071. Framework::Either<int, Framework::VecN<int, 4>> target,
  1072. const char* message,
  1073. unsigned short length)
  1074. {
  1075. if (!foreground) return;
  1076. cs.lock();
  1077. length += target.isA() ? 6 : 18;
  1078. foreground->send((char*)&length, 2);
  1079. char msgId = 4;
  1080. foreground->send(&msgId, 1);
  1081. bool isEntity = target.isA();
  1082. foreground->send((char*)&isEntity, 1);
  1083. if (target.isA())
  1084. {
  1085. int id = target.getA();
  1086. foreground->send((char*)&id, 4);
  1087. }
  1088. else
  1089. {
  1090. for (int i = 0; i < 4; i++)
  1091. {
  1092. int v = target.getB()[i];
  1093. foreground->send((char*)&v, 4);
  1094. }
  1095. }
  1096. foreground->send(message, length - (target.isA() ? 6 : 18));
  1097. cs.unlock();
  1098. }
  1099. void FactoryClient::uiRequest(
  1100. Framework::Text dialogId, const char* message, unsigned short length)
  1101. {
  1102. if (!foreground) return;
  1103. short nameLen = (short)dialogId.getLength();
  1104. length += nameLen + 3;
  1105. foreground->send((char*)&length, 2);
  1106. cs.lock();
  1107. char msgId = 8;
  1108. foreground->send(&msgId, 1);
  1109. foreground->send((char*)&nameLen, 2);
  1110. foreground->send(dialogId, nameLen);
  1111. foreground->send(message, length - nameLen - 3);
  1112. cs.unlock();
  1113. }
  1114. void FactoryClient::craftingUIMLRequest(int itemTypeId)
  1115. {
  1116. if (!foreground) return;
  1117. cs.lock();
  1118. short length = 5;
  1119. foreground->send((char*)&length, 2);
  1120. char msgId = 5;
  1121. foreground->send(&msgId, 1);
  1122. foreground->send((char*)&itemTypeId, 4);
  1123. cs.unlock();
  1124. }
  1125. void FactoryClient::sendChatMessage(Framework::Text message)
  1126. {
  1127. if (!background) return;
  1128. cs.lock();
  1129. short length = message.getLength() + 4;
  1130. background->send((char*)&length, 2);
  1131. char msgId = 6;
  1132. background->send(&msgId, 1);
  1133. msgId = 0;
  1134. background->send(&msgId, 1);
  1135. length = message.getLength();
  1136. background->send((char*)&length, 2);
  1137. background->send(message.getText(), message.getLength());
  1138. cs.unlock();
  1139. }
  1140. void FactoryClient::chatAPIRequest(const char* data, unsigned short length)
  1141. {
  1142. if (!background) return;
  1143. cs.lock();
  1144. short totalLength = length + 1;
  1145. background->send((char*)&totalLength, 2);
  1146. char msgId = 6;
  1147. background->send(&msgId, 1);
  1148. background->send(data, length);
  1149. cs.unlock();
  1150. }
  1151. void FactoryClient::giveItemRequest(int itemTypeId, bool fullStack)
  1152. {
  1153. if (!foreground) return;
  1154. cs.lock();
  1155. short length = 7;
  1156. foreground->send((char*)&length, 2);
  1157. char msgId = 2; // player message
  1158. foreground->send(&msgId, 1);
  1159. msgId = 11;
  1160. foreground->send(&msgId, 1); // give items
  1161. foreground->send((char*)&itemTypeId, 4);
  1162. foreground->send((char*)&fullStack, 1);
  1163. cs.unlock();
  1164. }
  1165. bool FactoryClient::isConnected()
  1166. {
  1167. return foreground && background && foreground->isConnected()
  1168. && background->isConnected();
  1169. }
  1170. void FactoryClient::leaveGame()
  1171. {
  1172. cs.lock();
  1173. disconnect();
  1174. cs.unlock();
  1175. }