Use GenericCAO in place of LuaEntityCAO and PlayerCAO

This commit is contained in:
Perttu Ahola 2012-03-29 17:46:21 +03:00
parent 443f45eca1
commit 92ae11bd3b
6 changed files with 559 additions and 963 deletions

@ -611,6 +611,7 @@ methods:
- get_wield_index(): returns the index of the wielded item - get_wield_index(): returns the index of the wielded item
- get_wielded_item() -> ItemStack - get_wielded_item() -> ItemStack
- set_wielded_item(item): replaces the wielded item, returns true if successful - set_wielded_item(item): replaces the wielded item, returns true if successful
- set_armor_groups({group1=rating, group2=rating, ...})
LuaEntitySAO-only: (no-op for other objects) LuaEntitySAO-only: (no-op for other objects)
- setvelocity({x=num, y=num, z=num}) - setvelocity({x=num, y=num, z=num})
- getvelocity() -> {x=num, y=num, z=num} - getvelocity() -> {x=num, y=num, z=num}
@ -623,7 +624,6 @@ LuaEntitySAO-only: (no-op for other objects)
- select_horiz_by_yawpitch=false) - select_horiz_by_yawpitch=false)
- ^ Select sprite from spritesheet with optional animation and DM-style - ^ Select sprite from spritesheet with optional animation and DM-style
- texture selection based on yaw relative to camera - texture selection based on yaw relative to camera
- set_armor_groups({group1=rating, group2=rating, ...})
- get_entity_name() (DEPRECATED: Will be removed in a future version) - get_entity_name() (DEPRECATED: Will be removed in a future version)
- get_luaentity() - get_luaentity()
Player-only: (no-op for other objects) Player-only: (no-op for other objects)

File diff suppressed because it is too large Load Diff

