Skip to content

Commit 281580b

Browse files
committed
Make argument of new waypoint-parametered script actions default to first unused waypoint
1 parent d4d2365 commit 281580b

3 files changed

Lines changed: 154 additions & 2 deletions

File tree

src/TSMapEditor/Models/Map.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Xna.Framework;
22
using Microsoft.Xna.Framework.Graphics;
33
using Rampastring.Tools;
4+
using Rampastring.XNAUI.XNAControls;
45
using System;
56
using System.Collections.Generic;
67
using System.Globalization;
@@ -2124,6 +2125,134 @@ private void CheckForAITriggerTeamWithMaxZeroIssue(AITriggerType aiTrigger, Team
21242125
}
21252126
}
21262127

2128+
public bool IsLikelyMultiplayer()
2129+
{
2130+
if (Basic.MultiplayerOnly)
2131+
return true;
2132+
2133+
const int MaxUniqueHouses = 32;
2134+
const int FirstSpawnHouseIndex = 50;
2135+
2136+
if (Houses.Count > 0 && Houses.Count < MaxUniqueHouses)
2137+
{
2138+
// Most likely singleplayer - SP maps always have houses,
2139+
// while regular MP maps don't have houses
2140+
// and scripted TS MP maps need more than 50 houses
2141+
return false;
2142+
}
2143+
2144+
if (Houses.Count > FirstSpawnHouseIndex)
2145+
{
2146+
// Most likely a scripted multiplayer scenario
2147+
return true;
2148+
}
2149+
2150+
return false;
2151+
}
2152+
2153+
public bool IsWaypointInUse(Waypoint waypoint)
2154+
{
2155+
foreach (Trigger trigger in Triggers)
2156+
{
2157+
foreach (var action in trigger.Actions)
2158+
{
2159+
var triggerActionType = EditorConfig.TriggerActionTypes.GetValueOrDefault(action.ActionIndex);
2160+
2161+
if (triggerActionType == null)
2162+
continue;
2163+
2164+
for (int i = 0; i < triggerActionType.Parameters.Length; i++)
2165+
{
2166+
if (triggerActionType.Parameters[i] == null)
2167+
continue;
2168+
2169+
var param = triggerActionType.Parameters[i];
2170+
2171+
if (param.TriggerParamType == TriggerParamType.Waypoint && action.Parameters[i] == waypoint.Identifier.ToString())
2172+
{
2173+
return true;
2174+
}
2175+
2176+
if (param.TriggerParamType == TriggerParamType.WaypointZZ && action.Parameters[i] == Helpers.WaypointNumberToAlphabeticalString(waypoint.Identifier))
2177+
{
2178+
return true;
2179+
}
2180+
}
2181+
}
2182+
2183+
foreach (var condition in trigger.Conditions)
2184+
{
2185+
var triggerEventType = EditorConfig.TriggerEventTypes.GetValueOrDefault(condition.ConditionIndex);
2186+
2187+
if (triggerEventType == null)
2188+
continue;
2189+
2190+
for (int i = 0; i < triggerEventType.Parameters.Length; i++)
2191+
{
2192+
if (triggerEventType.Parameters[i] == null)
2193+
continue;
2194+
2195+
var param = triggerEventType.Parameters[i];
2196+
2197+
if (param.TriggerParamType == TriggerParamType.Waypoint && condition.Parameters[i] == waypoint.Identifier.ToString())
2198+
{
2199+
return true;
2200+
}
2201+
2202+
if (param.TriggerParamType == TriggerParamType.WaypointZZ && condition.Parameters[i] == Helpers.WaypointNumberToAlphabeticalString(waypoint.Identifier))
2203+
{
2204+
return true;
2205+
}
2206+
}
2207+
}
2208+
}
2209+
2210+
foreach (Script script in Scripts)
2211+
{
2212+
foreach (var actionEntry in script.Actions)
2213+
{
2214+
var scriptAction = EditorConfig.ScriptActions.GetValueOrDefault(actionEntry.Action);
2215+
2216+
if (scriptAction == null)
2217+
{
2218+
continue;
2219+
}
2220+
2221+
if (scriptAction.ParamType == TriggerParamType.Waypoint && actionEntry.Argument == waypoint.Identifier)
2222+
{
2223+
return true;
2224+
}
2225+
}
2226+
}
2227+
2228+
foreach (TeamType team in TeamTypes)
2229+
{
2230+
if (team.Waypoint == Helpers.WaypointNumberToAlphabeticalString(waypoint.Identifier))
2231+
{
2232+
return true;
2233+
}
2234+
}
2235+
2236+
return false;
2237+
}
2238+
2239+
public Waypoint GetFirstUnusedWaypoint()
2240+
{
2241+
int firstValidIdentifier = IsLikelyMultiplayer() ? 8 : 0;
2242+
2243+
for (int i = 0; i < Waypoints.Count; i++)
2244+
{
2245+
Waypoint wp = Waypoints[i];
2246+
if (wp.Identifier < firstValidIdentifier)
2247+
continue;
2248+
2249+
if (!IsWaypointInUse(wp))
2250+
return wp;
2251+
}
2252+
2253+
return null;
2254+
}
2255+
21272256
public void Clear()
21282257
{
21292258
LoadedINI = null;

src/TSMapEditor/Settings/UserSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public UserSettings()
4646
Theme,
4747
UseBoldFont,
4848
SmartScriptActionCloning,
49+
SmartScriptActionDefaultValues,
4950
AutoSaveInterval,
5051
SidebarWidth,
5152

@@ -104,6 +105,7 @@ public async Task SaveSettingsAsync()
104105
public StringSetting Theme = new StringSetting(General, nameof(Theme), "Default");
105106
public BoolSetting UseBoldFont = new BoolSetting(General, nameof(UseBoldFont), false);
106107
public BoolSetting SmartScriptActionCloning = new BoolSetting(General, nameof(SmartScriptActionCloning), true);
108+
public BoolSetting SmartScriptActionDefaultValues = new BoolSetting(General, nameof(SmartScriptActionDefaultValues), true);
107109
public IntSetting AutoSaveInterval = new IntSetting(General, nameof(AutoSaveInterval), 300);
108110
public IntSetting SidebarWidth = new IntSetting(General, nameof(SidebarWidth), 250);
109111

src/TSMapEditor/UI/Windows/ScriptsWindow.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,27 @@ private void SelTypeOfAction_MouseLeftDown(object sender, EventArgs e)
507507
selectScriptActionWindow.Open(scriptAction);
508508
}
509509

