Merge pull request #568 from ennorehling/develop

change how dead units' factions are found
This commit is contained in:
Enno Rehling 2016-09-11 20:24:42 +02:00 committed by GitHub
commit 4a1d29c92f
15 changed files with 202 additions and 154 deletions

View file

@ -1,9 +1,7 @@
#!/bin/bash #!/bin/bash
set -e #set -e
VERSION="$1" VERSION=$(git describe --tags --match 'v*.*.*')
echo "submitting version $VERSION" echo "submitting version $VERSION"
shift
DESC="$*"
cd Debug cd Debug
make clean make clean
../../coverity/bin/cov-build --dir cov-int make eressea ../../coverity/bin/cov-build --dir cov-int make eressea
@ -12,5 +10,5 @@ curl --form token=IISXKH3A1ngZGfFmBz_aSA \
--form email=enno.rehling@gmail.com \ --form email=enno.rehling@gmail.com \
--form file=@eressea.tgz \ --form file=@eressea.tgz \
--form version="$VERSION" \ --form version="$VERSION" \
--form description="$DESC" \ --form description="Eressea coverity check" \
https://scan.coverity.com/builds?project=eressea%2Fserver https://scan.coverity.com/builds?project=eressea%2Fserver

View file

@ -39,13 +39,17 @@ local function read_players()
-- return {{ email = "noreply@mailinator.com", race = "dwarf", lang = "de" }} -- return {{ email = "noreply@mailinator.com", race = "dwarf", lang = "de" }}
local players = {} local players = {}
local input = io.open("newfactions", "r") local input = io.open("newfactions", "r")
while input do if input then
local str = input:read("*line") local str = input:read("*line")
if str==nil then break end while str do
local email, race, lang = str:match("([^ ]*) ([^ ]*) ([^ ]*)") if str==nil then break end
if email and string.char(string.byte(email, 1))~='#' then local email, race, lang = str:match("([^ ]*) ([^ ]*) ([^ ]*)")
table.insert(players, { race = race, lang = lang, email = email }) if email and string.char(string.byte(email, 1))~='#' then
table.insert(players, { race = race, lang = lang, email = email })
end
str = input:read("*line")
end end
input:close()
end end
return players return players
end end

View file

@ -23,6 +23,7 @@ function cursed.init()
if f then if f then
print("found cursed.txt") print("found cursed.txt")
curse(f) curse(f)
f:close()
end end
end end

View file

@ -124,30 +124,33 @@ end
function seed() function seed()
local input = io.open(config.basepath .. "/parteien.txt") local input = io.open(config.basepath .. "/parteien.txt")
for f in factions() do if input then
if f.race=="vampunicorn" then for f in factions() do
local str = input:read("*line") if f.race=="vampunicorn" then
if str==nil then break end local str = input:read("*line")
local race, lang, email = str:match("([^ ]*) ([^ ]*) ([^ ]*)") if str==nil then break end
f.race = race:lower() local race, lang, email = str:match("([^ ]*) ([^ ]*) ([^ ]*)")
f.options = f.options + 4096 f.race = race:lower()
f.email = email f.options = f.options + 4096
f.locale = lang f.email = email
for u in f.units do f.locale = lang
u.race = race:lower() for u in f.units do
u.hp = u.hp_max u.race = race:lower()
local b = building.create(u.region, "castle") u.hp = u.hp_max
if lang=="de" then local b = building.create(u.region, "castle")
u.name = "Entdecker" if lang=="de" then
b.name = "Heimat" u.name = "Entdecker"
else b.name = "Heimat"
u.name = "Explorer" else
b.name = "Home" u.name = "Explorer"
b.name = "Home"
end
b.size = 10
u.building = b
end
end end
b.size = 10
u.building = b
end end
end input:close()
end end
for r in regions() do for r in regions() do
r:set_resource("sapling", r:get_resource("tree")/4) r:set_resource("sapling", r:get_resource("tree")/4)

View file

