server/core/src/kernel/group.c

250 lines
5.3 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include <platform.h>
#include <kernel/config.h>
#include "group.h"
/* kernel includes */
#include "ally.h"
2010-08-08 10:06:34 +02:00
#include "faction.h"
#include "save.h"
#include "unit.h"
2010-08-08 10:06:34 +02:00
#include "version.h"
/* attrib includes */
#include <attributes/raceprefix.h>
/* util includes */
#include <util/attrib.h>
#include <util/base36.h>
#include <util/resolve.h>
#include <util/storage.h>
#include <util/unicode.h>
/* libc includes */
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#define GMAXHASH 2039
2011-03-07 08:02:35 +01:00
static group *ghash[GMAXHASH];
2010-08-08 10:06:34 +02:00
static int maxgid;
2011-03-07 08:02:35 +01:00
static group *new_group(faction * f, const char *name, int gid)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
group **gp = &f->groups;
int index = gid % GMAXHASH;
group *g = calloc(sizeof(group), 1);
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
while (*gp)
gp = &(*gp)->next;
*gp = g;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
maxgid = MAX(gid, maxgid);
g->name = _strdup(name);
2011-03-07 08:02:35 +01:00
g->gid = gid;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
g->nexthash = ghash[index];
return ghash[index] = g;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void init_group(faction * f, group * g)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
ally *a, **an;
an = &g->allies;
for (a = f->allies; a; a = a->next)
if (a->faction) {
ally *ga = calloc(sizeof(ally), 1);
*ga = *a;
*an = ga;
an = &ga->next;
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static group *find_groupbyname(group * g, const char *name)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
while (g && unicode_utf8_strcasecmp(name, g->name) != 0)
g = g->next;
return g;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static group *find_group(int gid)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
int index = gid % GMAXHASH;
group *g = ghash[index];
while (g && g->gid != gid)
g = g->nexthash;
return g;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static int read_group(attrib * a, void *owner, struct storage *store)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
group *g;
2010-08-08 10:06:34 +02:00
int gid = store->r_int(store);
a->data.v = g = find_group(gid);
2011-03-07 08:02:35 +01:00
if (g != 0) {
2010-08-08 10:06:34 +02:00
g->members++;
return AT_READ_OK;
}
return AT_READ_FAIL;
}
static void
2011-03-07 08:02:35 +01:00
write_group(const attrib * a, const void *owner, struct storage *store)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
group *g = (group *) a->data.v;
2010-08-08 10:06:34 +02:00
store->w_int(store, g->gid);
}
2011-03-07 08:02:35 +01:00
attrib_type at_group = { /* attribute for units assigned to a group */
"grp",
DEFAULT_INIT,
DEFAULT_FINALIZE, DEFAULT_AGE, write_group, read_group, ATF_UNIQUE};
void free_group(group * g)
2010-08-08 10:06:34 +02:00
{
int index = g->gid % GMAXHASH;
2011-03-07 08:02:35 +01:00
group **g_ptr = ghash + index;
while (*g_ptr && (*g_ptr)->gid != g->gid)
g_ptr = &(*g_ptr)->nexthash;
assert(*g_ptr == g);
2010-08-08 10:06:34 +02:00
*g_ptr = g->nexthash;
while (g->allies) {
2011-03-07 08:02:35 +01:00
ally *a = g->allies;
2010-08-08 10:06:34 +02:00
g->allies = a->next;
free(a);
}
free(g->name);
free(g);
}
group * get_group(const struct unit *u)
{
if (fval(u, UFL_GROUP)) {
attrib * a = a_find(u->attribs, &at_group);
if (a) {
return (group *) a->data.v;
}
}
return 0;
}
2011-03-07 08:02:35 +01:00
void set_group(struct unit *u, struct group *g)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
attrib *a = NULL;
2010-08-08 10:06:34 +02:00
if (fval(u, UFL_GROUP)) {
a = a_find(u->attribs, &at_group);
}
if (a) {
2011-03-07 08:02:35 +01:00
group *og = (group *) a->data.v;
if (og == g)
return;
2010-08-08 10:06:34 +02:00
--og->members;
}
if (g) {
if (!a) {
a = a_add(&u->attribs, a_new(&at_group));
fset(u, UFL_GROUP);
}
a->data.v = g;
g->members++;
} else if (a) {
a_remove(&u->attribs, a);
freset(u, UFL_GROUP);
}
}
bool join_group(unit * u, const char *name)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
group *g = NULL;
2010-08-08 10:06:34 +02:00
if (name && name[0]) {
g = find_groupbyname(u->faction->groups, name);
2011-03-07 08:02:35 +01:00
if (g == NULL) {
2010-08-08 10:06:34 +02:00
g = new_group(u->faction, name, ++maxgid);
init_group(u->faction, g);
}
}
set_group(u, g);
return true;
}
2011-03-07 08:02:35 +01:00
void write_groups(struct storage *store, group * g)
2010-08-08 10:06:34 +02:00
{
while (g) {
2011-03-07 08:02:35 +01:00
ally *a;
2010-08-08 10:06:34 +02:00
store->w_int(store, g->gid);
store->w_str(store, g->name);
2011-03-07 08:02:35 +01:00
for (a = g->allies; a; a = a->next) {
2010-08-08 10:06:34 +02:00
if (a->faction) {
write_faction_reference(a->faction, store);
store->w_int(store, a->status);
}
}
store->w_id(store, 0);
a_write(store, g->attribs, g);
store->w_brk(store);
2011-03-07 08:02:35 +01:00
g = g->next;
2010-08-08 10:06:34 +02:00
}
store->w_int(store, 0);
}
2011-03-07 08:02:35 +01:00
void read_groups(struct storage *store, faction * f)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
for (;;) {
ally **pa;
group *g;
2010-08-08 10:06:34 +02:00
int gid;
char buf[1024];
gid = store->r_int(store);
2011-03-07 08:02:35 +01:00
if (gid == 0)
break;
2010-08-08 10:06:34 +02:00
store->r_str_buf(store, buf, sizeof(buf));
g = new_group(f, buf, gid);
pa = &g->allies;
for (;;) {
2011-03-07 08:02:35 +01:00
ally *a;
2010-08-08 10:06:34 +02:00
variant fid;
fid.i = store->r_id(store);
2011-03-07 08:02:35 +01:00
if (fid.i <= 0)
break;
if (store->version < STORAGE_VERSION && fid.i == 0)
break;
2010-08-08 10:06:34 +02:00
a = malloc(sizeof(ally));
*pa = a;
pa = &a->next;
a->status = store->r_int(store);
a->faction = findfaction(fid.i);
2011-03-07 08:02:35 +01:00
if (!a->faction)
ur_add(fid, &a->faction, resolve_faction);
2010-08-08 10:06:34 +02:00
}
*pa = 0;
a_read(store, &g->attribs, g);
}
}