text/html; charset=UTF-8 Last-Modified: Tue, 24 Nov 2026 10:44:39 GMT Expires: Fri, 21 Nov 2036 10:44:39 GMT staticobject.h « src - minetest.git - modified minetest for gpcfs purposes
aboutsummaryrefslogtreecommitdiff
path: root/src/staticobject.h
blob: c3369af3cd6b5b2724bdc1828e77e239e2457af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Minetest-c55
Copyright (C) 2010-2011 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 STATICOBJECT_HEADER
#define STATICOBJECT_HEADER

#include "common_irrlicht.h"
#include <string>
#include <sstream>
#include "utility.h"

struct StaticObject
{
	u8 type;
	v3f pos;
	std::string data;

	StaticObject():
		type(0),
		pos(0,0,0)
	{
	}
	StaticObject(u8 type_, v3f pos_, const std::string &data_):
		type(type_),
		pos(pos_),
		data(data_)
	{
	}

	void serialize(std::ostream &os)
	{
		char buf[12];
		// type
		buf[0] = type;
		os.write(buf, 1);
		// pos
		writeV3S32((u8*)buf, v3s32(pos.X*1000,pos.Y*1000,pos.Z*1000));
		os.write(buf, 12);
		// data
		os<<serializeString(data);
	}
	void deSerialize(std::istream &is, u8 version)
	{
		char buf[12];
		// type
		is.read(buf, 1);
		type = buf[0];
		// pos
		is.read(buf, 12);
		v3s32 intp = readV3S32((u8*)buf);
		pos.X = (f32)intp.X/1000;
		pos.Y = (f32)intp.Y/1000;
		pos.Z = (f32)intp.Z/1000;
		// data
		data = deSerializeString(is);
	}
};

class StaticObjectList
{
public:
	/*
		Inserts an object to the container.
		Id must be unique or 0.
	*/
	void insert(u16 id, StaticObject obj)
	{
		if(id == 0)
		{
			m_stored.push_back(obj);
		}
		else
		{
			if(m_active.find(id) != NULL)
			{
				dstream<<"ERROR: StaticObjectList::insert(): "
						<<"id already exists"<<std::endl;
				assert(0);
				return;
			}
			m_active.insert(id, obj);
		}
	}

	void remove(u16 id)
	{
		assert(id != 0);
		if(m_active.find(id) == NULL)
		{
			dstream<<"WARNING: StaticObjectList::remove(): id="<<id
					<<" not found"<<std::endl;
			return;
		}
		m_active.remove(id);
	}

	void serialize(std::ostream &os)
	{
		char buf[12];
		// version
		buf[0] = 0;
		os.write(buf, 1);
		// count
		u16 count = m_stored.size() + m_active.size();
		writeU16((u8*)buf, count);
		os.write(buf, 2);
		for(core::list<StaticObject>::Iterator
				i = m_stored.begin();
				i != m_stored.end(); i++)
		{
			StaticObject &s_obj = *i;
			s_obj.serialize(os);
		}
		for(core::map<u16, StaticObject>::Iterator
				i = m_active.getIterator();
				i.atEnd()==false; i++)
		{
			StaticObject s_obj = i.getNode()->getValue();
			s_obj.serialize(os);
		}
	}
	void deSerialize(std::istream &is)
	{
		char buf[12];
		// version
		is.read(buf, 1);
		u8 version = buf[0];
		// count
		is.read(buf, 2);
		u16 count = readU16((u8*)buf);
		for(u16 i=0; i<count; i++)
		{
			StaticObject s_obj;
			s_obj.deSerialize(is, version);
			m_stored.push_back(s_obj);
		}
	}
	
	/*
		NOTE: When an object is transformed to active, it is removed
		from m_stored and inserted to m_active.
		The caller directly manipulates these containers.
	*/
	core::list<StaticObject> m_stored;
	core::map<u16, StaticObject> m_active;

private:
};

#endif

