objectspawner.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. class ObjectSpawnerHandler
  2. {
  3. protected static const ref TStringArray VALID_PATHS = {
  4. "DZ\\plants","DZ\\plants_bliss", "DZ\\plants_sakhal",
  5. "DZ\\rocks", "DZ\\rocks_bliss", "DZ\\rocks_sakhal",
  6. "DZ/plants","DZ/plants_bliss", "DZ/plants_sakhal",
  7. "DZ/rocks", "DZ/rocks_bliss", "DZ/rocks_sakhal",
  8. };
  9. //---------------------------------------------------------------------------------------
  10. static void SpawnObjects()
  11. {
  12. if (CfgGameplayHandler.GetObjectSpawnersArr() && CfgGameplayHandler.GetObjectSpawnersArr().Count() > 0)
  13. {
  14. TStringArray arr = CfgGameplayHandler.GetObjectSpawnersArr();
  15. foreach (string spawnerFilePath: arr)
  16. {
  17. string path = "$mission:" + spawnerFilePath;
  18. string errorMessage;
  19. ObjectSpawnerJson spawner;
  20. if (JsonFileLoader<ObjectSpawnerJson>.LoadFile(path, spawner, errorMessage))
  21. {
  22. foreach (ITEM_SpawnerObject o : spawner.Objects)
  23. SpawnObject(o);
  24. }
  25. else
  26. ErrorEx(errorMessage);
  27. }
  28. }
  29. }
  30. //---------------------------------------------------------------------------------------
  31. static void SpawnObject(ITEM_SpawnerObject item)
  32. {
  33. Object object;
  34. float scale = item.scale;
  35. if (scale == 0)
  36. scale = 1;
  37. if (item.name.Contains("\\") || item.name.Contains("/"))
  38. {
  39. if (ValidatePath(item.name))
  40. object = GetGame().CreateStaticObjectUsingP3D(item.name, vector.ArrayToVec(item.pos), vector.ArrayToVec(item.ypr),scale);
  41. }
  42. else
  43. {
  44. int flags = ECE_SETUP | ECE_UPDATEPATHGRAPH | ECE_CREATEPHYSICS | ECE_NOLIFETIME | ECE_DYNAMIC_PERSISTENCY;
  45. if (item.enableCEPersistency)
  46. {
  47. flags &= ~ECE_DYNAMIC_PERSISTENCY;
  48. flags &= ~ECE_NOLIFETIME;
  49. }
  50. object = GetGame().CreateObjectEx(item.name, vector.ArrayToVec(item.pos), flags, RF_IGNORE);
  51. if (object)
  52. {
  53. object.SetOrientation( vector.ArrayToVec(item.ypr));
  54. if (item.scale != 1)
  55. object.SetScale(scale);
  56. object.OnSpawnByObjectSpawner(item);
  57. }
  58. }
  59. if (!object)
  60. PrintToRPT("Object spawner failed to spawn "+item.name);
  61. }
  62. //---------------------------------------------------------------------------------------
  63. static void OnGameplayDataHandlerLoad()
  64. {
  65. if (g_Game && g_Game.IsServer())
  66. {
  67. SpawnObjects();
  68. GetGame().GetWorld().ProcessMarkedObjectsForPathgraphUpdate();
  69. }
  70. }
  71. //---------------------------------------------------------------------------------------
  72. static bool ValidatePath(string path)
  73. {
  74. foreach (string p: VALID_PATHS)
  75. {
  76. if (path.Contains(p))
  77. return true;
  78. }
  79. PrintToRPT("Object spawner: invalid path "+ path);
  80. return false;
  81. }
  82. //---------------------------------------------------------------------------------------
  83. };
  84. class ObjectSpawnerJson
  85. {
  86. ref array<ref ITEM_SpawnerObject> Objects;
  87. };
  88. class ITEM_SpawnerObject
  89. {
  90. string name;
  91. float pos[3];
  92. float ypr[3];
  93. float scale;
  94. bool enableCEPersistency;
  95. string customString;
  96. };
  97. //! Utility class that converts init.c format type of spawn commands to a json file, fill in the SpawnInit() with your data and call SpawnDataConverter.SpawnObjects() through script editor console, result is a json file
  98. class SpawnDataConverter
  99. {
  100. static ref array<ref ITEM_SpawnerObject> Objects = new array<ref ITEM_SpawnerObject>;
  101. static string m_Path = "$mission:myspawndata.json";
  102. static void SpawnObjects()
  103. {
  104. Objects.Clear();
  105. SpawnInit();
  106. ObjectSpawnerJson j = new ObjectSpawnerJson();
  107. j.Objects = Objects;
  108. string errorMessage;
  109. if (!JsonFileLoader<ObjectSpawnerJson>.SaveFile(m_Path, j, errorMessage))
  110. ErrorEx(errorMessage);
  111. }
  112. static void SpawnInit()
  113. {
  114. AddSpawnData("Land_Wall_Gate_FenR", "8406.501953 107.736824 12782.338867", "0.000000 0.000000 0.000000");
  115. AddSpawnData("Land_Wall_Gate_FenR", "8410.501953 107.736824 12782.338867", "0.000000 0.000000 0.000000");
  116. AddSpawnData("Land_Wall_Gate_FenR", "8416.501953 107.736824 12782.338867", "0.000000 0.000000 0.000000");
  117. AddSpawnData("Land_Wall_Gate_FenR", "8422.501953 107.736824 12782.338867", "0.000000 0.000000 0.000000");
  118. }
  119. static void AddSpawnData(string objectName, vector position, vector orientation)
  120. {
  121. ITEM_SpawnerObject obj = new ITEM_SpawnerObject();
  122. obj.name = objectName;
  123. obj.pos[0] = position[0];
  124. obj.pos[1] = position[1];
  125. obj.pos[2] = position[2];
  126. obj.ypr[0] = orientation[0];
  127. obj.ypr[1] = orientation[1];
  128. obj.ypr[2] = orientation[2];
  129. Objects.Insert(obj);
  130. }
  131. }