aboutsummaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2015-05-15 00:03:19 +0200
committerest31 <MTest31@outlook.com>2015-05-15 00:06:18 +0200
commit178f536f082fd586a13d03ec9aa0775f1c009d5e (patch)
tree7f75d914abeab08a7621543f61421483ac955f8c /builtin
parentb70e67df8c55c0ccf3c95f9c45e194d1433ba53f (diff)
downloadminetest-178f536f082fd586a13d03ec9aa0775f1c009d5e.tar.gz
minetest-178f536f082fd586a13d03ec9aa0775f1c009d5e.tar.bz2
minetest-178f536f082fd586a13d03ec9aa0775f1c009d5e.zip
Item entity merging refactor
Don't ident too much, and add a comment.
Diffstat (limited to 'builtin')
-rw-r--r--builtin/game/item_entity.lua101
1 files changed, 56 insertions, 45 deletions
diff --git a/builtin/game/item_entity.lua b/builtin/game/item_entity.lua
index d6781feca..d848fd855 100644
--- a/builtin/game/item_entity.lua
+++ b/builtin/game/item_entity.lua
@@ -96,6 +96,56 @@ core.register_entity(":__builtin:item", {
self:set_item(self.itemstring)
end,
+ try_merge_with = function(self, own_stack, object, obj)
+ local stack = ItemStack(obj.itemstring)
+ if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
+ local overflow = false
+ local count = stack:get_count() + own_stack:get_count()
+ local max_count = stack:get_stack_max()
+ if count > max_count then
+ overflow = true
+ count = count - max_count
+ else
+ self.itemstring = ''
+ end
+ local pos = object:getpos()
+ pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
+ object:moveto(pos, false)
+ local s, c
+ local max_count = stack:get_stack_max()
+ local name = stack:get_name()
+ if not overflow then
+ obj.itemstring = name .. " " .. count
+ s = 0.2 + 0.1 * (count / max_count)
+ c = s
+ object:set_properties({
+ visual_size = {x = s, y = s},
+ collisionbox = {-c, -c, -c, c, c, c}
+ })
+ self.object:remove()
+ -- merging succeeded
+ return true
+ else
+ s = 0.4
+ c = 0.3
+ object:set_properties({
+ visual_size = {x = s, y = s},
+ collisionbox = {-c, -c, -c, c, c, c}
+ })
+ obj.itemstring = name .. " " .. max_count
+ s = 0.2 + 0.1 * (count / max_count)
+ c = s
+ self.object:set_properties({
+ visual_size = {x = s, y = s},
+ collisionbox = {-c, -c, -c, c, c, c}
+ })
+ self.itemstring = name .. " " .. count
+ end
+ end
+ -- merging didn't succeed
+ return false
+ end,
+
on_step = function(self, dtime)
self.age = self.age + dtime
if time_to_live > 0 and self.age > time_to_live then
@@ -111,52 +161,13 @@ core.register_entity(":__builtin:item", {
if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
if self.physical_state then
local own_stack = ItemStack(self.object:get_luaentity().itemstring)
- for _,object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
+ -- Merge with close entities of the same item
+ for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
local obj = object:get_luaentity()
- if obj and obj.name == "__builtin:item" and obj.physical_state == false then
- local stack = ItemStack(obj.itemstring)
- if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
- local overflow = false
- local count = stack:get_count() + own_stack:get_count()
- local max_count = stack:get_stack_max()
- if count>max_count then
- overflow = true
- count = count - max_count
- else
- self.itemstring = ''
- end
- local pos=object:getpos()
- pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
- object:moveto(pos, false)
- local s, c
- local max_count = stack:get_stack_max()
- local name = stack:get_name()
- if not overflow then
- obj.itemstring = name.." "..count
- s = 0.2 + 0.1 * (count / max_count)
- c = s
- object:set_properties({
- visual_size = {x = s, y = s},
- collisionbox = {-c, -c, -c, c, c, c}
- })
- self.object:remove()
- return
- else
- s = 0.4
- c = 0.3
- object:set_properties({
- visual_size = {x = s, y = s},
- collisionbox = {-c, -c, -c, c, c, c}
- })
- obj.itemstring = name.." "..max_count
- s = 0.2 + 0.1 * (count / max_count)
- c = s
- self.object:set_properties({
- visual_size = {x = s, y = s},
- collisionbox = {-c, -c, -c, c, c, c}
- })
- self.itemstring = name.." "..count
- end
+ if obj and obj.name == "__builtin:item"
+ and obj.physical_state == false then
+ if self:try_merge_with(own_stack, object, obj) then
+ return
end
end
end
> 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
/*
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 <cmath>
#include "util/enriched_string.h"
#include "util/numeric.h"
#include "util/string.h"

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

	void runTests(IGameDef *gamedef);

	void testAngleWrapAround();
	void testWrapDegrees_0_360_v3f();
	void testLowercase();
	void testTrim();
	void testIsYes();
	void testRemoveStringEnd();
	void testUrlEncode();
	void testUrlDecode();
	void testPadString();
	void testStartsWith();
	void testStrEqual();
	void testStringTrim();
	void testStrToIntConversion();
	void testStringReplace();
	void testStringAllowed();
	void testAsciiPrintableHelper();
	void testUTF8();
	void testRemoveEscapes();
	void testWrapRows();
	void testEnrichedString();
	void testIsNumber();
	void testIsPowerOfTwo();
	void testMyround();
	void testStringJoin();
	void testEulerConversion();
};

static TestUtilities g_test_instance;

void TestUtilities::runTests(IGameDef *gamedef)
{
	TEST(testAngleWrapAround);
	TEST(testWrapDegrees_0_360_v3f);
	TEST(testLowercase);
	TEST(testTrim);
	TEST(testIsYes);
	TEST(testRemoveStringEnd);
	TEST(testUrlEncode);
	TEST(testUrlDecode);
	TEST(testPadString);
	TEST(testStartsWith);
	TEST(testStrEqual);
	TEST(testStringTrim);
	TEST(testStrToIntConversion);
	TEST(testStringReplace);
	TEST(testStringAllowed);
	TEST(testAsciiPrintableHelper);
	TEST(testUTF8);
	TEST(testRemoveEscapes);
	TEST(testWrapRows);
	TEST(testEnrichedString);
	TEST(testIsNumber);
	TEST(testIsPowerOfTwo);
	TEST(testMyround);
	TEST(testStringJoin);
	TEST(testEulerConversion);
}

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

inline float ref_WrapDegrees180(float f)
{
	// This is a slower alternative to the wrapDegrees_180() function;
	// used as a reference for testing
	float value = fmodf(f + 180, 360);
	if (value < 0)
		value += 360;
	return value - 180;
}


inline float ref_WrapDegrees_0_360(float f)
{
	// This is a slower alternative to the wrapDegrees_0_360() function;
	// used as a reference for testing
	float value = fmodf(f, 360);
	if (value < 0)
		value += 360;
	return value < 0 ? value + 360 : value;
}


void TestUtilities::testAngleWrapAround() {
    UASSERT(fabs(modulo360f(100.0) - 100.0) < 0.001);
    UASSERT(fabs(modulo360f(720.5) - 0.5) < 0.001);
    UASSERT(fabs(modulo360f(-0.5) - (-0.5)) < 0.001);
    UASSERT(fabs(modulo360f(-365.5) - (-5.5)) < 0.001);

    for (float f = -720; f <= -360; f += 0.25) {
        UASSERT(std::fabs(modulo360f(f) - modulo360f(f + 360)) < 0.001);
    }

    for (float f = -1440; f <= 1440; f += 0.25) {
        UASSERT(std::fabs(modulo360f(f) - fmodf(f, 360)) < 0.001);
        UASSERT(std::fabs(wrapDegrees_180(f) - ref_WrapDegrees180(f)) < 0.001);
        UASSERT(std::fabs(wrapDegrees_0_360(f) - ref_WrapDegrees_0_360(f)) < 0.001);
        UASSERT(wrapDegrees_0_360(
                std::fabs(wrapDegrees_180(f) - wrapDegrees_0_360(f))) < 0.001);
    }

}

void TestUtilities::testWrapDegrees_0_360_v3f()
{
    // only x test with little step
	for (float x = -720.f; x <= 720; x += 0.05) {
        v3f r = wrapDegrees_0_360_v3f(v3f(x, 0, 0));
        UASSERT(r.X >= 0.0f && r.X < 360.0f)
        UASSERT(r.Y == 0.0f)
        UASSERT(r.Z == 0.0f)
    }

    // only y test with little step
    for (float y = -720.f; y <= 720; y += 0.05) {
        v3f r = wrapDegrees_0_360_v3f(v3f(0, y, 0));
        UASSERT(r.X == 0.0f)
        UASSERT(r.Y >= 0.0f && r.Y < 360.0f)
        UASSERT(r.Z == 0.0f)
    }

    // only z test with little step
    for (float z = -720.f; z <= 720; z += 0.05) {
        v3f r = wrapDegrees_0_360_v3f(v3f(0, 0, z));
        UASSERT(r.X == 0.0f)
        UASSERT(r.Y == 0.0f)
        UASSERT(r.Z >= 0.0f && r.Z < 360.0f)
	}

    // test the whole coordinate translation
    for (float x = -720.f; x <= 720; x += 2.5) {
        for (float y = -720.f; y <= 720; y += 2.5) {
            for (float z = -720.f; z <= 720; z += 2.5) {
                v3f r = wrapDegrees_0_360_v3f(v3f(x, y, z));
                UASSERT(r.X >= 0.0f && r.X < 360.0f)
                UASSERT(r.Y >= 0.0f && r.Y < 360.0f)
                UASSERT(r.Z >= 0.0f && r.Z < 360.0f)
            }
        }
    }
}


void TestUtilities::testLowercase()
{
	UASSERT(lowercase("Foo bAR") == "foo bar");
	UASSERT(lowercase("eeeeeeaaaaaaaaaaaààààà") == "eeeeeeaaaaaaaaaaaààààà");
	UASSERT(lowercase("MINETEST-powa") == "minetest-powa");
}


void TestUtilities::testTrim()
{
	UASSERT(trim("") == "");
	UASSERT(trim("dirt_with_grass") == "dirt_with_grass");
	UASSERT(trim("\n \t\r  Foo bAR  \r\n\t\t  ") == "Foo bAR");
	UASSERT(trim("\n \t\r    \r\n\t\t  ") == "");
}


void TestUtilities::testIsYes()
{
	UASSERT(is_yes("YeS") == true);
	UASSERT(is_yes("") == false);
	UASSERT(is_yes("FAlse") == false);
	UASSERT(is_yes("-1") == true);
	UASSERT(is_yes("0") == false);
	UASSERT(is_yes("1") == true);
	UASSERT(is_yes("2") == true);
}


void TestUtilities::testRemoveStringEnd()
{
	const char *ends[] = {"abc", "c", "bc", "", NULL};
	UASSERT(removeStringEnd("abc", ends) == "");
	UASSERT(removeStringEnd("bc", ends) == "b");
	UASSERT(removeStringEnd("12c", ends) == "12");
	UASSERT(removeStringEnd("foo", ends) == "");
}


void TestUtilities::testUrlEncode()
{
	UASSERT(urlencode("\"Aardvarks lurk, OK?\"")
			== "%22Aardvarks%20lurk%2C%20OK%3F%22");
}


void TestUtilities::testUrlDecode()
{
	UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22")
			== "\"Aardvarks lurk, OK?\"");
}


void TestUtilities::testPadString()
{
	UASSERT(padStringRight("hello", 8) == "hello   ");
}

void TestUtilities::testStartsWith()
{
	UASSERT(str_starts_with(std::string(), std::string()) == true);
	UASSERT(str_starts_with(std::string("the sharp pickaxe"),
		std::string()) == true);
	UASSERT(str_starts_with(std::string("the sharp pickaxe"),
		std::string("the")) == true);
	UASSERT(str_starts_with(std::string("the sharp pickaxe"),
		std::string("The")) == false);
	UASSERT(str_starts_with(std::string("the sharp pickaxe"),
		std::string("The"), true) == true);
	UASSERT(str_starts_with(std::string("T"), std::string("The")) == false);
}

void TestUtilities::testStrEqual()
{
	UASSERT(str_equal(utf8_to_wide("abc"), utf8_to_wide("abc")));
	UASSERT(str_equal(utf8_to_wide("ABC"), utf8_to_wide("abc"), true));
}


void TestUtilities::testStringTrim()
{
	UASSERT(trim("  a") == "a");
	UASSERT(trim("   a  ") == "a");
	UASSERT(trim("a   ") == "a");
	UASSERT(trim("") == "");
}


void TestUtilities::testStrToIntConversion()
{
	UASSERT(mystoi("123", 0, 1000) == 123);
	UASSERT(mystoi("123", 0, 10) == 10);
}


void TestUtilities::testStringReplace()
{
	std::string test_str;
	test_str = "Hello there";
	str_replace(test_str, "there", "world");
	UASSERT(test_str == "Hello world");
	test_str = "ThisAisAaAtest";
	str_replace(test_str, 'A', ' ');
	UASSERT(test_str == "This is a test");
}


void TestUtilities::testStringAllowed()
{
	UASSERT(string_allowed("hello", "abcdefghijklmno") == true);
	UASSERT(string_allowed("123", "abcdefghijklmno") == false);
	UASSERT(string_allowed_blacklist("hello", "123") == true);
	UASSERT(string_allowed_blacklist("hello123", "123") == false);
}

void TestUtilities::testAsciiPrintableHelper()
{
	UASSERT(IS_ASCII_PRINTABLE_CHAR('e') == true);
	UASSERT(IS_ASCII_PRINTABLE_CHAR('\0') == false);

	// Ensures that there is no cutting off going on...
	// If there were, 331 would be cut to 75 in this example
	// and 73 is a valid ASCII char.
	int ch = 331;
	UASSERT(IS_ASCII_PRINTABLE_CHAR(ch) == false);
}

void TestUtilities::testUTF8()
{
	UASSERT(utf8_to_wide("¤") == L"¤");

	UASSERT(wide_to_utf8(L"¤") == "¤");

	UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("")), "");
	UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("the shovel dug a crumbly node!")),
		"the shovel dug a crumbly node!");
	UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("-ä-")),
		"-ä-");
	UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("-\xF0\xA0\x80\x8B-")),
		"-\xF0\xA0\x80\x8B-");

}

void TestUtilities::testRemoveEscapes()
{
	UASSERT(unescape_enriched<wchar_t>(
		L"abc\x1bXdef") == L"abcdef");
	UASSERT(unescape_enriched<wchar_t>(
		L"abc\x1b(escaped)def") == L"abcdef");
	UASSERT(unescape_enriched<wchar_t>(
		L"abc\x1b((escaped with parenthesis\\))def") == L"abcdef");
	UASSERT(unescape_enriched<wchar_t>(
		L"abc\x1b(incomplete") == L"abc");
	UASSERT(unescape_enriched<wchar_t>(
		L"escape at the end\x1b") == L"escape at the end");
	// Nested escapes not supported
	UASSERT(unescape_enriched<wchar_t>(
		L"abc\x1b(outer \x1b(inner escape)escape)def") == L"abcescape)def");
}

void TestUtilities::testWrapRows()
{
	UASSERT(wrap_rows("12345678",4) == "1234\n5678");
	// test that wrap_rows doesn't wrap inside multibyte sequences
	{
		const unsigned char s[] = {
			0x2f, 0x68, 0x6f, 0x6d, 0x65, 0x2f, 0x72, 0x61, 0x70, 0x74, 0x6f,
			0x72, 0x2f, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x2f,
			0x6d, 0x69, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x62, 0x69,
			0x6e, 0x2f, 0x2e, 0x2e, 0};
		std::string str((char *)s);
		UASSERT(utf8_to_wide(wrap_rows(str, 20)) != L"<invalid UTF-8 string>");
	};
	{
		const unsigned char s[] = {
			0x74, 0x65, 0x73, 0x74, 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81,
			0xd1, 0x82, 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82,
			0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0};
		std::string str((char *)s);
		UASSERT(utf8_to_wide(wrap_rows(str, 8)) != L"<invalid UTF-8 string>");
	}
}

void TestUtilities::testEnrichedString()
{
	EnrichedString str(L"Test bar");
	irr::video::SColor color(0xFF, 0, 0, 0xFF);

	UASSERT(str.substr(1, 3).getString() == L"est");
	str += L" BUZZ";
	UASSERT(str.substr(9, std::string::npos).getString() == L"BUZZ");
	str.setDefaultColor(color); // Blue foreground
	UASSERT(str.getColors()[5] == color);
	// Green background, then white and yellow text
	str = L"\x1b(b@#0F0)Regular \x1b(c@#FF0)yellow";
	UASSERT(str.getColors()[2] == 0xFFFFFFFF);
	str.setDefaultColor(color); // Blue foreground
	UASSERT(str.getColors()[13] == 0xFFFFFF00); // Still yellow text
	UASSERT(str.getBackground() == 0xFF00FF00); // Green background
}

void TestUtilities::testIsNumber()
{
	UASSERT(is_number("123") == true);
	UASSERT(is_number("") == false);
	UASSERT(is_number("123a") == false);
}


void TestUtilities::testIsPowerOfTwo()
{
	UASSERT(is_power_of_two(0) == false);
	UASSERT(is_power_of_two(1) == true);
	UASSERT(is_power_of_two(2) == true);
	UASSERT(is_power_of_two(3) == false);
	for (int exponent = 2; exponent <= 31; ++exponent) {
		UASSERT(is_power_of_two((1 << exponent) - 1) == false);
		UASSERT(is_power_of_two((1 << exponent)) == true);
		UASSERT(is_power_of_two((1 << exponent) + 1) == false);
	}
	UASSERT(is_power_of_two(U32_MAX) == false);
}

void TestUtilities::testMyround()
{
	UASSERT(myround(4.6f) == 5);