#define ACTIVEOBJECT_TYPE_INVALID 0
#define ACTIVEOBJECT_TYPE_TEST 1
+#define ACTIVEOBJECT_TYPE_ITEM 2
/*
Parent class for ServerActiveObject and ClientActiveObject
return NULL;
}
+ClientActiveObject * Client::getSelectedActiveObject(
+ f32 max_d,
+ v3f from_pos_f_on_map,
+ core::line3d<f32> shootline_on_map
+ )
+{
+ core::array<DistanceSortedActiveObject> objects;
+
+ m_env.getActiveObjects(from_pos_f_on_map, max_d, objects);
+
+ //dstream<<"Collected "<<objects.size()<<" nearby objects"<<std::endl;
+
+ // Sort them.
+ // After this, the closest object is the first in the array.
+ objects.sort();
+
+ for(u32 i=0; i<objects.size(); i++)
+ {
+ ClientActiveObject *obj = objects[i].obj;
+
+ core::aabbox3d<f32> *selection_box = obj->getSelectionBox();
+ if(selection_box == NULL)
+ continue;
+
+ v3f pos = obj->getPosition();
+
+ core::aabbox3d<f32> offsetted_box(
+ selection_box->MinEdge + pos,
+ selection_box->MaxEdge + pos
+ );
+
+ if(offsetted_box.intersectsWithLine(shootline_on_map))
+ {
+ //dstream<<"Returning selected object"<<std::endl;
+ return obj;
+ }
+ }
+
+ //dstream<<"No object selected; returning NULL."<<std::endl;
+ return NULL;
+}
+
void Client::printDebugInfo(std::ostream &os)
{
//JMutexAutoLock lock1(m_fetchblock_mutex);
core::line3d<f32> shootline_on_map
);
+ // Gets closest object pointed by the shootline
+ // Returns NULL if not found
+ ClientActiveObject * getSelectedActiveObject(
+ f32 max_d,
+ v3f from_pos_f_on_map,
+ core::line3d<f32> shootline_on_map
+ );
+
// Prints a line or two of info
void printDebugInfo(std::ostream &os);
#include "constants.h"
#include "utility.h"
+core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
+
ClientActiveObject::ClientActiveObject(u16 id):
ActiveObject(id)
{
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
- {
- 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)
}
}
+/*
+ ItemCAO
+*/
+
+#include "inventory.h"
+
+// Prototype
+ItemCAO proto_ItemCAO;
+
+ItemCAO::ItemCAO():
+ ClientActiveObject(0),
+ m_selection_box(-BS*0.4,0.0,-BS*0.4, BS*0.4,BS*0.8,BS*0.4),
+ m_node(NULL),
+ m_position(v3f(0,10*BS,0))
+{
+ ClientActiveObject::registerType(getType(), create);
+}
+
+ItemCAO::~ItemCAO()
+{
+}
+
+ClientActiveObject* ItemCAO::create()
+{
+ return new ItemCAO();
+}
+
+void ItemCAO::addToScene(scene::ISceneManager *smgr)
+{
+ if(m_node != NULL)
+ return;
+
+ video::IVideoDriver* driver = smgr->getVideoDriver();
+
+ 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);
+ buf->getMaterial().setTexture
+ (0, driver->getTexture(porting::getDataPath("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 ItemCAO::removeFromScene()
+{
+ if(m_node == NULL)
+ return;
+
+ m_node->remove();
+ m_node = NULL;
+}
+
+void ItemCAO::updateLight(u8 light_at_pos)
+{
+}
+
+v3s16 ItemCAO::getLightPosition()
+{
+ return floatToInt(m_position, BS);
+}
+
+void ItemCAO::updateNodePos()
+{
+ if(m_node == NULL)
+ return;
+
+ m_node->setPosition(m_position);
+}
+
+void ItemCAO::step(float dtime)
+{
+ if(m_node)
+ {
+ v3f rot = m_node->getRotation();
+ rot.Y += dtime * 120;
+ m_node->setRotation(rot);
+ }
+}
+
+void ItemCAO::processMessage(const std::string &data)
+{
+ dstream<<"ItemCAO: Got data: "<<data<<std::endl;
+ std::istringstream is(data, std::ios::binary);
+ u16 cmd;
+ is>>cmd;
+ if(cmd == 0)
+ {
+ v3f newpos;
+ is>>newpos.X;
+ is>>newpos.Y;
+ is>>newpos.Z;
+ m_position = newpos;
+ updateNodePos();
+ }
+}
+
+void ItemCAO::initialize(const std::string &data)
+{
+ dstream<<"ItemCAO: Got init data: "<<data<<std::endl;
+
+ Strfnd fn(data);
+
+ v3f newpos;
+ newpos.X = stoi(fn.next(","));
+ newpos.Y = stoi(fn.next(","));
+ newpos.Z = stoi(fn.next(":"));
+ m_position = newpos;
+ updateNodePos();
+
+ m_inventorystring = fn.next("");
+
+ if(m_node == NULL)
+ return;
+
+ scene::IMesh *mesh = m_node->getMesh();
+
+ if(mesh == NULL)
+ return;
+
+ 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;
+ }
+ }
+ catch(SerializationError &e)
+ {
+ dstream<<"WARNING: "<<__FUNCTION_NAME
+ <<": error deSerializing inventorystring \""
+ <<m_inventorystring<<"\""<<std::endl;
+ }
+
+ // Set meshbuffer texture
+ buf->getMaterial().setTexture(0, texture);
+
+}
+
// 0 <= light_at_pos <= LIGHT_SUN
virtual void updateLight(u8 light_at_pos){}
virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
+ virtual core::aabbox3d<f32>* getSelectionBox(){return NULL;}
+ virtual core::aabbox3d<f32>* getCollisionBox(){return NULL;}
+ virtual v3f getPosition(){return v3f(0,0,0);}
// Step object in time
virtual void step(float dtime){}
virtual void processMessage(const std::string &data){}
/*
- This takes the return value of getClientInitializationData
- TODO: Usage of this
+ This takes the return value of
+ ServerActiveObject::getClientInitializationData
*/
virtual void initialize(const std::string &data){}
static ClientActiveObject* create(u8 type);
protected:
+ typedef ClientActiveObject* (*Factory)();
+ static void registerType(u16 type, Factory f);
+private:
+ static core::map<u16, Factory> m_types;
};
+struct DistanceSortedActiveObject
+{
+ ClientActiveObject *obj;
+ f32 d;
+
+ DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
+ {
+ obj = a_obj;
+ d = a_d;
+ }
+
+ bool operator < (DistanceSortedActiveObject &other)
+ {
+ return d < other.d;
+ }
+};
+
+/*
+ TestCAO
+*/
+
class TestCAO : public ClientActiveObject
{
public:
- TestCAO(u16 id);
+ TestCAO();
virtual ~TestCAO();
u8 getType() const
return ACTIVEOBJECT_TYPE_TEST;
}
+ static ClientActiveObject* create();
+
+ void addToScene(scene::ISceneManager *smgr);
+ void removeFromScene();
+ void updateLight(u8 light_at_pos);
+ v3s16 getLightPosition();
+ void updateNodePos();
+
+ void step(float dtime);
+
+ void processMessage(const std::string &data);
+
+private:
+ scene::IMeshSceneNode *m_node;
+ v3f m_position;
+};
+
+/*
+ ItemCAO
+*/
+
+class ItemCAO : public ClientActiveObject
+{
+public:
+ ItemCAO();
+ virtual ~ItemCAO();
+
+ u8 getType() const
+ {
+ return ACTIVEOBJECT_TYPE_ITEM;
+ }
+
+ static ClientActiveObject* create();
+
void addToScene(scene::ISceneManager *smgr);
void removeFromScene();
void updateLight(u8 light_at_pos);
void processMessage(const std::string &data);
+ void initialize(const std::string &data);
+
+ core::aabbox3d<f32>* getSelectionBox()
+ {return &m_selection_box;}
+ v3f getPosition()
+ {return m_position;}
+
private:
+ core::aabbox3d<f32> m_selection_box;
scene::IMeshSceneNode *m_node;
v3f m_position;
+ std::string m_inventorystring;
};
#endif
m_random_spawn_timer -= dtime;
if(m_random_spawn_timer < 0)
{
- m_random_spawn_timer += myrand_range(2.0, 20.0);
+ //m_random_spawn_timer += myrand_range(2.0, 20.0);
+ m_random_spawn_timer += 2.0;
/*
Find some position
);
/*
- Create a TestSAO object
+ Create a ServerActiveObject
*/
- TestSAO *obj = new TestSAO(this, 0,
- v3f(myrand_range(-2*BS,2*BS), BS*5, myrand_range(-2*BS,2*BS)));
+ //TestSAO *obj = new TestSAO(this, 0, pos);
+ ServerActiveObject *obj = new ItemSAO(this, 0, pos, "CraftItem Stick 1");
// Add the object to the environment
addActiveObject(obj);
obj->processMessage(data);
}
+void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
+ core::array<DistanceSortedActiveObject> &dest)
+{
+ for(core::map<u16, ClientActiveObject*>::Iterator
+ i = m_active_objects.getIterator();
+ i.atEnd()==false; i++)
+ {
+ ClientActiveObject* obj = i.getNode()->getValue();
+
+ f32 d = (obj->getPosition() - origin).getLength();
+
+ if(d > max_d)
+ continue;
+
+ DistanceSortedActiveObject dso(obj, d);
+
+ dest.push_back(dso);
+ }
+}
+
+
#endif // #ifndef SERVER
void removeActiveObject(u16 id);
void processActiveObjectMessage(u16 id, const std::string &data);
-
+
+ // Get all nearby objects
+ void getActiveObjects(v3f origin, f32 max_d,
+ core::array<DistanceSortedActiveObject> &dest);
+
private:
ClientMap *m_map;
scene::ISceneManager *m_smgr;
Game content:\r
-------------\r
- When furnace is destroyed, move items to player's inventory\r
-- Add lots of stuff, no matter if they have really no real purpose.\r
+- Add lots of stuff\r
- Glass blocks\r
- Growing grass, decaying leaves\r
- - This can be done in the active blocks I guess.\r
- - Lots of stuff can be done in the active blocks.\r
- - Uh, is there an active block list somewhere?\r
+ - This can be done in the active blocks I guess.\r
+ - Lots of stuff can be done in the active blocks.\r
+ - Uh, is there an active block list somewhere? I think not. Add it.\r
- Player health points\r
- - When player dies, throw items on map\r
+ - When player dies, throw items on map (needs better item-on-map\r
+ implementation)\r
- Cobble to get mossy if near water\r
- More slots in furnace source list, so that multiple ingredients\r
are possible.\r
+- Keys to chests?\r
\r
Documentation:\r
--------------\r
Objects:\r
--------\r
\r
-TODO: Get rid of MapBlockObjects\r
+TODO: Get rid of MapBlockObjects and use ActiveObjects\r
\r
Map:\r
----\r
MapBlockObject *selected_object = client.getSelectedObject\r
(d*BS, camera_position, shootline);\r
\r
- /*\r
- If it's pointing to a MapBlockObject\r
- */\r
+ ClientActiveObject *selected_active_object\r
+ = client.getSelectedActiveObject\r
+ (d*BS, camera_position, shootline);\r
\r
if(selected_object != NULL)\r
{\r
}\r
}\r
}\r
+ else if(selected_active_object != NULL)\r
+ {\r
+ //dstream<<"Client returned selected_active_object != NULL"<<std::endl;\r
+ \r
+ core::aabbox3d<f32> *selection_box\r
+ = selected_active_object->getSelectionBox();\r
+ // Box should exist because it was returned in the first place\r
+ assert(selection_box);\r
+\r
+ v3f pos = selected_active_object->getPosition();\r
+\r
+ core::aabbox3d<f32> box_on_map(\r
+ selection_box->MinEdge + pos,\r
+ selection_box->MaxEdge + pos\r
+ );\r
+\r
+ hilightboxes.push_back(box_on_map);\r
+\r
+ infotext = narrow_to_wide("A ClientActiveObject");\r
+ //infotext = narrow_to_wide(selected_object->infoText());\r
+\r
+ if(g_input->getLeftClicked())\r
+ {\r
+ std::cout<<DTIME<<"Left-clicked object"<<std::endl;\r
+#if 0\r
+ client.clickObject(0, selected_object->getBlock()->getPos(),\r
+ selected_object->getId(), g_selected_item);\r
+#endif\r
+ }\r
+ else if(g_input->getRightClicked())\r
+ {\r
+ std::cout<<DTIME<<"Right-clicked object"<<std::endl;\r
+#if 0\r
+ /*\r
+ Check if we want to modify the object ourselves\r
+ */\r
+ if(selected_object->getTypeId() == MAPBLOCKOBJECT_TYPE_SIGN)\r
+ {\r
+ dstream<<"Sign object right-clicked"<<std::endl;\r
+ \r
+ if(random_input == false)\r
+ {\r
+ // Get a new text for it\r
+\r
+ TextDest *dest = new TextDestSign(\r
+ selected_object->getBlock()->getPos(),\r
+ selected_object->getId(),\r
+ &client);\r
+\r
+ SignObject *sign_object = (SignObject*)selected_object;\r
+\r
+ std::wstring wtext =\r
+ narrow_to_wide(sign_object->getText());\r
+\r
+ (new GUITextInputMenu(guienv, guiroot, -1,\r
+ &g_menumgr, dest,\r
+ wtext))->drop();\r
+ }\r
+ }\r
+ /*\r
+ Otherwise pass the event to the server as-is\r
+ */\r
+ else\r
+ {\r
+ client.clickObject(1, selected_object->getBlock()->getPos(),\r
+ selected_object->getId(), g_selected_item);\r
+ }\r
+#endif\r
+ }\r
+ }\r
else // selected_object == NULL\r
{\r
\r
SignNodeMetadata
*/
+// Prototype
+SignNodeMetadata proto_SignNodeMetadata("");
+
SignNodeMetadata::SignNodeMetadata(std::string text):
m_text(text)
{
ChestNodeMetadata
*/
+// Prototype
+ChestNodeMetadata proto_ChestNodeMetadata;
+
ChestNodeMetadata::ChestNodeMetadata()
{
NodeMetadata::registerType(typeId(), create);
FurnaceNodeMetadata
*/
+// Prototype
+FurnaceNodeMetadata proto_FurnaceNodeMetadata;
+
FurnaceNodeMetadata::FurnaceNodeMetadata()
{
NodeMetadata::registerType(typeId(), create);
//u16 peer_id = i.getNode()->getKey();
RemoteClient *client = i.getNode()->getValue();
Player *player = m_env.getPlayer(client->peer_id);
- std::cout<<player->getName()<<" ";
+ std::cout<<player->getName()<<"\t";
client->PrintInfo(std::cout);
}
}
Check added and deleted active objects
*/
{
+ //dstream<<"Server: Checking added and deleted active objects"<<std::endl;
+
JMutexAutoLock envlock(m_env_mutex);
JMutexAutoLock conlock(m_con_mutex);
RemoteClient *client = i.getNode()->getValue();
Player *player = m_env.getPlayer(client->peer_id);
if(player==NULL)
+ {
+ dstream<<"WARNING: "<<__FUNCTION_NAME<<": Client "<<client->peer_id
+ <<" has no associated player"<<std::endl;
continue;
+ }
v3s16 pos = floatToInt(player->getPosition(), BS);
core::map<u16, bool> removed_objects;
// Ignore if nothing happened
if(removed_objects.size() == 0 && added_objects.size() == 0)
+ {
+ //dstream<<"INFO: active objects: none changed"<<std::endl;
continue;
+ }
std::string data_buffer;
void PrintInfo(std::ostream &o)
{
o<<"RemoteClient "<<peer_id<<": "
- <<", m_blocks_sent.size()="<<m_blocks_sent.size()
+ <<"m_blocks_sent.size()="<<m_blocks_sent.size()
<<", m_blocks_sending.size()="<<m_blocks_sending.size()
<<", m_nearest_unsent_d="<<m_nearest_unsent_d
<<", m_excess_gotblocks="<<m_excess_gotblocks
}
+/*
+ ItemSAO
+*/
+
+ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string inventorystring):
+ ServerActiveObject(env, id, pos),
+ m_inventorystring(inventorystring)
+{
+ dstream<<"Server: ItemSAO created with inventorystring=\""
+ <<m_inventorystring<<"\""<<std::endl;
+}
+
+void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
+{
+}
+
+std::string ItemSAO::getClientInitializationData()
+{
+ dstream<<__FUNCTION_NAME<<std::endl;
+ std::string data;
+ data += itos(m_base_position.X);
+ data += ",";
+ data += itos(m_base_position.Y);
+ data += ",";
+ data += itos(m_base_position.Z);
+ data += ":";
+ data += m_inventorystring;
+ return data;
+}
+
// Number of players which know about this object
u16 m_known_by_count;
/*
- Whether this object is to be removed when nobody knows about
- it anymore.
- Removal is delayed to preserve the id for the time during which
- it could be confused to some other object by some client.
+ - Whether this object is to be removed when nobody knows about
+ it anymore.
+ - Removal is delayed to preserve the id for the time during which
+ it could be confused to some other object by some client.
+ - This is set to true by the step() method when the object wants
+ to be deleted.
*/
bool m_removed;
float m_age;
};
+class ItemSAO : public ServerActiveObject
+{
+public:
+ ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string inventorystring);
+ u8 getType() const
+ {
+ return ACTIVEOBJECT_TYPE_ITEM;
+ }
+ void step(float dtime, Queue<ActiveObjectMessage> &messages);
+ std::string getClientInitializationData();
+private:
+ std::string m_inventorystring;
+};
+
#endif