diff --git a/s/setup b/s/setup index 1cb6a1867..69584d53b 100755 --- a/s/setup +++ b/s/setup @@ -91,7 +91,8 @@ ini_add lua install $SOURCE ini_add lua paths $SOURCE/scripts:$SOURCE/lunit ini_add lua rules $rules -ln -f $SOURCE/bin/eressea -ln -f $SOURCE/scripts/run-turn.lua -ln -f $SOURCE/scripts/reports.lua -ln -f $SOURCE/scripts/config.lua +touch newfactions +ln -sf $SOURCE/bin/eressea +ln -sf $SOURCE/scripts/run-turn.lua +ln -sf $SOURCE/scripts/reports.lua +ln -sf $SOURCE/scripts/config.lua diff --git a/scripts/newplayer.lua b/scripts/newplayer.lua index fa8e9f18f..c01593465 100644 --- a/scripts/newplayer.lua +++ b/scripts/newplayer.lua @@ -1,22 +1,38 @@ -function seed(r, email, race, lang) +dofile("config.lua") +p = require("populate") + +local function read_players() +-- return {{ email = "noreply@mailinator.com", race = "dwarf", lang = "de" }} + local players = {} + local input = open("newfactions", "r") + while input do + local str = input:read("*line") + if str==nil then break end + local email, race, lang = str:match("([^ ]*) ([^ ]*) ([^ ]*)") + table.insert(players, { race = race, lang = lang, email = email }) + end + return players +end + +local function seed(r, email, race, lang) local f = faction.create(email, race, lang) local u = unit.create(f, r) u:set_skill("perception", 30) - u:add_item("money", 10000) + u:add_item("money", 20000) items = { - log = 50, - stone = 50, - iron = 50, - laen = 10, - mallorn = 10, - skillpotion = 5 + log = 50, + stone = 50, + iron = 50, + laen = 10, + mallorn = 10, + skillpotion = 5 } for it, num in pairs(items) do u:add_item(it, num) end + u = nil skills ={ "crossbow", - "mining", "bow", "building", "trade", @@ -31,58 +47,80 @@ function seed(r, email, race, lang) "sailing", "polearm", "espionage", - "quarrying", "roadwork", "tactics", "stealth", - "entertainment", "weaponsmithing", "cartmaking", "taxation", "stamina" } + unit.create(f, r, 50):set_skill("entertainment", 15) + unit.create(f, r, 5):set_skill("mining", 30) + unit.create(f, r, 5):set_skill("quarrying", 30) for _, sk in ipairs(skills) do - u = unit.create(f, r, 5) - u:set_skill(sk, 15) + u = u or unit.create(f, r, 5) + if u:set_skill(sk, 15)>0 then u=nil end end return f end -turn = 923 -dofile("config.lua") -p = require("populate") -eressea.read_game(("%d.dat"):format(turn)) -best = { score = 0, r = nil } -limit = 30000 -sel = p.select(regions(), limit) -for _, r in ipairs(sel) do - score = p.score(r) - if score > best.score then - best.r = r - best.score = score +local function dump_selection(sel) + local best = { score = 0, r = nil } + local r, score + for _, r in ipairs(sel) do + score = p.score(r) + if score > best.score then + best.r = r + best.score = score + end + print(score, r, r.terrain) + end + return best +end + +players = read_players() +local limit = 30000 +local turn = get_turn() +local sel +if #players > 0 then + read_game(turn) + eressea.read_game(("%d.dat"):format(turn)) + sel = p.select(regions(), limit) + if #sel > 0 then + local best = dump_selection(sel) + print("finest region, " .. best.score .. " points: " .. tostring(best.r)) end - print(score, r, r.terrain) end --- print(best.r, best.score) math.randomseed(os.time()) -print(#sel, limit) - -players = { -{ email = "Reinhardt.Karnapke@Freenet.de", race = "dwarf", lang = "de" } -} - +local newbs = {} for _, p in ipairs(players) do local index = math.random(#sel) local start = nil while not start or start.units() do start = sel[index] end - f = seed(start, p.email, p.race or "human", p.lang or "de") - print(f, start) - init_reports() - write_report(f) + local dupe = false + for f in factions() do + if f.email==p.email then + print("seed: duplicate email " .. p.email .. " already used by faction " .. tostring(f)) + dupe = true + break + end + end + if not dupe then + f = seed(start, p.email, p.race or "human", p.lang or "de") + print("new faction ".. tostring(f) .. " starts in ".. tostring(start)) + table.insert(newbs, f) + end end -eressea.write_game(("%d.dat.new"):format(turn)) +if #newbs > 0 then + init_reports() + for _, f in ipairs(newbs) do + write_report(f) + end + eressea.write_game(("%d.dat.new"):format(turn)) +end diff --git a/scripts/tests/study.lua b/scripts/tests/study.lua index 7d27cc7d2..e5ab84018 100644 --- a/scripts/tests/study.lua +++ b/scripts/tests/study.lua @@ -7,7 +7,8 @@ function setup() "races" : { "human" : {} }, "terrains" : { "plain" : { "flags" : [ "land" ] } }, "keywords" : { "de" : { "study": "LERNEN" } }, - "skills" : { "de": { "alchemy" : "Alchemie", "crossbow" : "Armbrust" } } + "skills" : { "de": { "alchemy" : "Alchemie", "crossbow" : "Armbrust" } }, + "spells" : { "fireball" : { "syntax" : "u+" } } }]] eressea.game.reset() eressea.config.reset(); @@ -36,3 +37,17 @@ function test_study_expensive() assert_equal(1, u:get_skill("alchemy")) assert_equal(0, u:get_item("money")) end + +function test_unit_spells() + local r = region.create(0, 0, "plain") + local f = faction.create("test@example.com", "human", "de") + local u = unit.create(f, r, 1) + u.magic = "gray" + u:set_skill("magic", 1) + u:add_spell("toast") + assert_equal(nil, u.spells) + u:add_spell("fireball", 2) + local sp = u.spells() + assert_equal("fireball", sp.name) + assert_equal(2, sp.level) +end diff --git a/src/bind_faction.c b/src/bind_faction.c index 697a164d9..010bd0f12 100644 --- a/src/bind_faction.c +++ b/src/bind_faction.c @@ -297,7 +297,7 @@ static int tolua_faction_set_origin(lua_State * L) plane *pl = rplane(r); int id = pl ? pl->id : 0; - set_ursprung(f, id, r->x - plane_center_x(pl), r->y - plane_center_y(pl)); + set_origin(f, id, r->x - plane_center_x(pl), r->y - plane_center_y(pl)); return 0; } @@ -507,14 +507,6 @@ static int tolua_faction_tostring(lua_State * L) return 1; } -#ifdef TODO /* these usertypes are undefined */ -static int tolua_faction_get_spells(lua_State * L) -{ - faction *self = (faction *) tolua_tousertype(L, 1, 0); - return tolua_quicklist_push(L, "spellbook", "spellbook_entry", self->spellbook->spells); -} -#endif - void tolua_faction_open(lua_State * L) { /* register user types */ @@ -544,9 +536,6 @@ void tolua_faction_open(lua_State * L) &tolua_faction_set_info); tolua_variable(L, TOLUA_CAST "units", tolua_faction_get_units, NULL); tolua_variable(L, TOLUA_CAST "heroes", tolua_faction_get_heroes, NULL); -#ifdef TODO - tolua_variable(L, TOLUA_CAST "spells", tolua_faction_get_spells, 0); -#endif tolua_variable(L, TOLUA_CAST "maxheroes", tolua_faction_get_maxheroes, NULL); tolua_variable(L, TOLUA_CAST "password", tolua_faction_get_password, diff --git a/src/bind_unit.c b/src/bind_unit.c index 5e06cab37..4f262114c 100755 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -560,7 +560,7 @@ static int tolua_unit_addspell(lua_State * L) spell *sp = find_spell(str); if (!sp) { - log_error("spell %s could not be found\n", str); + log_warning("spell %s could not be found\n", str); return EINVAL; } else { @@ -758,23 +758,22 @@ static int tolua_unit_get_items(lua_State * L) return 1; } -#ifdef TODO /* spellbooks */ static int tolua_unit_get_spells(lua_State * L) { unit *self = (unit *) tolua_tousertype(L, 1, 0); - sc_mage *mage = get_mage(self); + sc_mage *mage = self ? get_mage(self) : 0; + spellbook *sb = mage ? mage->spellbook : 0; quicklist *slist = 0; - - if (mage) { - quicklist **slist_ptr = get_spelllist(mage, self->faction); + if (sb) { + quicklist **slist_ptr = &sb->spells; if (slist_ptr) { slist = *slist_ptr; } } - - return tolua_quicklist_push(L, "spell_list", "spell", slist); + return tolua_quicklist_push(L, "spellbook", "spell_entry", slist); } +#ifdef TODO /* spellbooks */ static void unit_removespell(unit * u, spell * sp) { quicklist **isptr; @@ -1034,8 +1033,8 @@ void tolua_unit_open(lua_State * L) tolua_function(L, TOLUA_CAST "add_spell", &tolua_unit_addspell); #ifdef TODO /* spellbooks */ tolua_function(L, TOLUA_CAST "remove_spell", &tolua_unit_removespell); - tolua_variable(L, TOLUA_CAST "spells", &tolua_unit_get_spells, 0); #endif + tolua_variable(L, TOLUA_CAST "spells", &tolua_unit_get_spells, 0); tolua_function(L, TOLUA_CAST "cast_spell", &tolua_unit_castspell); tolua_variable(L, TOLUA_CAST "magic", &tolua_unit_get_magic, diff --git a/src/bindings.c b/src/bindings.c index 0385506cf..169c91d0c 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -47,6 +47,7 @@ without prior permission by the authors of Eressea. #include #include #include +#include #include "creport.h" #include "economy.h" @@ -988,9 +989,22 @@ static int tolua_get_spell_level(lua_State * L) static int tolua_get_spell_name(lua_State * L) { - const struct locale *lang = default_locale; spell *self = (spell *)tolua_tousertype(L, 1, 0); - lua_pushstring(L, spell_name(self, lang)); + lua_pushstring(L, self->sname); + return 1; +} + +static int tolua_get_spell_entry_name(lua_State * L) +{ + spellbook_entry *self = (spellbook_entry*)tolua_tousertype(L, 1, 0); + lua_pushstring(L, self->sp->sname); + return 1; +} + +static int tolua_get_spell_entry_level(lua_State * L) +{ + spellbook_entry *self = (spellbook_entry*)tolua_tousertype(L, 1, 0); + lua_pushinteger(L, self->level); return 1; } @@ -1085,6 +1099,8 @@ int tolua_bindings_open(lua_State * L) tolua_bind_open(L); /* register user types */ + tolua_usertype(L, TOLUA_CAST "spellbook"); + tolua_usertype(L, TOLUA_CAST "spell_entry"); tolua_usertype(L, TOLUA_CAST "spell"); tolua_usertype(L, TOLUA_CAST "spell_list"); tolua_usertype(L, TOLUA_CAST "order"); @@ -1112,12 +1128,16 @@ int tolua_bindings_open(lua_State * L) { tolua_function(L, TOLUA_CAST "__tostring", tolua_get_spell_name); tolua_variable(L, TOLUA_CAST "name", tolua_get_spell_name, 0); -#ifdef TODO - tolua_variable(L, TOLUA_CAST "school", tolua_get_spell_school, 0); - tolua_variable(L, TOLUA_CAST "level", tolua_get_spell_level, 0); -#endif tolua_variable(L, TOLUA_CAST "text", tolua_get_spell_text, 0); } tolua_endmodule(L); + tolua_cclass(L, TOLUA_CAST "spell_entry", TOLUA_CAST "spell_entry", TOLUA_CAST "", + NULL); + tolua_beginmodule(L, TOLUA_CAST "spell_entry"); + { + tolua_function(L, TOLUA_CAST "__tostring", tolua_get_spell_entry_name); + tolua_variable(L, TOLUA_CAST "name", tolua_get_spell_entry_name, 0); + tolua_variable(L, TOLUA_CAST "level", tolua_get_spell_entry_level, 0); + } tolua_endmodule(L); tolua_module(L, TOLUA_CAST "report", 1); tolua_beginmodule(L, TOLUA_CAST "report"); { diff --git a/src/gmtool.c b/src/gmtool.c index 91134e8b5..fc9fccdb9 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -929,6 +929,7 @@ static void handlekey(state * st, int c) } } break; + case 'f': case 0x14: /* C-t */ terraform_at(&st->cursor, select_terrain(st, NULL)); st->modified = 1; @@ -1011,6 +1012,7 @@ static void handlekey(state * st, int c) statusline(st->wnd_status->handle, "tag-"); doupdate(); switch (getch()) { + case 'f': case 't': terraform_selection(st->selected, select_terrain(st, NULL)); st->modified = 1; diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 139f0e06c..72643c3cb 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -257,7 +257,7 @@ unit *addplayer(region * r, faction * f) char buffer[32]; assert(f->units == NULL); - set_ursprung(f, 0, r->x, r->y); + set_origin(f, 0, r->x, r->y); u = create_unit(r, f, 1, f->race, 0, NULL, NULL); equip_items(&u->faction->items, get_equipment("new_faction")); equip_unit(u, get_equipment("first_unit")); diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 94c6a77c1..4852b08dc 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -109,6 +110,20 @@ static void test_get_monsters(CuTest *tc) { test_cleanup(); } +static void test_set_origin(CuTest *tc) { + faction *f; + + test_cleanup(); + test_create_world(); + f = test_create_faction(0); + CuAssertPtrEquals(tc, 0, f->ursprung); + set_origin(f, 0, 1, 1); + CuAssertIntEquals(tc, 0, f->ursprung->id); + CuAssertIntEquals(tc, 1, f->ursprung->x); + CuAssertIntEquals(tc, 1, f->ursprung->y); + test_cleanup(); +} + CuSuite *get_faction_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -117,5 +132,6 @@ CuSuite *get_faction_suite(void) SUITE_ADD_TEST(suite, test_remove_empty_factions_allies); SUITE_ADD_TEST(suite, test_remove_dead_factions); SUITE_ADD_TEST(suite, test_get_monsters); + SUITE_ADD_TEST(suite, test_set_origin); return suite; } diff --git a/src/kernel/plane.c b/src/kernel/plane.c index 6be95f41b..f610a4a64 100644 --- a/src/kernel/plane.c +++ b/src/kernel/plane.c @@ -136,7 +136,7 @@ ursprung_x(const faction * f, const plane * pl, const region * rdefault) } if (!rdefault) return 0; - set_ursprung((faction *)f, id, rdefault->x - plane_center_x(pl), + set_origin((faction *)f, id, rdefault->x - plane_center_x(pl), rdefault->y - plane_center_y(pl)); return rdefault->x - plane_center_x(pl); } @@ -159,7 +159,7 @@ ursprung_y(const faction * f, const plane * pl, const region * rdefault) } if (!rdefault) return 0; - set_ursprung((faction *)f, id, rdefault->x - plane_center_x(pl), + set_origin((faction *)f, id, rdefault->x - plane_center_x(pl), rdefault->y - plane_center_y(pl)); return rdefault->y - plane_center_y(pl); } @@ -221,7 +221,7 @@ const region * r) *y = ny; } -void set_ursprung(faction * f, int id, int x, int y) +void set_origin(faction * f, int id, int x, int y) { ursprung *ur; assert(f != NULL); diff --git a/src/kernel/plane.h b/src/kernel/plane.h index 3ef387754..76c10fdca 100644 --- a/src/kernel/plane.h +++ b/src/kernel/plane.h @@ -70,7 +70,7 @@ extern "C" { struct plane *getplanebyid(int id); int plane_center_x(const struct plane *pl); int plane_center_y(const struct plane *pl); - void set_ursprung(struct faction *f, int id, int x, int y); + void set_origin(struct faction *f, int id, int x, int y); struct plane *create_new_plane(int id, const char *name, int minx, int maxx, int miny, int maxy, int flags); struct plane *getplanebyname(const char *); diff --git a/src/kernel/save.c b/src/kernel/save.c index ef89b9daf..d9a18f2b3 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1250,7 +1250,7 @@ faction *readfaction(struct gamedata * data) READ_INT(data->store, &id); READ_INT(data->store, &ux); READ_INT(data->store, &uy); - set_ursprung(f, id, ux, uy); + set_origin(f, id, ux, uy); } f->newbies = 0; diff --git a/src/laws.c b/src/laws.c index 28a08dca1..00553cde5 100755 --- a/src/laws.c +++ b/src/laws.c @@ -2544,7 +2544,7 @@ int origin_cmd(unit * u, struct order *ord) px = (short)getint(); py = (short)getint(); - set_ursprung(u->faction, getplaneid(u->region), px, py); + set_origin(u->faction, getplaneid(u->region), px, py); return 0; } diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index 44b93943c..eac860185 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -181,7 +181,7 @@ newfaction *read_newfactions(const char *filename) password[0] = '\0'; if (sscanf(buf, "%54s %20s %8s %d %d %16s %d", email, race, lang, &bonus, - &subscription, password, &alliance) < 6) + &subscription, password, &alliance) < 3) break; if (email[0] == '\0') break;