-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCPower.cpp
343 lines (260 loc) · 7.29 KB
/
CPower.cpp
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
// CPower.cpp
//
// CPower class
#include "PreComp.h"
#define INVOKE_TAG CONSTLIT("Invoke")
#define ON_SHOW_TAG CONSTLIT("OnShow")
#define ON_INVOKED_BY_PLAYER_TAG CONSTLIT("OnInvokedByPlayer")
#define ON_INVOKED_BY_NON_PLAYER_TAG CONSTLIT("OnInvokedByNonPlayer")
#define ON_DESTROY_CHECK_TAG CONSTLIT("OnDestroyCheck")
#define UNID_ATTRIB CONSTLIT("UNID")
#define COST_ATTRIB CONSTLIT("cost")
#define KEY_ATTRIB CONSTLIT("key")
#define NAME_ATTRIB CONSTLIT("name")
#define PROPERTY_NAME CONSTLIT("name")
#define STR_A_CAUSE CONSTLIT("aCause")
#define STR_A_DESTROYER CONSTLIT("aDestroyer")
static char *CACHED_EVENTS[CPower::evtCount] =
{
"OnInvoke",
"OnShow",
"OnInvokedByPlayer",
"OnInvokedByNonPlayer",
"OnDestroyCheck",
};
CPower::CPower (void)
// CPower constructor
{
}
CPower::~CPower (void)
// CPower destructor
{
}
void CPower::InitOldStyleEvent (ECachedHandlers iEvent, ICCItem *pCode)
// InitOldStyleEvent
//
// Initialize the event, if necessary.
{
if (m_CachedEvents[iEvent].pCode == NULL && pCode)
{
m_CachedEvents[iEvent].pCode = pCode;
m_CachedEvents[iEvent].pExtension = GetExtension();
}
}
void CPower::Invoke (CSpaceObject *pSource, CSpaceObject *pTarget, CString *retsError)
// Invoke
//
// Invoke the power
{
SEventHandlerDesc Event;
if (!FindEventHandler(evtCode, &Event))
return;
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(pSource);
Ctx.DefineSpaceObject(CONSTLIT("gTarget"), pTarget);
ICCItemPtr pResult = Ctx.RunCode(Event);
if (retsError)
{
if (pResult->IsError())
*retsError = pResult->GetStringValue();
else
*retsError = NULL_STR;
}
}
void CPower::InvokeByPlayer (CSpaceObject *pSource, CSpaceObject *pTarget, CString *retsError)
// Invoke
//
// Invoke the power for a player
{
if (retsError) *retsError = NULL_STR;
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(pSource);
Ctx.DefineSpaceObject(CONSTLIT("gTarget"), pTarget);
// First handle the OnInvokedByPlayer check. If it is not defined, then skip it.
SEventHandlerDesc Event;
if (FindEventHandler(evtOnInvokedByPlayer, &Event))
{
ICCItemPtr pResult = Ctx.RunCode(Event);
if (pResult->IsError())
{
if (retsError)
*retsError = pResult->GetStringValue();
// Can't invoke
return;
}
// If OnInvokedByPlayer returns Nil, then we do not invoke
else if (pResult->IsNil())
return;
}
// Invoke
if (FindEventHandler(evtCode, &Event))
{
ICCItemPtr pResult = Ctx.RunCode(Event);
if (pResult->IsError())
{
if (retsError)
*retsError = pResult->GetStringValue();
}
}
}
void CPower::InvokeByNonPlayer(CSpaceObject *pSource, CSpaceObject *pTarget, CString *retsError)
// Invoke
//
// Invoke the power for a non-player
{
if (retsError) *retsError = NULL_STR;
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(pSource);
Ctx.DefineSpaceObject(CONSTLIT("gTarget"), pTarget);
// First handle the OnInvokedByNonPlayer check. If it is not defined, then skip it.
SEventHandlerDesc Event;
if (FindEventHandler(evtOnInvokedByNonPlayer, &Event))
{
ICCItemPtr pResult = Ctx.RunCode(Event);
if (pResult->IsError())
{
if (retsError)
*retsError = pResult->GetStringValue();
// Can't invoke
return;
}
// If OnInvokedByNonPlayer returns Nil, then we do not invoke
else if (pResult->IsNil())
return;
}
// Invoke
if (FindEventHandler(evtCode, &Event))
{
ICCItemPtr pResult = Ctx.RunCode(Event);
if (pResult->IsError())
{
if (retsError)
*retsError = pResult->GetStringValue();
}
}
}
ALERROR CPower::OnCreateFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc)
// OnCreateFromXML
//
// Create from XML
{
int i;
// Load basic stuff
m_sName = pDesc->GetAttribute(NAME_ATTRIB);
m_iInvokeCost = pDesc->GetAttributeInteger(COST_ATTRIB);
m_sInvokeKey = pDesc->GetAttribute(KEY_ATTRIB);
// Load various code blocks
m_pCode = NULL;
m_pOnShow = NULL;
m_pOnInvokedByPlayer = NULL;
m_pOnInvokedByNonPlayer = NULL;
m_pOnDestroyCheck = NULL;
for (i = 0; i < pDesc->GetContentElementCount(); i++)
{
CXMLElement *pBlock = pDesc->GetContentElement(i);
if (strEquals(pBlock->GetTag(), INVOKE_TAG))
{
if (!m_pCode.Load(pBlock->GetContentText(0), &Ctx.sError))
return ComposeLoadError(Ctx, Ctx.sError);
}
else if (strEquals(pBlock->GetTag(), ON_SHOW_TAG))
{
if (!m_pOnShow.Load(pBlock->GetContentText(0), &Ctx.sError))
return ComposeLoadError(Ctx, Ctx.sError);
}
else if (strEquals(pBlock->GetTag(), ON_INVOKED_BY_PLAYER_TAG))
{
if (!m_pOnInvokedByPlayer.Load(pBlock->GetContentText(0), &Ctx.sError))
return ComposeLoadError(Ctx, Ctx.sError);
}
else if (strEquals(pBlock->GetTag(), ON_INVOKED_BY_NON_PLAYER_TAG))
{
if (!m_pOnInvokedByNonPlayer.Load(pBlock->GetContentText(0), &Ctx.sError))
return ComposeLoadError(Ctx, Ctx.sError);
}
else if (strEquals(pBlock->GetTag(), ON_DESTROY_CHECK_TAG))
{
if (!m_pOnDestroyCheck.Load(pBlock->GetContentText(0), &Ctx.sError))
return ComposeLoadError(Ctx, Ctx.sError);
}
else if (IsValidLoadXML(pBlock->GetTag()))
;
else
{
return ComposeLoadError(Ctx, strPatternSubst(CONSTLIT("Unknown element: <%s>"), pBlock->GetTag()));
}
}
// Done
return NOERROR;
}
ALERROR CPower::OnBindDesign (SDesignLoadCtx &Ctx)
// OnBindDesign
//
// Bind design.
{
// Init from standard events
InitCachedEvents(evtCount, CACHED_EVENTS, m_CachedEvents);
// See if we have old-style events.
InitOldStyleEvent(evtCode, m_pCode);
InitOldStyleEvent(evtOnShow, m_pOnShow);
InitOldStyleEvent(evtOnInvokedByPlayer, m_pOnInvokedByPlayer);
InitOldStyleEvent(evtOnInvokedByNonPlayer, m_pOnInvokedByNonPlayer);
InitOldStyleEvent(evtOnDestroyCheck, m_pOnDestroyCheck);
// Done
return NOERROR;
}
bool CPower::OnDestroyCheck (CSpaceObject *pSource, DestructionTypes iCause, const CDamageSource &Attacker)
// OnDestroyCheck
//
// Returns TRUE if ship can be destroyed; otherwise, FALSE
{
SEventHandlerDesc Event;
if (!FindEventHandler(evtOnDestroyCheck, &Event))
return true;
// Set up parameters
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(pSource);
Ctx.DefineSpaceObject(STR_A_DESTROYER, Attacker.GetObj());
Ctx.DefineInteger(STR_A_CAUSE, iCause);
// Invoke
ICCItemPtr pResult = Ctx.RunCode(Event);
return (pResult->IsNil() ? false : true);
}
ICCItemPtr CPower::OnGetProperty (CCodeChainCtx &Ctx, const CString &sProperty) const
// OnGetProperty
//
// Returns a property.
{
CCodeChain &CC = g_pUniverse->GetCC();
if (strEquals(sProperty, PROPERTY_NAME))
return ICCItemPtr(CC.CreateString(GetName()));
else
return NULL;
}
bool CPower::OnShow (CSpaceObject *pSource, CSpaceObject *pTarget, CString *retsError)
// OnShow
//
// Returns TRUE if we should show this power on the menu
{
SEventHandlerDesc Event;
if (!FindEventHandler(evtOnShow, &Event))
return true;
// Set up parameters
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(pSource);
Ctx.DefineSpaceObject(CONSTLIT("gTarget"), pTarget);
ICCItemPtr pResult = Ctx.RunCode(Event);
if (retsError)
{
if (pResult->IsError())
*retsError = pResult->GetStringValue();
else
*retsError = NULL_STR;
}
return !pResult->IsNil();
}