FactoryClient.cpp 40 KB

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