forked from ElunaLuaEngine/Eluna
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapMethods.h
373 lines (342 loc) · 9.78 KB
/
MapMethods.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
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
/*
* Copyright (C) 2010 - 2016 Eluna Lua Engine <http://emudevs.com/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#ifndef MAPMETHODS_H
#define MAPMETHODS_H
#include "ElunaInstanceAI.h"
/***
* A game map, e.g. Azeroth, Eastern Kingdoms, the Molten Core, etc.
*
* Inherits all methods from: none
*/
namespace LuaMap
{
#ifndef CLASSIC
/**
* Returns `true` if the [Map] is an arena [BattleGround], `false` otherwise.
*
* @return bool isArena
*/
int IsArena(lua_State* L, Map* map)
{
Eluna::Push(L, map->IsBattleArena());
return 1;
}
#endif
/**
* Returns `true` if the [Map] is a non-arena [BattleGround], `false` otherwise.
*
* @return bool isBattleGround
*/
int IsBattleground(lua_State* L, Map* map)
{
#if defined TRINITY || AZEROTHCORE
Eluna::Push(L, map->IsBattleground());
#else
Eluna::Push(L, map->IsBattleGround());
#endif
return 1;
}
/**
* Returns `true` if the [Map] is a dungeon, `false` otherwise.
*
* @return bool isDungeon
*/
int IsDungeon(lua_State* L, Map* map)
{
Eluna::Push(L, map->IsDungeon());
return 1;
}
/**
* Returns `true` if the [Map] has no [Player]s, `false` otherwise.
*
* @return bool isEmpty
*/
int IsEmpty(lua_State* L, Map* map)
{
Eluna::Push(L, map->isEmpty());
return 1;
}
#ifndef CLASSIC
/**
* Returns `true` if the [Map] is a heroic, `false` otherwise.
*
* @return bool isHeroic
*/
int IsHeroic(lua_State* L, Map* map)
{
Eluna::Push(L, map->IsHeroic());
return 1;
}
#endif
/**
* Returns `true` if the [Map] is a raid, `false` otherwise.
*
* @return bool isRaid
*/
int IsRaid(lua_State* L, Map* map)
{
Eluna::Push(L, map->IsRaid());
return 1;
}
/**
* Returns the name of the [Map].
*
* @return string mapName
*/
int GetName(lua_State* L, Map* map)
{
Eluna::Push(L, map->GetMapName());
return 1;
}
/**
* Returns the height of the [Map] at the given X and Y coordinates.
*
* In case of no height found nil is returned
*
* @param float x
* @param float y
* @return float z
*/
int GetHeight(lua_State* L, Map* map)
{
float x = Eluna::CHECKVAL<float>(L, 2);
float y = Eluna::CHECKVAL<float>(L, 3);
#if (defined(TBC) || defined(CLASSIC))
float z = map->GetHeight(x, y, MAX_HEIGHT);
#else
uint32 phasemask = Eluna::CHECKVAL<uint32>(L, 4, 1);
float z = map->GetHeight(phasemask, x, y, MAX_HEIGHT);
#endif
if (z != INVALID_HEIGHT)
Eluna::Push(L, z);
return 1;
}
/**
* Returns the difficulty of the [Map].
*
* Always returns 0 if the expansion is pre-TBC.
*
* @return int32 difficulty
*/
int GetDifficulty(lua_State* L, Map* map)
{
#ifndef CLASSIC
Eluna::Push(L, map->GetDifficulty());
#else
Eluna::Push(L, (Difficulty)0);
#endif
return 1;
}
/**
* Returns the instance ID of the [Map].
*
* @return uint32 instanceId
*/
int GetInstanceId(lua_State* L, Map* map)
{
Eluna::Push(L, map->GetInstanceId());
return 1;
}
/**
* Returns the player count currently on the [Map] (excluding GMs).
*
* @return uint32 playerCount
*/
int GetPlayerCount(lua_State* L, Map* map)
{
Eluna::Push(L, map->GetPlayersCountExceptGMs());
return 1;
}
/**
* Returns the ID of the [Map].
*
* @return uint32 mapId
*/
int GetMapId(lua_State* L, Map* map)
{
Eluna::Push(L, map->GetId());
return 1;
}
/**
* Returns the area ID of the [Map] at the specified X, Y, and Z coordinates.
*
* @param float x
* @param float y
* @param float z
* @param uint32 phasemask = PHASEMASK_NORMAL
* @return uint32 areaId
*/
int GetAreaId(lua_State* L, Map* map)
{
float x = Eluna::CHECKVAL<float>(L, 2);
float y = Eluna::CHECKVAL<float>(L, 3);
float z = Eluna::CHECKVAL<float>(L, 4);
#if defined TRINITY || defined AZEROTHCORE
float phasemask = Eluna::CHECKVAL<uint32>(L, 5, PHASEMASK_NORMAL);
Eluna::Push(L, map->GetAreaId(phasemask, x, y, z));
#else
Eluna::Push(L, map->GetTerrain()->GetAreaId(x, y, z));
#endif
return 1;
}
/**
* Returns a [WorldObject] by its GUID from the map if it is spawned.
*
* @param ObjectGuid guid
* @return [WorldObject] object
*/
int GetWorldObject(lua_State* L, Map* map)
{
ObjectGuid guid = Eluna::CHECKVAL<ObjectGuid>(L, 2);
#if defined TRINITY || AZEROTHCORE
switch (guid.GetHigh())
{
case HIGHGUID_PLAYER:
Eluna::Push(L, eObjectAccessor()GetPlayer(map, guid));
break;
case HIGHGUID_TRANSPORT:
case HIGHGUID_MO_TRANSPORT:
case HIGHGUID_GAMEOBJECT:
Eluna::Push(L, map->GetGameObject(guid));
break;
case HIGHGUID_VEHICLE:
case HIGHGUID_UNIT:
Eluna::Push(L, map->GetCreature(guid));
break;
case HIGHGUID_PET:
Eluna::Push(L, map->GetPet(guid));
break;
case HIGHGUID_DYNAMICOBJECT:
Eluna::Push(L, map->GetDynamicObject(guid));
break;
case HIGHGUID_CORPSE:
Eluna::Push(L, map->GetCorpse(guid));
break;
default:
break;
}
#else
Eluna::Push(L, map->GetWorldObject(guid));
#endif
return 1;
}
/**
* Sets the [Weather] type based on [WeatherType] and grade supplied.
*
* enum WeatherType
* {
* WEATHER_TYPE_FINE = 0,
* WEATHER_TYPE_RAIN = 1,
* WEATHER_TYPE_SNOW = 2,
* WEATHER_TYPE_STORM = 3,
* WEATHER_TYPE_THUNDERS = 86,
* WEATHER_TYPE_BLACKRAIN = 90
* };
*
* @param uint32 zone : id of the zone to set the weather for
* @param [WeatherType] type : the [WeatherType], see above available weather types
* @param float grade : the intensity/grade of the [Weather], ranges from 0 to 1
*/
int SetWeather(lua_State* L, Map* map)
{
(void)map; // ensure that the variable is referenced in order to pass compiler checks
uint32 zoneId = Eluna::CHECKVAL<uint32>(L, 2);
uint32 weatherType = Eluna::CHECKVAL<uint32>(L, 3);
float grade = Eluna::CHECKVAL<float>(L, 4);
#if defined TRINITY
if (Weather * weather = map->GetOrGenerateZoneDefaultWeather(zoneId))
weather->SetWeather((WeatherType)weatherType, grade);
#elif defined AZEROTHCORE
Weather* weather = WeatherMgr::FindWeather(zoneId);
if (!weather)
weather = WeatherMgr::AddWeather(zoneId);
if (weather)
weather->SetWeather((WeatherType)weatherType, grade);
#else
if (Weather::IsValidWeatherType(weatherType))
map->SetWeather(zoneId, (WeatherType)weatherType, grade, false);
#endif
return 0;
}
/**
* Gets the instance data table for the [Map], if it exists.
*
* The instance must be scripted using Eluna for this to succeed.
* If the instance is scripted in C++ this will return `nil`.
*
* @return table instance_data : instance data table, or `nil`
*/
int GetInstanceData(lua_State* L, Map* map)
{
#if defined TRINITY || AZEROTHCORE
ElunaInstanceAI* iAI = NULL;
if (InstanceMap* inst = map->ToInstanceMap())
iAI = dynamic_cast<ElunaInstanceAI*>(inst->GetInstanceScript());
#else
ElunaInstanceAI* iAI = dynamic_cast<ElunaInstanceAI*>(map->GetInstanceData());
#endif
if (iAI)
Eluna::GetEluna(L)->PushInstanceData(L, iAI, false);
else
Eluna::Push(L); // nil
return 1;
}
/**
* Saves the [Map]'s instance data to the database.
*/
int SaveInstanceData(lua_State* /*L*/, Map* map)
{
#if defined TRINITY || AZEROTHCORE
ElunaInstanceAI* iAI = NULL;
if (InstanceMap* inst = map->ToInstanceMap())
iAI = dynamic_cast<ElunaInstanceAI*>(inst->GetInstanceScript());
#else
ElunaInstanceAI* iAI = dynamic_cast<ElunaInstanceAI*>(map->GetInstanceData());
#endif
if (iAI)
iAI->SaveToDB();
return 0;
}
/**
* Returns a table with all the current [Player]s in the map
*
* enum TeamId
* {
* TEAM_ALLIANCE = 0,
* TEAM_HORDE = 1,
* TEAM_NEUTRAL = 2
* };
*
* @param [TeamId] team : optional check team of the [Player], Alliance, Horde or Neutral (All)
* @return table mapPlayers
*/
int GetPlayers(lua_State* L, Map* map)
{
uint32 team = Eluna::CHECKVAL<uint32>(L, 2, TEAM_NEUTRAL);
lua_newtable(L);
int tbl = lua_gettop(L);
uint32 i = 0;
Map::PlayerList const& players = map->GetPlayers();
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
{
#if defined TRINITY || AZEROTHCORE
Player* player = itr->GetSource();
#else
Player* player = itr->getSource();
#endif
if (!player)
continue;
if (player->GetSession() && (team >= TEAM_NEUTRAL || player->GetTeamId() == team))
{
Eluna::Push(L, player);
lua_rawseti(L, tbl, ++i);
}
}
lua_settop(L, tbl);
return 1;
}
};
#endif