@ -27,27 +27,29 @@ else
end end
local f=assert(io.open("factions", "r")) local f=assert(io.open("factions", "r"))
line=f:read("*line") if f then
players = {}
emails = {}
patrons = {}
nplayers = 0
while line~=nil do
fields = {}
line:gsub("([^\t]*)\t*", function(c) table.insert(fields, c) end)
line=f:read("*line") line=f:read("*line")
email = fields[1] players = {}
if fields[2]=='yes' then emails = {}
table.insert(patrons, email) patrons = {}
else nplayers = 0
table.insert(emails, email) while line~=nil do
fields = {}
line:gsub("([^\t]*)\t*", function(c) table.insert(fields, c) end)
line=f:read("*line")
email = fields[1]
if fields[2]=='yes' then
table.insert(patrons, email)
else
table.insert(emails, email)
end
if fields[3]=='German' then lang='de' else lang='en' end
race=string.gsub(fields[4], "/.*", ''):lower()
players[email] = { ['lang'] = lang, ['race'] = race }
nplayers = nplayers + 1
end end
if fields[3]=='German' then lang='de' else lang='en' end f:close()
race=string.gsub(fields[4], "/.*", ''):lower()
players[email] = { ['lang'] = lang, ['race'] = race }
nplayers = nplayers + 1
end end
for k, r in ipairs(homes) do for k, r in ipairs(homes) do
print(k, r) print(k, r)
end end

View file

@ -27,27 +27,29 @@ else
end end
local f=assert(io.open("factions", "r")) local f=assert(io.open("factions", "r"))
line=f:read("*line") if f then
players = {}
emails = {}
patrons = {}
nplayers = 0
while line~=nil do
fields = {}
line:gsub("([^\t]*)\t*", function(c) table.insert(fields, c) end)
line=f:read("*line") line=f:read("*line")
email = fields[1] players = {}
if fields[2]=='yes' then emails = {}
table.insert(patrons, email) patrons = {}
else nplayers = 0
table.insert(emails, email) while line~=nil do
fields = {}
line:gsub("([^\t]*)\t*", function(c) table.insert(fields, c) end)
line=f:read("*line")
email = fields[1]
if fields[2]=='yes' then
table.insert(patrons, email)
else
table.insert(emails, email)
end
if fields[3]=='German' then lang='de' else lang='en' end
race=string.gsub(fields[4], "/.*", ''):lower()
players[email] = { ['lang'] = lang, ['race'] = race }
nplayers = nplayers + 1
end end
if fields[3]=='German' then lang='de' else lang='en' end f:close()
race=string.gsub(fields[4], "/.*", ''):lower()
players[email] = { ['lang'] = lang, ['race'] = race }
nplayers = nplayers + 1
end end
for k, r in ipairs(homes) do for k, r in ipairs(homes) do
print(k, r) print(k, r)
end end

View file

