Skip to content

Commit 62f36d4

Browse files
Add full support for losing levels (#2492)
* refactor give exp editor * added level down support do give exp command * fix: level lost string * reverse take exp loop * rename enable level down variable * fixing strings * move common start common event and send chat to bottom * cache class descriptor * force take exp when is a event command * refactor addlevels * fix wrong condition * fix: correct naming, mesage colors * simplify experience code * Give/TakeExperience parameter validation --------- Co-authored-by: Robbie Lodico <pandacoder@pm.me>
1 parent f5cb50c commit 62f36d4

File tree

10 files changed

+448
-362
lines changed

10 files changed

+448
-362
lines changed

Framework/Intersect.Framework.Core/GameObjects/Events/Commands/EventCommands.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ public partial class GiveExperienceCommand : EventCommand
331331
/// The Variable Id to use.
332332
/// </summary>
333333
public Guid VariableId { get; set; }
334+
335+
/// <summary>
336+
/// If true, when a player have their experience reduced, they will be able to level down.
337+
/// </summary>
338+
public bool EnableLosingLevels { get; set; } = false;
334339
}
335340

336341
public partial class ChangeLevelCommand : EventCommand

Framework/Intersect.Framework.Core/GameObjects/Events/CommonEventTrigger.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ public enum CommonEventTrigger
3939
MapChanged,
4040

4141
UserVariableChange,
42+
43+
LevelDown,
4244
}

Intersect (Core)/CustomColors.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ public sealed partial class CombatNamespace
194194

195195
public Color LevelUp = Color.Cyan;
196196

197+
public Color LevelLost = Color.Red;
198+
197199
public Color MagicDamage = new Color(255, 255, 0, 255);
198200

199201
public Color Missed = new Color(255, 255, 255, 255);

Intersect.Editor/Forms/Editors/Events/Event Commands/EventCommand_GiveExperience.Designer.cs

Lines changed: 223 additions & 198 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Intersect.Editor/Forms/Editors/Events/Event Commands/EventCommand_GiveExperience.cs

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,35 @@
55

66
namespace Intersect.Editor.Forms.Editors.Events.Event_Commands;
77

