sceneobject.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. class SceneObject
  2. {
  3. static const int COLOR_OBJ_BBOX_NORMAL = 0x00000000;
  4. static const int COLOR_OBJ_BBOX_SELECT = 0x1f007C00;
  5. protected EntityAI m_ObjectPtr;
  6. protected Shape m_DebugShapeBBox;
  7. protected string m_InitScript;
  8. protected string m_ObjectName;
  9. protected string m_ObjectNameOrigin;
  10. protected ref array<SceneObject> m_LinkedSceneObjects;
  11. protected ref map<SceneObject, Shape> m_LinkedSceneObjectsShapes;
  12. ref array<int> m_LinkedSceneObjectsIndices;
  13. //========================================
  14. // SceneObject
  15. //========================================
  16. SceneObject Init(string obj_name, vector pos)
  17. {
  18. if (obj_name != STRING_EMPTY)
  19. {
  20. m_ObjectNameOrigin = obj_name;
  21. bool is_ai = GetGame().IsKindOf(obj_name, "DZ_LightAI");
  22. PluginDeveloper module_dev = PluginDeveloper.Cast(GetPlugin(PluginDeveloper));
  23. EntityAI e = module_dev.SpawnEntityOnGroundPos(PluginSceneManager.PLAYER, obj_name, 100, 0.0, pos);
  24. if (e != NULL)
  25. {
  26. if (e.IsInherited(ItemBase))
  27. {
  28. ItemBase item = ItemBase.Cast(e);
  29. if (item.HasQuantity())
  30. item.SetQuantity(item.GetQuantityMax());
  31. }
  32. m_ObjectName = e.GetType();
  33. LinkEntityAI(e);
  34. }
  35. else if (obj_name != "player")
  36. {
  37. return NULL;
  38. }
  39. }
  40. m_LinkedSceneObjects = new array<SceneObject>;
  41. m_LinkedSceneObjectsShapes = new map<SceneObject, Shape>;
  42. m_LinkedSceneObjectsIndices = new array<int>;
  43. return this;
  44. }
  45. //----------------------------------------
  46. // GetObject
  47. //----------------------------------------
  48. EntityAI GetObject()
  49. {
  50. return m_ObjectPtr;
  51. }
  52. //----------------------------------------
  53. // GetName
  54. //----------------------------------------
  55. string GetName()
  56. {
  57. return m_ObjectName;
  58. }
  59. //----------------------------------------
  60. // IsPlayer
  61. //----------------------------------------
  62. bool IsPlayer()
  63. {
  64. return false;
  65. }
  66. //----------------------------------------
  67. // GetInitScript
  68. //----------------------------------------
  69. string GetInitScript()
  70. {
  71. return m_InitScript;
  72. }
  73. //----------------------------------------
  74. // SetInitScript
  75. //----------------------------------------
  76. void SetInitScript(string init_script)
  77. {
  78. m_InitScript = init_script;
  79. }
  80. //========================================
  81. // EditorShapeUpdatePos
  82. //========================================
  83. void EditorShapeUpdatePos()
  84. {
  85. if (m_DebugShapeBBox != NULL)
  86. {
  87. vector mat[4];
  88. GetObject().GetTransform(mat);
  89. if (m_DebugShapeBBox != NULL)
  90. {
  91. m_DebugShapeBBox.SetMatrix(mat);
  92. }
  93. }
  94. }
  95. //========================================
  96. // EditorShapeSetColor
  97. //========================================
  98. void EditorShapeSetColor(int color)
  99. {
  100. if (m_DebugShapeBBox)
  101. {
  102. m_DebugShapeBBox.SetColor(color);
  103. }
  104. }
  105. //========================================
  106. // EditorShapeSelect
  107. //========================================
  108. void EditorShapeSelect()
  109. {
  110. EditorShapeSetColor(COLOR_OBJ_BBOX_SELECT);
  111. }
  112. //========================================
  113. // EditorShapeDeselect
  114. //========================================
  115. void EditorShapeDeselect()
  116. {
  117. EditorShapeSetColor(COLOR_OBJ_BBOX_NORMAL);
  118. }
  119. //========================================
  120. // GetSize
  121. //========================================
  122. vector GetSize()
  123. {
  124. vector size = Vector(1,1,1);
  125. vector min_max[2];
  126. if (GetObject())
  127. {
  128. GetObject().GetCollisionBox(min_max);
  129. size[0] = min_max[1][0] - min_max[0][0];
  130. size[2] = min_max[1][2] - min_max[0][2];
  131. size[1] = min_max[1][1] - min_max[0][1];
  132. return size;
  133. }
  134. else
  135. {
  136. Print("Error: SceneObject "+ m_ObjectNameOrigin +" dont has game object.");
  137. }
  138. return size;
  139. }
  140. //========================================
  141. // EditorShapeAdd
  142. //========================================
  143. void EditorShapeAdd()
  144. {
  145. if (m_DebugShapeBBox != NULL)
  146. return;
  147. vector min = "0 0 0";
  148. vector max = "0 0 0";
  149. vector size = GetSize();
  150. float width = size[0];
  151. float height = size[1];
  152. float length = size[2];
  153. float width_h = width*0.5;
  154. float lenght_h = length*0.5;
  155. min[0] = -width_h;
  156. min[1] = 0;
  157. min[2] = -lenght_h;
  158. max[0] = width_h;
  159. max[1] = height;
  160. max[2] = lenght_h;
  161. //Log("EditorShapeAdd -> "+m_ObjectPtr.Ptr().GetType());
  162. m_DebugShapeBBox = Debug.DrawBox(min, max);
  163. EditorShapeUpdatePos();
  164. EditorShapeDeselect();
  165. }
  166. //========================================
  167. // EditorShapeRemove
  168. //========================================
  169. void EditorShapeRemove()
  170. {
  171. if (m_DebugShapeBBox != NULL)
  172. {
  173. m_DebugShapeBBox.Destroy();
  174. m_DebugShapeBBox = NULL;
  175. }
  176. }
  177. //========================================
  178. // EditorLineRemove
  179. //========================================
  180. void EditorLineRemove(SceneObject obj)
  181. {
  182. for (int i = 0; i < m_LinkedSceneObjectsShapes.Count(); i++)
  183. {
  184. if (m_LinkedSceneObjectsShapes.GetKey(i) == obj)
  185. {
  186. m_LinkedSceneObjectsShapes.GetElement(i).Destroy();
  187. m_LinkedSceneObjectsShapes.Remove(obj);
  188. break;
  189. }
  190. }
  191. }
  192. //========================================
  193. // EditorLineAdd
  194. //========================================
  195. void EditorLineAdd(SceneObject obj)
  196. {
  197. if (obj.GetObject() != NULL && GetObject() != NULL)
  198. {
  199. if (m_LinkedSceneObjectsShapes.Contains(obj))
  200. {
  201. EditorLineRemove(obj);
  202. }
  203. vector pos1 = obj.GetSize();
  204. pos1[0] = 0; pos1[1] = pos1[1] / 2; pos1[2] = 0;
  205. pos1 = pos1 + obj.GetObject().GetPosition();
  206. vector pos2 = GetSize();
  207. pos2[0] = 0; pos2[1] = pos2[1] / 2; pos2[2] = 0;
  208. pos2 = pos2 + GetObject().GetPosition();
  209. m_LinkedSceneObjectsShapes.Insert(obj, Debug.DrawArrow(pos1, pos2));
  210. }
  211. }
  212. //========================================
  213. // LinkEntityAI
  214. //========================================
  215. void LinkEntityAI(EntityAI e)
  216. {
  217. m_ObjectPtr = e;
  218. }
  219. //========================================
  220. // IsLinkedWithSceneObject
  221. //========================================
  222. bool IsLinkedWithSceneObject(SceneObject scene_object)
  223. {
  224. int index = m_LinkedSceneObjects.Find(scene_object);
  225. if (index >= 0)
  226. {
  227. return true;
  228. }
  229. else
  230. {
  231. return false;
  232. }
  233. }
  234. //========================================
  235. // LinkEntityAI
  236. //========================================
  237. void LinkSceneObject(SceneObject scene_object, bool draw_line = true)
  238. {
  239. if (!IsLinkedWithSceneObject(scene_object))
  240. {
  241. if (draw_line)
  242. {
  243. EditorLineAdd(scene_object);
  244. }
  245. m_LinkedSceneObjects.Insert(scene_object);
  246. }
  247. }
  248. //========================================
  249. // UnlinkSceneObject
  250. //========================================
  251. void UnlinkSceneObject(SceneObject scene_object)
  252. {
  253. int index = m_LinkedSceneObjects.Find(scene_object);
  254. if (index >= 0 && index < m_LinkedSceneObjects.Count())
  255. {
  256. EditorLineRemove(scene_object);
  257. m_LinkedSceneObjects.Remove(index);
  258. }
  259. }
  260. //========================================
  261. // UnlinkAll
  262. //========================================
  263. void UnlinkAll()
  264. {
  265. int link_count = GetLinkedSceneObjectsCount();
  266. if (link_count > 0)
  267. {
  268. for (int i = 0; i < link_count; ++i)
  269. {
  270. PluginSceneManager.GetInstance().UnlinkSceneObjects(this, GetLinkedSceneObject(0));
  271. }
  272. }
  273. }
  274. //========================================
  275. // GetLinkedSceneObjects
  276. //========================================
  277. array<SceneObject> GetLinkedSceneObjects()
  278. {
  279. return m_LinkedSceneObjects;
  280. }
  281. //========================================
  282. // GetLinkedSceneObjectsCount
  283. //========================================
  284. int GetLinkedSceneObjectsCount()
  285. {
  286. return m_LinkedSceneObjects.Count();
  287. }
  288. //========================================
  289. // GetLinkedSceneObject
  290. //========================================
  291. SceneObject GetLinkedSceneObject(int i)
  292. {
  293. return m_LinkedSceneObjects.Get(i);
  294. }
  295. //========================================
  296. // GetLinkedObject
  297. //========================================
  298. EntityAI GetLinkedObject(int i)
  299. {
  300. return GetLinkedSceneObject(i).GetObject();
  301. }
  302. //========================================
  303. // Destructor
  304. //========================================
  305. void ~SceneObject()
  306. {
  307. if (m_ObjectPtr && m_ObjectPtr != GetGame().GetPlayer())
  308. {
  309. GetGame().ObjectDelete(m_ObjectPtr);
  310. m_ObjectPtr = NULL;
  311. }
  312. for (int i = 0; i < m_LinkedSceneObjects.Count(); i++)
  313. {
  314. EditorLineRemove(m_LinkedSceneObjects.Get(i));
  315. }
  316. EditorShapeRemove();
  317. }
  318. //========================================
  319. // GetTypeName
  320. //========================================
  321. string GetTypeName()
  322. {
  323. return m_ObjectPtr.GetType();
  324. }
  325. //========================================
  326. // PlaceOnSurface
  327. //========================================
  328. void PlaceOnSurface()
  329. {
  330. if (m_ObjectPtr)
  331. {
  332. if (GetGame().IsClient() && GetGame().IsMultiplayer())
  333. {
  334. Param par = new Param3<string, EntityAI, Param>("PlaceOnSurface" , m_ObjectPtr, NULL);
  335. SceneObjectSynch(par);
  336. }
  337. else
  338. {
  339. m_ObjectPtr.PlaceOnSurface();
  340. }
  341. }
  342. }
  343. //========================================
  344. // SetPosition
  345. //========================================
  346. void SetPosition(vector pos)
  347. {
  348. if (m_ObjectPtr)
  349. {
  350. if (GetGame().IsClient() && GetGame().IsMultiplayer())
  351. {
  352. Param par = new Param3<string, EntityAI, Param>("SetPosition" , m_ObjectPtr, new Param1<vector>(pos));
  353. SceneObjectSynch(par);
  354. }
  355. else
  356. {
  357. m_ObjectPtr.SetPosition(pos);
  358. }
  359. }
  360. PlaceOnSurface();
  361. EditorShapeUpdatePos();
  362. }
  363. //========================================
  364. // GetPosition
  365. //========================================
  366. vector GetPosition()
  367. {
  368. if (m_ObjectPtr)
  369. return m_ObjectPtr.GetPosition();
  370. return Vector(0,0,0);
  371. }
  372. //========================================
  373. // GetHealth
  374. //========================================
  375. float GetHealth()
  376. {
  377. if (m_ObjectPtr)
  378. return m_ObjectPtr.GetHealth("", "");
  379. return 0;
  380. }
  381. //========================================
  382. // GetHealth
  383. //========================================
  384. float GetMaxHealth()
  385. {
  386. if (m_ObjectPtr)
  387. return m_ObjectPtr.GetMaxHealth("", "");
  388. return 0;
  389. }
  390. //========================================
  391. // SetHealth
  392. //========================================
  393. void SetHealth(float value)
  394. {
  395. if (m_ObjectPtr)
  396. {
  397. if (GetGame().IsClient() && GetGame().IsMultiplayer())
  398. {
  399. Param par = new Param3<string, EntityAI, Param>("SetHealth" , m_ObjectPtr, new Param1<float>(value));
  400. SceneObjectSynch(par);
  401. }
  402. else
  403. {
  404. m_ObjectPtr.SetHealth("", "", value);
  405. }
  406. }
  407. }
  408. //========================================
  409. // GetPositionAsString
  410. //========================================
  411. string GetPositionAsString()
  412. {
  413. if (m_ObjectPtr)
  414. return m_ObjectPtr.GetPosition().ToString(false);
  415. return Vector(0,0,0).ToString(false);
  416. }
  417. //========================================
  418. // SetPositionAsString
  419. //========================================
  420. void SetPositionAsString(string string_pos)
  421. {
  422. SetPosition(string_pos.ToVector());
  423. }
  424. //========================================
  425. // GetRotation
  426. //========================================
  427. float GetRotation()
  428. {
  429. if (m_ObjectPtr)
  430. {
  431. vector v = m_ObjectPtr.GetOrientation();
  432. return v[0];
  433. }
  434. return 0;
  435. }
  436. //========================================
  437. // SetRotation
  438. //========================================
  439. void SetRotation(float rot)
  440. {
  441. if (m_ObjectPtr)
  442. {
  443. vector v = m_ObjectPtr.GetOrientation();
  444. v[0] = rot;
  445. m_ObjectPtr.SetOrientation(v);
  446. EditorShapeUpdatePos();
  447. }
  448. }
  449. void SceneObjectSynch(Param p)
  450. {
  451. GetGame().RPCSingleParam(GetGame().GetPlayer(), ERPCs.RPC_SYNC_SCENE_OBJECT, p, true, NULL);
  452. }
  453. //========================================
  454. // AddRotation
  455. //========================================
  456. void AddRotation(float add_rot)
  457. {
  458. if (m_ObjectPtr)
  459. {
  460. if (GetGame().IsClient() && GetGame().IsMultiplayer())
  461. {
  462. Param par = new Param3<string, EntityAI, Param>("AddRotation" , m_ObjectPtr, new Param1<float>(add_rot));
  463. SceneObjectSynch(par);
  464. }
  465. else
  466. {
  467. vector v = m_ObjectPtr.GetOrientation();
  468. v[0] = v[0] + add_rot;
  469. m_ObjectPtr.SetOrientation(v);
  470. EditorShapeUpdatePos();
  471. }
  472. }
  473. }
  474. //========================================
  475. // AddRotation
  476. //========================================
  477. void AddAttachment(string att_name)
  478. {
  479. GetObject().GetInventory().CreateAttachment(att_name);
  480. }
  481. //========================================
  482. // CanAttachment
  483. //========================================
  484. bool CanAttachment(EntityAI e)
  485. {
  486. return GetObject().GetInventory().CanAddAttachment(e);
  487. }
  488. //========================================
  489. // AddRotation
  490. //========================================
  491. void RemoveAttachment(EntityAI e)
  492. {
  493. GetGame().ObjectDelete(e);
  494. }
  495. //========================================
  496. // GetAttachments
  497. //========================================
  498. array<EntityAI> GetAttachments()
  499. {
  500. array<EntityAI> ret = new array<EntityAI>;
  501. for (int i = 0; i < GetObject().GetInventory().AttachmentCount(); ++i)
  502. {
  503. ret.Insert(GetObject().GetInventory().GetAttachmentFromIndex(i));
  504. }
  505. return ret;
  506. }
  507. //========================================
  508. // GetConfigAttachments
  509. //========================================
  510. TStringArray GetConfigAttachments()
  511. {
  512. string type_name = GetTypeName();
  513. TStringArray cfg_attachments = new TStringArray;
  514. string cfg_path;
  515. if (GetGame().ConfigIsExisting(CFG_VEHICLESPATH+" "+type_name))
  516. {
  517. cfg_path = CFG_VEHICLESPATH+" "+type_name+" attachments";
  518. }
  519. else if (GetGame().ConfigIsExisting(CFG_WEAPONSPATH+" "+type_name))
  520. {
  521. cfg_path = CFG_WEAPONSPATH+" "+type_name+" attachments";
  522. }
  523. else if (GetGame().ConfigIsExisting(CFG_MAGAZINESPATH+" "+type_name))
  524. {
  525. cfg_path = CFG_MAGAZINESPATH+" "+type_name+" attachments";
  526. }
  527. GetGame().ConfigGetTextArray(cfg_path, cfg_attachments);
  528. return cfg_attachments;
  529. }
  530. }