This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlogical_event_condition.pas
410 lines (335 loc) · 10.6 KB
/
logical_event_condition.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
{ This file is a part of Map editor for VCMI project
Copyright (C) 2015-2017 Alexander Shishkin alexvins@users.sourceforge.net
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit logical_event_condition;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, fpjson, editor_classes, logical_expression, editor_types,
vcmi_json, locale_manager, position;
type
TTriggeredEventType = (victory, defeat);
{ TLogicalEventConditionItem }
TLogicalEventConditionItem = class(TLogicalExpressionItem, ISerializeSpecial)
private
Ftype: AnsiString;
FConditionType: TWinLossCondition;
FObjectLink: string;
FPosition: TPosition;
FValue: Int32;
function IsPositionStored: Boolean;
procedure SetConditionType(AValue: TWinLossCondition);
procedure SetObjectLink(AValue: string);
procedure Settype(AValue: AnsiString);
procedure SetValue(AValue: Int32);
protected
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
public
//ISerializeSpecial
function Serialize(AHandler: TVCMIJSONStreamer): TJSONData;
procedure Deserialize(AHandler: TVCMIJSONDestreamer; ASrc: TJSONData);
public
property ConditionType: TWinLossCondition read FConditionType write SetConditionType;
function AddSubCondition:TLogicalEventConditionItem;
published
property Value:Int32 read FValue write SetValue default 0;
property &Object: string read FObjectLink write SetObjectLink;
property Position: TPosition read FPosition stored IsPositionStored;
property &type: AnsiString read FType write SetType;
end;
{ TLogicalEventCondition }
TLogicalEventCondition = class(TLogicalExpression, IReferenceNotify)
public //IReferenceNotify
procedure NotifyReferenced(AOldIdentifier, ANewIdentifier: AnsiString);
end;
{ TTriggeredEventEffect }
TTriggeredEventEffect = class(TPersistent)
private
Ftype: TTriggeredEventType;
FMessageToSend: TLocalizedString;
procedure SetMessageToSend(AValue: TLocalizedString);
protected
procedure AssignTo(Dest: TPersistent); override;
procedure Clear;
published
property MessageToSend: TLocalizedString read FMessageToSend write SetMessageToSend;
property &type: TTriggeredEventType read Ftype write Ftype nodefault;
end;
{ TTriggeredEvent }
TTriggeredEvent = class(TNamedCollectionItem)
private
FCondition: TLogicalEventCondition;
FEffect: TTriggeredEventEffect;
FMessage: TLocalizedString;
procedure SetMessage(AValue: TLocalizedString);
protected
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
function AddCondition:TLogicalEventConditionItem;
procedure Clear;
published
property Message: TLocalizedString read FMessage write SetMessage;
property Condition:TLogicalEventCondition read FCondition;
property Effect: TTriggeredEventEffect read FEffect;
end;
{ TTriggeredEvents }
TTriggeredEvents = class(specialize TGNamedCollection<TTriggeredEvent>)
private
FLM: TLocaleManager;
public
constructor Create(lm:TLocaleManager);
procedure AddStandardVictory();
procedure AddStandardDefeat();
end;
implementation
uses typinfo;
{ TTriggeredEvents }
constructor TTriggeredEvents.Create(lm: TLocaleManager);
begin
inherited Create;
FLM:=lm
end;
procedure TTriggeredEvents.AddStandardVictory;
var
standard_victory: TTriggeredEvent;
condition:TLogicalEventConditionItem;
begin
standard_victory := Add;
standard_victory.Effect.&type := TTriggeredEventType.victory;
standard_victory.Effect.MessageToSend := FLM.GeneralTexts[0,5];
standard_victory.Identifier:='standardVictory';
standard_victory.Message:=FLM.GeneralTexts[0,659];
condition := standard_victory.AddCondition;
condition.ConditionType:=TWinLossCondition.standardWin;
end;
procedure TTriggeredEvents.AddStandardDefeat;
var
standard_defeat: TTriggeredEvent;
condition:TLogicalEventConditionItem;
begin
standard_defeat := Add;
standard_defeat.Effect.&type := TTriggeredEventType.defeat;
standard_defeat.Effect.MessageToSend := FLM.GeneralTexts[0,8];
standard_defeat.Identifier:='standardDefeat';
standard_defeat.Message:=FLM.GeneralTexts[0,7];
condition := standard_defeat.AddCondition;
condition.ConditionType:=TWinLossCondition.daysWithoutTown;
condition.Value := 7;
end;
{ TLogicalEventCondition }
procedure TLogicalEventCondition.NotifyReferenced(AOldIdentifier,
ANewIdentifier: AnsiString);
begin
end;
{ TLogicalEventConditionItem }
procedure TLogicalEventConditionItem.SetConditionType(AValue: TWinLossCondition);
begin
if FConditionType=AValue then Exit;
FConditionType:=AValue;
end;
function TLogicalEventConditionItem.IsPositionStored: Boolean;
begin
Result := not FPosition.IsEmpty;
end;
procedure TLogicalEventConditionItem.SetObjectLink(AValue: string);
begin
if FObjectLink=AValue then Exit;
FObjectLink:=AValue;
end;
procedure TLogicalEventConditionItem.Settype(AValue: AnsiString);
begin
if Ftype=AValue then Exit;
Ftype:=AValue;
end;
procedure TLogicalEventConditionItem.SetValue(AValue: Int32);
begin
if FValue=AValue then Exit;
FValue:=AValue;
end;
procedure TLogicalEventConditionItem.AssignTo(Dest: TPersistent);
var
dest_typed: TLogicalEventConditionItem;
begin
if Dest is TLogicalEventConditionItem then
begin
dest_typed := TLogicalEventConditionItem(Dest);
dest_typed.ConditionType := ConditionType;
dest_typed.Value:=Value;
dest_typed.&Object:=&Object;
dest_typed.Position.X:=Position.X;
dest_typed.Position.Y:=Position.Y;
dest_typed.Position.L:=Position.L;
dest_typed.&type:=&type;
end;
inherited AssignTo(Dest);
end;
function TLogicalEventConditionItem.Serialize(AHandler: TVCMIJSONStreamer
): TJSONData;
var
item: TCollectionItem;
o: TJSONObject;
begin
Result := CreateJSONArray([]);
if SubExpressions.Count > 0 then
begin
TJSONArray(Result).Add(GetEnumName( TypeInfo(TLogicalOperator), Integer(LogicalOperator)));
for item in SubExpressions do
begin
TJSONArray(Result).Add((item as TLogicalEventConditionItem).Serialize(AHandler));
end;
end
else
begin
TJSONArray(Result).Add(GetEnumName(TypeInfo(TWinLossCondition), Integer(ConditionType)));
o := AHandler.ObjectToJSON(Self);
if o.Count = 0 then
begin
o.Free;
end
else
begin
TJSONArray(Result).Add(o);
end;
end;
end;
procedure TLogicalEventConditionItem.Deserialize(AHandler: TVCMIJSONDestreamer;
ASrc: TJSONData);
var
ASrcArray: TJSONArray;
instruction_name: TJSONStringType;
raw_instruction: Integer;
i: Integer;
SubExpression: TLogicalEventConditionItem;
o: TJSONObject;
begin
if ASrc.JSONType <> jtArray then
begin
raise Exception.Create('invalid format for event condition, array required');
end;
ASrcArray := TJSONArray(ASrc);
if ASrcArray.Count = 0 then
begin
raise Exception.Create('invalid format for event condition, array with size > 0 required');
end;
instruction_name := ASrcArray.Strings[0];
raw_instruction := GetEnumValue(TypeInfo(TLogicalOperator), instruction_name);
if raw_instruction >=0 then
begin
LogicalOperator:=TLogicalOperator(raw_instruction);
for i := 1 to ASrcArray.Count - 1 do
begin
SubExpression := TLogicalEventConditionItem (SubExpressions.Add);
SubExpression.Deserialize(AHandler, ASrcArray.Items[i]);
end;
end
else
begin
raw_instruction := GetEnumValue(TypeInfo(TWinLossCondition), instruction_name);
if raw_instruction <0 then
begin
raise Exception.Create('invalid instruction for event condition: '+instruction_name);
end;
ConditionType := TWinLossCondition(raw_instruction);
if(ASrcArray.Count > 1) and (ASrcArray.Types[1] = jtObject) then
begin
o := ASrcArray.Objects[1];
AHandler.JSONToObject(o,Self);
end;
end;
end;
function TLogicalEventConditionItem.AddSubCondition: TLogicalEventConditionItem;
begin
Result := SubExpressions.Add as TLogicalEventConditionItem;
end;
constructor TLogicalEventConditionItem.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
FPosition := TPosition.Create;
end;
destructor TLogicalEventConditionItem.Destroy;
begin
FPosition.Free;
inherited Destroy;
end;
{ TTriggeredEventEffect }
procedure TTriggeredEventEffect.SetMessageToSend(AValue: TLocalizedString);
begin
if FMessageToSend=AValue then Exit;
FMessageToSend:=AValue;
end;
procedure TTriggeredEventEffect.AssignTo(Dest: TPersistent);
var
dest_typed: TTriggeredEventEffect;
begin
if Dest is TTriggeredEventEffect then
begin
dest_typed := TTriggeredEventEffect(Dest);
dest_typed.MessageToSend := MessageToSend;
dest_typed.&type := &type;
end
else
begin
inherited AssignTo(Dest);
end;
end;
procedure TTriggeredEventEffect.Clear;
begin
MessageToSend:='';
&type := TTriggeredEventType.victory;
end;
{ TTriggeredEvent }
procedure TTriggeredEvent.SetMessage(AValue: TLocalizedString);
begin
if FMessage=AValue then Exit;
FMessage:=AValue;
end;
procedure TTriggeredEvent.AssignTo(Dest: TPersistent);
var
dest_typed: TTriggeredEvent;
begin
if Dest is TTriggeredEvent then
begin
dest_typed := TTriggeredEvent(Dest);
dest_typed.Condition.Assign(Condition);
dest_typed.Effect.Assign(Effect);
dest_typed.Message:=Message;
end;
inherited AssignTo(Dest);
end;
constructor TTriggeredEvent.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
FCondition := TLogicalEventCondition.CreateRoot(TLogicalEventConditionItem);
FEffect := TTriggeredEventEffect.Create;
end;
destructor TTriggeredEvent.Destroy;
begin
FEffect.Free;
FCondition.Free;
inherited Destroy;
end;
function TTriggeredEvent.AddCondition: TLogicalEventConditionItem;
begin
Result := Condition.Add as TLogicalEventConditionItem;
end;
procedure TTriggeredEvent.Clear;
begin
FMessage:='';
Condition.Clear;
Effect.Clear;
end;
end.