mirror of
https://github.com/azerothcore/mod-ale
synced 2025-11-29 15:38:17 +08:00
Some fixes for TC and changes overall Pass map object to hook via function arguments The map object is no longer stored inside the instance data table. Fix mistake in base64 decoder It was failing whenever it encountered a '=' character, which is completely valid. Make ElunaInstanceAI::Load always load something When it failed to load data, it was leaving nothing on the stack. Since subsequent code expected Load to always load something, this was causing issues. Now, when Load fails to load anything, it just leaves a new empty table on the stack. Also: the error messages for Load have been improved. Modify lua-marshal to allow saving of functions/userdata. Some additional code was needed to save functions due to the inclusion of a reference to _ENV within their upvalues (since Lua 5.2). During encoding, a placeholder is left where the _ENV reference would be. During decoding, a reference to the current _G is swapped with the placeholder. Make ElunaInstanceAI::Load re-initialize if data failed to load. Also improve error messages by not including the raw data. Improve storage format of upvalues Instead of storing the upvalues by name, store by index. A wrapper is still used in case the upvalue is nil, to prevent holes in the upvalues table. A special field in the upvalues table, "E", is used to store the index of the _ENV reference (if there was one). A reference to the current globals table is set as the upvalue upon decoding. Remove wrapping from upvalue storing, instead save amount of upvalues
325 lines
8.6 KiB
C++
325 lines
8.6 KiB
C++
/*
|
|
* Copyright (C) 2010 - 2015 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(Eluna* /*E*/, 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(Eluna* /*E*/, lua_State* L, Map* map)
|
|
{
|
|
#ifndef TRINITY
|
|
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(Eluna* /*E*/, 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(Eluna* /*E*/, 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(Eluna* /*E*/, 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(Eluna* /*E*/, lua_State* L, Map* map)
|
|
{
|
|
Eluna::Push(L, map->IsRaid());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns the name of the [Map].
|
|
*
|
|
* @return string mapName
|
|
*/
|
|
int GetName(Eluna* /*E*/, 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(Eluna* /*E*/, 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(Eluna* /*E*/, 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(Eluna* /*E*/, 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(Eluna* /*E*/, lua_State* L, Map* map)
|
|
{
|
|
Eluna::Push(L, map->GetPlayersCountExceptGMs());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns the ID of the [Map].
|
|
*
|
|
* @return uint32 mapId
|
|
*/
|
|
int GetMapId(Eluna* /*E*/, 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
|
|
* @return uint32 areaId
|
|
*/
|
|
int GetAreaId(Eluna* /*E*/, 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);
|
|
|
|
#ifndef TRINITY
|
|
Eluna::Push(L, map->GetTerrain()->GetAreaId(x, y, z));
|
|
#else
|
|
Eluna::Push(L, map->GetAreaId(x, y, z));
|
|
#endif
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns a [WorldObject] by its GUID from the map if it is spawned.
|
|
*
|
|
* @param uint64 guid
|
|
*/
|
|
int GetWorldObject(Eluna* /*E*/, lua_State* L, Map* map)
|
|
{
|
|
uint64 guid = Eluna::CHECKVAL<uint64>(L, 2);
|
|
|
|
#ifndef TRINITY
|
|
Eluna::Push(L, map->GetWorldObject(ObjectGuid(guid)));
|
|
#else
|
|
switch (GUID_HIPART(guid))
|
|
{
|
|
case HIGHGUID_PLAYER:
|
|
Eluna::Push(L, sObjectAccessor->GetObjectInMap(ObjectGuid(guid), map, (Player*)NULL));
|
|
break;
|
|
case HIGHGUID_TRANSPORT:
|
|
case HIGHGUID_MO_TRANSPORT:
|
|
case HIGHGUID_GAMEOBJECT:
|
|
Eluna::Push(L, sObjectAccessor->GetObjectInMap(ObjectGuid(guid), map, (GameObject*)NULL));
|
|
break;
|
|
case HIGHGUID_VEHICLE:
|
|
case HIGHGUID_UNIT:
|
|
Eluna::Push(L, sObjectAccessor->GetObjectInMap(ObjectGuid(guid), map, (Creature*)NULL));
|
|
break;
|
|
case HIGHGUID_PET:
|
|
Eluna::Push(L, sObjectAccessor->GetObjectInMap(ObjectGuid(guid), map, (Pet*)NULL));
|
|
break;
|
|
case HIGHGUID_DYNAMICOBJECT:
|
|
Eluna::Push(L, sObjectAccessor->GetObjectInMap(ObjectGuid(guid), map, (DynamicObject*)NULL));
|
|
break;
|
|
case HIGHGUID_CORPSE:
|
|
Eluna::Push(L, sObjectAccessor->GetObjectInMap(ObjectGuid(guid), map, (Corpse*)NULL));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
#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(Eluna* /*E*/, lua_State* L, Map* map)
|
|
{
|
|
uint32 zoneId = Eluna::CHECKVAL<uint32>(L, 2);
|
|
uint32 weatherType = Eluna::CHECKVAL<uint32>(L, 3);
|
|
float grade = Eluna::CHECKVAL<float>(L, 4);
|
|
|
|
#if defined(TRINITY)
|
|
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(Eluna* E, lua_State* L, Map* map)
|
|
{
|
|
#ifdef TRINITY
|
|
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)
|
|
sEluna->PushInstanceData(L, iAI, false);
|
|
else
|
|
Eluna::Push(L); // nil
|
|
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Saves the [Map]'s instance data to the database.
|
|
*/
|
|
int SaveInstanceData(Eluna* E, lua_State* L, Map* map)
|
|
{
|
|
#ifdef TRINITY
|
|
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;
|
|
}
|
|
};
|
|
#endif
|