1
0
mirror of https://github.com/moparisthebest/minetest synced 2025-01-11 05:38:01 -05:00

Split declaration of GenericCAO from implementation

This commit is contained in:
sapier 2014-05-03 11:14:22 +02:00
parent 87b4bce594
commit d9f6f9e7a8
2 changed files with 1343 additions and 1257 deletions

View File

@ -53,21 +53,7 @@ struct ToolCapabilities;
std::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types; std::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
/* SmoothTranslator::SmoothTranslator():
SmoothTranslator
*/
struct SmoothTranslator
{
v3f vect_old;
v3f vect_show;
v3f vect_aim;
f32 anim_counter;
f32 anim_time;
f32 anim_time_counter;
bool aim_is_end;
SmoothTranslator():
vect_old(0,0,0), vect_old(0,0,0),
vect_show(0,0,0), vect_show(0,0,0),
vect_aim(0,0,0), vect_aim(0,0,0),
@ -75,10 +61,10 @@ struct SmoothTranslator
anim_time(0), anim_time(0),
anim_time_counter(0), anim_time_counter(0),
aim_is_end(true) aim_is_end(true)
{} {}
void init(v3f vect) void SmoothTranslator::init(v3f vect)
{ {
vect_old = vect; vect_old = vect;
vect_show = vect; vect_show = vect;
vect_aim = vect; vect_aim = vect;
@ -86,15 +72,15 @@ struct SmoothTranslator
anim_time = 0; anim_time = 0;
anim_time_counter = 0; anim_time_counter = 0;
aim_is_end = true; aim_is_end = true;
} }
void sharpen() void SmoothTranslator::sharpen()
{ {
init(vect_show); init(vect_show);
} }
void update(v3f vect_new, bool is_end_position=false, float update_interval=-1) void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
{ {
aim_is_end = is_end_position; aim_is_end = is_end_position;
vect_old = vect_show; vect_old = vect_show;
vect_aim = vect_new; vect_aim = vect_new;
@ -108,10 +94,10 @@ struct SmoothTranslator
} }
anim_time_counter = 0; anim_time_counter = 0;
anim_counter = 0; anim_counter = 0;
} }
void translate(f32 dtime) void SmoothTranslator::translate(f32 dtime)
{ {
anim_time_counter = anim_time_counter + dtime; anim_time_counter = anim_time_counter + dtime;
anim_counter = anim_counter + dtime; anim_counter = anim_counter + dtime;
v3f vect_move = vect_aim - vect_old; v3f vect_move = vect_aim - vect_old;
@ -126,13 +112,12 @@ struct SmoothTranslator
if(moveratio > move_end) if(moveratio > move_end)
moveratio = move_end; moveratio = move_end;
vect_show = vect_old + vect_move * moveratio; vect_show = vect_old + vect_move * moveratio;
} }
bool is_moving() bool SmoothTranslator::is_moving()
{ {
return ((anim_time_counter / anim_time) < 1.4); return ((anim_time_counter / anim_time) < 1.4);
} }
};
/* /*
Other stuff Other stuff
@ -552,56 +537,7 @@ void ItemCAO::initialize(const std::string &data)
#include "genericobject.h" #include "genericobject.h"
class GenericCAO : public ClientActiveObject GenericCAO::GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
{
private:
// Only set at initialization
std::string m_name;
bool m_is_player;
bool m_is_local_player;
int m_id;
// Property-ish things
ObjectProperties m_prop;
//
scene::ISceneManager *m_smgr;
IrrlichtDevice *m_irr;
core::aabbox3d<f32> m_selection_box;
scene::IMeshSceneNode *m_meshnode;
scene::IAnimatedMeshSceneNode *m_animated_meshnode;
scene::IBillboardSceneNode *m_spritenode;
scene::ITextSceneNode* m_textnode;
v3f m_position;
v3f m_velocity;
v3f m_acceleration;
float m_yaw;
s16 m_hp;
SmoothTranslator pos_translator;
// Spritesheet/animation stuff
v2f m_tx_size;
v2s16 m_tx_basepos;
bool m_initial_tx_basepos_set;
bool m_tx_select_horiz_by_yawpitch;
v2s32 m_animation_range;
int m_animation_speed;
int m_animation_blend;
std::map<std::string, core::vector2d<v3f> > m_bone_position; // stores position and rotation for each bone name
std::string m_attachment_bone;
v3f m_attachment_position;
v3f m_attachment_rotation;
bool m_attached_to_local;
int m_anim_frame;
int m_anim_num_frames;
float m_anim_framelength;
float m_anim_timer;
ItemGroupList m_armor_groups;
float m_reset_textures_timer;
bool m_visuals_expired;
float m_step_distance_counter;
u8 m_last_light;
bool m_is_visible;
public:
GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
ClientActiveObject(0, gamedef, env), ClientActiveObject(0, gamedef, env),
// //
m_is_player(false), m_is_player(false),
@ -641,12 +577,12 @@ public:
m_step_distance_counter(0), m_step_distance_counter(0),
m_last_light(255), m_last_light(255),
m_is_visible(false) m_is_visible(false)
{ {
if(gamedef == NULL) if(gamedef == NULL)
ClientActiveObject::registerType(getType(), create); ClientActiveObject::registerType(getType(), create);
} }
bool getCollisionBox(aabb3f *toset) { bool GenericCAO::getCollisionBox(aabb3f *toset) {
if (m_prop.physical) { if (m_prop.physical) {
//update collision box //update collision box
toset->MinEdge = m_prop.collisionbox.MinEdge * BS; toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
@ -659,14 +595,14 @@ public:
} }
return false; return false;
} }
bool collideWithObjects() { bool GenericCAO::collideWithObjects() {
return m_prop.collideWithObjects; return m_prop.collideWithObjects;
} }
void initialize(const std::string &data) void GenericCAO::initialize(const std::string &data)
{ {
infostream<<"GenericCAO: Got init data"<<std::endl; infostream<<"GenericCAO: Got init data"<<std::endl;
std::istringstream is(data, std::ios::binary); std::istringstream is(data, std::ios::binary);
int num_messages = 0; int num_messages = 0;
@ -714,32 +650,24 @@ public:
} }
m_env->addPlayerName(m_name.c_str()); m_env->addPlayerName(m_name.c_str());
} }
} }
~GenericCAO() GenericCAO::~GenericCAO()
{ {
if(m_is_player){ if(m_is_player){
m_env->removePlayerName(m_name.c_str()); m_env->removePlayerName(m_name.c_str());
} }
} }
static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env) core::aabbox3d<f32>* GenericCAO::getSelectionBox()
{ {
return new GenericCAO(gamedef, env);
}
u8 getType() const
{
return ACTIVEOBJECT_TYPE_GENERIC;
}
core::aabbox3d<f32>* getSelectionBox()
{
if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL) if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
return NULL; return NULL;
return &m_selection_box; return &m_selection_box;
} }
v3f getPosition()
{ v3f GenericCAO::getPosition()
{
if(getParent() != NULL){ if(getParent() != NULL){
if(m_meshnode) if(m_meshnode)
return m_meshnode->getAbsolutePosition(); return m_meshnode->getAbsolutePosition();
@ -750,46 +678,36 @@ public:
return m_position; return m_position;
} }
return pos_translator.vect_show; return pos_translator.vect_show;
} }
scene::IMeshSceneNode *getMeshSceneNode() scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
{ {
if(m_meshnode) if(m_meshnode)
return m_meshnode; return m_meshnode;
return NULL; return NULL;
} }
scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
{ {
if(m_animated_meshnode) if(m_animated_meshnode)
return m_animated_meshnode; return m_animated_meshnode;
return NULL; return NULL;
} }
scene::IBillboardSceneNode *getSpriteSceneNode() scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
{ {
if(m_spritenode) if(m_spritenode)
return m_spritenode; return m_spritenode;
return NULL; return NULL;
} }
bool isPlayer() void GenericCAO::setAttachments()
{ {
return m_is_player;
}
bool isLocalPlayer()
{
return m_is_local_player;
}
void setAttachments()
{
updateAttachments(); updateAttachments();
} }
ClientActiveObject *getParent() ClientActiveObject* GenericCAO::getParent()
{ {
ClientActiveObject *obj = NULL; ClientActiveObject *obj = NULL;
for(std::vector<core::vector2d<int> >::const_iterator cii = m_env->attachment_list.begin(); cii != m_env->attachment_list.end(); cii++) for(std::vector<core::vector2d<int> >::const_iterator cii = m_env->attachment_list.begin(); cii != m_env->attachment_list.end(); cii++)
{ {
@ -805,10 +723,10 @@ public:
if(obj) if(obj)
return obj; return obj;
return NULL; return NULL;
} }
void removeFromScene(bool permanent) void GenericCAO::removeFromScene(bool permanent)
{ {
if(permanent) // Should be true when removing the object permanently and false when refreshing (eg: updating visuals) if(permanent) // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
{ {
// Detach this object's children // Detach this object's children
@ -845,11 +763,11 @@ public:
m_spritenode->remove(); m_spritenode->remove();
m_spritenode = NULL; m_spritenode = NULL;
} }
} }
void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc, void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
IrrlichtDevice *irr) IrrlichtDevice *irr)
{ {
m_smgr = smgr; m_smgr = smgr;
m_irr = irr; m_irr = irr;
@ -1026,15 +944,10 @@ public:
updateAnimation(); updateAnimation();
updateBonePosition(); updateBonePosition();
updateAttachments(); updateAttachments();
} }
void expireVisuals() void GenericCAO::updateLight(u8 light_at_pos)
{ {
m_visuals_expired = true;
}
void updateLight(u8 light_at_pos)
{
u8 li = decode_light(light_at_pos); u8 li = decode_light(light_at_pos);
if(li != m_last_light){ if(li != m_last_light){
m_last_light = li; m_last_light = li;
@ -1046,15 +959,15 @@ public:
if(m_spritenode) if(m_spritenode)
m_spritenode->setColor(color); m_spritenode->setColor(color);
} }
} }
v3s16 getLightPosition() v3s16 GenericCAO::getLightPosition()
{ {
return floatToInt(m_position, BS); return floatToInt(m_position, BS);
} }
void updateNodePos() void GenericCAO::updateNodePos()
{ {
if(getParent() != NULL) if(getParent() != NULL)
return; return;
@ -1074,10 +987,10 @@ public:
if(m_spritenode){ if(m_spritenode){
m_spritenode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS)); m_spritenode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
} }
} }
void step(float dtime, ClientEnvironment *env) void GenericCAO::step(float dtime, ClientEnvironment *env)
{ {
// Handel model of local player instantly to prevent lags // Handel model of local player instantly to prevent lags
if(m_is_local_player) { if(m_is_local_player) {
LocalPlayer *player = m_env->getLocalPlayer(); LocalPlayer *player = m_env->getLocalPlayer();
@ -1133,6 +1046,7 @@ public:
player->last_animation_speed = m_animation_speed; player->last_animation_speed = m_animation_speed;
} else { } else {
player->last_animation = NO_ANIM; player->last_animation = NO_ANIM;
if (old_anim != NO_ANIM) { if (old_anim != NO_ANIM) {
m_animation_range = player->local_animations[0]; m_animation_range = player->local_animations[0];
updateAnimation(); updateAnimation();
@ -1289,10 +1203,10 @@ public:
m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI + m_prop.automatic_face_movement_dir_offset; m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI + m_prop.automatic_face_movement_dir_offset;
updateNodePos(); updateNodePos();
} }
} }
void updateTexturePos() void GenericCAO::updateTexturePos()
{ {
if(m_spritenode){ if(m_spritenode){
scene::ICameraSceneNode* camera = scene::ICameraSceneNode* camera =
m_spritenode->getSceneManager()->getActiveCamera(); m_spritenode->getSceneManager()->getActiveCamera();
@ -1337,10 +1251,10 @@ public:
setBillboardTextureMatrix(m_spritenode, setBillboardTextureMatrix(m_spritenode,
txs, tys, col, row); txs, tys, col, row);
} }
} }
void updateTextures(const std::string &mod) void GenericCAO::updateTextures(const std::string &mod)
{ {
ITextureSource *tsrc = m_gamedef->tsrc(); ITextureSource *tsrc = m_gamedef->tsrc();
bool use_trilinear_filter = g_settings->getBool("trilinear_filter"); bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
@ -1505,19 +1419,19 @@ public:
} }
} }
} }
} }
void updateAnimation() void GenericCAO::updateAnimation()
{ {
if(m_animated_meshnode == NULL) if(m_animated_meshnode == NULL)
return; return;
m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y); m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
m_animated_meshnode->setAnimationSpeed(m_animation_speed); m_animated_meshnode->setAnimationSpeed(m_animation_speed);
m_animated_meshnode->setTransitionTime(m_animation_blend); m_animated_meshnode->setTransitionTime(m_animation_blend);
} }
void updateBonePosition() void GenericCAO::updateBonePosition()
{ {
if(!m_bone_position.size() || m_animated_meshnode == NULL) if(!m_bone_position.size() || m_animated_meshnode == NULL)
return; return;
@ -1533,10 +1447,10 @@ public:
bone->setRotation(bone_rot); bone->setRotation(bone_rot);
} }
} }
} }
void updateAttachments() void GenericCAO::updateAttachments()
{ {
m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer(); m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
m_is_visible = !m_attached_to_local; // Objects attached to the local player should always be hidden m_is_visible = !m_attached_to_local; // Objects attached to the local player should always be hidden
@ -1686,10 +1600,10 @@ public:
player->isAttached = true; player->isAttached = true;
} }
} }
} }
void processMessage(const std::string &data) void GenericCAO::processMessage(const std::string &data)
{ {
//infostream<<"GenericCAO: Got message"<<std::endl; //infostream<<"GenericCAO: Got message"<<std::endl;
std::istringstream is(data, std::ios::binary); std::istringstream is(data, std::ios::binary);
// command // command
@ -1806,7 +1720,8 @@ public:
is_known = true; is_known = true;
} }
if(!is_known || if(!is_known ||
(player->local_animations[1].Y + player->local_animations[2].Y < 1)) { (player->local_animations[1].Y + player->local_animations[2].Y < 1))
{
updateAnimation(); updateAnimation();
} }
} }
@ -1876,11 +1791,11 @@ public:
m_armor_groups[name] = rating; m_armor_groups[name] = rating;
} }
} }
} }
bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL, bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
float time_from_last_punch=1000000) float time_from_last_punch)
{ {
assert(punchitem); assert(punchitem);
const ToolCapabilities *toolcap = const ToolCapabilities *toolcap =
&punchitem->getToolCapabilities(m_gamedef->idef()); &punchitem->getToolCapabilities(m_gamedef->idef());
@ -1912,10 +1827,10 @@ public:
} }
return false; return false;
} }
std::string debugInfoText() std::string GenericCAO::debugInfoText()
{ {
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
os<<"GenericCAO hp="<<m_hp<<"\n"; os<<"GenericCAO hp="<<m_hp<<"\n";
os<<"armor={"; os<<"armor={";
@ -1925,10 +1840,7 @@ public:
} }
os<<"}"; os<<"}";
return os.str(); return os.str();
} }
};
// Prototype // Prototype
GenericCAO proto_GenericCAO(NULL, NULL); GenericCAO proto_GenericCAO(NULL, NULL);

View File

@ -20,5 +20,179 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef CONTENT_CAO_HEADER #ifndef CONTENT_CAO_HEADER
#define CONTENT_CAO_HEADER #define CONTENT_CAO_HEADER
#include <map>
#include "irrlichttypes_extrabloated.h"
#include "content_object.h"
#include "clientobject.h"
#include "object_properties.h"
#include "itemgroup.h"
/*
SmoothTranslator
*/
struct SmoothTranslator
{
v3f vect_old;
v3f vect_show;
v3f vect_aim;
f32 anim_counter;
f32 anim_time;
f32 anim_time_counter;
bool aim_is_end;
SmoothTranslator();
void init(v3f vect);
void sharpen();
void update(v3f vect_new, bool is_end_position=false, float update_interval=-1);
void translate(f32 dtime);
bool is_moving();
};
class GenericCAO : public ClientActiveObject
{
private:
// Only set at initialization
std::string m_name;
bool m_is_player;
bool m_is_local_player;
int m_id;
// Property-ish things
ObjectProperties m_prop;
//
scene::ISceneManager *m_smgr;
IrrlichtDevice *m_irr;
core::aabbox3d<f32> m_selection_box;
scene::IMeshSceneNode *m_meshnode;
scene::IAnimatedMeshSceneNode *m_animated_meshnode;
scene::IBillboardSceneNode *m_spritenode;
scene::ITextSceneNode* m_textnode;
v3f m_position;
v3f m_velocity;
v3f m_acceleration;
float m_yaw;
s16 m_hp;
SmoothTranslator pos_translator;
// Spritesheet/animation stuff
v2f m_tx_size;
v2s16 m_tx_basepos;
bool m_initial_tx_basepos_set;
bool m_tx_select_horiz_by_yawpitch;
v2s32 m_animation_range;
int m_animation_speed;
int m_animation_blend;
std::map<std::string, core::vector2d<v3f> > m_bone_position; // stores position and rotation for each bone name
std::string m_attachment_bone;
v3f m_attachment_position;
v3f m_attachment_rotation;
bool m_attached_to_local;
int m_anim_frame;
int m_anim_num_frames;
float m_anim_framelength;
float m_anim_timer;
ItemGroupList m_armor_groups;
float m_reset_textures_timer;
bool m_visuals_expired;
float m_step_distance_counter;
u8 m_last_light;
bool m_is_visible;
public:
GenericCAO(IGameDef *gamedef, ClientEnvironment *env);
~GenericCAO();
static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
{
return new GenericCAO(gamedef, env);
}
inline u8 getType() const
{
return ACTIVEOBJECT_TYPE_GENERIC;
}
void initialize(const std::string &data);
ClientActiveObject *getParent();
bool getCollisionBox(aabb3f *toset);
bool collideWithObjects();
core::aabbox3d<f32>* getSelectionBox();
v3f getPosition();
scene::IMeshSceneNode *getMeshSceneNode();
scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode();
scene::IBillboardSceneNode *getSpriteSceneNode();
inline bool isPlayer() const
{
return m_is_player;
}
inline bool isLocalPlayer() const
{
return m_is_local_player;
}
inline bool isVisible() const
{
return m_is_visible;
}
inline void setVisible(bool toset)
{
m_is_visible = toset;
}
void setAttachments();
void removeFromScene(bool permanent);
void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
IrrlichtDevice *irr);
inline void expireVisuals()
{
m_visuals_expired = true;
}
void updateLight(u8 light_at_pos);
v3s16 getLightPosition();
void updateNodePos();
void step(float dtime, ClientEnvironment *env);
void updateTexturePos();
void updateTextures(const std::string &mod);
void updateAnimation();
void updateBonePosition();
void updateAttachments();
void processMessage(const std::string &data);
bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
float time_from_last_punch=1000000);
std::string debugInfoText();
};
#endif #endif