diff options
Diffstat (limited to 'src/clientobject.cpp')
-rw-r--r-- | src/clientobject.cpp | 874 |
1 files changed, 469 insertions, 405 deletions
diff --git a/src/clientobject.cpp b/src/clientobject.cpp index bbe108e15..402535ffc 100644 --- a/src/clientobject.cpp +++ b/src/clientobject.cpp @@ -22,6 +22,14 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" #include "constants.h" #include "utility.h" +#include "environment.h" +#include "tile.h" + +/* + ClientActiveObject +*/ + +core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types; ClientActiveObject::ClientActiveObject(u16 id): ActiveObject(id) @@ -35,47 +43,55 @@ ClientActiveObject::~ClientActiveObject() ClientActiveObject* ClientActiveObject::create(u8 type) { - if(type == ACTIVEOBJECT_TYPE_INVALID) + // Find factory function + core::map<u16, Factory>::Node *n; + n = m_types.find(type); + if(n == NULL) { - dstream<<"ClientActiveObject::create(): passed " - <<"ACTIVEOBJECT_TYPE_INVALID"<<std::endl; - return NULL; - } - else if(type == ACTIVEOBJECT_TYPE_TEST) - { - dstream<<"ClientActiveObject::create(): passed " - <<"ACTIVEOBJECT_TYPE_TEST"<<std::endl; - return new TestCAO(0); - } - else if(type == ACTIVEOBJECT_TYPE_LUA) - { - dstream<<"ClientActiveObject::create(): passed " - <<"ACTIVEOBJECT_TYPE_LUA"<<std::endl; - return new LuaCAO(0); - } - else - { - dstream<<"ClientActiveObject::create(): passed " - <<"unknown type="<<type<<std::endl; + // If factory is not found, just return. + dstream<<"WARNING: ClientActiveObject: No factory for type=" + <<type<<std::endl; return NULL; } + + Factory f = n->getValue(); + ClientActiveObject *object = (*f)(); + return object; +} + +void ClientActiveObject::registerType(u16 type, Factory f) +{ + core::map<u16, Factory>::Node *n; + n = m_types.find(type); + if(n) + return; + m_types.insert(type, f); } /* TestCAO */ -TestCAO::TestCAO(u16 id): - ClientActiveObject(id), +// Prototype +TestCAO proto_TestCAO; + +TestCAO::TestCAO(): + ClientActiveObject(0), m_node(NULL), m_position(v3f(0,10*BS,0)) { + ClientActiveObject::registerType(getType(), create); } TestCAO::~TestCAO() { } +ClientActiveObject* TestCAO::create() +{ + return new TestCAO(); +} + void TestCAO::addToScene(scene::ISceneManager *smgr) { if(m_node != NULL) @@ -99,7 +115,7 @@ void TestCAO::addToScene(scene::ISceneManager *smgr) buf->getMaterial().setFlag(video::EMF_LIGHTING, false); buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false); buf->getMaterial().setTexture - (0, driver->getTexture(porting::getDataPath("rat.png").c_str())); + (0, driver->getTexture(getTexturePath("rat.png").c_str())); buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true); buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; @@ -138,7 +154,7 @@ void TestCAO::updateNodePos() //m_node->setRotation(v3f(0, 45, 0)); } -void TestCAO::step(float dtime) +void TestCAO::step(float dtime, ClientEnvironment *env) { if(m_node) { @@ -167,511 +183,559 @@ void TestCAO::processMessage(const std::string &data) } /* - LuaCAO + ItemCAO */ -extern "C"{ -#include "lstring.h" +#include "inventory.h" + +// Prototype +ItemCAO proto_ItemCAO; + +ItemCAO::ItemCAO(): + ClientActiveObject(0), + m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.), + m_node(NULL), + m_position(v3f(0,10*BS,0)) +{ + ClientActiveObject::registerType(getType(), create); } -/* - Callbacks in script: - - on_step(self, dtime) - on_process_message(self, data) - on_initialize(self, data) - TODO: - string on_get_info_text(self) - on_block_removed_near({X=,Y=,Z=}, node) - on_block_placed_near({X=,Y=,Z=}, node) -*/ +ItemCAO::~ItemCAO() +{ +} -/* - object_set_position(self, p) -*/ -static int lf_object_set_position(lua_State *L) -{ - // 2: position - assert(lua_istable(L, -1)); - lua_pushstring(L, "X"); - lua_gettable(L, -2); - lua_Number x = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pushstring(L, "Y"); - lua_gettable(L, -2); - lua_Number y = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pushstring(L, "Z"); - lua_gettable(L, -2); - lua_Number z = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pop(L, 1); - // 1: self - LuaCAO *self = (LuaCAO*)lua_touserdata(L, -1); - lua_pop(L, 1); - - assert(self); - - self->setPosition(v3f(x*BS,y*BS,z*BS)); - - return 0; // Number of return values +ClientActiveObject* ItemCAO::create() +{ + return new ItemCAO(); } -/* - object_set_rotation(self, p) -*/ -static int lf_object_set_rotation(lua_State *L) -{ - // 2: position - assert(lua_istable(L, -1)); - lua_pushstring(L, "X"); - lua_gettable(L, -2); - lua_Number x = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pushstring(L, "Y"); - lua_gettable(L, -2); - lua_Number y = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pushstring(L, "Z"); - lua_gettable(L, -2); - lua_Number z = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pop(L, 1); - // 1: self - LuaCAO *self = (LuaCAO*)lua_touserdata(L, -1); - lua_pop(L, 1); - - assert(self); +void ItemCAO::addToScene(scene::ISceneManager *smgr) +{ + if(m_node != NULL) + return; - self->setRotation(v3f(x,y,z)); + video::IVideoDriver* driver = smgr->getVideoDriver(); - return 0; // Number of return values + scene::SMesh *mesh = new scene::SMesh(); + scene::IMeshBuffer *buf = new scene::SMeshBuffer(); + video::SColor c(255,255,255,255); + video::S3DVertex vertices[4] = + { + /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1), + video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1), + video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0), + video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/ + video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1), + video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1), + video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0), + video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0), + }; + u16 indices[] = {0,1,2,2,3,0}; + buf->append(vertices, 4, indices, 6); + // Set material + buf->getMaterial().setFlag(video::EMF_LIGHTING, false); + buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false); + //buf->getMaterial().setTexture(0, NULL); + // Initialize with the stick texture + buf->getMaterial().setTexture + (0, driver->getTexture(getTexturePath("stick.png").c_str())); + buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); + buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true); + buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; + // Add to mesh + mesh->addMeshBuffer(buf); + buf->drop(); + m_node = smgr->addMeshSceneNode(mesh, NULL); + mesh->drop(); + // Set it to use the materials of the meshbuffers directly. + // This is needed for changing the texture in the future + m_node->setReadOnlyMaterials(true); + updateNodePos(); } -/* - object_add_to_mesh(self, image, corners, backface_culling) - corners is an array like this: - {{x,y,z},{x,y,z},{x,y,z},{x,y,z}} -*/ -static int lf_object_add_to_mesh(lua_State *L) +void ItemCAO::removeFromScene() { - // 4: backface_culling - bool backface_culling = lua_toboolean(L, -1); - lua_pop(L, 1); - // 3: corners - if(lua_istable(L, -1) == false) - { - dstream<<"ERROR: object_add_to_mesh(): parameter 3 not a table" - <<std::endl; - return 0; - } - v3f corners[4]; - // Loop table - for(int i=0; i<4; i++) + if(m_node == NULL) + return; + + m_node->remove(); + m_node = NULL; +} + +void ItemCAO::updateLight(u8 light_at_pos) +{ + if(m_node == NULL) + return; + + u8 li = decode_light(light_at_pos); + video::SColor color(255,li,li,li); + + scene::IMesh *mesh = m_node->getMesh(); + if(mesh == NULL) + return; + + u16 mc = mesh->getMeshBufferCount(); + for(u16 j=0; j<mc; j++) { - // Get child table - lua_pushinteger(L, i+1); - lua_gettable(L, -2); - if(lua_istable(L, -1) == false) + scene::IMeshBuffer *buf = mesh->getMeshBuffer(j); + video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); + u16 vc = buf->getVertexCount(); + for(u16 i=0; i<vc; i++) { - dstream<<"ERROR: object_add_to_mesh(): parameter 3 not a" - " table of tables"<<std::endl; - return 0; + vertices[i].Color = color; } - - // Get x, y and z from the child table - - lua_pushinteger(L, 1); - lua_gettable(L, -2); - corners[i].X = lua_tonumber(L, -1) * BS; - lua_pop(L, 1); - - lua_pushinteger(L, 2); - lua_gettable(L, -2); - corners[i].Y = lua_tonumber(L, -1) * BS; - lua_pop(L, 1); - - lua_pushinteger(L, 3); - lua_gettable(L, -2); - corners[i].Z = lua_tonumber(L, -1) * BS; - lua_pop(L, 1); - - // Pop child table - lua_pop(L, 1); } - lua_pop(L, 1); - // 2: image - const char *image = lua_tostring(L, -1); - lua_pop(L, 1); - // 1: self - LuaCAO *self = (LuaCAO*)lua_touserdata(L, -1); - lua_pop(L, 1); - - assert(self); - - self->addToMesh(image, corners, backface_culling); - - return 0; // Number of return values } -/* - object_clear_mesh(self) -*/ -static int lf_object_clear_mesh(lua_State *L) +v3s16 ItemCAO::getLightPosition() { - // 1: self - LuaCAO *self = (LuaCAO*)lua_touserdata(L, -1); - lua_pop(L, 1); - - assert(self); + return floatToInt(m_position, BS); +} - self->clearMesh(); +void ItemCAO::updateNodePos() +{ + if(m_node == NULL) + return; - return 0; + m_node->setPosition(m_position); } -LuaCAO::LuaCAO(u16 id): - ClientActiveObject(id), - L(NULL), - m_smgr(NULL), - m_node(NULL), - m_mesh(NULL), - m_position(v3f(0,10*BS,0)) +void ItemCAO::step(float dtime, ClientEnvironment *env) { - dstream<<"LuaCAO::LuaCAO(): id="<<id<<std::endl; - L = lua_open(); - assert(L); - - // Load libraries - luaopen_base(L); - luaopen_table(L); - luaopen_string(L); - luaopen_math(L); - - // Disable some stuff - const char *to_disable[] = { - "arg", - "debug", - "dofile", - "io", - "loadfile", - "os", - "package", - "require", - NULL - }; - const char **td = to_disable; - do{ - lua_pushnil(L); - lua_setglobal(L, *td); - }while(*(++td)); - - // Add globals - //lua_pushlightuserdata(L, this); - //lua_setglobal(L, "self"); - - // Register functions - lua_register(L, "object_set_position", lf_object_set_position); - lua_register(L, "object_set_rotation", lf_object_set_rotation); - lua_register(L, "object_add_to_mesh", lf_object_add_to_mesh); - lua_register(L, "object_clear_mesh", lf_object_clear_mesh); + if(m_node) + { + /*v3f rot = m_node->getRotation(); + rot.Y += dtime * 120; + m_node->setRotation(rot);*/ + LocalPlayer *player = env->getLocalPlayer(); + assert(player); + v3f rot = m_node->getRotation(); + rot.Y = 180.0 - (player->getYaw()); + m_node->setRotation(rot); + } } -LuaCAO::~LuaCAO() +void ItemCAO::processMessage(const std::string &data) { - lua_close(L); + dstream<<"ItemCAO: Got message"<<std::endl; + std::istringstream is(data, std::ios::binary); + // command + u8 cmd = readU8(is); + if(cmd == 0) + { + // pos + m_position = readV3F1000(is); + updateNodePos(); + } } -void LuaCAO::step(float dtime) +void ItemCAO::initialize(const std::string &data) { - /* - Call step(self, dtime) from lua - */ + dstream<<"ItemCAO: Got init data"<<std::endl; - const char *funcname = "on_step"; - lua_getglobal(L, funcname); - if(!lua_isfunction(L,-1)) { - lua_pop(L,1); - dstream<<"WARNING: LuaCAO: Function not found: " - <<funcname<<std::endl; - return; + std::istringstream is(data, std::ios::binary); + // version + u8 version = readU8(is); + // check version + if(version != 0) + return; + // pos + m_position = readV3F1000(is); + // inventorystring + m_inventorystring = deSerializeString(is); } - // Parameters: - // 1: self - lua_pushlightuserdata(L, this); - // 2: dtime - lua_pushnumber(L, dtime); - - // Call (2 parameters, 0 result) - if(lua_pcall(L, 2, 0, 0)) - { - dstream<<"WARNING: LuaCAO: Error running function " - <<funcname<<": " - <<lua_tostring(L,-1)<<std::endl; - return; - } -} + updateNodePos(); -void LuaCAO::processMessage(const std::string &data) -{ /* - Call process_message(self, data) from lua + Update image of node */ + + if(m_node == NULL) + return; + + scene::IMesh *mesh = m_node->getMesh(); + + if(mesh == NULL) + return; - const char *funcname = "on_process_message"; - lua_getglobal(L, funcname); - if(!lua_isfunction(L,-1)) - { - lua_pop(L,1); - dstream<<"WARNING: LuaCAO: Function not found: " - <<funcname<<std::endl; + scene::IMeshBuffer *buf = mesh->getMeshBuffer(0); + + if(buf == NULL) return; + + // Create an inventory item to see what is its image + std::istringstream is(m_inventorystring, std::ios_base::binary); + video::ITexture *texture = NULL; + try{ + InventoryItem *item = NULL; + item = InventoryItem::deSerialize(is); + dstream<<__FUNCTION_NAME<<": m_inventorystring=\"" + <<m_inventorystring<<"\" -> item="<<item + <<std::endl; + if(item) + { + texture = item->getImage(); + delete item; + } } - - // Parameters: - // 1: self - lua_pushlightuserdata(L, this); - // 2: data - lua_pushlstring(L, data.c_str(), data.size()); - - // Call (2 parameters, 0 results) - if(lua_pcall(L, 2, 1, 0)) + catch(SerializationError &e) { - dstream<<"WARNING: LuaCAO: Error running function " - <<funcname<<": " - <<lua_tostring(L,-1)<<std::endl; - return; + dstream<<"WARNING: "<<__FUNCTION_NAME + <<": error deSerializing inventorystring \"" + <<m_inventorystring<<"\""<<std::endl; } + + // Set meshbuffer texture + buf->getMaterial().setTexture(0, texture); + } -void LuaCAO::initialize(const std::string &data) +/* + RatCAO +*/ + +#include "inventory.h" + +// Prototype +RatCAO proto_RatCAO; + +RatCAO::RatCAO(): + ClientActiveObject(0), + m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS/2.,BS/3.), + m_node(NULL), + m_position(v3f(0,10*BS,0)), + m_yaw(0) { - dstream<<"LuaCAO::initialize(): id="<<getId()<<std::endl; + ClientActiveObject::registerType(getType(), create); +} - std::istringstream is(data, std::ios::binary); - std::string script = deSerializeLongString(is); - std::string other = deSerializeLongString(is); +RatCAO::~RatCAO() +{ +} - /*dstream<<"=== script (size="<<script.size()<<")"<<std::endl - <<script<<std::endl - <<"==="<<std::endl;*/ - dstream<<"LuaCAO::initialize(): script size="<<script.size()<<std::endl; - - /*dstream<<"other.size()="<<other.size()<<std::endl; - dstream<<"other=\""<<other<<"\""<<std::endl;*/ - - // Load the script to lua - loadScript(script); +ClientActiveObject* RatCAO::create() +{ + return new RatCAO(); +} - /* - Call initialize(self, data) in the script - */ - - const char *funcname = "on_initialize"; - lua_getglobal(L, funcname); - if(!lua_isfunction(L,-1)) - { - lua_pop(L,1); - dstream<<"WARNING: LuaCAO: Function not found: " - <<funcname<<std::endl; +void RatCAO::addToScene(scene::ISceneManager *smgr) +{ + if(m_node != NULL) return; - } - // Parameters: - // 1: self - lua_pushlightuserdata(L, this); - // 2: data (other) - lua_pushlstring(L, other.c_str(), other.size()); + video::IVideoDriver* driver = smgr->getVideoDriver(); - // Call (2 parameters, 0 result) - if(lua_pcall(L, 2, 0, 0)) + scene::SMesh *mesh = new scene::SMesh(); + scene::IMeshBuffer *buf = new scene::SMeshBuffer(); + video::SColor c(255,255,255,255); + video::S3DVertex vertices[4] = { - dstream<<"WARNING: LuaCAO: Error running function " - <<funcname<<": " - <<lua_tostring(L,-1)<<std::endl; + video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1), + video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1), + video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0), + video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0), + }; + u16 indices[] = {0,1,2,2,3,0}; + buf->append(vertices, 4, indices, 6); + // Set material + buf->getMaterial().setFlag(video::EMF_LIGHTING, false); + buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false); + //buf->getMaterial().setTexture(0, NULL); + buf->getMaterial().setTexture + (0, driver->getTexture(getTexturePath("rat.png").c_str())); + buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); + buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true); + buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; + // Add to mesh + mesh->addMeshBuffer(buf); + buf->drop(); + m_node = smgr->addMeshSceneNode(mesh, NULL); + mesh->drop(); + // Set it to use the materials of the meshbuffers directly. + // This is needed for changing the texture in the future + m_node->setReadOnlyMaterials(true); + updateNodePos(); +} + +void RatCAO::removeFromScene() +{ + if(m_node == NULL) return; - } + m_node->remove(); + m_node = NULL; } -void LuaCAO::loadScript(const std::string script) +void RatCAO::updateLight(u8 light_at_pos) { - int ret; - ret = luaL_loadstring(L, script.c_str()); - if(ret) - { - const char *message = lua_tostring(L, -1); - lua_pop(L, 1); - dstream<<"LuaCAO::loadScript(): lua_loadstring failed: " - <<message<<std::endl; - assert(0); + if(m_node == NULL) return; - } - ret = lua_pcall(L, 0, 0, 0); - if(ret) - { - const char *message = lua_tostring(L, -1); - lua_pop(L, 1); - dstream<<"LuaCAO::loadScript(): lua_pcall failed: " - <<message<<std::endl; - assert(0); + + u8 li = decode_light(light_at_pos); + video::SColor color(255,li,li,li); + + scene::IMesh *mesh = m_node->getMesh(); + if(mesh == NULL) return; + + u16 mc = mesh->getMeshBufferCount(); + for(u16 j=0; j<mc; j++) + { + scene::IMeshBuffer *buf = mesh->getMeshBuffer(j); + video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); + u16 vc = buf->getVertexCount(); + for(u16 i=0; i<vc; i++) + { + vertices[i].Color = color; + } } } -void LuaCAO::addToScene(scene::ISceneManager *smgr) +v3s16 RatCAO::getLightPosition() { - if(m_smgr != NULL) - { - dstream<<"WARNING: LuaCAO::addToScene() called more than once" - <<std::endl; + return floatToInt(m_position+v3f(0,BS*0.5,0), BS); +} + +void RatCAO::updateNodePos() +{ + if(m_node == NULL) return; - } - if(m_node != NULL) + //m_node->setPosition(m_position); + m_node->setPosition(pos_translator.vect_show); + + v3f rot = m_node->getRotation(); + rot.Y = 180.0 - m_yaw; + m_node->setRotation(rot); +} + +void RatCAO::step(float dtime, ClientEnvironment *env) +{ + pos_translator.translate(dtime); + updateNodePos(); +} + +void RatCAO::processMessage(const std::string &data) +{ + //dstream<<"RatCAO: Got message"<<std::endl; + std::istringstream is(data, std::ios::binary); + // command + u8 cmd = readU8(is); + if(cmd == 0) { - dstream<<"WARNING: LuaCAO::addToScene(): m_node != NULL" - <<std::endl; - return; + // pos + m_position = readV3F1000(is); + pos_translator.update(m_position); + // yaw + m_yaw = readF1000(is); + updateNodePos(); } +} + +void RatCAO::initialize(const std::string &data) +{ + //dstream<<"RatCAO: Got init data"<<std::endl; - m_smgr = smgr; - - if(m_mesh == NULL) - { - m_mesh = new scene::SMesh(); - m_node = smgr->addMeshSceneNode(m_mesh, NULL); - - /*v3f corners[4] = { - v3f(-BS/2,-BS/4,0), - v3f(BS/2,-BS/4,0), - v3f(BS/2,BS/4,0), - v3f(-BS/2,BS/4,0), - }; - addToMesh("rat.png", corners, false);*/ - } - else { - dstream<<"WARNING: LuaCAO::addToScene(): m_mesh != NULL" - <<std::endl; - return; + std::istringstream is(data, std::ios::binary); + // version + u8 version = readU8(is); + // check version + if(version != 0) + return; + // pos + m_position = readV3F1000(is); + pos_translator.init(m_position); } updateNodePos(); } -void LuaCAO::addToMesh(const char *image, v3f *corners, bool backface_culling) +/* + Oerkki1CAO +*/ + +#include "inventory.h" + +// Prototype +Oerkki1CAO proto_Oerkki1CAO; + +Oerkki1CAO::Oerkki1CAO(): + ClientActiveObject(0), + m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.,BS/3.), + m_node(NULL), + m_position(v3f(0,10*BS,0)), + m_yaw(0) { - dstream<<"INFO: addToMesh called"<<std::endl; + ClientActiveObject::registerType(getType(), create); +} - video::IVideoDriver* driver = m_smgr->getVideoDriver(); +Oerkki1CAO::~Oerkki1CAO() +{ +} + +ClientActiveObject* Oerkki1CAO::create() +{ + return new Oerkki1CAO(); +} + +void Oerkki1CAO::addToScene(scene::ISceneManager *smgr) +{ + if(m_node != NULL) + return; + + video::IVideoDriver* driver = smgr->getVideoDriver(); - assert(m_mesh); - + scene::SMesh *mesh = new scene::SMesh(); scene::IMeshBuffer *buf = new scene::SMeshBuffer(); video::SColor c(255,255,255,255); video::S3DVertex vertices[4] = { - video::S3DVertex(corners[0], v3f(0,0,0), c, v2f(0,1)), - video::S3DVertex(corners[1], v3f(0,0,0), c, v2f(1,1)), - video::S3DVertex(corners[2], v3f(0,0,0), c, v2f(1,0)), - video::S3DVertex(corners[3], v3f(0,0,0), c, v2f(0,0)), - /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1), - video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1), - video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0), - video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/ + video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1), + video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1), + video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0), + video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0), }; u16 indices[] = {0,1,2,2,3,0}; buf->append(vertices, 4, indices, 6); // Set material buf->getMaterial().setFlag(video::EMF_LIGHTING, false); - buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, backface_culling); + buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false); + //buf->getMaterial().setTexture(0, NULL); buf->getMaterial().setTexture - (0, driver->getTexture(porting::getDataPath(image).c_str())); + (0, driver->getTexture(getTexturePath("oerkki1.png").c_str())); buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true); buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; // Add to mesh - m_mesh->addMeshBuffer(buf); + mesh->addMeshBuffer(buf); buf->drop(); - // Reset mesh - if(m_node) - m_node->setMesh(m_mesh); + m_node = smgr->addMeshSceneNode(mesh, NULL); + mesh->drop(); + // Set it to use the materials of the meshbuffers directly. + // This is needed for changing the texture in the future + m_node->setReadOnlyMaterials(true); + updateNodePos(); } -void LuaCAO::clearMesh() +void Oerkki1CAO::removeFromScene() { - if(m_node) - { - m_node->setMesh(NULL); - } - - if(m_mesh) - { - m_mesh->drop(); - m_mesh = NULL; - } + if(m_node == NULL) + return; + + m_node->remove(); + m_node = NULL; } -void LuaCAO::removeFromScene() +void Oerkki1CAO::updateLight(u8 light_at_pos) { if(m_node == NULL) return; - if(m_node) - { - m_node->remove(); - m_node = NULL; - } - if(m_mesh) + if(light_at_pos <= 2) { - m_mesh->drop(); - m_mesh = NULL; + m_node->setVisible(false); + return; } - m_smgr = NULL; -} + m_node->setVisible(true); -void LuaCAO::updateLight(u8 light_at_pos) -{ + u8 li = decode_light(light_at_pos); + video::SColor color(255,li,li,li); + + scene::IMesh *mesh = m_node->getMesh(); + if(mesh == NULL) + return; + + u16 mc = mesh->getMeshBufferCount(); + for(u16 j=0; j<mc; j++) + { + scene::IMeshBuffer *buf = mesh->getMeshBuffer(j); + video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); + u16 vc = buf->getVertexCount(); + for(u16 i=0; i<vc; i++) + { + vertices[i].Color = color; + } + } } -v3s16 LuaCAO::getLightPosition() +v3s16 Oerkki1CAO::getLightPosition() { - return floatToInt(m_position, BS); + return floatToInt(m_position+v3f(0,BS*1.5,0), BS); } -void LuaCAO::updateNodePos() +void Oerkki1CAO::updateNodePos() { if(m_node == NULL) return; - m_node->setPosition(m_position); - m_node->setRotation(m_rotation); + //m_node->setPosition(m_position); + m_node->setPosition(pos_translator.vect_show); + + v3f rot = m_node->getRotation(); + rot.Y = 180.0 - m_yaw + 90.0; + m_node->setRotation(rot); } -void LuaCAO::setPosition(v3f pos) +void Oerkki1CAO::step(float dtime, ClientEnvironment *env) { - m_position = pos; + pos_translator.translate(dtime); updateNodePos(); -} -v3f LuaCAO::getPosition() -{ - return m_position; + LocalPlayer *player = env->getLocalPlayer(); + assert(player); + + v3f playerpos = player->getPosition(); + v2f playerpos_2d(playerpos.X,playerpos.Z); + v2f objectpos_2d(m_position.X,m_position.Z); + + if(fabs(m_position.Y - playerpos.Y) < 3.0*BS && + objectpos_2d.getDistanceFrom(playerpos_2d) < 1.0*BS) + { + if(m_attack_interval.step(dtime, 0.5)) + { + env->damageLocalPlayer(2); + } + } } -void LuaCAO::setRotation(v3f rot) +void Oerkki1CAO::processMessage(const std::string &data) { - m_rotation = rot; - updateNodePos(); + //dstream<<"Oerkki1CAO: Got message"<<std::endl; + std::istringstream is(data, std::ios::binary); + // command + u8 cmd = readU8(is); + if(cmd == 0) + { + // pos + m_position = readV3F1000(is); + pos_translator.update(m_position); + // yaw + m_yaw = readF1000(is); + updateNodePos(); + } } -v3f LuaCAO::getRotation() +void Oerkki1CAO::initialize(const std::string &data) { - return m_rotation; + //dstream<<"Oerkki1CAO: Got init data"<<std::endl; + + { + std::istringstream is(data, std::ios::binary); + // version + u8 version = readU8(is); + // check version + if(version != 0) + return; + // pos + m_position = readV3F1000(is); + pos_translator.init(m_position); + } + + updateNodePos(); } |