- m_anim_timer += dtime;
- if(m_anim_timer >= m_anim_framelength){
- m_anim_timer -= m_anim_framelength;
- m_anim_frame++;
- if(m_anim_frame >= m_anim_num_frames)
- m_anim_frame = 0;
- }
-
- updateTexturePos();
-
- if(m_reset_textures_timer >= 0){
- m_reset_textures_timer -= dtime;
- if(m_reset_textures_timer <= 0){
- m_reset_textures_timer = -1;
- updateTextures("");
- }
- }
- }
-
- void updateTexturePos()
- {
- if(m_spritenode){
- scene::ICameraSceneNode* camera =
- m_spritenode->getSceneManager()->getActiveCamera();
- if(!camera)
- return;
- v3f cam_to_entity = m_spritenode->getAbsolutePosition()
- - camera->getAbsolutePosition();
- cam_to_entity.normalize();
-
- int row = m_tx_basepos.Y;
- int col = m_tx_basepos.X;
-
- if(m_tx_select_horiz_by_yawpitch)
- {
- if(cam_to_entity.Y > 0.75)
- col += 5;
- else if(cam_to_entity.Y < -0.75)
- col += 4;
- else{
- float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / PI * 180.;
- float dir = mob_dir - m_yaw;
- dir = wrapDegrees_180(dir);
- //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
- if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
- col += 2;
- else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
- col += 3;
- else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
- col += 0;
- else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
- col += 1;
- else
- col += 4;
- }
- }
-
- // Animation goes downwards
- row += m_anim_frame;
-
- float txs = m_tx_size.X;
- float tys = m_tx_size.Y;
- setBillboardTextureMatrix(m_spritenode,
- txs, tys, col, row);
- }
- }
-
- void updateTextures(const std::string &mod)
- {
- ITextureSource *tsrc = m_gamedef->tsrc();
-
- if(m_spritenode){
- std::string texturestring = "unknown_block.png";
- if(m_prop->textures.size() >= 1)
- texturestring = m_prop->textures[0];
- texturestring += mod;
- m_spritenode->setMaterialTexture(0,
- tsrc->getTextureRaw(texturestring));
- }
- if(m_meshnode){
- for (u32 i = 0; i < 6; ++i)
- {
- std::string texturestring = "unknown_block.png";
- if(m_prop->textures.size() > i)
- texturestring = m_prop->textures[i];
- texturestring += mod;
- AtlasPointer ap = tsrc->getTexture(texturestring);
-
- // Get the tile texture and atlas transformation
- video::ITexture* atlas = ap.atlas;
- v2f pos = ap.pos;
- v2f size = ap.size;
-
- // Set material flags and texture
- video::SMaterial& material = m_meshnode->getMaterial(i);
- material.setFlag(video::EMF_LIGHTING, false);
- material.setFlag(video::EMF_BILINEAR_FILTER, false);
- material.setTexture(0, atlas);
- material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
- material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
- }
- }
- }
-
- void processMessage(const std::string &data)
- {
- //infostream<<"LuaEntityCAO: Got message"<<std::endl;
- std::istringstream is(data, std::ios::binary);
- // command
- u8 cmd = readU8(is);
- if(cmd == LUAENTITY_CMD_UPDATE_POSITION) // update position
- {
- // do_interpolate
- bool do_interpolate = readU8(is);
- // pos
- m_position = readV3F1000(is);
- // velocity
- m_velocity = readV3F1000(is);
- // acceleration
- m_acceleration = readV3F1000(is);
- // yaw
- m_yaw = readF1000(is);
- // is_end_position (for interpolation)
- bool is_end_position = readU8(is);
- // update_interval
- float update_interval = readF1000(is);
-
- if(do_interpolate){
- if(!m_prop->physical)
- pos_translator.update(m_position, is_end_position, update_interval);
- } else {
- pos_translator.init(m_position);
- }
- updateNodePos();
- }
- else if(cmd == LUAENTITY_CMD_SET_TEXTURE_MOD) // set texture modification
- {
- std::string mod = deSerializeString(is);
- updateTextures(mod);
- }
- else if(cmd == LUAENTITY_CMD_SET_SPRITE) // set sprite
- {
- v2s16 p = readV2S16(is);
- int num_frames = readU16(is);
- float framelength = readF1000(is);
- bool select_horiz_by_yawpitch = readU8(is);
-
- m_tx_basepos = p;
- m_anim_num_frames = num_frames;
- m_anim_framelength = framelength;
- m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
-
- updateTexturePos();
- }
- else if(cmd == LUAENTITY_CMD_PUNCHED)
- {
- /*s16 damage =*/ readS16(is);
- s16 result_hp = readS16(is);
-
- m_hp = result_hp;
- // TODO: Execute defined fast response
- }
- else if(cmd == LUAENTITY_CMD_UPDATE_ARMOR_GROUPS)
- {
- m_armor_groups.clear();
- int armor_groups_size = readU16(is);
- for(int i=0; i<armor_groups_size; i++){
- std::string name = deSerializeString(is);
- int rating = readS16(is);
- m_armor_groups[name] = rating;
- }
- }
- }
-
- bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
- float time_from_last_punch=1000000)
- {
- assert(punchitem);
- const ToolCapabilities *toolcap =
- &punchitem->getToolCapabilities(m_gamedef->idef());
- PunchDamageResult result = getPunchDamage(
- m_armor_groups,
- toolcap,
- punchitem,
- time_from_last_punch);
-
- if(result.did_punch && result.damage != 0)
- {
- if(result.damage < m_hp){
- m_hp -= result.damage;
- } else {
- m_hp = 0;
- // TODO: Execute defined fast response
- // As there is no definition, make a smoke puff
- ClientSimpleObject *simple = createSmokePuff(
- m_smgr, m_env, m_position,
- m_prop->visual_size * BS);
- m_env->addSimpleObject(simple);
- }
- // TODO: Execute defined fast response
- // Flashing shall suffice as there is no definition
- updateTextures("^[brighten");
- m_reset_textures_timer = 0.1;
- }
-
- return false;
- }
-
- std::string debugInfoText()
- {
- std::ostringstream os(std::ios::binary);
- os<<"LuaEntityCAO hp="<<m_hp<<"\n";
- os<<"armor={";
- for(ItemGroupList::const_iterator i = m_armor_groups.begin();
- i != m_armor_groups.end(); i++){
- os<<i->first<<"="<<i->second<<", ";
- }
- os<<"}";
- return os.str();
- }
-};
-
-// Prototype
-LuaEntityCAO proto_LuaEntityCAO(NULL, NULL);
-
-/*
- PlayerCAO
-*/
-
-class PlayerCAO : public ClientActiveObject
-{
-private:
- core::aabbox3d<f32> m_selection_box;
- scene::IMeshSceneNode *m_node;
- scene::ITextSceneNode* m_text;
- std::string m_name;
- v3f m_position;
- float m_yaw;
- SmoothTranslator pos_translator;
- bool m_is_local_player;
- LocalPlayer *m_local_player;
- float m_damage_visual_timer;
- bool m_dead;
- float m_step_distance_counter;
- std::string m_wielded_item;
-
-public:
- PlayerCAO(IGameDef *gamedef, ClientEnvironment *env):
- ClientActiveObject(0, gamedef, env),
- m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.0,BS/3.),
- m_node(NULL),
- m_text(NULL),
- m_position(v3f(0,10*BS,0)),
- m_yaw(0),
- m_is_local_player(false),
- m_local_player(NULL),
- m_damage_visual_timer(0),
- m_dead(false),
- m_step_distance_counter(0),
- m_wielded_item("")
- {
- if(gamedef == NULL)
- ClientActiveObject::registerType(getType(), create);
- }
-
- void initialize(const std::string &data)
- {
- infostream<<"PlayerCAO: Got init data"<<std::endl;
-
- std::istringstream is(data, std::ios::binary);
- // version
- u8 version = readU8(is);
- // check version
- if(version != 0)
- return;
- // name
- m_name = deSerializeString(is);
- // pos
- m_position = readV3F1000(is);
- // yaw
- m_yaw = readF1000(is);
- // dead
- m_dead = readU8(is);
- // wielded item
- try{
- m_wielded_item = deSerializeString(is);
- }
- catch(SerializationError &e){}
-
- pos_translator.init(m_position);
-
- Player *player = m_env->getPlayer(m_name.c_str());
- if(player && player->isLocal()){
- m_is_local_player = true;
- m_local_player = (LocalPlayer*)player;
- }
- }
-
- ~PlayerCAO()
- {
- if(m_node)
- m_node->remove();
- }
-
- static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
- {
- return new PlayerCAO(gamedef, env);
- }
-
- u8 getType() const
- {
- return ACTIVEOBJECT_TYPE_PLAYER;
- }
- core::aabbox3d<f32>* getSelectionBox()
- {
- if(m_is_local_player)
- return NULL;
- if(m_dead)
- return NULL;
- return &m_selection_box;
- }
- v3f getPosition()
- {
- return pos_translator.vect_show;
- }
-
- void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
- IrrlichtDevice *irr)
- {
- if(m_node != NULL)
- return;
- if(m_is_local_player)
- return;
-
- //video::IVideoDriver* driver = smgr->getVideoDriver();
- gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
-
- scene::SMesh *mesh = new scene::SMesh();
- { // Front
- scene::IMeshBuffer *buf = new scene::SMeshBuffer();
- video::SColor c(255,255,255,255);
- video::S3DVertex vertices[4] =
- {
- 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_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();
- }
- { // Back
- scene::IMeshBuffer *buf = new scene::SMeshBuffer();
- video::SColor c(255,255,255,255);
- video::S3DVertex vertices[4] =
- {
- video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
- video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
- video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
- video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,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_BILINEAR_FILTER, false);
- buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
- buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
- // 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();
-
- // Add a text node for showing the name
- std::wstring wname = narrow_to_wide(m_name);
- m_text = smgr->addTextSceneNode(gui->getBuiltInFont(),
- wname.c_str(), video::SColor(255,255,255,255), m_node);
- m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
-
- updateTextures("");
- updateVisibility();
- updateNodePos();
- }
-
- void removeFromScene()
- {
- if(m_node == NULL)
- return;
-
- m_node->remove();
- m_node = NULL;
- }
-
- void updateLight(u8 light_at_pos)
- {
- if(m_node == NULL)
- return;
-
- u8 li = decode_light(light_at_pos);
- video::SColor color(255,li,li,li);
- setMeshColor(m_node->getMesh(), color);
-
- updateVisibility();
- }
-
- v3s16 getLightPosition()
- {
- return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
- }
-
- void updateVisibility()
- {
- if(m_node == NULL)
- return;
-
- m_node->setVisible(!m_dead);
- }
-
- void updateNodePos()
- {
- if(m_node == NULL)
- return;
-
- m_node->setPosition(pos_translator.vect_show);
-
- v3f rot = m_node->getRotation();
- rot.Y = -m_yaw;
- m_node->setRotation(rot);
- }
-
- void step(float dtime, ClientEnvironment *env)
- {
- v3f lastpos = pos_translator.vect_show;
- pos_translator.translate(dtime);