summaryrefslogtreecommitdiff
path: root/src/unittest/test_collision.cpp
blob: 332d3fa13f7210f698fbff60978a9bc7c7970ca0 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Minetest
Copyright (C) 2013 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 Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser 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.
*/

#include "test.h"

#include "collision.h"

class TestCollision : public TestBase {
public:
	TestCollision() { TestManager::registerTestModule(this); }
	const char *getName() { return "TestCollision"; }

	void runTests(IGameDef *gamedef);

	void testAxisAlignedCollision();
};

static TestCollision g_test_instance;

void TestCollision::runTests(IGameDef *gamedef)
{
	TEST(testAxisAlignedCollision);
}

////////////////////////////////////////////////////////////////////////////////

void TestCollision::testAxisAlignedCollision()
{
	for (s16 bx = -3; bx <= 3; bx++)
	for (s16 by = -3; by <= 3; by++)
	for (s16 bz = -3; bz <= 3; bz++) {
		// X-
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
			v3f v(1, 0, 0);
			f32 dtime = 0;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 0);
			UASSERT(fabs(dtime - 1.000) < 0.001);
		}
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
			v3f v(-1, 0, 0);
			f32 dtime = 0;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == -1);
		}
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
			v3f v(1, 0, 0);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == -1);
		}
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
			v3f v(0.5, 0.1, 0);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 0);
			UASSERT(fabs(dtime - 3.000) < 0.001);
		}
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
			v3f v(0.5, 0.1, 0);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 0);
			UASSERT(fabs(dtime - 3.000) < 0.001);
		}

		// X+
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
			v3f v(-1, 0, 0);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 0);
			UASSERT(fabs(dtime - 1.000) < 0.001);
		}
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
			v3f v(1, 0, 0);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == -1);
		}
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
			v3f v(-1, 0, 0);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == -1);
		}
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
			v3f v(-0.5, 0.2, 0);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 1);  // Y, not X!
			UASSERT(fabs(dtime - 2.500) < 0.001);
		}
		{
			aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
			aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
			v3f v(-0.5, 0.3, 0);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 0);
			UASSERT(fabs(dtime - 2.000) < 0.001);
		}

		// TODO: Y-, Y+, Z-, Z+

		// misc
		{
			aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
			aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
			v3f v(-1./3, -1./3, -1./3);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 0);
			UASSERT(fabs(dtime - 0.9) < 0.001);
		}
		{
			aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
			aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
			v3f v(-1./3, -1./3, -1./3);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 1);
			UASSERT(fabs(dtime - 0.9) < 0.001);
		}
		{
			aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
			aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
			v3f v(-1./3, -1./3, -1./3);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 2);
			UASSERT(fabs(dtime - 0.9) < 0.001);
		}
		{
			aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
			aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
			v3f v(1./7, 1./7, 1./7);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 0);
			UASSERT(fabs(dtime - 16.1) < 0.001);
		}
		{
			aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
			aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
			v3f v(1./7, 1./7, 1./7);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 1);
			UASSERT(fabs(dtime - 16.1) < 0.001);
		}
		{
			aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
			aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
			v3f v(1./7, 1./7, 1./7);
			f32 dtime;
			UASSERT(axisAlignedCollision(s, m, v, 0, &dtime) == 2);
			UASSERT(fabs(dtime - 16.1) < 0.001);
		}
	}
}
s, listname, index, stack, player) -- We need to find out if the circular_saw is already set to a -- specific material or not: local meta = minetest.get_meta(pos) local inv = meta:get_inventory() local stackname = stack:get_name() local count = stack:get_count() -- Putting something into the input slot is only possible if that had -- been empty before or did contain something of the same material: if listname == "input" then -- Each new block is worth 8 microblocks: circular_saw:update_inventory(pos, 8 * count) elseif listname == "recycle" then -- Lets look which shape this represents: local cost = circular_saw:get_cost(inv, stackname) circular_saw:update_inventory(pos, cost * count) end end function circular_saw.on_metadata_inventory_take( pos, listname, index, stack, player) -- If it is one of the offered stairs: find out how many -- microblocks have to be substracted: if listname == "output" then -- We do know how much each block at each position costs: local cost = circular_saw.cost_in_microblocks[index] * stack:get_count() circular_saw:update_inventory(pos, -cost) elseif listname == "micro" then -- Each microblock costs 1 microblock: circular_saw:update_inventory(pos, -stack:get_count()) elseif listname == "input" then -- Each normal (= full) block taken costs 8 microblocks: circular_saw:update_inventory(pos, 8 * -stack:get_count()) end -- The recycle field plays no role here since it is processed immediately. end gui_slots = "listcolors[#606060AA;#808080;#101010;#202020;#FFF]" function circular_saw.on_construct(pos) local meta = minetest.get_meta(pos) meta:set_string("formspec", "size[11,9]" ..gui_slots.. "label[0,0;" ..S("Input\nmaterial").. "]" .. "list[current_name;input;1.5,0;1,1;]" .. "label[0,1;" ..S("Left-over").. "]" .. "list[current_name;micro;1.5,1;1,1;]" .. "label[0,2;" ..S("Recycle\noutput").. "]" .. "list[current_name;recycle;1.5,2;1,1;]" .. "field[0.3,3.5;1,1;max_offered;" ..S("Max").. ":;${max_offered}]" .. "button[1,3.2;1,1;Set;" ..S("Set").. "]" .. "list[current_name;output;2.8,0;8,4;]" .. "list[current_player;main;1.5,5;8,4;]") meta:set_int("anz", 0) -- No microblocks inside yet. meta:set_string("max_offered", 99) -- How many items of this kind are offered by default? meta:set_string("infotext", S("Circular Saw is empty")) local inv = meta:get_inventory() inv:set_size("input", 1) -- Input slot for full blocks of material x. inv:set_size("micro", 1) -- Storage for 1-7 surplus microblocks. inv:set_size("recycle", 1) -- Surplus partial blocks can be placed here. inv:set_size("output", 4*8) -- 4x8 versions of stair-parts of material x. circular_saw:reset(pos) end function circular_saw.can_dig(pos,player) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() if not inv:is_empty("input") or not inv:is_empty("micro") or not inv:is_empty("recycle") then return false end -- Can be dug by anyone when empty, not only by the owner: return true end minetest.register_node("moreblocks:circular_saw", { description = S("Circular Saw"), drawtype = "nodebox", node_box = { type = "fixed", fixed = { {-0.4, -0.5, -0.4, -0.25, 0.25, -0.25}, -- Leg {0.25, -0.5, 0.25, 0.4, 0.25, 0.4}, -- Leg {-0.4, -0.5, 0.25, -0.25, 0.25, 0.4}, -- Leg {0.25, -0.5, -0.4, 0.4, 0.25, -0.25}, -- Leg {-0.5, 0.25, -0.5, 0.5, 0.375, 0.5}, -- Tabletop {-0.01, 0.4375, -0.125, 0.01, 0.5, 0.125}, -- Saw blade (top) {-0.01, 0.375, -0.1875, 0.01, 0.4375, 0.1875}, -- Saw blade (bottom) {-0.25, -0.0625, -0.25, 0.25, 0.25, 0.25}, -- Motor case }, }, tiles = {"moreblocks_circular_saw_top.png", "moreblocks_circular_saw_bottom.png", "moreblocks_circular_saw_side.png"}, paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", groups = {choppy = 2,oddly_breakable_by_hand = 2}, sounds = default.node_sound_wood_defaults(), on_construct = circular_saw.on_construct, can_dig = circular_saw.can_dig, -- Set the owner of this circular saw. after_place_node = function(pos, placer) local meta = minetest.get_meta(pos) local owner = placer and placer:get_player_name() or "" meta:set_string("owner", owner) meta:set_string("infotext", S("Circular Saw is empty (owned by %s)") :format(owner)) end, -- The amount of items offered per shape can be configured: on_receive_fields = circular_saw.on_receive_fields, allow_metadata_inventory_move = circular_saw.allow_metadata_inventory_move, -- Only input- and recycle-slot are intended as input slots: allow_metadata_inventory_put = circular_saw.allow_metadata_inventory_put, -- Taking is allowed from all slots (even the internal microblock slot). Moving is forbidden. -- Putting something in is slightly more complicated than taking anything because we have to make sure it is of a suitable material: on_metadata_inventory_put = circular_saw.on_metadata_inventory_put, on_metadata_inventory_take = circular_saw.on_metadata_inventory_take, })