Merge branch 'develop' of github.com:ennorehling/eressea into develop

This commit is contained in:
Enno Rehling 2018-10-24 07:12:04 +02:00
commit 5ab7c8cc65
33 changed files with 207 additions and 93 deletions

2
clibs

@ -1 +1 @@
Subproject commit 12139c7ce25e6731393f863e248ab16aa0578a3a
Subproject commit ed5c4fee3afbc3d8be79d64857f30702aed522f8

View file

@ -66,7 +66,7 @@ void new_potiontype(item_type * itype, int level)
{
potion_type *ptype;
ptype = (potion_type *)calloc(sizeof(potion_type), 1);
ptype = (potion_type *)calloc(1, sizeof(potion_type));
itype->flags |= ITF_POTION;
ptype->itype = itype;
ptype->level = level;
@ -181,7 +181,7 @@ int use_potion(unit * u, const item_type * itype, int amount, struct order *ord)
static void a_initeffect(variant *var)
{
var->v = calloc(sizeof(effect_data), 1);
var->v = calloc(1, sizeof(effect_data));
}
static void

View file

@ -3226,7 +3226,7 @@ fighter *make_fighter(battle * b, unit * u, side * s1, bool attack)
assert(w != WMAX);
}
assert(w >= 0);
fig->weapons = (weapon *)calloc(sizeof(weapon), (size_t)(w + 1));
fig->weapons = (weapon *)calloc((size_t)(w + 1), sizeof(weapon));
memcpy(fig->weapons, weapons, (size_t)w * sizeof(weapon));
for (i = 0; i != w; ++i) {
@ -3452,7 +3452,7 @@ battle *make_battle(region * r)
break;
}
if (!bf) {
bf = (bfaction *)calloc(sizeof(bfaction), 1);
bf = (bfaction *)calloc(1, sizeof(bfaction));
++b->nfactions;
bf->faction = u->faction;
bf->next = b->factions;

View file

@ -382,20 +382,8 @@ static int tolua_faction_set_origin(lua_State * L)
static int tolua_faction_get_origin(lua_State * L)
{
faction *self = (faction *)tolua_tousertype(L, 1, 0);
ursprung *origin = self->ursprung;
int x, y;
while (origin != NULL && origin->id != 0) {
origin = origin->next;
}
if (origin) {
x = origin->x;
y = origin->y;
}
else {
x = 0;
y = 0;
}
int x = 0, y = 0;
faction_getorigin(self, 0, &x, &y);
lua_pushinteger(L, x);
lua_pushinteger(L, y);

View file

@ -1361,7 +1361,7 @@ static void update_view(view * vi)
state *state_open(void)
{
state *st = calloc(sizeof(state), 1);
state *st = (state *)calloc(1, sizeof(state));
st->display.pl = get_homeplane();
st->cursor.pl = get_homeplane();
st->cursor.x = 0;

View file

@ -56,10 +56,8 @@ ally * ally_add(ally **al_p, struct faction *f) {
if (f && al->faction == f) return al;
al_p = &al->next;
}
al = (ally *)malloc(sizeof(ally));
al = (ally *)calloc(1, sizeof(ally));
al->faction = f;
al->status = 0;
al->next = 0;
*al_p = al;
return al;
}

View file

@ -54,16 +54,17 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/unit.h>
/* from libutil */
#include <util/base36.h>
#include <util/goodies.h>
#include <util/language.h>
#include <util/log.h>
#include <util/param.h>
#include <util/parser.h>
#include <util/resolve.h>
#include <util/strings.h>
/* from libc */
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
@ -180,8 +181,9 @@ int destroy_cmd(unit * u, struct order *ord)
s = gettoken(token, sizeof(token));
if (s && *s) {
n = atoi10((const char *)s);
ERRNO_CHECK();
n = atoi(s);
errno = 0;
if (n <= 0) {
n = INT_MAX;
}

View file

@ -135,7 +135,7 @@ building_type *bt_get_or_create(const char *name)
if (name != NULL) {
building_type *btype = bt_find_i(name);
if (btype == NULL) {
btype = calloc(sizeof(building_type), 1);
btype = (building_type *)calloc(1, sizeof(building_type));
btype->_name = str_strdup(name);
btype->flags = BTF_DEFAULT;
btype->auraregen = 1.0;

View file

@ -119,7 +119,7 @@ static void free_faction(faction * f)
i_freeall(&f->items);
freelist(f->ursprung);
freelist(f->origin);
}
#define FMAXHASH 2039
@ -727,10 +727,10 @@ bool faction_alive(const faction *f) {
void faction_getorigin(const faction * f, int id, int *x, int *y)
{
ursprung *ur;
origin *ur;
assert(f && x && y);
for (ur = f->ursprung; ur; ur = ur->next) {
for (ur = f->origin; ur; ur = ur->next) {
if (ur->id == id) {
*x = ur->x;
*y = ur->y;
@ -739,24 +739,27 @@ void faction_getorigin(const faction * f, int id, int *x, int *y)
}
}
void faction_setorigin(faction * f, int id, int x, int y)
{
ursprung *ur;
assert(f != NULL);
for (ur = f->ursprung; ur; ur = ur->next) {
if (ur->id == id) {
ur->x = ur->x + x;
ur->y = ur->y + y;
return;
}
}
ur = calloc(1, sizeof(ursprung));
static origin *new_origin(int id, int x, int y) {
origin *ur = (origin *)calloc(1, sizeof(origin));
ur->id = id;
ur->x = x;
ur->y = y;
return ur;
}
addlist(&f->ursprung, ur);
void faction_setorigin(faction * f, int id, int x, int y)
{
origin **urp;
assert(f != NULL);
for (urp = &f->origin; *urp; urp = &(*urp)->next) {
origin *ur = *urp;
if (ur->id == id) {
ur->x += x;
ur->y += y;
return;
}
}
*urp = new_origin(id, x, y);
}

View file

@ -37,7 +37,7 @@ extern "C" {
struct attrib_type;
struct gamedata;
struct selist;
/* faction flags */
#define FFL_NOAID (1<<0) /* Hilfsflag Kampf */
#define FFL_ISNEW (1<<1)
@ -54,6 +54,12 @@ extern "C" {
#define FFL_NPC (1<<25) /* eine Partei mit Monstern */
#define FFL_SAVEMASK (FFL_DEFENDER|FFL_NPC|FFL_NOIDLEOUT|FFL_CURSED)
typedef struct origin {
struct origin *next;
int id;
int x, y;
} origin;
typedef struct faction {
struct faction *next;
struct faction *nexthash;
@ -72,7 +78,7 @@ extern "C" {
const struct locale *locale;
int lastorders;
int age;
struct ursprung *ursprung;
struct origin *origin;
const struct race *race;
magic_t magiegebiet;
int newbies;

View file

@ -119,7 +119,7 @@ static void test_addfaction(CuTest *tc) {
CuAssertPtrEquals(tc, NULL, (void *)f->next);
CuAssertPtrEquals(tc, NULL, (void *)f->banner);
CuAssertPtrEquals(tc, NULL, (void *)f->spellbook);
CuAssertPtrEquals(tc, NULL, (void *)f->ursprung);
CuAssertPtrEquals(tc, NULL, (void *)f->origin);
CuAssertPtrEquals(tc, (void *)factions, (void *)f);
CuAssertStrEquals(tc, "test@eressea.de", f->email);
CuAssertTrue(tc, checkpasswd(f, "hurrdurr"));
@ -162,11 +162,11 @@ static void test_set_origin(CuTest *tc) {
test_setup();
pl = create_new_plane(0, "", 0, 19, 0, 19, 0);
f = test_create_faction(NULL);
CuAssertPtrEquals(tc, NULL, f->ursprung);
CuAssertPtrEquals(tc, NULL, f->origin);
faction_setorigin(f, 0, 1, 1);
CuAssertIntEquals(tc, 0, f->ursprung->id);
CuAssertIntEquals(tc, 1, f->ursprung->x);
CuAssertIntEquals(tc, 1, f->ursprung->y);
CuAssertIntEquals(tc, 0, f->origin->id);
CuAssertIntEquals(tc, 1, f->origin->x);
CuAssertIntEquals(tc, 1, f->origin->y);
faction_getorigin(f, 0, &x, &y);
CuAssertIntEquals(tc, 1, x);
CuAssertIntEquals(tc, 1, y);
@ -190,7 +190,7 @@ static void test_set_origin_bug(CuTest *tc) {
faction_setorigin(f, 0, -10, 3);
faction_setorigin(f, 0, -13, -4);
adjust_coordinates(f, &x, &y, pl);
CuAssertIntEquals(tc, 0, f->ursprung->id);
CuAssertIntEquals(tc, 0, f->origin->id);
CuAssertIntEquals(tc, -9, x);
CuAssertIntEquals(tc, 2, y);
test_teardown();

View file

@ -723,7 +723,7 @@ int change_money(unit * u, int v)
return 0;
}
static int add_resourcename_cb(const void * match, const void * key,
static int add_resourcename_cb(void * match, const void * key,
size_t keylen, void *data)
{
struct locale * lang = (struct locale *)data;
@ -773,7 +773,7 @@ attrib_type at_showitem = {
"showitem"
};
static int add_itemname_cb(const void * match, const void * key,
static int add_itemname_cb(void * match, const void * key,
size_t keylen, void *data)
{
struct locale * lang = (struct locale *)data;
@ -919,7 +919,7 @@ void free_rtype(resource_type *rtype) {
free(rtype);
}
static int free_rtype_cb(const void * match, const void * key,
static int free_rtype_cb(void * match, const void * key,
size_t keylen, void *cbdata)
{
resource_type *rtype = ((rt_entry *)match)->value;;

View file

@ -122,7 +122,7 @@ int getplaneid(const region * r)
static int
ursprung_x(const faction * f, const plane * pl, const region * rdefault)
{
ursprung *ur;
origin *ur;
int id = 0;
if (!f)
@ -131,7 +131,7 @@ ursprung_x(const faction * f, const plane * pl, const region * rdefault)
if (pl)
id = pl->id;
for (ur = f->ursprung; ur; ur = ur->next) {
for (ur = f->origin; ur; ur = ur->next) {
if (ur->id == id)
return ur->x;
}
@ -145,7 +145,7 @@ ursprung_x(const faction * f, const plane * pl, const region * rdefault)
static int
ursprung_y(const faction * f, const plane * pl, const region * rdefault)
{
ursprung *ur;
origin *ur;
int id = 0;
if (!f)
@ -154,7 +154,7 @@ ursprung_y(const faction * f, const plane * pl, const region * rdefault)
if (pl)
id = pl->id;
for (ur = f->ursprung; ur; ur = ur->next) {
for (ur = f->origin; ur; ur = ur->next) {
if (ur->id == id)
return ur->y;
}

View file

@ -774,7 +774,7 @@ region *new_region(int x, int y, struct plane *pl, int uid)
log_error("duplicate region contains units\n");
return r;
}
r = calloc(1, sizeof(region));
r = (region *)calloc(sizeof(region), 1);
assert_alloc(r);
r->x = x;
r->y = y;

View file

@ -357,14 +357,14 @@ static void read_skills(gamedata *data, unit *u)
size_t sz = u->skill_size * sizeof(skill);
qsort(skills, u->skill_size, sizeof(skill), skill_cmp);
u->skills = malloc(sz);
u->skills = (skill *)malloc(sz);
memcpy(u->skills, skills, sz);
}
}
else {
int i;
READ_INT(data->store, &u->skill_size);
u->skills = malloc(sizeof(skill)*u->skill_size);
u->skills = (skill *)malloc(sizeof(skill)*u->skill_size);
for (i = 0; i != u->skill_size; ++i) {
skill *sv = u->skills + i;
read_skill(data, sv);
@ -420,7 +420,7 @@ unit *read_unit(gamedata *data)
u_setfaction(u, NULL);
}
else {
u = calloc(sizeof(unit), 1);
u = (unit *)calloc(1, sizeof(unit));
assert_alloc(u);
u->no = n;
uhash(u);
@ -1095,7 +1095,7 @@ faction *read_faction(gamedata * data)
void write_faction(gamedata *data, const faction * f)
{
ally *sf;
ursprung *ur;
origin *ur;
assert(f->_alive);
assert(f->no > 0 && f->no <= MAX_UNIT_NR);
@ -1134,8 +1134,8 @@ void write_faction(gamedata *data, const faction * f)
WRITE_SECTION(data->store);
WRITE_TOK(data->store, "end");
WRITE_SECTION(data->store);
WRITE_INT(data->store, listlen(f->ursprung));
for (ur = f->ursprung; ur; ur = ur->next) {
WRITE_INT(data->store, listlen(f->origin));
for (ur = f->origin; ur; ur = ur->next) {
WRITE_INT(data->store, ur->id);
WRITE_INT(data->store, ur->x);
WRITE_INT(data->store, ur->y);

View file

@ -55,12 +55,6 @@ struct terrain_type;
struct unit;
struct weapon_type;
typedef struct ursprung {
struct ursprung *next;
int id;
int x, y;
} ursprung;
/* seen_mode: visibility in the report */
typedef enum {
seen_none,

View file

@ -28,7 +28,7 @@ void
insert_selection(list_selection ** p_sel, list_selection * prev,
const char *str, void *payload)
{
list_selection *sel = calloc(sizeof(list_selection), 1);
list_selection *sel = (list_selection *)calloc(1, sizeof(list_selection));
sel->str = str_strdup(str);
sel->data = payload;
if (*p_sel) {
@ -56,7 +56,7 @@ const char *str, void *payload)
list_selection **push_selection(list_selection ** p_sel, char *str,
void *payload)
{
list_selection *sel = calloc(sizeof(list_selection), 1);
list_selection *sel = (list_selection *)calloc(1, sizeof(list_selection));
list_selection *prev = NULL;
sel->str = str;
sel->data = payload;

View file

@ -2950,7 +2950,7 @@ void free_spellbook(spellbook *sb) {
free(sb);
}
static int free_spellbook_cb(const void *match, const void *key, size_t keylen, void *data) {
static int free_spellbook_cb(void *match, const void *key, size_t keylen, void *data) {
const sb_entry *ent = (const sb_entry *)match;
UNUSED_ARG(data);
UNUSED_ARG(keylen);

View file

@ -335,6 +335,8 @@ int main(int argc, char **argv)
game_done();
lua_done(L);
log_close();
stats_write(stdout, "");
stats_close();
if (d) {
iniparser_freedict(d);
}

View file

@ -133,7 +133,7 @@ newfaction *read_newfactions(const char *filename)
if (nf) {
continue;
}
nf = calloc(sizeof(newfaction), 1);
nf = (newfaction *)calloc(1, sizeof(newfaction));
if (check_email(email) == 0) {
nf->email = str_strdup(email);
} else {

View file

@ -608,6 +608,7 @@ static void recruit_dracoids(unit * dragon, int size)
region *r = dragon->region;
const struct item *weapon = NULL;
unit *un = create_unit(r, f, size, get_race(RC_DRACOID), 0, NULL, NULL);
stats_count("monsters.create.dracoid", 1);
fset(un, UFL_ISNEW | UFL_MOVED);
@ -868,6 +869,7 @@ static int nrand(int handle_start, int sub)
unit *spawn_seaserpent(region *r, faction *f) {
unit *u = create_unit(r, f, 1, get_race(RC_SEASERPENT), 0, NULL, NULL);
stats_count("monsters.create.seaserpent", 1);
fset(u, UFL_ISNEW | UFL_MOVED);
equip_unit(u, "seed_seaserpent");
return u;
@ -908,6 +910,7 @@ void spawn_dragons(void)
else {
u = create_unit(r, monsters, nrand(30, 20) + 1, get_race(RC_DRAGON), 0, NULL, NULL);
}
stats_count("monsters.create.dragon", 1);
fset(u, UFL_ISNEW | UFL_MOVED);
equip_unit(u, "seed_dragon");
@ -940,7 +943,7 @@ void spawn_undead(void)
}
if (r->land && unburied > rpeasants(r) / 20
&& rng_int() % 10000 < 200) {
&& rng_int() % 10000 < 100) {
message *msg;
unit *u;
/* es ist sinnfrei, wenn irgendwo im Wald 3er-Einheiten Untote entstehen.
@ -968,6 +971,7 @@ void spawn_undead(void)
}
u = create_unit(r, monsters, undead, rc, 0, NULL, NULL);
stats_count("monsters.create.undead", 1);
fset(u, UFL_ISNEW | UFL_MOVED);
if ((rc == get_race(RC_SKELETON) || rc == get_race(RC_ZOMBIE))
&& rng_int() % 10 < 4) {

View file

@ -79,7 +79,7 @@ const curse_type ct_firewall = {
static void wall_init(connection * b)
{
wall_data *fd = (wall_data *)calloc(sizeof(wall_data), 1);
wall_data *fd = (wall_data *)calloc(1, sizeof(wall_data));
fd->countdown = -1; /* infinite */
b->data.v = fd;
}

View file

@ -182,6 +182,7 @@ void spawn_braineaters(float chance)
u = create_unit(r, f, 1 + rng_int() % 10 + rng_int() % 10,
rc_brain, 0, NULL, NULL);
equip_unit(u, "seed_braineater");
stats_count("monsters.create.braineater", 1);
next = rng_int() % (int)(chance * 100);
}

View file

@ -175,6 +175,8 @@ int RunAllTests(int argc, char *argv[])
fail_count = summary->failCount;
CuSuiteDelete(summary);
game_done();
log_close();
stats_close();
return fail_count;
}
return 0;

View file

@ -234,6 +234,8 @@ static void test_reset(void) {
default_locale = 0;
calendar_cleanup();
close_orders();
log_close();
stats_close();
free_special_directions();
free_locales();
free_spells();

View file

@ -23,14 +23,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
int atoi10(const char *str)
{
int i = atoi(str);
errno = 0;
return i;
}
int atoi36(const char *str)
{

View file

@ -53,10 +53,7 @@
/* Just to make sure the prototypes match the actual definitions */
#include "crypt_blowfish.h"
#ifdef __i386__
#define BF_ASM 1
#define BF_SCALE 1
#elif defined(__x86_64__) || defined(__alpha__) || defined(__hppa__)
#if defined(__x86_64__) || defined(__alpha__) || defined(__hppa__)
#define BF_ASM 0
#define BF_SCALE 1
#else

View file

@ -16,6 +16,8 @@ without prior permission by the authors of Eressea.
#include "strings.h"
#include "unicode.h"
#include <critbit.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
@ -318,3 +320,77 @@ int log_level(log_t * log, int flags)
log->flags = flags;
return old;
}
static critbit_tree stats = CRITBIT_TREE();
int stats_count(const char *stat, int delta) {
size_t len;
char data[128];
void * match;
if (cb_find_prefix_str(&stats, stat, &match, 1, 0) == 0) {
len = cb_new_kv(stat, strlen(stat), &delta, sizeof(delta), data);
cb_insert(&stats, data, len);
return delta;
}
else {
int *num;
cb_get_kv_ex(match, (void **)&num);
return *num += delta;
}
}
#if 0
#define STATS_BATCH 8
void stats_walk(const char *prefix, void(*callback)(const char *, int, void *), void *udata) {
void *match[STATS_BATCH];
int n, off = 0;
do {
int i;
n = cb_find_prefix_str(&stats, prefix, match, STATS_BATCH, off);
if (n == 0) {
break;
}
off += n;
for (i = 0; i != n; ++i) {
const void *kv = match[i];
int *num;
cb_get_kv_ex(kv, &(void *)num);
callback(kv, *num, udata);
}
} while (n == STATS_BATCH);
}
#else
struct walk_data {
int (*callback)(const char *, int, void *);
void *udata;
};
static int walk_cb(void * match, const void * key, size_t keylen, void *udata) {
struct walk_data *data = (struct walk_data *)udata;
int *num;
cb_get_kv_ex(match, (void **)&num);
return data->callback((const char*)match, *num, data->udata);
}
int stats_walk(const char *prefix, int (*callback)(const char *, int, void *), void *udata) {
struct walk_data data;
data.callback = callback;
data.udata = udata;
return cb_foreach(&stats, prefix, strlen(prefix), walk_cb, &data);
}
#endif
static int write_cb(const char *key, int val, void *udata) {
FILE * F = (FILE *)udata;
fprintf(F, "%s: %d\n", (const char *)key, val);
return 0;
}
void stats_write(FILE *F, const char *prefix) {
stats_walk(prefix, write_cb, F);
}
void stats_close(void) {
cb_clear(&stats);
}

View file

@ -36,6 +36,12 @@ extern "C" {
void log_printf(FILE * ios, const char *format, ...);
void errno_check(const char *file, int line);
int stats_count(const char *stat, int delta);
void stats_write(FILE *F, const char *prefix);
int stats_walk(const char *prefix, int (*callback)(const char *key, int val, void * udata), void *udata);
void stats_close(void);
#define ERRNO_CHECK() errno_check(__FILE__, __LINE__)

View file

@ -1,11 +1,13 @@
#ifdef _MSC_VER
#include <platform.h>
#endif
#include <CuTest.h>
#include "log.h"
#include "macros.h"
#include <CuTest.h>
#include <tests.h>
#include <stdarg.h>
#include <string.h>
@ -32,9 +34,47 @@ static void test_logging(CuTest * tc)
CuAssertStrEquals(tc, "World", str2);
}
static int stats_cb(const char *stat, int num, void *udata) {
int *counter = (int *)udata;
if (counter) {
*counter += num;
}
return 0;
}
static void test_stats(CuTest * tc)
{
int n = 0;
test_setup();
CuAssertIntEquals(tc, 1, stats_count("foobar", 1));
CuAssertIntEquals(tc, 2, stats_count("test.one", 2));
CuAssertIntEquals(tc, 1, stats_count("test.two", 1));
CuAssertIntEquals(tc, 4, stats_count("test.one", 2));
CuAssertIntEquals(tc, 1, stats_count("test.two", 0));
n = 0;
CuAssertIntEquals(tc, 0, stats_walk("", stats_cb, &n));
CuAssertIntEquals(tc, 6, n);
n = 0;
CuAssertIntEquals(tc, 0, stats_walk("test", stats_cb, &n));
CuAssertIntEquals(tc, 5, n);
n = 0;
CuAssertIntEquals(tc, 0, stats_walk("test.one", stats_cb, &n));
CuAssertIntEquals(tc, 4, n);
n = 0;
CuAssertIntEquals(tc, 0, stats_walk("foobar", stats_cb, &n));
CuAssertIntEquals(tc, 1, n);
test_teardown();
}
CuSuite *get_log_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_logging);
SUITE_ADD_TEST(suite, test_stats);
return suite;
}

View file

@ -22,10 +22,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "strings.h"
/* libc includes */
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#ifdef HAVE_LIBBSD

View file

@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#define STRINGS_H
#include <stddef.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {

View file

@ -117,7 +117,7 @@ char * transliterate(char * out, size_t size, const char * in)
}
tnode * mknode(void) {
tnode * node = calloc(1, sizeof(tnode));
tnode * node = (tnode *)calloc(1, sizeof(tnode));
node->refcount = 1;
return node;
}