WIP: still converting to clibs/selist

This commit is contained in:
Enno Rehling 2017-01-26 10:12:12 +01:00
parent 955997d58c
commit d9213c8f25
8 changed files with 67 additions and 28 deletions

2
clibs

@ -1 +1 @@
Subproject commit 339ac5680ec9027c88c4516c72b9da8233b6f3e3 Subproject commit daa9de08314a75a4d77ef4e08c713088a96a5ca5

View File

@ -59,22 +59,18 @@ void free_donations(void) {
transfers = 0; transfers = 0;
} }
static void report_transfer(faction *f1, faction *f2, region *r, int amount) { static void report_transfer(void *data) {
struct message *msg = msg_message("donation", transfer *tf = (transfer *)data;
"from to amount", f1, f2, amount); if (tf->amount > 0) {
r_addmessage(r, f1, msg); struct message *msg = msg_message("donation",
r_addmessage(r, f2, msg); "from to amount", tf->f1, tf->f2, tf->amount);
msg_release(msg); r_addmessage(tf->r, tf->f1, msg);
r_addmessage(tf->r, tf->f2, msg);
msg_release(msg);
}
} }
void report_donations(void) void report_donations(void)
{ {
ql_iter qli = qli_init(&transfers); selist_foreach(transfers, report_transfer);
while (qli_more(qli)) {
transfer *tf = (transfer *)qli_next(&qli);
if (tf->amount > 0) {
report_transfer(tf->f1, tf->f2, tf->r, tf->amount);
}
}
} }

View File

@ -308,7 +308,7 @@ void ct_register(const curse_type * ct)
unsigned int hash = tolower(ct->cname[0]) & 0xFF; unsigned int hash = tolower(ct->cname[0]) & 0xFF;
quicklist **ctlp = cursetypes + hash; quicklist **ctlp = cursetypes + hash;
ql_set_insert(ctlp, (void *)ct); selist_set_insert(ctlp, (void *)ct, NULL);
++ct_changes; ++ct_changes;
} }

View File

@ -57,7 +57,7 @@ void free_spells(void) {
void add_spell(struct quicklist **slistp, spell * sp) void add_spell(struct quicklist **slistp, spell * sp)
{ {
if (!ql_set_insert(slistp, sp)) { if (!selist_set_insert(slistp, sp, NULL)) {
log_error("add_spell: the list already contains the spell '%s'.\n", sp->sname); log_error("add_spell: the list already contains the spell '%s'.\n", sp->sname);
} }
} }

View File

@ -1,4 +1,30 @@
#include "quicklist.h" #include "quicklist.h"
#include <stdlib.h>
bool ql_set_insert_ex(struct quicklist **qlp, void *data, int (*cmp_cb)(const void *lhs, const void *rhs)); bool ql_set_remove(struct quicklist **qlp, const void *data)
bool ql_set_find_ex(struct quicklist **qlp, int *qip, const void *data, int (*cmp_cb)(const void *lhs, const void *rhs)); {
int qi, qn;
quicklist *ql = *qlp;
if (!ql)
return false;
qn = selist_length(ql);
for (qi = 0; qi != qn; ++qi) {
void *qd = selist_get(ql, qi);
if (qd == data) {
return selist_delete(qlp, qi) == 0;
}
}
return false;
}
bool ql_set_insert(struct quicklist **qlp, void *data)
{
return selist_set_insert(qlp, data, NULL);
}
bool ql_set_find(struct quicklist **qlp, int *qip, const void *data)
{
return selist_set_find(qlp, qip, data, NULL);
}

View File

@ -2,7 +2,11 @@
#define selist quicklist #define selist quicklist
#define selist_free ql_free #define selist_free ql_free
#define selist_delete ql_delete
#define selist_foreach ql_foreach
#define selist_get ql_get #define selist_get ql_get
#define selist_replace ql_replace
#define selist_length ql_length
#define selist_push ql_push #define selist_push ql_push
#define selist_empty ql_empty #define selist_empty ql_empty
#define selist_advance ql_advance #define selist_advance ql_advance
@ -12,3 +16,4 @@
typedef void(*ql_cb)(void *); typedef void(*ql_cb)(void *);
bool ql_set_insert_ex(struct quicklist **qlp, void *data, int (*cmp_cb)(const void *lhs, const void *rhs)); bool ql_set_insert_ex(struct quicklist **qlp, void *data, int (*cmp_cb)(const void *lhs, const void *rhs));
bool ql_set_find_ex(struct quicklist **qlp, int *qip, const void *data, int (*cmp_cb)(const void *lhs, const void *rhs)); bool ql_set_find_ex(struct quicklist **qlp, int *qip, const void *data, int (*cmp_cb)(const void *lhs, const void *rhs));

View File

@ -99,18 +99,30 @@ bool travelthru_cansee(const struct region *r, const struct faction *f, const st
return false; return false;
} }
void travelthru_map(region * r, void(*cb)(region *, struct unit *, void *), void *cbdata) struct cb_data {
void(*call)(region *, struct unit *, void *);
void *data;
struct region *r;
};
void cb_map(void *data, void *ex) {
struct cb_data *cb = (struct cb_data *)ex;
struct unit *u = (struct unit *)data;
cb->call(cb->r, u, cb->data);
}
void travelthru_map(region * r, void(*cb)(region *, struct unit *, void *), void *data)
{ {
attrib *a; attrib *a;
struct cb_data cbdata;
assert(r); assert(r);
cbdata.call = cb;
cbdata.data = data;
cbdata.r = r;
a = a_find(r->attribs, &at_travelunit); a = a_find(r->attribs, &at_travelunit);
if (a) { if (a) {
quicklist *ql; quicklist *ql = (quicklist *)a->data.v;
ql_iter qi; selist_foreach_ex(ql, cb_map, &cbdata);
ql = (quicklist *)a->data.v;
for (qi = qli_init(&ql); qli_more(qi);) {
unit *u = (unit *)qli_next(&qi);
cb(r, u, cbdata);
}
} }
} }

View File

@ -215,7 +215,7 @@ const message_type *mt_register(message_type * type)
unsigned int hash = hashstring(type->name) % MT_MAXHASH; unsigned int hash = hashstring(type->name) % MT_MAXHASH;
quicklist **qlp = messagetypes + hash; quicklist **qlp = messagetypes + hash;
if (ql_set_insert(qlp, type)) { if (selist_set_insert(qlp, type, NULL)) {
type->key = mt_id(type); type->key = mt_id(type);
} }
return type; return type;