@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gamedef.h" #include "gamedef.h"
#include "player.h" #include "player.h"
#include "scriptapi.h" #include "scriptapi.h"
#include "genericobject.h"
core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types; core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
@ -351,6 +352,7 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
m_velocity(0,0,0), m_velocity(0,0,0),
m_acceleration(0,0,0), m_acceleration(0,0,0),
m_yaw(0), m_yaw(0),
m_properties_sent(true),
m_last_sent_yaw(0), m_last_sent_yaw(0),
m_last_sent_position(0,0,0), m_last_sent_position(0,0,0),
m_last_sent_velocity(0,0,0), m_last_sent_velocity(0,0,0),
@ -434,6 +436,15 @@ ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
void LuaEntitySAO::step(float dtime, bool send_recommended) void LuaEntitySAO::step(float dtime, bool send_recommended)
{ {
if(!m_properties_sent)
{
m_properties_sent = true;
std::string str = getPropertyPacket();
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom);
}
m_last_sent_position_timer += dtime; m_last_sent_position_timer += dtime;
if(m_prop->physical){ if(m_prop->physical){
@ -483,16 +494,10 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
if(m_armor_groups_sent == false){ if(m_armor_groups_sent == false){
m_armor_groups_sent = true; m_armor_groups_sent = true;
std::ostringstream os(std::ios::binary); std::string str = gob_cmd_update_armor_groups(
writeU8(os, LUAENTITY_CMD_UPDATE_ARMOR_GROUPS); m_armor_groups);
writeU16(os, m_armor_groups.size());
for(ItemGroupList::const_iterator i = m_armor_groups.begin();
i != m_armor_groups.end(); i++){
os<<serializeString(i->first);
writeS16(os, i->second);
}
// create message and add to list // create message and add to list
ActiveObjectMessage aom(getId(), true, os.str()); ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom); m_messages_out.push_back(aom);
} }
} }
@ -500,18 +505,19 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
std::string LuaEntitySAO::getClientInitializationData() std::string LuaEntitySAO::getClientInitializationData()
{ {
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
// version writeU8(os, 0); // version
writeU8(os, 1); os<<serializeString(""); // name
// pos writeU8(os, 0); // is_player
writeV3F1000(os, m_base_position); writeV3F1000(os, m_base_position);
// yaw
writeF1000(os, m_yaw); writeF1000(os, m_yaw);
// hp
writeS16(os, m_hp); writeS16(os, m_hp);
// properties writeU8(os, 3); // number of messages stuffed in here
std::ostringstream prop_os(std::ios::binary); os<<serializeLongString(getPropertyPacket()); // message 1
m_prop->serialize(prop_os); os<<serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
os<<serializeLongString(prop_os.str()); os<<serializeLongString(gob_cmd_set_sprite( // 3
m_prop->initial_sprite_basepos,
1, 1.0, false
));
// return result // return result
return os.str(); return os.str();
} }
@ -574,15 +580,9 @@ int LuaEntitySAO::punch(v3f dir,
<<" hp, health now "<<getHP()<<" hp"<<std::endl; <<" hp, health now "<<getHP()<<" hp"<<std::endl;
{ {
std::ostringstream os(std::ios::binary); std::string str = gob_cmd_punched(result.damage, getHP());
// command
writeU8(os, LUAENTITY_CMD_PUNCHED);
// damage
writeS16(os, result.damage);
// result_hp
writeS16(os, getHP());
// create message and add to list // create message and add to list
ActiveObjectMessage aom(getId(), true, os.str()); ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom); m_messages_out.push_back(aom);
} }
@ -683,29 +683,23 @@ float LuaEntitySAO::getYaw()
void LuaEntitySAO::setTextureMod(const std::string &mod) void LuaEntitySAO::setTextureMod(const std::string &mod)
{ {
std::ostringstream os(std::ios::binary); std::string str = gob_cmd_set_texture_mod(mod);
// command
writeU8(os, LUAENTITY_CMD_SET_TEXTURE_MOD);
// parameters
os<<serializeString(mod);
// create message and add to list // create message and add to list
ActiveObjectMessage aom(getId(), false, os.str()); ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom); m_messages_out.push_back(aom);
} }
void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength, void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
bool select_horiz_by_yawpitch) bool select_horiz_by_yawpitch)
{ {
std::ostringstream os(std::ios::binary); std::string str = gob_cmd_set_sprite(
// command p,
writeU8(os, LUAENTITY_CMD_SET_SPRITE); num_frames,
// parameters framelength,
writeV2S16(os, p); select_horiz_by_yawpitch
writeU16(os, num_frames); );
writeF1000(os, framelength);
writeU8(os, select_horiz_by_yawpitch);
// create message and add to list // create message and add to list
ActiveObjectMessage aom(getId(), false, os.str()); ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom); m_messages_out.push_back(aom);
} }
@ -714,6 +708,22 @@ std::string LuaEntitySAO::getName()
return m_init_name; return m_init_name;
} }
std::string LuaEntitySAO::getPropertyPacket()
{
return gob_cmd_set_properties(
m_prop->hp_max,
m_prop->physical,
m_prop->weight,
m_prop->collisionbox,
m_prop->visual,
m_prop->visual_size,
m_prop->textures,
m_prop->spritediv,
true, // is_visible
false // makes_footstep_sound
);
}
void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end) void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
{ {
m_last_sent_move_precision = m_base_position.getDistanceFrom( m_last_sent_move_precision = m_base_position.getDistanceFrom(
@ -726,28 +736,17 @@ void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
float update_interval = m_env->getSendRecommendedInterval(); float update_interval = m_env->getSendRecommendedInterval();
std::ostringstream os(std::ios::binary); std::string str = gob_cmd_update_position(
// command m_base_position,
writeU8(os, LUAENTITY_CMD_UPDATE_POSITION); m_velocity,
m_acceleration,
// do_interpolate m_yaw,
writeU8(os, do_interpolate); do_interpolate,
// pos is_movement_end,
writeV3F1000(os, m_base_position); update_interval
// velocity );
writeV3F1000(os, m_velocity);
// acceleration
writeV3F1000(os, m_acceleration);
// yaw
writeF1000(os, m_yaw);
// is_end_position (for interpolation)
writeU8(os, is_movement_end);
// update_interval (for interpolation)
writeF1000(os, update_interval);
// create message and add to list // create message and add to list
ActiveObjectMessage aom(getId(), false, os.str()); ActiveObjectMessage aom(getId(), false, str);
m_messages_out.push_back(aom);
} }
/* /*
@ -767,6 +766,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_):
m_wield_index(0), m_wield_index(0),
m_position_not_sent(false), m_position_not_sent(false),
m_armor_groups_sent(false), m_armor_groups_sent(false),
m_properties_sent(true),
m_teleported(false), m_teleported(false),
m_inventory_not_sent(false), m_inventory_not_sent(false),
m_hp_not_sent(false), m_hp_not_sent(false),
@ -827,18 +827,15 @@ bool PlayerSAO::unlimitedTransferDistance() const
std::string PlayerSAO::getClientInitializationData() std::string PlayerSAO::getClientInitializationData()
{ {
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
// version writeU8(os, 0); // version
writeU8(os, 0); os<<serializeString(m_player->getName()); // name
// name writeU8(os, 1); // is_player
os<<serializeString(m_player->getName()); writeV3F1000(os, m_player->getPosition() + v3f(0,BS*1,0));
// pos
writeV3F1000(os, m_player->getPosition());
// yaw
writeF1000(os, m_player->getYaw()); writeF1000(os, m_player->getYaw());
// dead writeS16(os, getHP());
writeU8(os, getHP() == 0); writeU8(os, 2); // number of messages stuffed in here
// wielded item os<<serializeLongString(getPropertyPacket()); // message 1
os<<serializeString(getWieldedItem().getItemString()); os<<serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
return os.str(); return os.str();
} }
@ -850,6 +847,15 @@ std::string PlayerSAO::getStaticData()
void PlayerSAO::step(float dtime, bool send_recommended) void PlayerSAO::step(float dtime, bool send_recommended)
{ {
if(!m_properties_sent)
{
m_properties_sent = true;
std::string str = getPropertyPacket();
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom);
}
m_time_from_last_punch += dtime; m_time_from_last_punch += dtime;
/* /*
@ -895,30 +901,33 @@ void PlayerSAO::step(float dtime, bool send_recommended)
if(m_position_not_sent) if(m_position_not_sent)
{ {
m_position_not_sent = false; m_position_not_sent = false;
float update_interval = m_env->getSendRecommendedInterval();
std::ostringstream os(std::ios::binary); std::string str = gob_cmd_update_position(
// command (0 = update position) m_player->getPosition() + v3f(0,BS*1,0),
writeU8(os, 0); v3f(0,0,0),
// pos v3f(0,0,0),
writeV3F1000(os, m_player->getPosition()); m_player->getYaw(),
// yaw true,
writeF1000(os, m_player->getYaw()); false,
update_interval
);
// create message and add to list // create message and add to list
ActiveObjectMessage aom(getId(), false, os.str()); ActiveObjectMessage aom(getId(), false, str);
m_messages_out.push_back(aom); m_messages_out.push_back(aom);
} }
if(m_wielded_item_not_sent) if(m_wielded_item_not_sent)
{ {
m_wielded_item_not_sent = false; m_wielded_item_not_sent = false;
// GenericCAO has no special way to show this
}
std::ostringstream os(std::ios::binary); if(m_armor_groups_sent == false){
// command (3 = wielded item) m_armor_groups_sent = true;
writeU8(os, 3); std::string str = gob_cmd_update_armor_groups(
// wielded item m_armor_groups);
os<<serializeString(getWieldedItem().getItemString());
// create message and add to list // create message and add to list
ActiveObjectMessage aom(getId(), false, os.str()); ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom); m_messages_out.push_back(aom);
} }
} }
@ -959,9 +968,14 @@ int PlayerSAO::punch(v3f dir,
// No effect if PvP disabled // No effect if PvP disabled
if(g_settings->getBool("enable_pvp") == false){ if(g_settings->getBool("enable_pvp") == false){
if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER){
std::string str = gob_cmd_punched(0, getHP());
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom);
return 0; return 0;
} }
}
HitParams hitparams = getHitParams(m_armor_groups, toolcap, HitParams hitparams = getHitParams(m_armor_groups, toolcap,
time_from_last_punch); time_from_last_punch);
@ -974,13 +988,9 @@ int PlayerSAO::punch(v3f dir,
if(hitparams.hp != 0) if(hitparams.hp != 0)
{ {
std::ostringstream os(std::ios::binary); std::string str = gob_cmd_punched(hitparams.hp, getHP());
// command (1 = punched)
writeU8(os, 1);
// damage
writeS16(os, hitparams.hp);
// create message and add to list // create message and add to list
ActiveObjectMessage aom(getId(), false, os.str()); ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push_back(aom); m_messages_out.push_back(aom);
} }
@ -1019,13 +1029,11 @@ void PlayerSAO::setHP(s16 hp)
// On death or reincarnation send an active object message // On death or reincarnation send an active object message
if((hp == 0) != (oldhp == 0)) if((hp == 0) != (oldhp == 0))
{ {
std::ostringstream os(std::ios::binary); // Will send new is_visible value based on (getHP()!=0)
// command (2 = update death state) m_properties_sent = false;
writeU8(os, 2); // Send new HP
// dead? std::string str = gob_cmd_punched(0, getHP());
writeU8(os, hp == 0); ActiveObjectMessage aom(getId(), true, str);
// create message and add to list
ActiveObjectMessage aom(getId(), false, os.str());
m_messages_out.push_back(aom); m_messages_out.push_back(aom);
} }
} }
@ -1097,3 +1105,22 @@ void PlayerSAO::createCreativeInventory()
scriptapi_get_creative_inventory(m_env->getLua(), this); scriptapi_get_creative_inventory(m_env->getLua(), this);
} }
std::string PlayerSAO::getPropertyPacket()
{
core::array<std::string> textures;
textures.push_back("player.png");
textures.push_back("player_back.png");
return gob_cmd_set_properties(
PLAYER_MAX_HP,
false,
75,
core::aabbox3d<f32>(-1/3.,-1.0,-1/3., 1/3.,1.0,1/3.),
"upright_sprite",
v2f(1, 2),
textures,
v2s16(1,1),
(getHP() != 0), // is_visible
true // makes_footstep_sound
);
}

@ -42,6 +42,8 @@ public:
~LuaEntitySAO(); ~LuaEntitySAO();
u8 getType() const u8 getType() const
{ return ACTIVEOBJECT_TYPE_LUAENTITY; } { return ACTIVEOBJECT_TYPE_LUAENTITY; }
u8 getSendType() const
{ return ACTIVEOBJECT_TYPE_GENERIC; }
virtual void addedToEnvironment(); virtual void addedToEnvironment();
static ServerActiveObject* create(ServerEnvironment *env, v3f pos, static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
const std::string &data); const std::string &data);
@ -72,6 +74,7 @@ public:
bool select_horiz_by_yawpitch); bool select_horiz_by_yawpitch);
std::string getName(); std::string getName();
private: private:
std::string getPropertyPacket();
void sendPosition(bool do_interpolate, bool is_movement_end); void sendPosition(bool do_interpolate, bool is_movement_end);
std::string m_init_name; std::string m_init_name;
@ -85,6 +88,7 @@ private:
float m_yaw; float m_yaw;
ItemGroupList m_armor_groups; ItemGroupList m_armor_groups;
bool m_properties_sent;
float m_last_sent_yaw; float m_last_sent_yaw;
v3f m_last_sent_position; v3f m_last_sent_position;
v3f m_last_sent_velocity; v3f m_last_sent_velocity;
@ -104,6 +108,8 @@ public:
~PlayerSAO(); ~PlayerSAO();
u8 getType() const u8 getType() const
{ return ACTIVEOBJECT_TYPE_PLAYER; } { return ACTIVEOBJECT_TYPE_PLAYER; }
u8 getSendType() const
{ return ACTIVEOBJECT_TYPE_GENERIC; }
std::string getDescription(); std::string getDescription();
/* /*
@ -174,6 +180,8 @@ public:
} }
private: private:
std::string getPropertyPacket();
Player *m_player; Player *m_player;
u16 m_peer_id; u16 m_peer_id;
Inventory *m_inventory; Inventory *m_inventory;
@ -184,6 +192,7 @@ private:
bool m_position_not_sent; bool m_position_not_sent;
ItemGroupList m_armor_groups; ItemGroupList m_armor_groups;
bool m_armor_groups_sent; bool m_armor_groups_sent;
bool m_properties_sent;
public: public:
// Some flags used by Server // Some flags used by Server

136
src/genericobject.cpp Normal file

@ -0,0 +1,136 @@
/*
Minetest-c55
Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "genericobject.h"
#include "utility.h"
#include <sstream>
std::string gob_cmd_set_properties(
s16 hp_max,
bool physical,
float weight,
core::aabbox3d<f32> collisionbox,
std::string visual,
v2f visual_size,
core::array<std::string> textures,
v2s16 spritediv,
bool is_visible,
bool makes_footstep_sound
){
std::ostringstream os(std::ios::binary);
writeU8(os, GENERIC_CMD_SET_PROPERTIES);
writeS16(os, hp_max);
writeU8(os, physical);
writeF1000(os, weight);
writeV3F1000(os, collisionbox.MinEdge);
writeV3F1000(os, collisionbox.MaxEdge);
os<<serializeString(visual);
writeV2F1000(os, visual_size);
writeU16(os, textures.size());
for(u32 i=0; i<textures.size(); i++){
os<<serializeString(textures[i]);
}
writeV2S16(os, spritediv);
writeU8(os, is_visible);
writeU8(os, makes_footstep_sound);
return os.str();
}
std::string gob_cmd_update_position(
v3f position,
v3f velocity,
v3f acceleration,
f32 yaw,
bool do_interpolate,
bool is_movement_end,
f32 update_interval
){
std::ostringstream os(std::ios::binary);
// command
writeU8(os, GENERIC_CMD_UPDATE_POSITION);
// pos
writeV3F1000(os, position);
// velocity
writeV3F1000(os, velocity);
// acceleration
writeV3F1000(os, acceleration);
// yaw
writeF1000(os, yaw);
// do_interpolate
writeU8(os, do_interpolate);
// is_end_position (for interpolation)
writeU8(os, is_movement_end);
// update_interval (for interpolation)
writeF1000(os, update_interval);
return os.str();
}
std::string gob_cmd_set_texture_mod(const std::string &mod)
{
std::ostringstream os(std::ios::binary);
// command
writeU8(os, GENERIC_CMD_SET_TEXTURE_MOD);
// parameters
os<<serializeString(mod);
return os.str();
}
std::string gob_cmd_set_sprite(
v2s16 p,
u16 num_frames,
f32 framelength,
bool select_horiz_by_yawpitch
){
std::ostringstream os(std::ios::binary);
// command
writeU8(os, GENERIC_CMD_SET_SPRITE);
// parameters
writeV2S16(os, p);
writeU16(os, num_frames);
writeF1000(os, framelength);
writeU8(os, select_horiz_by_yawpitch);
return os.str();
}
std::string gob_cmd_punched(s16 damage, s16 result_hp)
{
std::ostringstream os(std::ios::binary);
// command
writeU8(os, GENERIC_CMD_PUNCHED);
// damage
writeS16(os, damage);
// result_hp
writeS16(os, result_hp);
return os.str();
}
std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups)
{
std::ostringstream os(std::ios::binary);
writeU8(os, GENERIC_CMD_UPDATE_ARMOR_GROUPS);
writeU16(os, armor_groups.size());
for(ItemGroupList::const_iterator i = armor_groups.begin();
i != armor_groups.end(); i++){
os<<serializeString(i->first);
writeS16(os, i->second);
}
return os.str();
}

71
src/genericobject.h Normal file

@ -0,0 +1,71 @@
/*
Minetest-c55
Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef GENERICOBJECT_HEADER
#define GENERICOBJECT_HEADER
#include <string>
#include "irrlichttypes.h"
#define GENERIC_CMD_SET_PROPERTIES 0
#define GENERIC_CMD_UPDATE_POSITION 1
#define GENERIC_CMD_SET_TEXTURE_MOD 2
#define GENERIC_CMD_SET_SPRITE 3
#define GENERIC_CMD_PUNCHED 4
#define GENERIC_CMD_UPDATE_ARMOR_GROUPS 5
std::string gob_cmd_set_properties(
s16 hp_max,
bool physical,
float weight,
core::aabbox3d<f32> collisionbox,
std::string visual,
v2f visual_size,
core::array<std::string> textures,
v2s16 spritediv,
bool is_visible,
bool makes_footstep_sound
);
std::string gob_cmd_update_position(
v3f position,
v3f velocity,
v3f acceleration,
f32 yaw,
bool do_interpolate,
bool is_movement_end,
f32 update_interval
);
std::string gob_cmd_set_texture_mod(const std::string &mod);
std::string gob_cmd_set_sprite(
v2s16 p,
u16 num_frames,
f32 framelength,
bool select_horiz_by_yawpitch
);
std::string gob_cmd_punched(s16 damage, s16 result_hp);
#include "itemgroup.h"
std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups);
#endif