>1 << 4); JLO_B_PB(KeyType::HOTBAR_NEXT, bm | 1 << 5, fb | 1 << 5); // Accessible with start button and four pressed // TODO find usage for buttons 0, 1 and 4, 5 // Now about the buttons simulated by the axes // Movement buttons, important for vessels JLO_A_PB(KeyType::FORWARD, 1, 1, jlo.axes_deadzone); JLO_A_PB(KeyType::BACKWARD, 1, -1, jlo.axes_deadzone); JLO_A_PB(KeyType::LEFT, 0, 1, jlo.axes_deadzone); JLO_A_PB(KeyType::RIGHT, 0, -1, jlo.axes_deadzone); // Scroll buttons JLO_A_PB(KeyType::HOTBAR_PREV, 2, -1, jlo.axes_deadzone); JLO_A_PB(KeyType::HOTBAR_NEXT, 5, -1, jlo.axes_deadzone); return jlo; } JoystickLayout create_xbox_layout() { JoystickLayout jlo; jlo.axes_deadzone = 7000; const JoystickAxisLayout axes[JA_COUNT] = { {0, 1}, // JA_SIDEWARD_MOVE {1, 1}, // JA_FORWARD_MOVE {2, 1}, // JA_FRUSTUM_HORIZONTAL {3, 1}, // JA_FRUSTUM_VERTICAL }; memcpy(jlo.axes, axes, sizeof(jlo.axes)); // The back button means "ESC". JLO_B_PB(KeyType::ESC, 1 << 8, 1 << 8); // back JLO_B_PB(KeyType::ESC, 1 << 9, 1 << 9); // start // 4 Buttons JLO_B_PB(KeyType::JUMP, 1 << 0, 1 << 0); // A/green JLO_B_PB(KeyType::ESC, 1 << 1, 1 << 1); // B/red JLO_B_PB(KeyType::AUX1, 1 << 2, 1 << 2); // X/blue JLO_B_PB(KeyType::INVENTORY, 1 << 3, 1 << 3); // Y/yellow // Analog Sticks JLO_B_PB(KeyType::AUX1, 1 << 11, 1 << 11); // left JLO_B_PB(KeyType::SNEAK, 1 << 12, 1 << 12); // right // Triggers JLO_B_PB(KeyType::DIG, 1 << 6, 1 << 6); // lt JLO_B_PB(KeyType::PLACE, 1 << 7, 1 << 7); // rt JLO_B_PB(KeyType::HOTBAR_PREV, 1 << 4, 1 << 4); // lb JLO_B_PB(KeyType::HOTBAR_NEXT, 1 << 5, 1 << 5); // rb // D-PAD JLO_B_PB(KeyType::ZOOM, 1 << 15, 1 << 15); // up JLO_B_PB(KeyType::DROP, 1 << 13, 1 << 13); // left JLO_B_PB(KeyType::SCREENSHOT, 1 << 14, 1 << 14); // right JLO_B_PB(KeyType::FREEMOVE, 1 << 16, 1 << 16); // down // Movement buttons, important for vessels JLO_A_PB(KeyType::FORWARD, 1, 1, jlo.axes_deadzone); JLO_A_PB(KeyType::BACKWARD, 1, -1, jlo.axes_deadzone); JLO_A_PB(KeyType::LEFT, 0, 1, jlo.axes_deadzone); JLO_A_PB(KeyType::RIGHT, 0, -1, jlo.axes_deadzone); return jlo; } JoystickLayout create_dragonrise_gamecube_layout() { JoystickLayout jlo; jlo.axes_deadzone = 7000; const JoystickAxisLayout axes[JA_COUNT] = { // Control Stick {0, 1}, // JA_SIDEWARD_MOVE {1, 1}, // JA_FORWARD_MOVE // C-Stick {3, 1}, // JA_FRUSTUM_HORIZONTAL {4, 1}, // JA_FRUSTUM_VERTICAL }; memcpy(jlo.axes, axes, sizeof(jlo.axes)); // The center button JLO_B_PB(KeyType::ESC, 1 << 9, 1 << 9); // Start/Pause Button // Front right buttons JLO_B_PB(KeyType::JUMP, 1 << 2, 1 << 2); // A Button JLO_B_PB(KeyType::SNEAK, 1 << 3, 1 << 3); // B Button JLO_B_PB(KeyType::DROP, 1 << 0, 1 << 0); // Y Button JLO_B_PB(KeyType::AUX1, 1 << 1, 1 << 1); // X Button // Triggers JLO_B_PB(KeyType::DIG, 1 << 4, 1 << 4); // L Trigger JLO_B_PB(KeyType::PLACE, 1 << 5, 1 << 5); // R Trigger JLO_B_PB(KeyType::INVENTORY, 1 << 6, 1 << 6); // Z Button // D-Pad JLO_A_PB(KeyType::HOTBAR_PREV, 5, 1, jlo.axes_deadzone); // left JLO_A_PB(KeyType::HOTBAR_NEXT, 5, -1, jlo.axes_deadzone); // right // Axis are hard to actuate independantly, best to leave up and down unused. //JLO_A_PB(0, 6, 1, jlo.axes_deadzone); // up //JLO_A_PB(0, 6, -1, jlo.axes_deadzone); // down // Movements tied to Control Stick, important for vessels JLO_A_PB(KeyType::LEFT, 0, 1, jlo.axes_deadzone); JLO_A_PB(KeyType::RIGHT, 0, -1, jlo.axes_deadzone); JLO_A_PB(KeyType::FORWARD, 1, 1, jlo.axes_deadzone); JLO_A_PB(KeyType::BACKWARD, 1, -1, jlo.axes_deadzone); return jlo; } JoystickController::JoystickController() { doubling_dtime = std::max(g_settings->getFloat("repeat_joystick_button_time"), 0.001f); for (float &i : m_past_pressed_time) { i = 0; } m_layout.axes_deadzone = 0; clear(); } void JoystickController::onJoystickConnect(const std::vector<irr::SJoystickInfo> &joystick_infos) { s32 id = g_settings->getS32("joystick_id"); std::string layout = g_settings->get("joystick_type"); if (id < 0 || id >= (s32)joystick_infos.size()) { // TODO: auto detection id = 0; } if (id >= 0 && id < (s32)joystick_infos.size()) { if (layout.empty() || layout == "auto") setLayoutFromControllerName(joystick_infos[id].Name.c_str()); else setLayoutFromControllerName(layout); } // Irrlicht restriction. m_joystick_id = rangelim(id, 0, UINT8_MAX); } void JoystickController::setLayoutFromControllerName(const std::string &name) { if (lowercase(name).find("xbox") != std::string::npos) { m_layout = create_xbox_layout(); } else if (lowercase(name).find("dragonrise_gamecube") != std::string::npos) { m_layout = create_dragonrise_gamecube_layout(); } else { m_layout = create_default_layout(); } } bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev) { if (ev.Joystick != m_joystick_id) return false; m_internal_time = porting::getTimeMs() / 1000.f; std::bitset<KeyType::INTERNAL_ENUM_COUNT> keys_pressed; // First generate a list of keys pressed for (const auto &button_key : m_layout.button_keys) { if (button_key.isTriggered(ev)) { keys_pressed.set(button_key.key); } } for (const auto &axis_key : m_layout.axis_keys) { if (axis_key.isTriggered(ev)) { keys_pressed.set(axis_key.key); } } // Then update the values for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { if (keys_pressed[i]) { if (!m_past_keys_pressed[i] && m_past_pressed_time[i] < m_internal_time - doubling_dtime) { m_past_keys_pressed[i] = true; m_past_pressed_time[i] = m_internal_time; } } else if (m_keys_down[i]) { m_keys_released[i] = true; } if (keys_pressed[i] && !(m_keys_down[i])) m_keys_pressed[i] = true; m_keys_down[i] = keys_pressed[i]; } for (size_t i = 0; i < JA_COUNT; i++) { const JoystickAxisLayout &ax_la = m_layout.axes[i]; m_axes_vals[i] = ax_la.invert * ev.Axis[ax_la.axis_id]; } return true; } void JoystickController::clear() { m_keys_pressed.reset(); m_keys_down.reset(); m_past_keys_pressed.reset(); m_keys_released.reset(); memset(m_axes_vals, 0, sizeof(m_axes_vals)); } float JoystickController::getAxisWithoutDead(JoystickAxis axis) { s16 v = m_axes_vals[axis]; if (abs(v) < m_layout.axes_deadzone) return 0.0f; v += (v < 0 ? m_layout.axes_deadzone : -m_layout.axes_deadzone); return (float)v / ((float)(INT16_MAX - m_layout.axes_deadzone)); } float JoystickController::getMovementDirection() { return atan2(getAxisWithoutDead(JA_SIDEWARD_MOVE), -getAxisWithoutDead(JA_FORWARD_MOVE)); } float JoystickController::getMovementSpeed() { float speed = sqrt(pow(getAxisWithoutDead(JA_FORWARD_MOVE), 2) + pow(getAxisWithoutDead(JA_SIDEWARD_MOVE), 2)); if (speed > 1.0f) speed = 1.0f; return speed; }