objectspawner.c 4.0 KB

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