@ -336,40 +336,6 @@ void write_faction_reference(const faction * f, struct storage *store)
WRITE_INT(store, f ? f->no : 0); WRITE_INT(store, f ? f->no : 0);
} }
#define DMAXHASH 7919
typedef struct dead {
struct dead *nexthash;
faction *f;
int no;
} dead;
static dead *deadhash[DMAXHASH];
void dhash(int no, faction * f)
{
dead *hash = (dead *)calloc(1, sizeof(dead));
dead *old = deadhash[no % DMAXHASH];
hash->no = no;
hash->f = f;
deadhash[no % DMAXHASH] = hash;
hash->nexthash = old;
}
faction *dfindhash(int no)
{
dead *old;
if (no < 0)
return 0;
for (old = deadhash[no % DMAXHASH]; old; old = old->nexthash) {
if (old->no == no) {
return old->f;
}
}
return 0;
}
void free_flist(faction **fp) { void free_flist(faction **fp) {
faction * flist = *fp; faction * flist = *fp;
while (flist) { while (flist) {
@ -868,6 +834,7 @@ int writepasswd(void)
} }
void free_factions(void) { void free_factions(void) {
#ifdef DMAXHASH
int i; int i;
for (i = 0; i != DMAXHASH; ++i) { for (i = 0; i != DMAXHASH; ++i) {
while (deadhash[i]) { while (deadhash[i]) {
@ -876,6 +843,7 @@ void free_factions(void) {
free(d); free(d);
} }
} }
#endif
free_flist(&factions); free_flist(&factions);
free_flist(&dead_factions); free_flist(&dead_factions);
} }

View file

@ -180,10 +180,6 @@ extern "C" {
struct faction *getfaction(void); struct faction *getfaction(void);
/* looking up dead factions: */
void dhash(int no, struct faction * f);
struct faction *dfindhash(int no);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -191,30 +191,10 @@ static void test_set_origin_bug(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_deadhash(CuTest *tc) {
faction *f;
int no;
test_setup();
f = test_create_faction(0);
no = f->no;
CuAssertPtrEquals(tc, f, findfaction(no));
CuAssertPtrEquals(tc, NULL, dfindhash(42));
dhash(41, f);
dhash(42, f);
assert(f == factions);
destroyfaction(&factions);
CuAssertPtrEquals(tc, 0, findfaction(no));
CuAssertPtrEquals(tc, f, dfindhash(42));
CuAssertPtrEquals(tc, f, dfindhash(41));
test_cleanup();
}
CuSuite *get_faction_suite(void) CuSuite *get_faction_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_addfaction); SUITE_ADD_TEST(suite, test_addfaction);
SUITE_ADD_TEST(suite, test_deadhash);
SUITE_ADD_TEST(suite, test_remove_empty_factions); SUITE_ADD_TEST(suite, test_remove_empty_factions);
SUITE_ADD_TEST(suite, test_destroyfaction_allies); SUITE_ADD_TEST(suite, test_destroyfaction_allies);
SUITE_ADD_TEST(suite, test_remove_empty_factions_alliance); SUITE_ADD_TEST(suite, test_remove_empty_factions_alliance);

View file

@ -22,6 +22,7 @@
#include <tests.h> #include <tests.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
static const struct race * race_with_flag(const char * name) { static const struct race * race_with_flag(const char * name) {
char data[1024]; char data[1024];
@ -445,7 +446,9 @@ static void test_configs(CuTest * tc)
CuAssertPtrEquals(tc, 0, buildingtypes); CuAssertPtrEquals(tc, 0, buildingtypes);
json_config(json); json_config(json);
CuAssertPtrNotNull(tc, buildingtypes); CuAssertPtrNotNull(tc, buildingtypes);
unlink("test.json"); if (unlink("test.json")!=0 && errno==ENOENT) {
errno = 0;
}
cJSON_Delete(json); cJSON_Delete(json);
test_cleanup(); test_cleanup();
} }

View file

@ -1877,7 +1877,11 @@ int writegame(const char *filename)
join_path(datapath(), filename, path, sizeof(path)); join_path(datapath(), filename, path, sizeof(path));
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
/* make sure we don't overwrite an existing file (hard links) */ /* make sure we don't overwrite an existing file (hard links) */
unlink(path); if (unlink(path)!=0) {
if (errno==ENOENT) {
errno = 0;
}
}
#endif #endif
F = fopen(path, "wb"); F = fopen(path, "wb");
if (!F) { if (!F) {

View file

@ -366,6 +366,51 @@ int gift_items(unit * u, int flags)
static unit *deleted_units = NULL; static unit *deleted_units = NULL;
#define DMAXHASH 7919
#undef DMAXHASH // TODO: makes dfindhash slow!
#ifdef DMAXHASH
typedef struct dead {
struct dead *nexthash;
faction *f;
int no;
} dead;
static dead *deadhash[DMAXHASH];
static void dhash(int no, faction * f)
{
dead *hash = (dead *)calloc(1, sizeof(dead));
dead *old = deadhash[no % DMAXHASH];
hash->no = no;
hash->f = f;
deadhash[no % DMAXHASH] = hash;
hash->nexthash = old;
}
faction *dfindhash(int no)
{
dead *old;
if (no < 0)
return 0;
for (old = deadhash[no % DMAXHASH]; old; old = old->nexthash) {
if (old->no == no) {
return old->f;
}
}
return 0;
}
#else
struct faction *dfindhash(int no) {
unit *u = deleted_units;
while (u && u->no != no) {
u = u->next;
}
return u ? u->faction : NULL;
}
#endif
int remove_unit(unit ** ulist, unit * u) int remove_unit(unit ** ulist, unit * u)
{ {
int result; int result;
@ -379,8 +424,9 @@ int remove_unit(unit ** ulist, unit * u)
return -1; return -1;
} }
if (u->number) if (u->number) {
set_number(u, 0); set_number(u, 0);
}
leave(u, true); leave(u, true);
u->region = NULL; u->region = NULL;
@ -393,11 +439,22 @@ int remove_unit(unit ** ulist, unit * u)
*ulist = u->next; *ulist = u->next;
} }
if (u->prevF) {
u->prevF->nextF = u->nextF;
}
if (u->nextF) {
u->nextF->prevF = u->prevF;
}
u->nextF = 0;
u->prevF = 0;
u->next = deleted_units; u->next = deleted_units;
deleted_units = u; deleted_units = u;
#ifdef DMAXHASH
dhash(u->no, u->faction); dhash(u->no, u->faction);
#endif
// u_setfaction(u, NULL);
u_setfaction(u, NULL);
u->region = NULL; u->region = NULL;
return 0; return 0;

View file

@ -174,6 +174,9 @@ extern "C" {
const struct region *r, bool noitem); const struct region *r, bool noitem);
int remove_unit(struct unit **ulist, struct unit *u); int remove_unit(struct unit **ulist, struct unit *u);
/* looking up dead units' factions: */
struct faction *dfindhash(int no);
#define GIFT_SELF 1<<0 #define GIFT_SELF 1<<0
#define GIFT_FRIENDS 1<<1 #define GIFT_FRIENDS 1<<1
#define GIFT_PEASANTS 1<<2 #define GIFT_PEASANTS 1<<2

View file

@ -50,14 +50,16 @@ static void test_remove_empty_units_in_region(CuTest *tc) {
test_create_world(); test_create_world();
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0)); u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
u = test_create_unit(u->faction, u->region);
CuAssertPtrNotNull(tc, u->nextF);
uid = u->no; uid = u->no;
remove_empty_units_in_region(u->region); remove_empty_units_in_region(u->region);
CuAssertPtrNotNull(tc, findunit(uid)); CuAssertPtrNotNull(tc, findunit(uid));
u->number = 0; u->number = 0;
remove_empty_units_in_region(u->region); remove_empty_units_in_region(u->region);
CuAssertPtrEquals(tc, 0, findunit(uid)); CuAssertPtrEquals(tc, 0, findunit(uid));
CuAssertPtrEquals(tc, 0, u->nextF);
CuAssertPtrEquals(tc, 0, u->region); CuAssertPtrEquals(tc, 0, u->region);
CuAssertPtrEquals(tc, 0, u->faction);
test_cleanup(); test_cleanup();
} }
@ -401,6 +403,34 @@ static void test_unit_description(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_remove_unit(CuTest *tc) {
region *r;
unit *u;
faction *f;
int uno;
const resource_type *rtype;
test_setup();
init_resources();
rtype = get_resourcetype(R_SILVER);
r = test_create_region(0, 0, 0);
f = test_create_faction(0);
u = test_create_unit(f, r);
uno = u->no;
region_setresource(r, rtype, 0);
i_change(&u->items, rtype->itype, 100);
remove_unit(&r->units, u);
CuAssertIntEquals(tc, 100, region_getresource(r, rtype));
CuAssertIntEquals(tc, 0, u->number);
CuAssertPtrEquals(tc, 0, u->region);
CuAssertPtrEquals(tc, 0, u->items);
CuAssertPtrEquals(tc, 0, u->nextF);
CuAssertPtrEquals(tc, 0, r->units);
CuAssertPtrEquals(tc, 0, findunit(uno));
CuAssertPtrEquals(tc, f, dfindhash(uno));
test_cleanup();
}
CuSuite *get_unit_suite(void) CuSuite *get_unit_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -409,6 +439,7 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_unit_name); SUITE_ADD_TEST(suite, test_unit_name);
SUITE_ADD_TEST(suite, test_unit_name_from_race); SUITE_ADD_TEST(suite, test_unit_name_from_race);
SUITE_ADD_TEST(suite, test_update_monster_name); SUITE_ADD_TEST(suite, test_update_monster_name);
SUITE_ADD_TEST(suite, test_remove_unit);
SUITE_ADD_TEST(suite, test_remove_empty_units); SUITE_ADD_TEST(suite, test_remove_empty_units);
SUITE_ADD_TEST(suite, test_remove_units_ignores_spells); SUITE_ADD_TEST(suite, test_remove_units_ignores_spells);
SUITE_ADD_TEST(suite, test_remove_units_without_faction); SUITE_ADD_TEST(suite, test_remove_units_without_faction);

