|
| 1 | +using Rampastring.Tools; |
| 2 | +using Rampastring.XNAUI; |
| 3 | +using Rampastring.XNAUI.XNAControls; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using TSMapEditor.Models; |
| 7 | +using TSMapEditor.UI.Controls; |
| 8 | + |
| 9 | +namespace TSMapEditor.UI.Windows |
| 10 | +{ |
| 11 | + public class RandomTriggerSetTriggersCreatedEventArgs : EventArgs |
| 12 | + { |
| 13 | + public RandomTriggerSetTriggersCreatedEventArgs(Trigger baseTrigger) |
| 14 | + { |
| 15 | + BaseTrigger = baseTrigger; |
| 16 | + } |
| 17 | + |
| 18 | + public Trigger BaseTrigger { get; } |
| 19 | + } |
| 20 | + |
| 21 | + public class CreateRandomTriggerSetWindow : INItializableWindow |
| 22 | + { |
| 23 | + public CreateRandomTriggerSetWindow(WindowManager windowManager, Map map) : base(windowManager) |
| 24 | + { |
| 25 | + this.map = map; |
| 26 | + } |
| 27 | + |
| 28 | + private readonly Map map; |
| 29 | + |
| 30 | + private EditorTextBox tbName; |
| 31 | + private XNADropDown ddColor; |
| 32 | + private EditorNumberTextBox tbNumTriggers; |
| 33 | + private EditorNumberTextBox tbElapsedTime; |
| 34 | + private EditorNumberTextBox tbDelay; |
| 35 | + private XNACheckBox cbEveryDiff; |
| 36 | + private EditorButton btnApply; |
| 37 | + |
| 38 | + public event EventHandler<RandomTriggerSetTriggersCreatedEventArgs> RandomTriggerSetTriggersCreated; |
| 39 | + |
| 40 | + public override void Initialize() |
| 41 | + { |
| 42 | + Name = nameof(CreateRandomTriggerSetWindow); |
| 43 | + base.Initialize(); |
| 44 | + |
| 45 | + tbName = FindChild<EditorTextBox>(nameof(tbName)); |
| 46 | + ddColor = FindChild<XNADropDown>(nameof(ddColor)); |
| 47 | + tbNumTriggers = FindChild<EditorNumberTextBox>(nameof(tbNumTriggers)); |
| 48 | + tbElapsedTime = FindChild<EditorNumberTextBox>(nameof(tbElapsedTime)); |
| 49 | + tbDelay = FindChild<EditorNumberTextBox>(nameof(tbDelay)); |
| 50 | + cbEveryDiff = FindChild<XNACheckBox>(nameof(cbEveryDiff)); |
| 51 | + btnApply = FindChild<EditorButton>(nameof(btnApply)); |
| 52 | + |
| 53 | + ddColor.AddItem("None"); |
| 54 | + Array.ForEach(Trigger.SupportedColors, sc => |
| 55 | + { |
| 56 | + ddColor.AddItem(sc.Name, sc.Value); |
| 57 | + }); |
| 58 | + |
| 59 | + btnApply.LeftClick += BtnApply_LeftClick; |
| 60 | + } |
| 61 | + |
| 62 | + public void BtnApply_LeftClick(object sender, EventArgs e) |
| 63 | + { |
| 64 | + if (!Validate()) |
| 65 | + { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + string name = tbName.Text; |
| 70 | + string color = ddColor.SelectedItem.Text == "None" ? string.Empty : ddColor.SelectedItem.Text; |
| 71 | + int elapsedTime = tbElapsedTime.Value; |
| 72 | + int count = tbNumTriggers.Value; |
| 73 | + int delay = tbDelay.Value; |
| 74 | + bool createForEveryDifficulty = cbEveryDiff.Checked; |
| 75 | + |
| 76 | + List<Trigger> baseTriggers = []; |
| 77 | + |
| 78 | + if (createForEveryDifficulty) |
| 79 | + { |
| 80 | + baseTriggers.Add(CreateRandomTriggersSet(name, elapsedTime, count, delay, color, Difficulty.Hard)); |
| 81 | + baseTriggers.Add(CreateRandomTriggersSet(name, elapsedTime, count, delay, color, Difficulty.Medium)); |
| 82 | + baseTriggers.Add(CreateRandomTriggersSet(name, elapsedTime, count, delay, color, Difficulty.Easy)); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + baseTriggers.Add(CreateRandomTriggersSet(name, elapsedTime, count, delay, color)); |
| 87 | + } |
| 88 | + |
| 89 | + Hide(); |
| 90 | + RandomTriggerSetTriggersCreated?.Invoke(this, new RandomTriggerSetTriggersCreatedEventArgs(baseTriggers[0])); |
| 91 | + } |
| 92 | + |
| 93 | + private Trigger CreateRandomTriggersSet(string name, int elapsedTime, int count, int delay, string color, Difficulty? difficulty = null) |
| 94 | + { |
| 95 | + if (difficulty != null) |
| 96 | + { |
| 97 | + name = $"{difficulty.ToString()[0]} {name}"; |
| 98 | + } |
| 99 | + |
| 100 | + var baseTrigger = CreateBaseTrigger(name, elapsedTime, color, difficulty); |
| 101 | + var childTriggers = CreateChildTriggers(name, count, delay, color); |
| 102 | + AssociateTriggers(baseTrigger, childTriggers); |
| 103 | + |
| 104 | + return baseTrigger; |
| 105 | + } |
| 106 | + |
| 107 | + private bool Validate() |
| 108 | + { |
| 109 | + if (string.IsNullOrWhiteSpace(tbName.Text)) |
| 110 | + { |
| 111 | + EditorMessageBox.Show(WindowManager, "Missing Trigger Name", |
| 112 | + "Please enter a name for the triggers", MessageBoxButtons.OK); |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + if (tbNumTriggers.Value < 2) |
| 117 | + { |
| 118 | + EditorMessageBox.Show(WindowManager, "Invalid Number of Triggers", |
| 119 | + "Please enter a value of 2 or more", MessageBoxButtons.OK); |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + if (tbElapsedTime.Value < 0) |
| 124 | + { |
| 125 | + EditorMessageBox.Show(WindowManager, "Invalid Elapsed Time", |
| 126 | + "Please enter a value of 0 or more", MessageBoxButtons.OK); |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + if (tbDelay.Value < 10) |
| 131 | + { |
| 132 | + EditorMessageBox.Show(WindowManager, "Invalid Random Delay", |
| 133 | + "Please enter a value of 10 or more", MessageBoxButtons.OK); |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + private Trigger CreateBaseTrigger(string name, int elapsedTime, string color, Difficulty? difficulty) |
| 141 | + { |
| 142 | + string triggerName = $"{name} base"; |
| 143 | + |
| 144 | + var baseTrigger = new Trigger(map.GetNewUniqueInternalId()); |
| 145 | + baseTrigger.Name = triggerName; |
| 146 | + baseTrigger.HouseType = "Neutral"; |
| 147 | + |
| 148 | + if (!string.IsNullOrWhiteSpace(color)) |
| 149 | + { |
| 150 | + baseTrigger.EditorColor = color; |
| 151 | + } |
| 152 | + |
| 153 | + if (difficulty != null) |
| 154 | + { |
| 155 | + baseTrigger.Hard = difficulty == Difficulty.Hard; |
| 156 | + baseTrigger.Normal = difficulty == Difficulty.Medium; |
| 157 | + baseTrigger.Easy = difficulty == Difficulty.Easy; |
| 158 | + |
| 159 | + int diffGlobalVariableIndex = map.Rules.GlobalVariables.FindIndex(gv => gv.Name == $"Difficulty {difficulty}"); |
| 160 | + |
| 161 | + if (diffGlobalVariableIndex < 0) |
| 162 | + { |
| 163 | + Logger.Log($"{nameof(CreateRandomTriggerSetWindow)}.{nameof(CreateBaseTrigger)}: {difficulty} difficulty global variable not found!"); |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + var globalSetCondition = new TriggerCondition(); |
| 168 | + globalSetCondition.ConditionIndex = 27; // Global Is Set |
| 169 | + globalSetCondition.Parameters[1] = diffGlobalVariableIndex.ToString(); |
| 170 | + |
| 171 | + baseTrigger.Conditions.Add(globalSetCondition); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + var elapsedTimeCondition = new TriggerCondition(); |
| 176 | + elapsedTimeCondition.ConditionIndex = 13; // Elapsed Time |
| 177 | + elapsedTimeCondition.Parameters[1] = elapsedTime.ToString(); |
| 178 | + |
| 179 | + baseTrigger.Conditions.Add(elapsedTimeCondition); |
| 180 | + |
| 181 | + map.Triggers.Add(baseTrigger); |
| 182 | + map.Tags.Add(new Tag() { ID = map.GetNewUniqueInternalId(), Name = baseTrigger.Name + " (tag)", Trigger = baseTrigger, Repeating = 2 }); |
| 183 | + |
| 184 | + return baseTrigger; |
| 185 | + } |
| 186 | + |
| 187 | + private List<Trigger> CreateChildTriggers(string name, int count, int delay, string color) |
| 188 | + { |
| 189 | + List<Trigger> triggers = []; |
| 190 | + |
| 191 | + for (int i = 0; i < count; i++) |
| 192 | + { |
| 193 | + var childTrigger = new Trigger(map.GetNewUniqueInternalId()); |
| 194 | + childTrigger.Name = $"{name} {i + 1}"; |
| 195 | + childTrigger.HouseType = "Neutral"; |
| 196 | + childTrigger.Disabled = true; |
| 197 | + |
| 198 | + if (!string.IsNullOrWhiteSpace(color)) |
| 199 | + { |
| 200 | + childTrigger.EditorColor = color; |
| 201 | + } |
| 202 | + |
| 203 | + var randomDelayCondition = new TriggerCondition(); |
| 204 | + randomDelayCondition.ConditionIndex = 51; // Random Delay |
| 205 | + randomDelayCondition.Parameters[1] = delay.ToString(); |
| 206 | + |
| 207 | + childTrigger.Conditions.Add(randomDelayCondition); |
| 208 | + |
| 209 | + map.Triggers.Add(childTrigger); |
| 210 | + map.Tags.Add(new Tag() { ID = map.GetNewUniqueInternalId(), Name = childTrigger.Name + " (tag)", Trigger = childTrigger, Repeating = 2 }); |
| 211 | + |
| 212 | + triggers.Add(childTrigger); |
| 213 | + } |
| 214 | + |
| 215 | + return triggers; |
| 216 | + } |
| 217 | + |
| 218 | + private void AssociateTriggers(Trigger baseTrigger, List<Trigger> childTriggers) |
| 219 | + { |
| 220 | + foreach (var childTrigger in childTriggers) |
| 221 | + { |
| 222 | + // base trigger needs to enable each of the child triggers |
| 223 | + var enableTriggerAction = new TriggerAction(); |
| 224 | + enableTriggerAction.ActionIndex = 53; // Enable Trigger |
| 225 | + enableTriggerAction.Parameters[0] = "2"; |
| 226 | + enableTriggerAction.Parameters[1] = childTrigger.ID; |
| 227 | + |
| 228 | + baseTrigger.Actions.Add(enableTriggerAction); |
| 229 | + |
| 230 | + // each child trigger needs to disable itself and each other child trigger |
| 231 | + foreach (var siblingTrigger in childTriggers) |
| 232 | + { |
| 233 | + var disableTriggerAction = new TriggerAction(); |
| 234 | + disableTriggerAction.ActionIndex = 54; // Disable Trigger |
| 235 | + disableTriggerAction.Parameters[0] = "2"; |
| 236 | + disableTriggerAction.Parameters[1] = siblingTrigger.ID; |
| 237 | + |
| 238 | + childTrigger.Actions.Add(disableTriggerAction); |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + public void Open() |
| 244 | + { |
| 245 | + Show(); |
| 246 | + ResetValues(); |
| 247 | + } |
| 248 | + |
| 249 | + public void ResetValues() |
| 250 | + { |
| 251 | + tbName.Text = string.Empty; |
| 252 | + ddColor.SelectedIndex = 0; |
| 253 | + tbNumTriggers.Value = 3; |
| 254 | + tbElapsedTime.Value = 100; |
| 255 | + tbDelay.Value = 10; |
| 256 | + cbEveryDiff.Checked = false; |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments