-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack.asc
504 lines (441 loc) · 11 KB
/
Stack.asc
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// currently (AGS 3.4.0.3), managed structs cannot hold any pointer types
// (including String), so frustratingly we must attempt to pool them here
struct StringCache_t
{
String Pool[];
int Count;
int Capacity;
};
StringCache_t StringCache;
// Grows the String cache by a factor of 2 until it matches or exceeds minCapacity.
void noloopcheck Grow(this StringCache_t*, int minCapacity)
{
if (this.Capacity >= minCapacity) return;
if ((this.Capacity == 1000000) || (minCapacity > 1000000))
{
AbortGame("Stack module error!: Attempted to grow cache capacity beyond 1000000 items.");
}
int copy = this.Capacity;
do
{
if (this.Capacity == 0) this.Capacity = 1;
else if (this.Capacity >= 500000) this.Capacity = 1000000;
else this.Capacity *= 2;
} while (this.Capacity < minCapacity);
String pool[] = new String[this.Capacity];
int i;
for (i = 0; i < copy; i++)
{
pool[i] = this.Pool[i];
}
this.Pool = pool;
}
function game_start()
{
// reserve StringCache slot 0, because int is default 0
// and with no ctor we can't control the value of _scacheID
// for StackData instances, so StringCache.Pool[0] == null
StringCache.Count = 1;
StringCache.Grow(1);
}
// Checks if the String exists in the pool. If not, it is added.
// Returns the cached index of this String.
int Cache(this String*)
{
int i;
for (i = 0; i < StringCache.Count; i++)
{
if (StringCache.Pool[i] == this) return i;
}
StringCache.Count++;
StringCache.Grow(StringCache.Count);
StringCache.Pool[i] = this;
return i;
}
// StackData property definitions
int get_AsInt(this StackData*)
{
if (this.Type != eStackDataInt)
{
return 0;
}
return this._idata;
}
float get_AsFloat(this StackData*)
{
if (this.Type != eStackDataFloat)
{
return 0.0;
}
return this._fdata;
}
String get_AsString(this StackData*)
{
if ((this.Type != eStackDataString) || (this._scacheID < 0) ||
(this._scacheID >= StringCache.Count))
{
return null;
}
return StringCache.Pool[this._scacheID];
}
Character* get_AsCharacter(this StackData*)
{
if ((this.Type != eStackDataCharacter) || (this._idata == -1))
{
return null;
}
return character[this._idata];
}
InventoryItem* get_AsInventoryItem(this StackData*)
{
if ((this.Type != eStackDataInventoryItem) || (this._idata == -1))
{
return null;
}
return inventory[this._idata];
}
GUI* get_AsGUI(this StackData*)
{
if ((this.Type != eStackDataGUI) || (this._idata == -1))
{
return null;
}
return gui[this._idata];
}
GUIControl* get_AsGUIControl(this StackData*)
{
if ((this.Type != eStackDataGUIControl) || (this._idata == -1))
{
return null;
}
int owner = this._idata / AGS_MAX_CONTROLS_PER_GUI;
int ctrl = this._idata % AGS_MAX_CONTROLS_PER_GUI;
if ((owner < 0) || (owner >= Game.GUICount) ||
(ctrl >= gui[owner].ControlCount))
{
return null;
}
return gui[owner].Controls[ctrl];
}
// end StackData property definitions
// StackData helpers (for internal access to private members)
int GetIData(this StackData*)
{
return this._idata;
}
void SetIData(this StackData*, int value)
{
this._idata = value;
}
float GetFData(this StackData*)
{
return this._fdata;
}
void SetFData(this StackData*, float value)
{
this._fdata = value;
}
int GetSCacheID(this StackData*)
{
return this._scacheID;
}
void SetSCacheID(this StackData*, int value)
{
this._scacheID = value;
}
void SetType(this StackData*, StackDataType value)
{
this.Type = value;
}
// end StackData helpers
static bool StackData::IsNullOrInvalid(StackData value)
{
return ((value == null) || (value.Type == eStackDataInvalid));
}
StackData IDataToData(static StackData, int idata, StackDataType type)
{
StackData result = new StackData;
result.SetIData(idata);
result.SetType(type);
return result;
}
static StackData StackData::IntToData(int value)
{
return StackData.IDataToData(value, eStackDataInt);
}
static StackData StackData::FloatToData(float value)
{
StackData result = new StackData;
result.SetFData(value);
result.SetType(eStackDataFloat);
return result;
}
static StackData StackData::StringToData(String value)
{
StackData result = new StackData;
if (value != null)
{
result.SetSCacheID(value.Cache());
}
result.SetType(eStackDataString);
return result;
}
static StackData StackData::StringCachedToData(int cachedID)
{
StackData result = new StackData;
if ((cachedID > 0) && (cachedID < StringCache.Count))
{
result.SetSCacheID(cachedID);
}
result.SetType(eStackDataString);
return result;
}
static StackData StackData::CharacterToData(Character *value)
{
int id = -1;
if (value != null) id = value.ID;
return StackData.IDataToData(id, eStackDataCharacter);
}
static StackData StackData::InventoryItemToData(InventoryItem *value)
{
int id = -1;
if (value != null) id = value.ID;
return StackData.IDataToData(id, eStackDataInventoryItem);
}
static StackData StackData::GUIToData(GUI *value)
{
int id = -1;
if (value != null) id = value.ID;
return StackData.IDataToData(id, eStackDataGUI);
}
static StackData StackData::GUIControlToData(GUIControl *value)
{
int id = -1;
if (value != null) id = ((value.OwningGUI.ID * AGS_MAX_CONTROLS_PER_GUI) + value.ID);
return StackData.IDataToData(id, eStackDataGUIControl);
}
// Returns the cached index of a String
static int Stack::GetStringCachedID(String value)
{
if (value == null) return -1;
return value.Cache();
}
// Stack property definitions
int get_StringCacheCapacity(this Stack*)
{
return StringCache.Capacity;
}
void set_StringCacheCapacity(this Stack*, int value)
{
StringCache.Grow(value);
}
// Grows this Stack's item array until it matches or exceeds minCapacity.
void noloopcheck Grow(this Stack*, int minCapacity)
{
if (minCapacity <= this._capacity) return;
if ((this._capacity == 1000000) || (minCapacity > 1000000))
{
AbortGame("Stack module error!: Attempted to grow capacity beyond 1000000 items.");
}
int copy = this._capacity;
do
{
if (this._capacity == 0) this._capacity = 1;
else if (this._capacity >= 500000) this._capacity = 1000000;
else this._capacity *= 2;
} while (this._capacity < minCapacity);
StackData items[] = new StackData[this._capacity];
int i;
for (i = 0; i < copy; i++)
{
items[i] = this._items[i];
}
this._items = items;
}
int get_Capacity(this Stack*)
{
return this._capacity;
}
void set_Capacity(this Stack*, int value)
{
this.Grow(value);
}
// Safely overwrites an existing item in the Stack.
bool OverwriteItem(this Stack*, int index, StackData value)
{
if ((index < 0) || (index >= this.ItemCount) || (value == null) ||
(value.Type == eStackDataInvalid))
{
return false;
}
this._items[index] = value;
return true;
}
StackData geti_Items(this Stack*, int index)
{
if ((index < 0) || (index >= this.ItemCount)) return new StackData;
return this._items[index];
}
void seti_Items(this Stack*, int index, StackData value)
{
this.OverwriteItem(index, value);
}
// end Stack property definitions
void Stack::Clear()
{
int i;
for (i = 0; i < this.ItemCount; i++)
{
this._items = null;
}
}
// Push a generic item onto the Stack.
bool noloopcheck Stack::Push(StackData item, int index, bool insert)
{
if ((item == null) || (item.Type == eStackDataInvalid))
{
// item was not valid, abort
return false;
}
if ((index < 0) || (index > this.ItemCount) || (index == SCR_NO_VALUE))
{
// invalid index, force append
index = this.ItemCount;
}
else if (index != this.ItemCount)
{
// valid index for existing item
if (!insert)
{
// not inserting, overwrite item
return this.OverwriteItem(index, item);
}
}
// insert or append item
this.ItemCount++;
this.Grow(this.ItemCount);
// shift items, as needed
int i;
for (i = this.ItemCount - 1; i > index; i--)
{
this._items[i] = this._items[i - 1];
}
// and insert our new item
this._items[index] = item;
}
bool Stack::PushInt(int value, int index, bool insert)
{
return this.Push(StackData.IntToData(value), index, insert);
}
bool Stack::PushFloat(float value, int index, bool insert)
{
return this.Push(StackData.FloatToData(value), index, insert);
}
bool Stack::PushString(String value, int index, bool insert)
{
return this.Push(StackData.StringToData(value), index, insert);
}
bool Stack::PushStringCached(int cachedID, int index, bool insert)
{
return this.Push(StackData.StringCachedToData(cachedID), index, insert);
}
bool Stack::PushCharacter(Character *value, int index, bool insert)
{
return this.Push(StackData.CharacterToData(value), index, insert);
}
bool Stack::PushInventoryItem(InventoryItem *value, int index, bool insert)
{
return this.Push(StackData.InventoryItemToData(value), index, insert);
}
bool Stack::PushGUI(GUI *value, int index, bool insert)
{
return this.Push(StackData.GUIToData(value), index, insert);
}
bool Stack::PushGUIControl(GUIControl *value, int index, bool insert)
{
return this.Push(StackData.GUIControlToData(value), index, insert);
}
// Pops a StackData object.
StackData noloopcheck Pop(this Stack*, int index, bool remove)
{
// determine index based on Stack's pop style
if (index == SCR_NO_VALUE)
{
if (this.PopStyle == eStackPopFirstInFirstOut)
{
index = 0;
}
else if (this.PopStyle == eStackPopFirstInLastOut)
{
index = this.ItemCount - 1;
}
else
{
index = Random(this.ItemCount - 1);
}
}
if ((index < 0) || (index >= this.ItemCount))
{
// invalid index
return null;
}
StackData item = this._items[index];
if (!remove)
{
// if not removing the item, just return it
return item;
}
this.ItemCount--;
// shift the remaining items, to remove this one
int i;
for (i = index; i < this.ItemCount; i++)
{
this._items[i] = this._items[i + 1];
}
// clear the newly emptied slot
this._items[i] = null;
return item;
}
// Helper to ensure that the popped StackData is non-null.
// This prevents duplicating the defaults for invalid data casts.
StackData SafePop(this Stack*, int index, bool remove)
{
StackData result = this.Pop(index, remove);
if (result == null) return new StackData;
return result;
}
int Stack::PopInt(int index, bool remove)
{
StackData item = this.SafePop(index, remove);
return item.get_AsInt();
}
float Stack::PopFloat(int index, bool remove)
{
StackData item = this.SafePop(index, remove);
return item.get_AsFloat();
}
String Stack::PopString(int index, bool remove)
{
StackData item = this.SafePop(index, remove);
return item.get_AsString();
}
Character* Stack::PopCharacter(int index, bool remove)
{
StackData item = this.SafePop(index, remove);
return item.get_AsCharacter();
}
InventoryItem* Stack::PopInventoryItem(int index, bool remove)
{
StackData item = this.SafePop(index, remove);
return item.get_AsInventoryItem();
}
GUI* Stack::PopGUI(int index, bool remove)
{
StackData item = this.SafePop(index, remove);
return item.get_AsGUI();
}
GUIControl* Stack::PopGUIControl(int index, bool remove)
{
StackData item = this.SafePop(index, remove);
return item.get_AsGUIControl();
}