8-
98
public partial class EventCommandGiveExperience : UserControl
109
{
10+
private readonly FrmEvent _eventEditor;
1111

12-
private readonly FrmEvent mEventEditor;
13-
14-
private GiveExperienceCommand mMyCommand;
12+
private readonly GiveExperienceCommand _command;
1513

1614
public EventCommandGiveExperience(GiveExperienceCommand refCommand, FrmEvent editor)
1715
{
1816
InitializeComponent();
19-
mMyCommand = refCommand;
20-
mEventEditor = editor;
17+
_command = refCommand;
18+
_eventEditor = editor;
2119
InitLocalization();
2220

23-
rdoVariable.Checked = mMyCommand.UseVariable;
24-
rdoGlobalVariable.Checked = mMyCommand.VariableType == VariableType.ServerVariable;
25-
rdoGuildVariable.Checked = mMyCommand.VariableType == VariableType.GuildVariable;
21+
rdoVariable.Checked = _command.UseVariable;
22+
rdoGlobalVariable.Checked = _command.VariableType == VariableType.ServerVariable;
23+
rdoGuildVariable.Checked = _command.VariableType == VariableType.GuildVariable;
24+
25+
nudExperience.Minimum = -long.MaxValue;
26+
nudExperience.Maximum = long.MaxValue;
2627

27-
SetupAmountInput();
28+
chkEnableLevelDown.Checked = _command.EnableLosingLevels;
29+
30+
SetupAmountInput(default, default);
2831
}
2932

3033
private void InitLocalization()
3134
{
32-
grpGiveExperience.Text = Strings.EventGiveExperience.title;
33-
lblExperience.Text = Strings.EventGiveExperience.label;
35+
grpGiveExperience.Text = Strings.EventGiveExperience.Title;
36+
lblExperience.Text = Strings.EventGiveExperience.Label;
3437

3538
lblVariable.Text = Strings.EventGiveExperience.Variable;
3639

@@ -45,60 +48,40 @@ private void InitLocalization()
4548
rdoGlobalVariable.Text = Strings.EventGiveExperience.ServerVariable;
4649
rdoGuildVariable.Text = Strings.EventGiveExperience.GuildVariable;
4750

48-
btnSave.Text = Strings.EventGiveExperience.okay;
49-
btnCancel.Text = Strings.EventGiveExperience.cancel;
51+
chkEnableLevelDown.Text = Strings.EventGiveExperience.EnableLosingLevels;
52+
53+
btnSave.Text = Strings.General.Okay;
54+
btnCancel.Text = Strings.General.Cancel;
5055
}
5156

5257
private void btnSave_Click(object sender, EventArgs e)
5358
{
54-
mMyCommand.Exp = (long) nudExperience.Value;
59+
_command.Exp = (long)nudExperience.Value;
60+
5561
if (rdoPlayerVariable.Checked)
5662
{
57-
mMyCommand.VariableType = VariableType.PlayerVariable;
58-
mMyCommand.VariableId = PlayerVariableDescriptor.IdFromList(cmbVariable.SelectedIndex, VariableDataType.Integer);
63+
_command.VariableType = VariableType.PlayerVariable;
64+
_command.VariableId = PlayerVariableDescriptor.IdFromList(cmbVariable.SelectedIndex, VariableDataType.Integer);
5965
}
6066
else if (rdoGlobalVariable.Checked)
6167
{
62-
mMyCommand.VariableType = VariableType.ServerVariable;
63-
mMyCommand.VariableId = ServerVariableDescriptor.IdFromList(cmbVariable.SelectedIndex, VariableDataType.Integer);
68+
_command.VariableType = VariableType.ServerVariable;
69+
_command.VariableId = ServerVariableDescriptor.IdFromList(cmbVariable.SelectedIndex, VariableDataType.Integer);
6470
}
6571
else if (rdoGuildVariable.Checked)
6672
{
67-
mMyCommand.VariableType = VariableType.GuildVariable;
68-
mMyCommand.VariableId = GuildVariableDescriptor.IdFromList(cmbVariable.SelectedIndex, VariableDataType.Integer);
73+
_command.VariableType = VariableType.GuildVariable;
74+
_command.VariableId = GuildVariableDescriptor.IdFromList(cmbVariable.SelectedIndex, VariableDataType.Integer);
6975
}
70-
mMyCommand.UseVariable = !rdoManual.Checked;
71-
mEventEditor.FinishCommandEdit();
72-
}
73-
74-
private void btnCancel_Click(object sender, EventArgs e)
75-
{
76-
mEventEditor.CancelCommandEdit();
77-
}
7876

79-
private void rdoManual_CheckedChanged(object sender, EventArgs e)
80-
{
81-
SetupAmountInput();
82-
}
83-
84-
private void rdoVariable_CheckedChanged(object sender, EventArgs e)
85-
{
86-
SetupAmountInput();
77+
_command.UseVariable = !rdoManual.Checked;
78+
_command.EnableLosingLevels = chkEnableLevelDown.Checked;
79+
_eventEditor.FinishCommandEdit();
8780
}
8881

89-
private void rdoPlayerVariable_CheckedChanged(object sender, EventArgs e)
90-
{
91-
SetupAmountInput();
92-
}
93-
94-
private void rdoGlobalVariable_CheckedChanged(object sender, EventArgs e)
95-
{
96-
SetupAmountInput();
97-
}
98-
99-
private void rdoGuildVariable_CheckedChanged(object sender, EventArgs e)
82+
private void btnCancel_Click(object sender, EventArgs e)
10083
{
101-
SetupAmountInput();
84+
_eventEditor.CancelCommandEdit();
10285
}
10386

10487
private void VariableBlank()
@@ -114,7 +97,7 @@ private void VariableBlank()
11497
}
11598
}
11699

