summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2013-04-14 11:15:35 +0200
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2013-04-14 11:15:35 +0200
commit3a132996f798dbf552ac858b06fa656b6790a501 (patch)
tree050396efcc609d5234414f15d52b6f4c123e6f80 /src
parentff7041ab8d0c176a7c9764656c21177944f52c31 (diff)
downloadmanaserv-3a132996f798dbf552ac858b06fa656b6790a501.tar.gz
manaserv-3a132996f798dbf552ac858b06fa656b6790a501.tar.xz
manaserv-3a132996f798dbf552ac858b06fa656b6790a501.zip
Use a full user data for Entity references
Only moved a single script function to a member for now, will do others in a separate commit.
Diffstat (limited to 'src')
-rw-r--r--src/game-server/entity.cpp2
-rw-r--r--src/game-server/entity.h21
-rw-r--r--src/scripting/lua.cpp53
-rw-r--r--src/scripting/luascript.cpp9
-rw-r--r--src/scripting/luautil.cpp66
-rw-r--r--src/scripting/luautil.h8
6 files changed, 64 insertions, 95 deletions
diff --git a/src/game-server/entity.cpp b/src/game-server/entity.cpp
index cbfa96f..b40ac44 100644
--- a/src/game-server/entity.cpp
+++ b/src/game-server/entity.cpp
@@ -26,7 +26,7 @@ Entity::Entity(EntityType type, MapComposite *map) :
mType(type)
{
for (int i = 0; i < ComponentTypeCount; ++i)
- mComponents[i] = 0;
+ mComponents[i] = nullptr;
}
Entity::~Entity()
diff --git a/src/game-server/entity.h b/src/game-server/entity.h
index 81faa94..2f360df 100644
--- a/src/game-server/entity.h
+++ b/src/game-server/entity.h
@@ -44,7 +44,7 @@ class MapComposite;
class Entity : public sigc::trackable
{
public:
- Entity(EntityType type, MapComposite *map = 0);
+ Entity(EntityType type, MapComposite *map = nullptr);
virtual ~Entity();
@@ -52,8 +52,7 @@ class Entity : public sigc::trackable
template <class T> void addComponent(T *component);
template <class T> T *getComponent() const;
-
- Component *getComponent(ComponentType type) const;
+ template <class T> bool hasComponent() const;
bool isVisible() const;
bool canMove() const;
@@ -69,6 +68,8 @@ class Entity : public sigc::trackable
sigc::signal<void, Entity *> signal_map_changed;
private:
+ Component *getComponent(ComponentType type) const;
+
MapComposite *mMap; /**< Map the entity is on */
EntityType mType; /**< Type of this entity. */
@@ -101,7 +102,6 @@ inline void Entity::addComponent(T *component)
*/
inline Component *Entity::getComponent(ComponentType type) const
{
- assert(mComponents[type]);
return mComponents[type];
}
@@ -112,7 +112,18 @@ inline Component *Entity::getComponent(ComponentType type) const
template <class T>
inline T *Entity::getComponent() const
{
- return static_cast<T*>(getComponent(T::type));
+ T *component = static_cast<T*>(getComponent(T::type));
+ assert(component);
+ return component;
+}
+
+/**
+ * Returns whether this class has a certain component.
+ */
+template <class T>
+inline bool Entity::hasComponent() const
+{
+ return getComponent(T::type) != nullptr;
}
/**
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index d7a1323..2d9692b 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -314,7 +314,7 @@ static int npc_create(lua_State *s)
}
GameState::enqueueInsert(npc);
- lua_pushlightuserdata(s, npc);
+ push(s, npc);
return 1;
}
@@ -369,27 +369,19 @@ static int monster_create(lua_State *s)
actorComponent->setPosition(*monster, Point(x, y));
GameState::enqueueInsert(monster);
- lua_pushlightuserdata(s, monster);
+ push(s, monster);
return 1;
}
-/** LUA monster_remove (creation)
- * monster_remove(handle monster)
+/** LUA entity:remove (creation)
+ * entity:remove()
**
- * **Return value:** True if removing the monster suceeded.
- *
- * Remove the monster ''monster'' from the current map.
+ * Removes the entity from its current map.
*/
-static int monster_remove(lua_State *s)
+static int entity_remove(lua_State *s)
{
- bool monsterRemoved = false;
- if (Entity *m = getMonster(s, 1))
- {
- GameState::remove(m);
- monsterRemoved = true;
- }
- lua_pushboolean(s, monsterRemoved);
- return 1;
+ GameState::remove(LuaEntity::check(s, 1));
+ return 0;
}
/** LUA trigger_create (creation)
@@ -2157,10 +2149,7 @@ static int get_character_by_name(lua_State *s)
const char *name = luaL_checkstring(s, 1);
Entity *ch = gameHandler->getCharacterByNameSlow(name);
- if (!ch)
- lua_pushnil(s);
- else
- lua_pushlightuserdata(s, ch);
+ push(s, ch);
return 1;
}
@@ -2706,7 +2695,7 @@ static int log(lua_State *s)
/** LUA get_beings_in_circle (area)
* get_beings_in_circle(int x, int y, int radius)
- * get_beings_in_circle(handle being, int radius)
+ * get_beings_in_circle(handle actor, int radius)
**
* **Return value:** This function returns a lua table of all beings in a
* circle of radius (in pixels) ''radius'' centered either at the pixel at
@@ -2715,9 +2704,9 @@ static int log(lua_State *s)
static int get_beings_in_circle(lua_State *s)
{
int x, y, r;
- if (lua_islightuserdata(s, 1))
+ if (lua_isuserdata(s, 1))
{
- Entity *b = checkBeing(s, 1);
+ Entity *b = checkActor(s, 1);
const Point &pos = b->getComponent<ActorComponent>()->getPosition();
x = pos.x;
y = pos.y;
@@ -2747,7 +2736,7 @@ static int get_beings_in_circle(lua_State *s)
actorComponent->getSize(),
Point(x, y), r))
{
- lua_pushlightuserdata(s, b);
+ push(s, b);
lua_rawseti(s, tableStackPosition, tableIndex);
tableIndex++;
}
@@ -2784,7 +2773,7 @@ static int get_beings_in_rectangle(lua_State *s)
if ((t == OBJECT_NPC || t == OBJECT_CHARACTER || t == OBJECT_MONSTER) &&
rect.contains(b->getComponent<ActorComponent>()->getPosition()))
{
- lua_pushlightuserdata(s, b);
+ push(s, b);
lua_rawseti(s, tableStackPosition, tableIndex);
tableIndex++;
}
@@ -3453,7 +3442,6 @@ static int item_class_attacks(lua_State *s)
*/
static int test_tableget(lua_State *s)
{
-
std::list<float> list;
std::vector<std::string> svector;
std::vector<int> ivector;
@@ -3480,9 +3468,8 @@ static int test_tableget(lua_State *s)
LOG_INFO("Pushing Integer Vector");
ivector.resize(10);
for (int i = 1; i < 10; i++)
- {
- ivector[i-1] = i * i;
- }
+ ivector[i - 1] = i * i;
+
pushSTLContainer<int>(s, ivector);
LOG_INFO("Pushing String/String Map");
@@ -3499,7 +3486,6 @@ static int test_tableget(lua_State *s)
set.insert(10);
pushSTLContainer<int>(s, set);
-
return 5;
}
@@ -3609,7 +3595,6 @@ LuaScript::LuaScript():
{ "monster_change_anger", &monster_change_anger },
{ "monster_drop_anger", &monster_drop_anger },
{ "monster_get_angerlist", &monster_get_angerlist },
- { "monster_remove", &monster_remove },
{ "being_apply_status", &being_apply_status },
{ "being_remove_status", &being_remove_status },
{ "being_has_status", &being_has_status },
@@ -3694,6 +3679,11 @@ LuaScript::LuaScript():
{ NULL, NULL }
};
+ static luaL_Reg const members_Entity[] = {
+ { "remove", &entity_remove },
+ { NULL, NULL }
+ };
+
static luaL_Reg const members_ItemClass[] = {
{ "on", &item_class_on },
{ "attacks", &item_class_attacks },
@@ -3732,6 +3722,7 @@ LuaScript::LuaScript():
LuaAttackInfo::registerType(mRootState, "Attack", members_AttackInfo);
LuaDamage::registerType(mRootState, "Damage", members_Damage);
+ LuaEntity::registerType(mRootState, "Entity", members_Entity);
LuaItemClass::registerType(mRootState, "ItemClass", members_ItemClass);
LuaMapObject::registerType(mRootState, "MapObject", members_MapObject);
LuaMonsterClass::registerType(mRootState, "MonsterClass", members_MonsterClass);
diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp
index 83ac36b..f5fb6a4 100644
--- a/src/scripting/luascript.cpp
+++ b/src/scripting/luascript.cpp
@@ -75,24 +75,21 @@ void LuaScript::prepareResume(Thread *thread)
void LuaScript::push(int v)
{
assert(nbArgs >= 0);
- lua_pushinteger(mCurrentState, v);
+ ::push(mCurrentState, v);
++nbArgs;
}
void LuaScript::push(const std::string &v)
{
assert(nbArgs >= 0);
- lua_pushlstring(mCurrentState, v.c_str(), v.length());
+ ::push(mCurrentState, v);
++nbArgs;
}
void LuaScript::push(Entity *v)
{
assert(nbArgs >= 0);
- if (v)
- lua_pushlightuserdata(mCurrentState, v);
- else
- lua_pushnil(mCurrentState);
+ ::push(mCurrentState, v);
++nbArgs;
}
diff --git a/src/scripting/luautil.cpp b/src/scripting/luautil.cpp
index 261acd1..ea0e3dd 100644
--- a/src/scripting/luautil.cpp
+++ b/src/scripting/luautil.cpp
@@ -121,23 +121,6 @@ Script *getScript(lua_State *s)
valid in the map.
TODO: do it. */
-Entity *getBeing(lua_State *s, int p)
-{
- if (!lua_islightuserdata(s, p))
- return 0;
- return static_cast<Entity *>(lua_touserdata(s, p));
-}
-
-Entity *getCharacter(lua_State *s, int p)
-{
- if (!lua_islightuserdata(s, p))
- return 0;
- Entity *t = static_cast<Entity *>(lua_touserdata(s, p));
- if (t->getType() != OBJECT_CHARACTER)
- return 0;
- return static_cast<Entity *>(t);
-}
-
ItemClass *getItemClass(lua_State *s, int p)
{
ItemClass *itemClass = 0;
@@ -158,16 +141,6 @@ ItemClass *getItemClass(lua_State *s, int p)
return itemClass;
}
-Entity *getMonster(lua_State *s, int p)
-{
- if (!lua_islightuserdata(s, p))
- return 0;
- Entity *t = static_cast<Entity *>(lua_touserdata(s, p));
- if (t->getType() != OBJECT_MONSTER)
- return 0;
- return t;
-}
-
MonsterClass *getMonsterClass(lua_State *s, int p)
{
MonsterClass *monsterClass = 0;
@@ -188,29 +161,28 @@ MonsterClass *getMonsterClass(lua_State *s, int p)
return monsterClass;
}
-Entity *getNpc(lua_State *s, int p)
+
+Entity *checkActor(lua_State *s, int p)
{
- if (!lua_islightuserdata(s, p))
- return 0;
- Entity *t = static_cast<Entity *>(lua_touserdata(s, p));
- if (t->getType() != OBJECT_NPC)
- return 0;
- return t;
+ Entity *entity = LuaEntity::check(s, p);
+ luaL_argcheck(s, entity->hasComponent<ActorComponent>(), p,
+ "entity has no actor component");
+ return entity;
}
-
Entity *checkBeing(lua_State *s, int p)
{
- Entity *being = getBeing(s, p);
- luaL_argcheck(s, being, p, "being expected");
- return being;
+ Entity *entity = LuaEntity::check(s, p);
+ luaL_argcheck(s, entity->hasComponent<BeingComponent>(), p,
+ "entity has no being component");
+ return entity;
}
Entity *checkCharacter(lua_State *s, int p)
{
- Entity *character = getCharacter(s, p);
- luaL_argcheck(s, character, p, "character expected");
- return character;
+ Entity *entity = LuaEntity::check(s, p);
+ luaL_argcheck(s, entity->getType() == OBJECT_CHARACTER, p, "character expected");
+ return entity;
}
ItemClass *checkItemClass(lua_State *s, int p)
@@ -222,9 +194,9 @@ ItemClass *checkItemClass(lua_State *s, int p)
Entity *checkMonster(lua_State *s, int p)
{
- Entity *monster = getMonster(s, p);
- luaL_argcheck(s, monster, p, "monster expected");
- return monster;
+ Entity *entity = LuaEntity::check(s, p);
+ luaL_argcheck(s, entity->getType() == OBJECT_MONSTER, p, "monster expected");
+ return entity;
}
MonsterClass *checkMonsterClass(lua_State *s, int p)
@@ -236,9 +208,9 @@ MonsterClass *checkMonsterClass(lua_State *s, int p)
Entity *checkNpc(lua_State *s, int p)
{
- Entity *npc = getNpc(s, p);
- luaL_argcheck(s, npc, p, "npc expected");
- return npc;
+ Entity *entity = LuaEntity::check(s, p);
+ luaL_argcheck(s, entity->getType() == OBJECT_NPC, p, "npc expected");
+ return entity;
}
int checkSkill(lua_State *s, int p)
diff --git a/src/scripting/luautil.h b/src/scripting/luautil.h
index 1bc0293..6d078e3 100644
--- a/src/scripting/luautil.h
+++ b/src/scripting/luautil.h
@@ -156,6 +156,7 @@ template <typename T> const char * LuaUserData<T>::mTypeName;
typedef LuaUserData<AttackInfo> LuaAttackInfo;
typedef LuaUserData<Damage> LuaDamage;
+typedef LuaUserData<Entity> LuaEntity;
typedef LuaUserData<ItemClass> LuaItemClass;
typedef LuaUserData<MapObject> LuaMapObject;
typedef LuaUserData<MonsterClass> LuaMonsterClass;
@@ -164,13 +165,10 @@ typedef LuaUserData<SpecialManager::SpecialInfo> LuaSpecialInfo;
Script * getScript(lua_State *s);
-Entity * getBeing(lua_State *s, int p);
-Entity * getCharacter(lua_State *s, int p);
ItemClass * getItemClass(lua_State *s, int p);
-Entity * getMonster(lua_State *s, int p);
MonsterClass * getMonsterClass(lua_State *s, int p);
-Entity * getNpc(lua_State *s, int p);
+Entity * checkActor(lua_State *s, int p);
Entity * checkBeing(lua_State *s, int p);
Entity * checkCharacter(lua_State *s, int p);
ItemClass * checkItemClass(lua_State *s, int p);
@@ -199,7 +197,7 @@ inline void push(lua_State *s, const std::string &val)
inline void push(lua_State *s, Entity *val)
{
- lua_pushlightuserdata(s, val);
+ LuaEntity::push(s, val);
}
inline void push(lua_State *s, double val)