FactoryClient.cpp 40 KB

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