View file

@ -232,11 +232,11 @@ static void dragon_name(unit * u)
{ {
char name[NAMESIZE + 1]; char name[NAMESIZE + 1];
int rnd, ter = 0; int rnd, ter = 0;
int anzahl = 1;
static int num_postfix; static int num_postfix;
char zText[32]; char zText[32];
const char *str; const char *str;
assert(u);
if (num_postfix == 0) { if (num_postfix == 0) {
for (num_postfix = 0;; ++num_postfix) { for (num_postfix = 0;; ++num_postfix) {
sprintf(zText, "dragon_postfix_%d", num_postfix); sprintf(zText, "dragon_postfix_%d", num_postfix);
@ -248,26 +248,22 @@ static void dragon_name(unit * u)
num_postfix = -1; num_postfix = -1;
} }
if (u) { switch (rterrain(u->region)) {
region *r = u->region; case T_PLAIN:
anzahl = u->number; ter = 1;
switch (rterrain(r)) { break;
case T_PLAIN: case T_MOUNTAIN:
ter = 1; ter = 2;
break; break;
case T_MOUNTAIN: case T_DESERT:
ter = 2; ter = 3;
break; break;
case T_DESERT: case T_SWAMP:
ter = 3; ter = 4;
break; break;
case T_SWAMP: case T_GLACIER:
ter = 4; ter = 5;
break; break;
case T_GLACIER:
ter = 5;
break;
}
} }
if (num_postfix <=0) { if (num_postfix <=0) {
@ -285,7 +281,7 @@ static void dragon_name(unit * u)
str = locale_getstring(default_locale, zText); str = locale_getstring(default_locale, zText);
assert(str != NULL); assert(str != NULL);
if (anzahl > 1) { if (u->number > 1) {
const char *no_article = strchr((const char *)str, ' '); const char *no_article = strchr((const char *)str, ' ');
assert(no_article); assert(no_article);
// TODO: localization // TODO: localization
@ -308,7 +304,7 @@ static void dragon_name(unit * u)
sz += strlcat(name, " ", sizeof(name)); sz += strlcat(name, " ", sizeof(name));
sz += strlcat(name, n, sizeof(name)); sz += strlcat(name, n, sizeof(name));
} }
if (u && (rng_int() % 3 == 0)) { if (rng_int() % 3 == 0) {
sz += strlcat(name, " von ", sizeof(name)); sz += strlcat(name, " von ", sizeof(name));
sz += strlcat(name, (const char *)rname(u->region, default_locale), sizeof(name)); sz += strlcat(name, (const char *)rname(u->region, default_locale), sizeof(name));
} }