-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathBaseConflict.Types.Shared.pas
245 lines (209 loc) · 8.51 KB
/
BaseConflict.Types.Shared.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
unit BaseConflict.Types.Shared;
interface
uses
Engine.Helferlein.Windows,
BaseConflict.Constants;
type
{$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished]) FIELDS([vcPrivate, vcProtected, vcPublic])}
/// <summary>
/// gsLoading - Players are loading and connecting, game time has not been started
/// gsWarming - Players are all ready and game starts, countdown for first tick is running
/// gsPlaying - Players are playing and game time is running
/// gsShutdown - Game is shutting down
/// </summary>
EnumInGameStatus = (gsLoading, gsWarming, gsPlaying, gsShutdown);
EnumComparator = (coLowerEqual, coLower, coGreaterEqual, coGreater, coEqual);
function ResourceCompare(ResourceType : EnumResource; const Resource : RParam; Comparator : EnumComparator; ReferenceValue : integer) : boolean; overload;
function ResourceCompare(ResourceType : EnumResource; const Resource : RParam; Comparator : EnumComparator; ReferenceValue : single) : boolean; overload;
function ResourceCompare(ResourceType : EnumResource; const Resource : RParam; Comparator : EnumComparator; const ReferenceValue : RParam; ReferenceFactor : single = 1.0) : boolean; overload;
function ResourcePercentage(ResourceType : EnumResource; const Balance, Cap : RParam) : single;
function ResourceAdd(ResourceType : EnumResource; const Summand, Summand2 : RParam) : RParam;
function ResourceSubtract(ResourceType : EnumResource; const Minuend, Subtrahend : RParam) : RParam;
function ResourceAsSingle(ResourceType : EnumResource; const Resource : RParam) : single;
function ResourceOverride(ResourceType : EnumResource; const Resource : RParam; const OverrideValue : single) : RParam;
type
RResourceCost = record
ResourceType : EnumResource;
Amount : RParam;
end;
AResourceCost = array of RResourceCost;
AResourceCostHelper = record helper for AResourceCost
public
function ToRParam : RParam;
function Count : integer;
function TryGetValue(ResourceType : EnumResource; out Amount : RParam) : boolean;
function GetValue(ResourceType : EnumResource) : RParam;
end;
RIncome = record
Gold, Wood : single;
constructor Create(Gold, Wood : single);
function ToRParam : RParam;
class operator add(const a, b : RIncome) : RIncome; static;
end;
TGameTimer = class(TTimer)
protected
function GetTimeStamp : double; override;
public
property StartingTime : double read FLastTime write FLastTime;
end;
EnumLaneDirection = (ldNormal, ldReverse);
PLaneDirection = ^EnumLaneDirection;
{$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished]) FIELDS([vcPrivate, vcProtected, vcPublic])}
implementation
uses
BaseConflict.Globals;
function ResourceCompare(ResourceType : EnumResource; const Resource : RParam; Comparator : EnumComparator; ReferenceValue : integer) : boolean; overload;
begin
Result := False;
if ResourceType in RES_INT_RESOURCES then
begin
case Comparator of
coLowerEqual : Result := Resource.AsInteger <= ReferenceValue;
coLower : Result := Resource.AsInteger < ReferenceValue;
coGreaterEqual : Result := Resource.AsInteger >= ReferenceValue;
coGreater : Result := Resource.AsInteger > ReferenceValue;
coEqual : Result := Resource.AsInteger = ReferenceValue;
end;
end
else
begin
case Comparator of
coLowerEqual : Result := Resource.AsSingle <= ReferenceValue;
coLower : Result := Resource.AsSingle < ReferenceValue;
coGreaterEqual : Result := Resource.AsSingle >= ReferenceValue;
coGreater : Result := Resource.AsSingle > ReferenceValue;
coEqual : Result := Resource.AsSingle = ReferenceValue;
end;
end;
end;
function ResourceCompare(ResourceType : EnumResource; const Resource : RParam; Comparator : EnumComparator; ReferenceValue : single) : boolean; overload;
var
IntReferenceValue : integer;
begin
Result := False;
if ResourceType in RES_INT_RESOURCES then
begin
IntReferenceValue := round(ReferenceValue);
case Comparator of
coLowerEqual : Result := Resource.AsInteger <= IntReferenceValue;
coLower : Result := Resource.AsInteger < IntReferenceValue;
coGreaterEqual : Result := Resource.AsInteger >= IntReferenceValue;
coGreater : Result := Resource.AsInteger > IntReferenceValue;
coEqual : Result := Resource.AsInteger = IntReferenceValue;
end;
end
else
begin
case Comparator of
coLowerEqual : Result := Resource.AsSingle <= ReferenceValue;
coLower : Result := Resource.AsSingle < ReferenceValue;
coGreaterEqual : Result := Resource.AsSingle >= ReferenceValue;
coGreater : Result := Resource.AsSingle > ReferenceValue;
coEqual : Result := Resource.AsSingle = ReferenceValue;
end;
end;
end;
function ResourceCompare(ResourceType : EnumResource; const Resource : RParam; Comparator : EnumComparator; const ReferenceValue : RParam; ReferenceFactor : single) : boolean;
begin
Result := False;
if ResourceType in RES_INT_RESOURCES then
begin
case Comparator of
coLowerEqual : Result := Resource.AsInteger <= ReferenceValue.AsInteger * ReferenceFactor;
coLower : Result := Resource.AsInteger < ReferenceValue.AsInteger * ReferenceFactor;
coGreaterEqual : Result := Resource.AsInteger >= ReferenceValue.AsInteger * ReferenceFactor;
coGreater : Result := Resource.AsInteger > ReferenceValue.AsInteger * ReferenceFactor;
coEqual : Result := Resource.AsInteger = ReferenceValue.AsInteger * ReferenceFactor;
end;
end
else
begin
case Comparator of
coLowerEqual : Result := Resource.AsSingle <= ReferenceValue.AsSingle * ReferenceFactor;
coLower : Result := Resource.AsSingle < ReferenceValue.AsSingle * ReferenceFactor;
coGreaterEqual : Result := Resource.AsSingle >= ReferenceValue.AsSingle * ReferenceFactor;
coGreater : Result := Resource.AsSingle > ReferenceValue.AsSingle * ReferenceFactor;
coEqual : Result := Resource.AsSingle = ReferenceValue.AsSingle * ReferenceFactor;
end;
end;
end;
function ResourceSubtract(ResourceType : EnumResource; const Minuend, Subtrahend : RParam) : RParam;
begin
if ResourceType in RES_INT_RESOURCES then Result := Minuend.AsInteger - Subtrahend.AsInteger
else Result := Minuend.AsSingle - Subtrahend.AsSingle;
end;
function ResourceAdd(ResourceType : EnumResource; const Summand, Summand2 : RParam) : RParam;
begin
if ResourceType in RES_INT_RESOURCES then Result := Summand.AsInteger + Summand2.AsInteger
else Result := Summand.AsSingle + Summand2.AsSingle;
end;
function ResourcePercentage(ResourceType : EnumResource; const Balance, Cap : RParam) : single;
begin
if ResourceType in RES_INT_RESOURCES then Result := Balance.AsInteger / Cap.AsInteger
else Result := Balance.AsSingle / Cap.AsSingle;
end;
function ResourceAsSingle(ResourceType : EnumResource; const Resource : RParam) : single;
begin
if ResourceType in RES_INT_RESOURCES then Result := Resource.AsInteger
else Result := Resource.AsSingle;
end;
function ResourceOverride(ResourceType : EnumResource; const Resource : RParam; const OverrideValue : single) : RParam;
begin
if OverrideValue <= 0 then
Result := Resource
else
if ResourceType in RES_INT_RESOURCES then Result := integer(round(OverrideValue))
else Result := OverrideValue;
end;
{ AResourceCostHelper }
function AResourceCostHelper.Count : integer;
begin
Result := length(self);
end;
function AResourceCostHelper.GetValue(ResourceType : EnumResource) : RParam;
begin
if not TryGetValue(ResourceType, Result) then
Result := RPARAM_EMPTY;
end;
function AResourceCostHelper.ToRParam : RParam;
begin
Result := RParam.FromArray<RResourceCost>(TArray<RResourceCost>(self));
end;
function AResourceCostHelper.TryGetValue(ResourceType : EnumResource; out Amount : RParam) : boolean;
var
i : integer;
begin
Result := False;
for i := 0 to Count - 1 do
if self[i].ResourceType = ResourceType then
begin
Amount := self[i].Amount;
exit(True);
end;
end;
{ TGameTimer }
function TGameTimer.GetTimeStamp : double;
begin
{$IF defined(CLIENT) and not defined(MAPEDITOR)}
assert(assigned(Game));
Result := Game.ServerTime;
{$ELSE}
Result := GameTimeManager.GetFloatingTimestamp;
{$ENDIF}
end;
{ RIncome }
class operator RIncome.add(const a, b : RIncome) : RIncome;
begin
Result.Gold := a.Gold + b.Gold;
Result.Wood := a.Wood + b.Wood;
end;
constructor RIncome.Create(Gold, Wood : single);
begin
self.Gold := Gold;
self.Wood := Wood;
end;
function RIncome.ToRParam : RParam;
begin
Result := RParam.From<RIncome>(self);
end;
end.