117-
private void SetupAmountInput()
100+
private void SetupAmountInput(object? sender, EventArgs? e)
118101
{
119102
grpManualAmount.Visible = rdoManual.Checked;
120103
grpVariableAmount.Visible = !rdoManual.Checked;
@@ -124,9 +107,9 @@ private void SetupAmountInput()
124107
{
125108
cmbVariable.Items.AddRange(PlayerVariableDescriptor.GetNamesByType(VariableDataType.Integer));
126109
// Do not update if the wrong type of variable is saved
127-
if (mMyCommand.VariableType == VariableType.PlayerVariable)
110+
if (_command.VariableType == VariableType.PlayerVariable)
128111
{
129-
var index = PlayerVariableDescriptor.ListIndex(mMyCommand.VariableId, VariableDataType.Integer);
112+
var index = PlayerVariableDescriptor.ListIndex(_command.VariableId, VariableDataType.Integer);
130113
if (index > -1)
131114
{
132115
cmbVariable.SelectedIndex = index;
@@ -145,9 +128,9 @@ private void SetupAmountInput()
145128
{
146129
cmbVariable.Items.AddRange(ServerVariableDescriptor.GetNamesByType(VariableDataType.Integer));
147130
// Do not update if the wrong type of variable is saved
148-
if (mMyCommand.VariableType == VariableType.ServerVariable)
131+
if (_command.VariableType == VariableType.ServerVariable)
149132
{
150-
var index = ServerVariableDescriptor.ListIndex(mMyCommand.VariableId, VariableDataType.Integer);
133+
var index = ServerVariableDescriptor.ListIndex(_command.VariableId, VariableDataType.Integer);
151134
if (index > -1)
152135
{
153136
cmbVariable.SelectedIndex = index;
@@ -166,9 +149,9 @@ private void SetupAmountInput()
166149
{
167150
cmbVariable.Items.AddRange(GuildVariableDescriptor.GetNamesByType(VariableDataType.Integer));
168151
// Do not update if the wrong type of variable is saved
169-
if (mMyCommand.VariableType == VariableType.GuildVariable)
152+
if (_command.VariableType == VariableType.GuildVariable)
170153
{
171-
var index = GuildVariableDescriptor.ListIndex(mMyCommand.VariableId, VariableDataType.Integer);
154+
var index = GuildVariableDescriptor.ListIndex(_command.VariableId, VariableDataType.Integer);
172155
if (index > -1)
173156
{
174157
cmbVariable.SelectedIndex = index;
@@ -184,6 +167,6 @@ private void SetupAmountInput()
184167
}
185168
}
186169

187-
nudExperience.Value = Math.Max(1, mMyCommand.Exp);
170+
nudExperience.Value = _command.Exp;
188171
}
189172
}

Intersect.Editor/Forms/Editors/Events/Event Commands/EventCommand_GiveExperience.resx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3-
<!--
3+
<!--
44
Microsoft ResX Schema
5-
5+
66
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
1111
associated with the data types.
12-
12+
1313
Example:
14-
14+
1515
... ado.net/XML headers & schema ...
1616
<resheader name="resmimetype">text/microsoft-resx</resheader>
1717
<resheader name="version">2.0</resheader>
@@ -26,36 +26,36 @@
2626
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
2727
<comment>This is a comment</comment>
2828
</data>
29-
30-
There are any number of "resheader" rows that contain simple
29+
30+
There are any number of "resheader" rows that contain simple
3131
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
3737
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
4141
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
4545
read any of the formats listed below.
46-
46+
4747
mimetype: application/x-microsoft.net.object.binary.base64
48-
value : The object must be serialized with
48+
value : The object must be serialized with
4949
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
5050
: and then encoded with base64 encoding.
5151
5252
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
53+
value : The object must be serialized with
5454
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
5555
: and then encoded with base64 encoding.
5656
5757
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
58+
value : The object must be serialized into a byte array
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->

Intersect.Editor/Localization/Strings.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,7 @@ public partial struct EventEditor
28002800
{16, @"Inventory Changed"},
28012801
{17, @"Map Changed"},
28022802
{18, @"User Variable Changed"},
2803+
{19, @"Level Down"},
28032804
};
28042805

28052806
public static LocalizedString conditions = @"Conditions";
@@ -2947,20 +2948,17 @@ public partial struct EventEndQuest
29472948

29482949
public partial struct EventGiveExperience
29492950
{
2951+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2952+
public static LocalizedString AmountType = @"Amount Type";
29502953

2951-
public static LocalizedString cancel = @"Cancel";
2952-
2953-
public static LocalizedString label = @"Give Experience:";
2954-
2955-
public static LocalizedString okay = @"Ok";
2956-
2957-
public static LocalizedString title = @"Give Experience";
2954+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2955+
public static LocalizedString EnableLosingLevels = @"Enable losing levels?";
29582956

29592957
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2960-
public static LocalizedString AmountType = @"Amount Type";
2958+
public static LocalizedString GuildVariable = @"Guild Variable";
29612959

29622960
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2963-
public static LocalizedString Variable = @"Variable";
2961+
public static LocalizedString Label = @"Give Experience:";
29642962

29652963
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
29662964
public static LocalizedString Manual = @"Manual";
@@ -2972,8 +2970,10 @@ public partial struct EventGiveExperience
29722970
public static LocalizedString ServerVariable = @"Global Variable";
29732971

29742972
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2975-
public static LocalizedString GuildVariable = @"Guild Variable";
2973+
public static LocalizedString Title = @"Give Experience";
29762974

2975+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2976+
public static LocalizedString Variable = @"Variable";
29772977
}
29782978

29792979
public partial struct EventGotoLabel

Intersect.Server.Core/Entities/Events/CommandProcessing.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ private static void ProcessCommand(
421421
Stack<CommandInstance> callStack
422422
)
423423
{
424-
player.LevelUp();
424+
player.AddLevels();
425425
}
426426

427427
//Give Experience Command
@@ -454,7 +454,14 @@ Stack<CommandInstance> callStack
454454
}
455455
}
456456

457-
player.GiveExperience(quantity);
457+
if(quantity > 0)
458+
{
459+
player.GiveExperience(quantity);
460+
}
461+
else if (quantity < 0)
462+
{
463+
player.TakeExperience(Math.Abs(quantity), command.EnableLosingLevels, force: true);
464+
}
458465
}
459466

460467
//Change Level Command

0 commit comments

Comments
 (0)