1 /*
2 serverobject.cpp
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 */
5 
6 /*
7 This file is part of Freeminer.
8 
9 Freeminer is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Freeminer  is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Freeminer.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "serverobject.h"
24 #include <fstream>
25 #include "inventory.h"
26 #include "constants.h" // BS
27 #include "environment.h"
28 
ServerActiveObject(ServerEnvironment * env,v3f pos)29 ServerActiveObject::ServerActiveObject(ServerEnvironment *env, v3f pos):
30 	ActiveObject(0),
31 	m_known_by_count(0),
32 	m_removed(false),
33 	m_pending_deactivation(false),
34 	m_static_exists(false),
35 	m_static_block(1337,1337,1337),
36 	m_messages_out(env->m_active_object_messages),
37 	m_uptime_last(0),
38 	m_env(env),
39 	m_base_position(pos)
40 {
41 }
42 
~ServerActiveObject()43 ServerActiveObject::~ServerActiveObject()
44 {
45 }
46 
create(u8 type,ServerEnvironment * env,u16 id,v3f pos,const std::string & data)47 ServerActiveObject* ServerActiveObject::create(u8 type,
48 		ServerEnvironment *env, u16 id, v3f pos,
49 		const std::string &data)
50 {
51 	// Find factory function
52 	std::map<u16, Factory>::iterator n;
53 	n = m_types.find(type);
54 	if(n == m_types.end())
55 	{
56 		// If factory is not found, just return.
57 		dstream<<"WARNING: ServerActiveObject: No factory for type="
58 				<<type<<std::endl;
59 		return NULL;
60 	}
61 
62 	Factory f = n->second;
63 	ServerActiveObject *object = (*f)(env, pos, data);
64 	return object;
65 }
66 
registerType(u16 type,Factory f)67 void ServerActiveObject::registerType(u16 type, Factory f)
68 {
69 	std::map<u16, Factory>::iterator n;
70 	n = m_types.find(type);
71 	if(n != m_types.end())
72 		return;
73 	m_types[type] = f;
74 }
75 
getMinimumSavedMovement()76 float ServerActiveObject::getMinimumSavedMovement()
77 {
78 	return 2.0*BS;
79 }
80 
getWieldedItem() const81 ItemStack ServerActiveObject::getWieldedItem() const
82 {
83 	const Inventory *inv = getInventory();
84 	if(inv)
85 	{
86 		const InventoryList *list = inv->getList(getWieldList());
87 		if(list && (getWieldIndex() < (s32)list->getSize()))
88 			return list->getItem(getWieldIndex());
89 	}
90 	return ItemStack();
91 }
92 
setWieldedItem(const ItemStack & item)93 bool ServerActiveObject::setWieldedItem(const ItemStack &item)
94 {
95 	Inventory *inv = getInventory();
96 	if(inv)
97 	{
98 		InventoryList *list = inv->getList(getWieldList());
99 		if (list)
100 		{
101 			list->changeItem(getWieldIndex(), item);
102 			setInventoryModified();
103 			return true;
104 		}
105 	}
106 	return false;
107 }
108 
109