Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_WorldSetupPlugin_Entities.c
Go to the documentation of this file.
1#ifdef WORKBENCH
3 name: "Entities Setup",
4 category: "World Setup",
5 description: "Set up world's Entities",
6 shortcut: "", // "Ctrl+T", same as Script Editor Template
7 wbModules: { "WorldEditor" },
8 awesomeFontCode: 0xF0AC)] // F7A2 is already used
9class SCR_WorldSetupPlugin_Entities : SCR_WorldSetupPluginBasePlugin
10{
11 [Attribute(defvalue: DEFAULT_CONFIG, params: "conf class=SCR_WorldSetupPluginConfig")]
12 protected ResourceName m_sConfig;
13
14 [Attribute(defvalue: "0", desc: "Create Prefab's child from the provided one in the selected addon and use it instead of using the provided Prefab directly")]
15 protected bool m_bCopyPrefabsToAddon;
16
17 [Attribute(uiwidget: UIWidgets.ComboBox, desc: "Addon in which the Prefabs will be copied (if copied)", enums: SCR_ParamEnumArray.FromAddons(titleFormat: 2, hideCoreModules: 2))]
18 protected int m_iAddon;
19
20 // temp work variables
21 protected ref SCR_WorldSetupPluginConfig m_Config;
22 protected ref set<string> m_ClassNames = new set<string>();
23 protected ref set<string> m_Prefabs = new set<string>();
24
25 protected static const ResourceName DEFAULT_CONFIG = "{1DD914C62E44CDEB}Configs/Workbench/WorldEditor/WorldSetupPlugin/0_MinimalWorldSetup.conf";
26
27 //------------------------------------------------------------------------------------------------
28 protected override void Run()
29 {
30 if (!Init())
31 return;
32
33 if (!Workbench.ScriptDialog(
34 "World Setup (required entities creation)",
35 "This plugin helps generating default entities to setup a freshly created world properly.\n" +
36 "Pick a config of your choice (keep the default one if it is good enough).\n\n" +
37 "Entities will be created in the currently selected layer.",
38 this))
39 return;
40
41 if (!LoadConfig())
42 return;
43
44 CreateEntities();
45 Cleanup();
46 }
47
48 //------------------------------------------------------------------------------------------------
49 protected bool Init()
50 {
51 // has API?
52 WorldEditorAPI worldEditorAPI = SCR_WorldEditorToolHelper.GetWorldEditorAPI();
53 if (!worldEditorAPI)
54 {
55 Print("Could not obtain worldEditorAPI", LogLevel.ERROR);
56 return false;
57 }
58
59 // has world?
60 BaseWorld baseWorld = worldEditorAPI.GetWorld();
61 if (!baseWorld)
62 {
63 Print("No world is currently loaded", LogLevel.WARNING);
64 return false;
65 }
66
67 string worldPath;
68 worldEditorAPI.GetWorldPath(worldPath);
69 if (worldPath.IsEmpty())
70 {
71 Print("No world is currently loaded or the world has not yet been saved to storage", LogLevel.WARNING);
72 return false;
73 }
74
75 return true;
76 }
77
78 //------------------------------------------------------------------------------------------------
79 protected bool LoadConfig()
80 {
81 if (m_sConfig.IsEmpty())
82 m_sConfig = DEFAULT_CONFIG;
83
84 Resource resource = Resource.Load(m_sConfig);
85 if (!resource.IsValid())
86 {
87 Print("Could not load config", LogLevel.ERROR);
88 return false;
89 }
90
91 m_Config = new SCR_WorldSetupPluginConfig();
92 BaseContainerTools.WriteToInstance(m_Config, resource.GetResource().ToBaseContainer());
93
94 return true;
95 }
96
97 //------------------------------------------------------------------------------------------------
98 protected void CreateEntities()
99 {
100 WorldEditorAPI worldEditorAPI = SCR_WorldEditorToolHelper.GetWorldEditorAPI();
101 // array<IEntitySource> entitySources = {};
102 // array<IEntity> entities = {};
103 IEntitySource worldEntitySource;
104 IEntitySource entitySource;
105 BaseContainer ancestor;
106 for (int i, count = worldEditorAPI.GetEditorEntityCount(); i < count; i++)
107 {
108 entitySource = worldEditorAPI.GetEditorEntity(i);
109 if (!worldEntitySource && entitySource.GetClassName().ToType() == GenericWorldEntity)
110 worldEntitySource = entitySource;
111
112 // entitySources.Insert(entitySource);
113 // entities.Insert(worldEditorAPI.SourceToEntity(entitySource));
114 m_ClassNames.Insert(entitySource.GetClassName());
115
116 ancestor = entitySource.GetAncestor();
117 if (ancestor)
118 m_Prefabs.Insert(ancestor.GetResourceName());
119 }
120
121 // create Entities
122 bool manageEditAction = SCR_WorldEditorToolHelper.BeginEntityAction();
123 foreach (SCR_WorldSetupPluginConfig_Entity entry : m_Config.m_aEntities)
124 {
125 CreateEntityFromEntry(entry);
126 }
127 SCR_WorldEditorToolHelper.EndEntityAction(manageEditAction);
128
129 Print("Config applied successfully", LogLevel.NORMAL);
130 }
131
132 //------------------------------------------------------------------------------------------------
133 protected IEntitySource CreateEntityFromEntry(notnull SCR_WorldSetupPluginConfig_Entity entry)
134 {
135 if (entry.m_sPrefab.IsEmpty())
136 {
137 Print("An entry does not have class name or Prefab", LogLevel.WARNING);
138 return null;
139 }
140
141 if (entry.m_bMustBeUniqueByPrefab && m_Prefabs.Contains(entry.m_sPrefab)) // TODO: inheritance?
142 {
143 Print("Entry skipped as another Entity originates from the same Prefab " + entry.m_sPrefab, LogLevel.WARNING);
144 return null;
145 }
146
147 Resource resource = Resource.Load(entry.m_sPrefab);
148 if (!resource.IsValid())
149 {
150 Print("Entry has an invalid Prefab " + entry.m_sPrefab, LogLevel.WARNING);
151 return null;
152 }
153
154 string className = resource.GetResource().ToBaseContainer().GetClassName();
155
156 if (entry.m_bMustBeUniqueByClassName && m_ClassNames.Contains(className))
157 {
158 Print("Entry skipped as a " + className + " entity is already present", LogLevel.WARNING);
159 return null;
160 }
161
162 ResourceName prefab = entry.m_sPrefab;
163
164 // duplicate the Prefab
165 if (m_bCopyPrefabsToAddon)
166 prefab = CreatePrefabChildInAddon(prefab, m_iAddon);
167
168 if (prefab.IsEmpty())
169 {
170 Print("Child Prefab could not be created ", LogLevel.WARNING);
171 return null;
172 }
173
174 WorldEditorAPI worldEditorAPI = SCR_WorldEditorToolHelper.GetWorldEditorAPI();
175 IEntitySource entitySource = worldEditorAPI.CreateEntity(prefab, string.Empty, worldEditorAPI.GetCurrentEntityLayerId(), null, entry.m_vPosition, entry.m_vAngles);
176 if (!entitySource)
177 {
178 Print("Entity failed to be created by World Editor API " + prefab, LogLevel.WARNING);
179 return null;
180 }
181
182 if (entry.m_aAdditionalValues && !entry.m_aAdditionalValues.IsEmpty())
183 {
184 foreach (SCR_WorldSetupPluginConfig_EntitySourceKeyValue kvp : entry.m_aAdditionalValues)
185 {
186 if (!worldEditorAPI.SetVariableValue(entitySource, null, kvp.m_sKey, kvp.m_sValue))
187 PrintFormat("Cannot set variable value \"%1\" (value: %2)", kvp.m_sKey, kvp.m_sValue, level: LogLevel.WARNING);
188 }
189 }
190
191 m_ClassNames.Insert(className);
192 m_Prefabs.Insert(prefab);
193
194 return entitySource;
195 }
196
197 //------------------------------------------------------------------------------------------------
198 protected void Cleanup()
199 {
200 m_Config = null;
201 m_ClassNames.Clear();
202 m_Prefabs.Clear();
203 }
204
205 //------------------------------------------------------------------------------------------------
206 [ButtonAttribute("Run", true)]
207 protected bool ButtonRun()
208 {
209 return true;
210 }
211
212 //------------------------------------------------------------------------------------------------
213 [ButtonAttribute("Cancel")]
214 protected bool ButtonCancel()
215 {
216 return false;
217 }
218}
219#endif // WORKBENCH
override void Init()
GenerateFlowMaps WorkbenchPlugin WorkbenchPluginAttribute("Regenerate river flow-maps", "Generate and save/overwrite river flow-maps", "", "", {"WorldEditor"}, "", 0xf773)
Definition FlowmapTool.c:59
override void Run()
bool ButtonCancel()
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
void Cleanup()
Cleanup component.
class WorkbenchDialog_AbortRetryIgnore ButtonAttribute("OK", true)
Object holding reference to resource. In destructor release the resource.
Definition Resource.c:25
static ParamEnumArray FromAddons(int titleFormat=2, int hideCoreModules=0)
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
proto void PrintFormat(string fmt, void param1=NULL, void param2=NULL, void param3=NULL, void param4=NULL, void param5=NULL, void param6=NULL, void param7=NULL, void param8=NULL, void param9=NULL, LogLevel level=LogLevel.NORMAL)
SCR_FieldOfViewSettings Attribute