diff --git a/scripts/eressea/init.lua b/scripts/eressea/init.lua index 11aa78596..184120850 100644 --- a/scripts/eressea/init.lua +++ b/scripts/eressea/init.lua @@ -1,9 +1,4 @@ -if config.paths ~= nil then - for path in string.gmatch(config.paths, "([^:]+)") do - package.path = package.path .. ';' .. path .. '/?.lua;' .. path .. '/?/init.lua' - end -end - +require 'eressea.path' require 'eressea.resources' require 'eressea.spells' diff --git a/scripts/eressea/path.lua b/scripts/eressea/path.lua new file mode 100644 index 000000000..de30d0d33 --- /dev/null +++ b/scripts/eressea/path.lua @@ -0,0 +1,6 @@ +if config.paths ~= nil then + for path in string.gmatch(config.paths, "([^:]+)") do + package.path = package.path .. ';' .. path .. '/?.lua;' .. path .. '/?/init.lua' + end +end +print(package.path) diff --git a/scripts/run-tests.lua b/scripts/run-tests.lua index 4953645b8..a230f8e97 100644 --- a/scripts/run-tests.lua +++ b/scripts/run-tests.lua @@ -7,7 +7,7 @@ end package.path = package.path .. ';' .. path .. '/?.lua;' .. path .. '/?/init.lua' require 'eressea' -require 'eressea.tests' +require 'eressea.path' +require 'tests' require 'lunit' -read_xml() lunit.main() diff --git a/scripts/tests/faction.lua b/scripts/tests/faction.lua new file mode 100644 index 000000000..cd414d60d --- /dev/null +++ b/scripts/tests/faction.lua @@ -0,0 +1,20 @@ +module ('tests.eressea.faction', package.seeall, lunit.testcase) + +local f + +function setup() + conf = [[{ + "races": { + "human" : {} + } + }]] + eressea.config.reset() + assert(eressea.config.parse(conf)==0) + f = faction.create("faction@eressea.de", "human", "de") +end + +function test_faction_flags() + assert_equal(0, f.flags) + f.flags = 42 + assert_equal(42, f.flags) +end diff --git a/scripts/tests/init.lua b/scripts/tests/init.lua new file mode 100644 index 000000000..6db26c375 --- /dev/null +++ b/scripts/tests/init.lua @@ -0,0 +1,15 @@ +-- new tests 2014-06-11 +require 'tests.pool' +require 'tests.settings' +require 'tests.config' +require 'tests.faction' +require 'tests.locale' +require 'tests.regions' +require 'tests.study' +require 'tests.movement' + +-- require 'tests.bugs' +-- require 'tests.building' +-- require 'tests.castles' +-- require 'tests.spells' +-- require 'tests.ships' diff --git a/scripts/tests/locale.lua b/scripts/tests/locale.lua new file mode 100644 index 000000000..2770beb3f --- /dev/null +++ b/scripts/tests/locale.lua @@ -0,0 +1,17 @@ +require "lunit" + +module("tests.eressea.locale", package.seeall, lunit.testcase ) + +function setup() + eressea.free_game() +end + +function test_get_set() + local loc = "en" + assert_not_nil(eressea.locale) + eressea.locale.create(loc) + assert_equal(nil, eressea.locale.get(loc, "move")) + eressea.locale.set(loc, "move", "MOVE") + assert_equal("MOVE", eressea.locale.get(loc, "move")) +end + diff --git a/scripts/tests/movement.lua b/scripts/tests/movement.lua new file mode 100644 index 000000000..e27831cc8 --- /dev/null +++ b/scripts/tests/movement.lua @@ -0,0 +1,117 @@ +require "lunit" + +module("tests.movement", package.seeall, lunit.testcase) + +function setup() + eressea.free_game() + eressea.settings.set("nmr.removenewbie", "0") + eressea.settings.set("nmr.timeout", "0") + eressea.settings.set("rules.ships.storms", "0") + conf = [[{ + "races": { + "human" : { + "speed" : 1, + "weight" : 1000, + "capacity" : 1500, + "flags" : [ "walk" ] + }, + "troll" : {} + }, + "items" : { + "horse" : { + "capacity" : 7000, + "weight" : 5000, + "flags" : [ "big", "animal" ] + } + }, + "terrains" : { + "ocean": { "flags" : [ "sea", "sail" ] }, + "plain": { "flags" : [ "land", "walk", "sail" ] }, + "glacier": { "flags" : [ "land", "walk" ] } + }, + "directions" : { + "de" : { + "east" : "OSTEN", + "west" : "WESTEN" + } + }, + "keywords" : { + "de" : { + "move" : "NACH" + } + } + }]] + + eressea.config.reset() + eressea.config.parse(conf) +end + +function test_walk_to_land() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + local f = faction.create("walk@example.com", "human", "de") + local u = unit.create(f, r1, 1) + u:add_order("NACH O") + process_orders() + assert_equal(r2, u.region) +end + +function test_walk_into_ocean_fails() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "ocean") + local f = faction.create("test@example.com", "human", "de") + local u = unit.create(f, r1, 1) + u:add_order("NACH O") + process_orders() + assert_equal(r1, u.region) +end + +function test_walk_distance() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + region.create(2, 0, "plain") + local f = faction.create("test@example.com", "human", "de") + local u = unit.create(f, r1, 1) + u:add_order("NACH O O") + process_orders() + assert_equal(r2, u.region) +end + +function test_ride_max_distance() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(2, 0, "plain") + region.create(1, 0, "plain") + region.create(3, 0, "plain") + local f = faction.create("test@example.com", "human", "de") + local u = unit.create(f, r1, 1) + u:add_item("horse", 1) + u:set_skill("riding", 2) + u:add_order("NACH O O O") + process_orders() + assert_equal(r2, u.region, "should ride exactly two hexes") +end + +function test_ride_over_capacity_leads_horse() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + region.create(2, 0, "plain") + local f = faction.create("test@example.com", "human", "de") + local u = unit.create(f, r1, 3) + u:add_item("horse", 1) + u:set_skill("riding", 2) + u:add_order("NACH O O") + process_orders() + assert_equal(r2, u.region) +end + +function test_ride_no_skill_leads_horse() + local r1 = region.create(0, 0, "plain") + local r2 = region.create(1, 0, "plain") + region.create(2, 0, "plain") + local f = faction.create("test@example.com", "human", "de") + local u = unit.create(f, r1, 1) + u:add_item("horse", 1) + u:add_order("NACH O O") + process_orders() + assert_equal(r2, u.region) +end diff --git a/scripts/tests/pool.lua b/scripts/tests/pool.lua new file mode 100644 index 000000000..311d3171d --- /dev/null +++ b/scripts/tests/pool.lua @@ -0,0 +1,75 @@ +require "lunit" + +module("tests.eressea.pool", package.seeall, lunit.testcase ) + +function setup() + eressea.game.reset() + eressea.config.reset(); + eressea.settings.set("rules.economy.food", "0") + eressea.settings.set("nmr.removenewbie", "0") + eressea.settings.set("nmr.timeout", "0") + eressea.settings.set("rules.magic.playerschools", "") + conf = [[{ + "races": { + "human" : { "flags" : [ "giveitem", "getitem" ] } + }, + "terrains" : { + "plain": { "flags" : [ "land" ] } + }, + "keywords" : { + "de" : { + "give" : "GIB", + "contact" : "KONTAKTIERE" + } + }, + "strings" : { + "de" : { + "money" : "Silber" + } + } + }]] + + assert(eressea.config.parse(conf)==0) +end + +function test_give_nopool() + local r = region.create(1, 1, "plain") + local f = faction.create("test@example.com", "human", "de") + local u1 = unit.create(f, r, 1) + local u2 = unit.create(f, r, 1) + u1:add_item("money", 100) + u1:add_order("GIB " .. itoa36(u2.id) .. " 50 SILBER") + process_orders() + assert_equal(50, u1:get_item("money")) + assert_equal(50, u2:get_item("money")) +end + +function test_give_from_faction() + local r = region.create(1, 1, "plain") + local f = faction.create("test@example.com", "human", "de") + local u1 = unit.create(f, r, 1) + local u2 = unit.create(f, r, 1) + local u3 = unit.create(f, r, 1) + u1:add_item("money", 50) + u2:add_item("money", 50) + u1:add_order("GIB " .. itoa36(u3.id) .. " 100 SILBER") + process_orders() + assert_equal(0, u1:get_item("money")) + assert_equal(0, u2:get_item("money")) + assert_equal(100, u3:get_item("money")) +end + +function test_give_divisor() + eressea.settings.set("rules.items.give_divisor", 2) + local r = region.create(1, 1, "plain") + local f1 = faction.create("test@example.com", "human", "de") + local f2 = faction.create("test@example.com", "human", "de") + local u1 = unit.create(f1, r, 1) + local u2 = unit.create(f2, r, 1) + u2:add_order("KONTAKTIERE " .. itoa36(u1.id)) + u1:add_item("money", 100) + u1:add_order("GIB " .. itoa36(u2.id) .. " 100 SILBER") + process_orders() + assert_equal(0, u1:get_item("money")) + assert_equal(50, u2:get_item("money")) +end diff --git a/scripts/tests/regions.lua b/scripts/tests/regions.lua new file mode 100644 index 000000000..32197f0fd --- /dev/null +++ b/scripts/tests/regions.lua @@ -0,0 +1,22 @@ +require "lunit" + +module("tests.regions", package.seeall, lunit.testcase) + +function setup() + eressea.game.reset() + conf = [[{ + "terrains" : { + "ocean": {}, + "plain": {} + } + }]] + eressea.config.reset() + eressea.settings.set('rules.magic.playerschools', '') + eressea.config.parse(conf) +end + +function test_create() + local r + r = region.create(0, 0, "ocean") + assert_not_nil(r) +end diff --git a/scripts/tests/settings.lua b/scripts/tests/settings.lua index a454a1152..8f2c5e637 100644 --- a/scripts/tests/settings.lua +++ b/scripts/tests/settings.lua @@ -1,13 +1,13 @@ -require "lunit" +require 'lunit' -module("tests.eressea.settings", package.seeall, lunit.testcase ) +module('tests.eressea.settings', package.seeall, lunit.testcase ) function setup() eressea.free_game() end function test_settings() - assert_equal(nil, eressea.settings.get("foo")) - eressea.settings.set("foo", "bar") - assert_equal("bar", eressea.settings.get("foo")) + assert_equal(nil, eressea.settings.get('foo')) + eressea.settings.set('foo', 'bar') + assert_equal('bar', eressea.settings.get('foo')) end diff --git a/scripts/tests/study.lua b/scripts/tests/study.lua new file mode 100644 index 000000000..7d27cc7d2 --- /dev/null +++ b/scripts/tests/study.lua @@ -0,0 +1,38 @@ +require "lunit" + +module("tests.eressea.study", package.seeall, lunit.testcase) + +function setup() + conf = [[{ + "races" : { "human" : {} }, + "terrains" : { "plain" : { "flags" : [ "land" ] } }, + "keywords" : { "de" : { "study": "LERNEN" } }, + "skills" : { "de": { "alchemy" : "Alchemie", "crossbow" : "Armbrust" } } + }]] + eressea.game.reset() + eressea.config.reset(); + eressea.settings.set('rules.magic.playerschools', '') + eressea.config.parse(conf) +end + +function test_study() + local r = region.create(0, 0, "plain") + local f = faction.create("test@example.com", "human", "de") + local u = unit.create(f, r, 1) + u:add_order("LERNEN Armbrust") + process_orders() + assert_equal(1, u:get_skill("crossbow")) +end + +function test_study_expensive() + local r = region.create(0, 0, "plain") + local f = faction.create("test@example.com", "human", "de") + local u = unit.create(f, r, 1) + eressea.settings.set("skills.cost.alchemy", "50") + eressea.settings.set("rules.encounters", "0") + u:add_order("LERNEN Alchemie") + u:add_item("money", 50) + process_orders() + assert_equal(1, u:get_skill("alchemy")) + assert_equal(0, u:get_item("money")) +end diff --git a/src/kernel/race.c b/src/kernel/race.c index 110dd4c5a..508b2d00b 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -146,26 +146,14 @@ void free_races(void) { } } -static const char *racealias[][2] = { - {"uruk", "orc"}, /* there was a time when the orc race was called uruk (and there were other orcs). That was really confusing */ - {"skeletton lord", "skeleton lord"}, /* we once had a typo here. it is fixed */ - {NULL, NULL} -}; - static race *rc_find_i(const char *name) { const char *rname = name; race *rc = races; - int i; - for (i = 0; racealias[i][0]; ++i) { - if (strcmp(racealias[i][0], name) == 0) { - rname = racealias[i][1]; - break; - } + while (rc && !strcmp(rname, rc->_name[0]) == 0) { + rc = rc->next; } - while (rc && !strcmp(rname, rc->_name[0]) == 0) - rc = rc->next; return rc; }