510+
private ScriptActionEntry CreateNewScriptActionEntry(int actionId)
511+
{
512+
var entry = new ScriptActionEntry(actionId, 0);
513+
514+
if (UserSettings.Instance.SmartScriptActionDefaultValues)
515+
{
516+
var scriptActionType = map.EditorConfig.ScriptActions[actionId];
517+
518+
switch (scriptActionType.ParamType)
519+
{
520+
case TriggerParamType.Waypoint:
521+
Waypoint wp = map.GetFirstUnusedWaypoint();
522+
if (wp != null)
523+
entry.Argument = wp.Identifier;
524+
break;
525+
}
526+
}
527+
528+
return entry;
529+
}
530+
510531
private void SelectScriptActionDarkeningPanel_Hidden(object sender, EventArgs e)
511532
{
512533
if (editedScript == null)
@@ -527,15 +548,15 @@ private void SelectScriptActionDarkeningPanel_Hidden(object sender, EventArgs e)
527548
{
528549
int viewTop = lbActions.ViewTop;
529550
int index = lbActions.SelectedIndex;
530-
editedScript.Actions.Insert(index, new ScriptActionEntry(selectScriptActionWindow.SelectedObject.ID, 0));
551+
editedScript.Actions.Insert(index, CreateNewScriptActionEntry(selectScriptActionWindow.SelectedObject.ID));
531552
EditScript(editedScript);
532553
lbActions.SelectedIndex = index;
533554
lbActions.ViewTop = viewTop;
534555
insertIndex = -1;
535556
}
536557
else
537558
{
538-
editedScript.Actions.Add(new ScriptActionEntry(selectScriptActionWindow.SelectedObject.ID, 0));
559+
editedScript.Actions.Add(CreateNewScriptActionEntry(selectScriptActionWindow.SelectedObject.ID));
539560
EditScript(editedScript);
540561
lbActions.SelectedIndex = lbActions.Items.Count - 1;
541562
lbActions.ScrollToBottom();

0 commit comments

Comments
 (0)