f0ef7d8d60225135ca31514305926e6fe3695377
[oweals/minetest.git] / src / serverobject.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "serverobject.h"
21 #include <fstream>
22 #include "environment.h"
23 #include "inventory.h"
24
25 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
26
27 ServerActiveObject::ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos):
28         ActiveObject(id),
29         m_known_by_count(0),
30         m_removed(false),
31         m_static_exists(false),
32         m_static_block(1337,1337,1337),
33         m_env(env),
34         m_base_position(pos)
35 {
36 }
37
38 ServerActiveObject::~ServerActiveObject()
39 {
40 }
41
42 ServerActiveObject* ServerActiveObject::create(u8 type,
43                 ServerEnvironment *env, u16 id, v3f pos,
44                 const std::string &data)
45 {
46         // Find factory function
47         core::map<u16, Factory>::Node *n;
48         n = m_types.find(type);
49         if(n == NULL)
50         {
51                 // If factory is not found, just return.
52                 dstream<<"WARNING: ServerActiveObject: No factory for type="
53                                 <<type<<std::endl;
54                 return NULL;
55         }
56
57         Factory f = n->getValue();
58         ServerActiveObject *object = (*f)(env, id, pos, data);
59         return object;
60 }
61
62 void ServerActiveObject::registerType(u16 type, Factory f)
63 {
64         core::map<u16, Factory>::Node *n;
65         n = m_types.find(type);
66         if(n)
67                 return;
68         m_types.insert(type, f);
69 }
70
71
72 /*
73         TestSAO
74 */
75
76 // Prototype
77 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
78
79 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
80         ServerActiveObject(env, id, pos),
81         m_timer1(0),
82         m_age(0)
83 {
84         ServerActiveObject::registerType(getType(), create);
85 }
86
87 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
88                 const std::string &data)
89 {
90         return new TestSAO(env, id, pos);
91 }
92
93 void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
94 {
95         m_age += dtime;
96         if(m_age > 10)
97         {
98                 m_removed = true;
99                 return;
100         }
101
102         m_base_position.Y += dtime * BS * 2;
103         if(m_base_position.Y > 8*BS)
104                 m_base_position.Y = 2*BS;
105
106         m_timer1 -= dtime;
107         if(m_timer1 < 0.0)
108         {
109                 m_timer1 += 0.125;
110                 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
111
112                 std::string data;
113
114                 data += itos(0); // 0 = position
115                 data += " ";
116                 data += itos(m_base_position.X);
117                 data += " ";
118                 data += itos(m_base_position.Y);
119                 data += " ";
120                 data += itos(m_base_position.Z);
121
122                 ActiveObjectMessage aom(getId(), false, data);
123                 messages.push_back(aom);
124         }
125 }
126
127
128 /*
129         ItemSAO
130 */
131
132 // Prototype
133 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
134
135 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
136                 const std::string inventorystring):
137         ServerActiveObject(env, id, pos),
138         m_inventorystring(inventorystring)
139 {
140         dstream<<"Server: ItemSAO created with inventorystring=\""
141                         <<m_inventorystring<<"\""<<std::endl;
142         ServerActiveObject::registerType(getType(), create);
143 }
144
145 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
146                 const std::string &data)
147 {
148         std::istringstream is(data, std::ios::binary);
149         char buf[1];
150         is.read(buf, 1); // read version
151         std::string inventorystring = deSerializeString(is);
152         dstream<<"ItemSAO::create(): Creating item \""
153                         <<inventorystring<<"\""<<std::endl;
154         return new ItemSAO(env, id, pos, inventorystring);
155 }
156
157 void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
158 {
159 }
160
161 std::string ItemSAO::getClientInitializationData()
162 {
163         dstream<<__FUNCTION_NAME<<std::endl;
164         std::string data;
165         data += itos(m_base_position.X);
166         data += ",";
167         data += itos(m_base_position.Y);
168         data += ",";
169         data += itos(m_base_position.Z);
170         data += ":";
171         data += m_inventorystring;
172         return data;
173 }
174
175 std::string ItemSAO::getStaticData()
176 {
177         dstream<<__FUNCTION_NAME<<std::endl;
178         std::ostringstream os(std::ios::binary);
179         char buf[1];
180         buf[0] = 0; //version
181         os.write(buf, 1);
182         os<<serializeString(m_inventorystring);
183         return os.str();
184 }
185
186 InventoryItem * ItemSAO::createInventoryItem()
187 {
188         try{
189                 std::istringstream is(m_inventorystring, std::ios_base::binary);
190                 InventoryItem *item = InventoryItem::deSerialize(is);
191                 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
192                                 <<m_inventorystring<<"\" -> item="<<item
193                                 <<std::endl;
194                 return item;
195         }
196         catch(SerializationError &e)
197         {
198                 dstream<<__FUNCTION_NAME<<": serialization error: "
199                                 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
200                 return NULL;
201         }
202 }
203
204
205