remove region.donations, replace with a global list.

This commit is contained in:
Enno Rehling 2015-09-09 16:47:09 +02:00
parent d29d38f710
commit 337aca9b3c
2 changed files with 56 additions and 45 deletions

View File

@ -4,57 +4,76 @@
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/messages.h> #include <kernel/messages.h>
#include <quicklist.h>
#include <stdlib.h> #include <stdlib.h>
typedef struct transfer {
struct region *r;
struct faction *f1, *f2;
int amount;
} transfer;
static quicklist *transfers = 0;
int cmp_transfer(const void *v1, const void *v2) {
const transfer *t1 = (const transfer *)v1;
const transfer *t2 = (const transfer *)v2;
if (t1->r == t2->r) {
if (t1->f1 == t2->f1) {
if (t1->f2 == t2->f2) {
return 0;
}
return t1->f2->no - t2->f2->no;
}
return t1->f1->no - t2->f1->no;
}
return t1->r->uid - t2->r->uid;
}
void add_donation(faction * f1, faction * f2, int amount, region * r) void add_donation(faction * f1, faction * f2, int amount, region * r)
{ {
donation *sp; transfer tr, *tf;
quicklist *ql = transfers;
int qi = 0;
sp = r->donations_; tr.r = r;
tr.f1 = f1;
while (sp) { tr.f2 = f2;
if (sp->f1 == f1 && sp->f2 == f2) { tr.amount = amount;
sp->amount += amount; if (ql_set_find_ex(&ql, &qi, &tr, cmp_transfer)) {
return; tf = (transfer *)ql_get(ql, qi);
tf->amount += amount;
} }
sp = sp->next; else {
tf = malloc(sizeof(transfer));
memcpy(tf, &tr, sizeof(transfer));
} }
ql_set_insert_ex(&transfers, tf, cmp_transfer);
sp = calloc(1, sizeof(donation));
sp->f1 = f1;
sp->f2 = f2;
sp->amount = amount;
sp->next = r->donations_;
r->donations_ = sp;
} }
void free_donations(void) { void free_donations(void) {
region *r; ql_foreach(transfers, free);
for (r = regions; r; r = r->next) { ql_free(transfers);
while (r->donations_) { transfers = 0;
donation *don = r->donations_; }
r->donations_ = don->next;
free(don); static void report_transfer(faction *f1, faction *f2, region *r, int amount) {
} struct message *msg = msg_message("donation",
} "from to amount", f1, f2, amount);
r_addmessage(r, f1, msg);
r_addmessage(r, f2, msg);
msg_release(msg);
} }
void report_donations(void) void report_donations(void)
{ {
region *r; ql_iter qli = qli_init(&transfers);;
for (r = regions; r; r = r->next) {
while (r->donations_) { while (qli_more(qli)) {
donation *sp = r->donations_; transfer *tf = (transfer *)qli_next(&qli);
if (sp->amount > 0) { if (tf->amount > 0) {
struct message *msg = msg_message("donation", report_transfer(tf->f1, tf->f2, tf->r, tf->amount);
"from to amount", sp->f1, sp->f2, sp->amount);
r_addmessage(r, sp->f1, msg);
r_addmessage(r, sp->f2, msg);
msg_release(msg);
}
r->donations_ = sp->next;
free(sp);
} }
} }
} }

View File

@ -62,7 +62,6 @@ extern "C" {
struct message; struct message;
struct message_list; struct message_list;
struct rawmaterial; struct rawmaterial;
struct donation;
struct item; struct item;
#define MORALE_TAX_FACTOR 0.005 /* 0.5% tax per point of morale */ #define MORALE_TAX_FACTOR 0.005 /* 0.5% tax per point of morale */
@ -104,12 +103,6 @@ extern "C" {
struct region_owner *ownership; struct region_owner *ownership;
} land_region; } land_region;
typedef struct donation {
struct donation *next;
struct faction *f1, *f2;
int amount;
} donation;
typedef struct region { typedef struct region {
struct region *next; struct region *next;
struct land_region *land; struct land_region *land;
@ -133,7 +126,6 @@ extern "C" {
struct message_list *msgs; struct message_list *msgs;
} *individual_messages; } *individual_messages;
struct attrib *attribs; struct attrib *attribs;
struct donation *donations_;
const struct terrain_type *terrain; const struct terrain_type *terrain;
struct rawmaterial *resources; struct rawmaterial *resources;
#ifdef FAST_CONNECT #ifdef FAST_CONNECT