Some progress on transitioning from MapBlockObject to ActiveObject.
[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
24 ServerActiveObject::ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos):
25         ActiveObject(id),
26         m_known_by_count(0),
27         m_removed(false),
28         m_env(env),
29         m_base_position(pos)
30 {
31 }
32
33 ServerActiveObject::~ServerActiveObject()
34 {
35 }
36
37 /*
38         TestSAO
39 */
40
41 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
42         ServerActiveObject(env, id, pos),
43         m_timer1(0),
44         m_age(0)
45 {
46 }
47
48 void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
49 {
50         m_age += dtime;
51         if(m_age > 10)
52         {
53                 m_removed = true;
54                 return;
55         }
56
57         m_base_position.Y += dtime * BS * 2;
58         if(m_base_position.Y > 8*BS)
59                 m_base_position.Y = 2*BS;
60
61         m_timer1 -= dtime;
62         if(m_timer1 < 0.0)
63         {
64                 m_timer1 += 0.125;
65                 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
66
67                 std::string data;
68
69                 data += itos(0); // 0 = position
70                 data += " ";
71                 data += itos(m_base_position.X);
72                 data += " ";
73                 data += itos(m_base_position.Y);
74                 data += " ";
75                 data += itos(m_base_position.Z);
76
77                 ActiveObjectMessage aom(getId(), false, data);
78                 messages.push_back(aom);
79         }
80 }
81
82
83 /*
84         ItemSAO
85 */
86
87 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
88                 const std::string inventorystring):
89         ServerActiveObject(env, id, pos),
90         m_inventorystring(inventorystring)
91 {
92         dstream<<"Server: ItemSAO created with inventorystring=\""
93                         <<m_inventorystring<<"\""<<std::endl;
94 }
95
96 void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
97 {
98 }
99
100 std::string ItemSAO::getClientInitializationData()
101 {
102         dstream<<__FUNCTION_NAME<<std::endl;
103         std::string data;
104         data += itos(m_base_position.X);
105         data += ",";
106         data += itos(m_base_position.Y);
107         data += ",";
108         data += itos(m_base_position.Z);
109         data += ":";
110         data += m_inventorystring;
111         return data;
112 }
113
114