fix qsort in wormhole code. pointers are hard.

This commit is contained in:
Enno Rehling 2014-09-29 20:38:01 +02:00
parent 3e829e2880
commit cbf9bb8985
1 changed files with 123 additions and 122 deletions

View File

@ -8,7 +8,7 @@
This program may not be used, modified or distributed This program may not be used, modified or distributed
without prior permission by the authors of Eressea. without prior permission by the authors of Eressea.
*/ */
#include <platform.h> #include <platform.h>
#include <kernel/config.h> #include <kernel/config.h>
@ -46,11 +46,11 @@ static bool good_region(const region * r)
static int cmp_age(const void *v1, const void *v2) static int cmp_age(const void *v1, const void *v2)
{ {
const region *r1 = (const region *)v1; const region **r1 = (const region **)v1;
const region *r2 = (const region *)v2; const region **r2 = (const region **)v2;
if (r1->age < r2->age) if ((*r1)->age < (*r2)->age)
return -1; return -1;
if (r1->age > r2->age) if ((*r1)->age > (*r2)->age)
return 1; return 1;
return 0; return 0;
} }
@ -72,7 +72,7 @@ static void wormhole_done(struct attrib *a)
static int wormhole_age(struct attrib *a) static int wormhole_age(struct attrib *a)
{ {
wormhole_data *data = (wormhole_data *) a->data.v; wormhole_data *data = (wormhole_data *)a->data.v;
int maxtransport = data->entry->size; int maxtransport = data->entry->size;
region *r = data->entry->region; region *r = data->entry->region;
unit *u = r->units; unit *u = r->units;
@ -82,7 +82,8 @@ static int wormhole_age(struct attrib *a)
message *m = NULL; message *m = NULL;
if (u->number > maxtransport || has_limited_skills(u)) { if (u->number > maxtransport || has_limited_skills(u)) {
m = msg_message("wormhole_requirements", "unit region", u, u->region); m = msg_message("wormhole_requirements", "unit region", u, u->region);
} else if (data->exit != NULL) { }
else if (data->exit != NULL) {
move_unit(u, data->exit, NULL); move_unit(u, data->exit, NULL);
maxtransport -= u->number; maxtransport -= u->number;
m = msg_message("wormhole_exit", "unit region", u, data->exit); m = msg_message("wormhole_exit", "unit region", u, data->exit);
@ -105,7 +106,7 @@ static int wormhole_age(struct attrib *a)
static void static void
wormhole_write(const struct attrib *a, const void *owner, struct storage *store) wormhole_write(const struct attrib *a, const void *owner, struct storage *store)
{ {
wormhole_data *data = (wormhole_data *) a->data.v; wormhole_data *data = (wormhole_data *)a->data.v;
write_building_reference(data->entry, store); write_building_reference(data->entry, store);
write_region_reference(data->exit, store); write_region_reference(data->exit, store);
} }
@ -125,7 +126,7 @@ static int resolve_exit(variant id, void *address)
static int wormhole_read(struct attrib *a, void *owner, struct storage *store) static int wormhole_read(struct attrib *a, void *owner, struct storage *store)
{ {
wormhole_data *data = (wormhole_data *) a->data.v; wormhole_data *data = (wormhole_data *)a->data.v;
resolve_fun resolver = (global.data_version < UIDHASH_VERSION) resolve_fun resolver = (global.data_version < UIDHASH_VERSION)
? resolve_exit : resolve_region_id; ? resolve_exit : resolve_region_id;
read_fun reader = (global.data_version < UIDHASH_VERSION) read_fun reader = (global.data_version < UIDHASH_VERSION)
@ -160,8 +161,8 @@ make_wormhole(const building_type * bt_wormhole, region * r1, region * r2)
building *b2 = new_building(bt_wormhole, r2, default_locale); building *b2 = new_building(bt_wormhole, r2, default_locale);
attrib *a1 = a_add(&b1->attribs, a_new(&at_wormhole)); attrib *a1 = a_add(&b1->attribs, a_new(&at_wormhole));
attrib *a2 = a_add(&b2->attribs, a_new(&at_wormhole)); attrib *a2 = a_add(&b2->attribs, a_new(&at_wormhole));
wormhole_data *d1 = (wormhole_data *) a1->data.v; wormhole_data *d1 = (wormhole_data *)a1->data.v;
wormhole_data *d2 = (wormhole_data *) a2->data.v; wormhole_data *d2 = (wormhole_data *)a2->data.v;
d1->entry = b1; d1->entry = b1;
d2->entry = b2; d2->entry = b2;
d1->exit = b2->region; d1->exit = b2->region;
@ -204,9 +205,9 @@ void create_wormholes(void)
if (count < 2) if (count < 2)
return; return;
match = (region **) malloc(sizeof(region *) * count); match = (region **)malloc(sizeof(region *) * count);
for (ql = rlist,qi = 0; i != count; ql_advance(&ql, &qi, 1)) { for (ql = rlist, qi = 0; i != count; ql_advance(&ql, &qi, 1)) {
match[i++] = (region *)ql_get(ql, qi); match[i++] = (region *)ql_get(ql, qi);
} }
qsort(match, count, sizeof(region *), cmp_age); qsort(match, count, sizeof(region *), cmp_age);