/
LuaHandleSynced.h
242 lines (185 loc) · 7.01 KB
/
LuaHandleSynced.h
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef LUA_HANDLE_SYNCED
#define LUA_HANDLE_SYNCED
#include <string>
using std::string;
#include "LuaHandle.h"
#include "LuaRulesParams.h"
#include "System/UnorderedMap.hpp"
struct lua_State;
class LuaSyncedCtrl;
class CLuaHandleSynced;
struct BuildInfo;
class CUnsyncedLuaHandle : public CLuaHandle
{
friend class CLuaHandleSynced;
public: // call-ins
bool DrawUnit(const CUnit* unit);
bool DrawFeature(const CFeature* feature);
bool DrawShield(const CUnit* unit, const CWeapon* weapon);
bool DrawProjectile(const CProjectile* projectile);
public: // all non-eventhandler callins
void RecvFromSynced(lua_State* srcState, int args); // not an engine call-in
protected:
CUnsyncedLuaHandle(CLuaHandleSynced* base, const string& name, int order);
virtual ~CUnsyncedLuaHandle();
bool Init(const string& code, const string& file);
static CUnsyncedLuaHandle* GetUnsyncedHandle(lua_State* L) {
assert(dynamic_cast<CUnsyncedLuaHandle*>(CLuaHandle::GetHandle(L)));
return static_cast<CUnsyncedLuaHandle*>(CLuaHandle::GetHandle(L));
}
protected:
CLuaHandleSynced& base;
};
class CSyncedLuaHandle : public CLuaHandle
{
friend class CLuaHandleSynced;
public: // call-ins
bool CommandFallback(const CUnit* unit, const Command& cmd);
bool AllowCommand(const CUnit* unit, const Command& cmd, bool fromSynced);
bool AllowUnitCreation(const UnitDef* unitDef, const CUnit* builder, const BuildInfo* buildInfo);
bool AllowUnitTransfer(const CUnit* unit, int newTeam, bool capture);
bool AllowUnitBuildStep(const CUnit* builder, const CUnit* unit, float part);
bool AllowFeatureCreation(const FeatureDef* featureDef, int allyTeamID, const float3& pos);
bool AllowFeatureBuildStep(const CUnit* builder, const CFeature* feature, float part);
bool AllowResourceLevel(int teamID, const string& type, float level);
bool AllowResourceTransfer(int oldTeam, int newTeam, const string& type, float amount);
bool AllowDirectUnitControl(int playerID, const CUnit* unit);
bool AllowBuilderHoldFire(const CUnit* unit, int action);
bool AllowStartPosition(int playerID, int teamID, unsigned char readyState, const float3& clampedPos, const float3& rawPickPos);
bool TerraformComplete(const CUnit* unit, const CUnit* build);
bool MoveCtrlNotify(const CUnit* unit, int data);
int AllowWeaponTargetCheck(unsigned int attackerID, unsigned int attackerWeaponNum, unsigned int attackerWeaponDefID);
bool AllowWeaponTarget(
unsigned int attackerID,
unsigned int targetID,
unsigned int attackerWeaponNum,
unsigned int attackerWeaponDefID,
float* targetPriority
);
bool AllowWeaponInterceptTarget(const CUnit* interceptorUnit, const CWeapon* interceptorWeapon, const CProjectile* interceptorTarget);
bool UnitPreDamaged(
const CUnit* unit,
const CUnit* attacker,
float damage,
int weaponDefID,
int projectileID,
bool paralyzer,
float* newDamage,
float* impulseMult);
bool FeaturePreDamaged(
const CFeature* feature,
const CUnit* attacker,
float damage,
int weaponDefID,
int projectileID,
float* newDamage,
float* impulseMult);
bool ShieldPreDamaged(
const CProjectile* projectile,
const CWeapon* shieldEmitter,
const CUnit* shieldCarrier,
bool bounceProjectile,
const CWeapon* beamEmitter,
const CUnit* beamCarrier);
bool SyncedActionFallback(const string& line, int playerID);
protected:
CSyncedLuaHandle(CLuaHandleSynced* base, const string& name, int order);
virtual ~CSyncedLuaHandle();
bool Init(const string& code, const string& file);
static CSyncedLuaHandle* GetSyncedHandle(lua_State* L) {
assert(dynamic_cast<CSyncedLuaHandle*>(CLuaHandle::GetHandle(L)));
return static_cast<CSyncedLuaHandle*>(CLuaHandle::GetHandle(L));
}
protected:
CLuaHandleSynced& base;
spring::unordered_map<string, string> textCommands; // name, help
private:
int origNextRef;
private: // call-outs
static int SyncedRandom(lua_State* L);
static int SyncedRandomSeed(lua_State* L);
static int SyncedNext(lua_State* L);
static int SyncedPairs(lua_State* L);
static int SendToUnsynced(lua_State* L);
static int AddSyncedActionFallback(lua_State* L);
static int RemoveSyncedActionFallback(lua_State* L);
static int GetWatchUnitDef(lua_State* L);
static int SetWatchUnitDef(lua_State* L);
static int GetWatchFeatureDef(lua_State* L);
static int SetWatchFeatureDef(lua_State* L);
static int GetWatchWeaponDef(lua_State* L);
static int SetWatchWeaponDef(lua_State* L);
};
class CLuaHandleSynced
{
public: // Non-eventhandler call-ins
bool GotChatMsg(const string& msg, int playerID) {
return syncedLuaHandle.GotChatMsg(msg, playerID) ||
unsyncedLuaHandle.GotChatMsg(msg, playerID);
}
bool RecvLuaMsg(const string& msg, int playerID) {
return syncedLuaHandle.RecvLuaMsg(msg, playerID);
}
public:
void CheckStack() {
syncedLuaHandle.CheckStack();
unsyncedLuaHandle.CheckStack();
}
static CUnsyncedLuaHandle* GetUnsyncedHandle(lua_State* L) {
if (!CLuaHandle::GetHandleSynced(L))
return CUnsyncedLuaHandle::GetUnsyncedHandle(L);
auto slh = CSyncedLuaHandle::GetSyncedHandle(L);
return &slh->base.unsyncedLuaHandle;
}
static CSyncedLuaHandle* GetSyncedHandle(lua_State* L) {
if (CLuaHandle::GetHandleSynced(L))
return CSyncedLuaHandle::GetSyncedHandle(L);
auto ulh = CUnsyncedLuaHandle::GetUnsyncedHandle(L);
return &ulh->base.syncedLuaHandle;
}
protected:
CLuaHandleSynced(const string& name, int order);
virtual ~CLuaHandleSynced();
string LoadFile(const string& filename, const string& modes) const;
void Init(const string& syncedFile, const string& unsyncedFile, const string& modes);
bool IsValid() const {
return (syncedLuaHandle.IsValid() && unsyncedLuaHandle.IsValid());
}
void KillLua(bool inFreeHandler = false) {
syncedLuaHandle.KillLua(inFreeHandler);
unsyncedLuaHandle.KillLua(inFreeHandler);
}
#define SET_PERMISSION(name, type) \
void Set ## name(const type arg) { \
syncedLuaHandle.Set ## name(arg); \
unsyncedLuaHandle.Set ## name(arg); \
}
SET_PERMISSION(FullCtrl, bool);
SET_PERMISSION(FullRead, bool);
SET_PERMISSION(CtrlTeam, int);
SET_PERMISSION(ReadTeam, int);
SET_PERMISSION(ReadAllyTeam, int);
SET_PERMISSION(SelectTeam, int);
#undef SET_PERMISSION
protected:
friend class CUnsyncedLuaHandle;
friend class CSyncedLuaHandle;
// hooks to add code during initialization
virtual bool AddSyncedCode(lua_State* L) = 0;
virtual bool AddUnsyncedCode(lua_State* L) = 0;
// call-outs
static int LoadStringData(lua_State* L);
static int CallAsTeam(lua_State* L);
public:
CSyncedLuaHandle syncedLuaHandle;
CUnsyncedLuaHandle unsyncedLuaHandle;
public:
static void ClearGameParams() { spring::clear_unordered_map(gameParams); }
static const LuaRulesParams::Params& GetGameParams() { return gameParams; }
private:
//FIXME: add to CREG?
static LuaRulesParams::Params gameParams;
friend class LuaSyncedCtrl;
};
#endif /* LUA_HANDLE_SYNCED */