summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPhilipp Sehmisch <tmw@crushnet.org>2008-03-15 00:01:09 +0000
committerPhilipp Sehmisch <tmw@crushnet.org>2008-03-15 00:01:09 +0000
commit68c0625ee9a3a01090f0bc93612bf84dd71eaa67 (patch)
treed1cd93e73bb2e5a2746f8cbd2c1cfac2c5dcce39 /src
parent4736ec595c1ed46f8b076f89bc31660ae21e9231 (diff)
downloadmanaserv-68c0625ee9a3a01090f0bc93612bf84dd71eaa67.tar.gz
manaserv-68c0625ee9a3a01090f0bc93612bf84dd71eaa67.tar.bz2
manaserv-68c0625ee9a3a01090f0bc93612bf84dd71eaa67.tar.xz
manaserv-68c0625ee9a3a01090f0bc93612bf84dd71eaa67.zip
Implemented script binding for controllig movement of beings and fixed a crash caused by the new blocking system (thanks to peavey for reporting).
Diffstat (limited to 'src')
-rw-r--r--src/game-server/map.cpp13
-rw-r--r--src/game-server/movingobject.cpp15
-rw-r--r--src/game-server/object.hpp3
-rw-r--r--src/scripting/lua.cpp31
4 files changed, 55 insertions, 7 deletions
diff --git a/src/game-server/map.cpp b/src/game-server/map.cpp
index c4f2c02e..ceb48b04 100644
--- a/src/game-server/map.cpp
+++ b/src/game-server/map.cpp
@@ -98,9 +98,12 @@ Map::setSize(int width, int height)
void Map::blockTile(int x, int y, BlockType type)
{
- if (type == BLOCKTYPE_NONE) return;
+ if (type == BLOCKTYPE_NONE || x < 0 || y < 0 || x >= mWidth || y >= mHeight)
+ {
+ return;
+ }
+
int tileNum = x + y * mWidth;
- assert (tileNum <= mWidth * mHeight);
if (++mOccupation[type][tileNum])
{
@@ -124,10 +127,12 @@ void Map::blockTile(int x, int y, BlockType type)
void Map::freeTile(int x, int y, BlockType type)
{
- if (type == BLOCKTYPE_NONE) return;
+ if (type == BLOCKTYPE_NONE || x < 0 || y < 0 || x >= mWidth || y >= mHeight)
+ {
+ return;
+ }
int tileNum = x + y * mWidth;
- assert (tileNum <= mWidth * mHeight);
if (!(--mOccupation[type][tileNum]))
{
diff --git a/src/game-server/movingobject.cpp b/src/game-server/movingobject.cpp
index f7db2d93..70334341 100644
--- a/src/game-server/movingobject.cpp
+++ b/src/game-server/movingobject.cpp
@@ -20,6 +20,8 @@
* $Id$
*/
+#include <cassert>
+
#include "game-server/map.hpp"
#include "game-server/mapcomposite.hpp"
#include "game-server/movingobject.hpp"
@@ -43,14 +45,22 @@ void MovingObject::setPosition(const Point &p)
void MovingObject::setMap(MapComposite *map)
{
- Point p = getPosition();
+ assert (map);
MapComposite *oldMap = getMap();
+ Point p = getPosition();
+
if (oldMap)
{
oldMap->getMap()->freeTile(p.x / 32, p.y / 32, getBlockType());
}
- map->getMap()->blockTile(p.x / 32, p.y / 32, getBlockType());
Object::setMap(map);
+ map->getMap()->blockTile(p.x / 32, p.y / 32, getBlockType());
+ /* the last line might look illogical because the current position is
+ * invalid on the new map, but it is necessary to block the old position
+ * because the next call of setPosition() will automatically free the old
+ * position. When we don't block the position now the occupation counting
+ * will be off.
+ */
}
void MovingObject::setDestination(Point const &dst)
@@ -63,6 +73,7 @@ void MovingObject::setDestination(Point const &dst)
void MovingObject::move()
{
mOld = getPosition();
+
if (mActionTime > 100)
{
// Current move has not yet ended
diff --git a/src/game-server/object.hpp b/src/game-server/object.hpp
index f50dff4d..92980d02 100644
--- a/src/game-server/object.hpp
+++ b/src/game-server/object.hpp
@@ -51,7 +51,8 @@ class Object : public Thing
*/
Object(int type)
: Thing(type),
- mUpdateFlags(0)
+ mUpdateFlags(0),
+ mPos(Point(0, 0))
{}
/**
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index a5de9917..7437af83 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -106,6 +106,13 @@ static Character *getCharacter(lua_State *s, int p)
return static_cast<Character *>(t);
}
+static Being *getBeing(lua_State *s, int p)
+{
+ if (!lua_islightuserdata(s, p)) return NULL;
+ Thing *t = static_cast<Thing *>(lua_touserdata(s, p));
+ return static_cast<Being *>(t);
+}
+
/**
* Callback for sending a NPC_MESSAGE.
* tmw.npc_message(npc, character, string)
@@ -370,6 +377,29 @@ static int LuaNpc_Trade(lua_State *s)
}
/**
+ * Function for making a being walk to a position
+ * being_walk(Being *being, int x, int y, int speed)
+ */
+static int LuaBeing_Walk(lua_State *s)
+{
+ if (!lua_isnumber(s, 2) || !lua_isnumber(s, 3) || !lua_isnumber(s, 4))
+ {
+ LOG_WARN("LuaBeing_walk called with incorrect parameters.");
+ return 0;
+ }
+
+ lua_pushlightuserdata(s, (void *)&registryKey);
+ lua_gettable(s, LUA_REGISTRYINDEX);
+
+ Being *being = getBeing(s, 1);
+ Point destination(lua_tointeger(s, 2), lua_tointeger(s, 3));
+ being->setDestination(destination);
+ being->setSpeed(lua_tointeger(s, 4));
+
+ return 0;
+}
+
+/**
* Callback for creating a monster on the current map.
* tmw.monster_create(int type, int x, int y)
*/
@@ -493,6 +523,7 @@ LuaScript::LuaScript():
{ "chr_get_quest", &LuaChr_GetQuest },
{ "chr_set_quest", &LuaChr_SetQuest },
{ "monster_create", &LuaMonster_Create },
+ { "being_walk", &LuaBeing_Walk },
{ NULL, NULL }
};
luaL_register(mState, "tmw", callbacks);