server/src/kernel/alliance.c

532 lines
14 KiB
C
Raw Normal View History

/*
2010-08-08 10:06:34 +02:00
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
| | Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
| (c) 1998 - 2003 | Henning Peters <faroul@beyond.kn-bremen.de>
| | Ingo Wilken <Ingo.Wilken@informatik.uni-oldenburg.de>
+-------------------+ Stefan Reich <reich@halbling.de>
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#include <platform.h>
#include <kernel/config.h>
#include "alliance.h"
#include <attributes/key.h>
/* kernel includes */
#include <kernel/building.h>
#include <kernel/faction.h>
#include <kernel/messages.h>
2010-08-08 10:06:34 +02:00
#include <kernel/order.h>
#include <kernel/region.h>
#include <kernel/unit.h>
#include <kernel/item.h>
#include <kernel/command.h>
/* util includes */
#include <util/attrib.h>
#include <util/base36.h>
2012-05-29 21:17:25 +02:00
#include <util/bsdstring.h>
2010-08-08 10:06:34 +02:00
#include <util/language.h>
#include <util/parser.h>
#include <quicklist.h>
2010-08-08 10:06:34 +02:00
#include <util/rng.h>
#include <util/umlaut.h>
/* libc includes */
#include <assert.h>
#include <stdlib.h>
#include <string.h>
2011-03-07 08:02:35 +01:00
alliance *alliances = NULL;
2010-08-08 10:06:34 +02:00
2014-12-09 14:43:11 +01:00
void free_alliances(void)
2010-08-08 10:06:34 +02:00
{
2014-12-09 14:43:11 +01:00
while (alliances) {
alliance *al = alliances;
alliances = al->next;
free(al->name);
if (al->members) {
ql_free(al->members);
}
free(al);
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
alliance *makealliance(int id, const char *name)
2010-08-08 10:06:34 +02:00
{
alliance *al;;
for (;;) {
if (id > 0) {
for (al = alliances; al; al = al->next) {
if (al->id == id) {
id = 0;
break;
}
}
if (id > 0)
break;
2010-08-08 10:06:34 +02:00
}
id = id ? id : (1 + (rng_int() % MAX_UNIT_NR));
}
al = calloc(1, sizeof(alliance));
al->id = id;
if (name) {
al->name = _strdup(name);
2010-08-08 10:06:34 +02:00
}
else {
al->flags |= ALF_NON_ALLIED;
}
al->next = alliances;
return alliances = al;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
alliance *findalliance(int id)
2010-08-08 10:06:34 +02:00
{
alliance *al;
for (al = alliances; al; al = al->next) {
if (al->id == id)
return al;
}
return NULL;
2010-08-08 10:06:34 +02:00
}
typedef struct alliance_transaction {
struct alliance_transaction *next;
unit *u;
order *ord;
/* alliance * al; */
/* variant userdata; */
2010-08-08 10:06:34 +02:00
} alliance_transaction;
2011-03-07 08:02:35 +01:00
static struct alliance_transaction *transactions[ALLIANCE_MAX];
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
faction *alliance_get_leader(alliance * al)
2010-08-08 10:06:34 +02:00
{
if (!al->_leader) {
if (al->members) {
al->_leader = (faction *)ql_get(al->members, 0);
}
2010-08-08 10:06:34 +02:00
}
return al->_leader;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void create_transaction(int type, unit * u, order * ord)
2010-08-08 10:06:34 +02:00
{
alliance_transaction *tr =
(alliance_transaction *)calloc(1, sizeof(alliance_transaction));
tr->ord = ord;
tr->u = u;
tr->next = transactions[type];
transactions[type] = tr;
2010-08-08 10:06:34 +02:00
}
static void cmd_kick(const void *tnext, struct unit *u, struct order *ord)
2010-08-08 10:06:34 +02:00
{
create_transaction(ALLIANCE_KICK, u, ord);
2010-08-08 10:06:34 +02:00
}
static void cmd_leave(const void *tnext, struct unit *u, struct order *ord)
2010-08-08 10:06:34 +02:00
{
create_transaction(ALLIANCE_LEAVE, u, ord);
2010-08-08 10:06:34 +02:00
}
static void cmd_transfer(const void *tnext, struct unit *u, struct order *ord)
2010-08-08 10:06:34 +02:00
{
create_transaction(ALLIANCE_TRANSFER, u, ord);
2010-08-08 10:06:34 +02:00
}
static void cmd_new(const void *tnext, struct unit *u, struct order *ord)
2010-08-08 10:06:34 +02:00
{
create_transaction(ALLIANCE_NEW, u, ord);
2010-08-08 10:06:34 +02:00
}
static void cmd_invite(const void *tnext, struct unit *u, struct order *ord)
2010-08-08 10:06:34 +02:00
{
create_transaction(ALLIANCE_INVITE, u, ord);
2010-08-08 10:06:34 +02:00
}
static void cmd_join(const void *tnext, struct unit *u, struct order *ord)
2010-08-08 10:06:34 +02:00
{
create_transaction(ALLIANCE_JOIN, u, ord);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void perform_kick(void)
2010-08-08 10:06:34 +02:00
{
alliance_transaction **tap = transactions + ALLIANCE_KICK;
while (*tap) {
alliance_transaction *ta = *tap;
alliance *al = f_get_alliance(ta->u->faction);
if (al && alliance_get_leader(al) == ta->u->faction) {
faction *f;
init_order(ta->ord);
skip_token();
f = getfaction();
if (f && f_get_alliance(f) == al) {
setalliance(f, NULL);
}
}
*tap = ta->next;
free(ta);
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
static void perform_new(void)
2010-08-08 10:06:34 +02:00
{
alliance_transaction **tap = transactions + ALLIANCE_NEW;
while (*tap) {
alliance_transaction *ta = *tap;
alliance *al;
int id;
faction *f = ta->u->faction;
init_order(ta->ord);
skip_token();
id = getid();
al = makealliance(id, itoa36(id));
setalliance(f, al);
*tap = ta->next;
free(ta);
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void perform_leave(void)
2010-08-08 10:06:34 +02:00
{
alliance_transaction **tap = transactions + ALLIANCE_LEAVE;
while (*tap) {
alliance_transaction *ta = *tap;
faction *f = ta->u->faction;
2010-08-08 10:06:34 +02:00
setalliance(f, NULL);
2010-08-08 10:06:34 +02:00
*tap = ta->next;
free(ta);
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void perform_transfer(void)
2010-08-08 10:06:34 +02:00
{
alliance_transaction **tap = transactions + ALLIANCE_TRANSFER;
while (*tap) {
alliance_transaction *ta = *tap;
alliance *al = f_get_alliance(ta->u->faction);
if (al && alliance_get_leader(al) == ta->u->faction) {
faction *f;
init_order(ta->ord);
skip_token();
f = getfaction();
if (f && f_get_alliance(f) == al) {
al->_leader = f;
}
}
*tap = ta->next;
free(ta);
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
static void perform_join(void)
2010-08-08 10:06:34 +02:00
{
alliance_transaction **tap = transactions + ALLIANCE_JOIN;
while (*tap) {
alliance_transaction *ta = *tap;
faction *fj = ta->u->faction;
int aid;
init_order(ta->ord);
skip_token();
aid = getid();
if (aid) {
alliance *al = findalliance(aid);
if (al && f_get_alliance(fj) != al) {
alliance_transaction **tip = transactions + ALLIANCE_INVITE;
alliance_transaction *ti = *tip;
while (ti) {
faction *fi = ti->u->faction;
if (fi && f_get_alliance(fi) == al) {
int fid;
init_order(ti->ord);
skip_token();
fid = getid();
if (fid == fj->no) {
break;
}
}
tip = &ti->next;
ti = *tip;
}
if (ti) {
setalliance(fj, al);
*tip = ti->next;
free(ti);
}
else {
/* TODO: error message */
}
2010-08-08 10:06:34 +02:00
}
}
*tap = ta->next;
free(ta);
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
static void execute(const struct syntaxtree *syntax, keyword_t kwd)
2010-08-08 10:06:34 +02:00
{
int run = 0;
region **rp = &regions;
while (*rp) {
region *r = *rp;
unit **up = &r->units;
while (*up) {
unit *u = *up;
if (u->number) {
const struct locale *lang = u->faction->locale;
void *root = stree_find(syntax, lang);
order *ord;
for (ord = u->orders; ord; ord = ord->next) {
if (getkeyword(ord) == kwd) {
do_command(root, u, ord);
run = 1;
}
}
}
if (u == *up)
up = &u->next;
2010-08-08 10:06:34 +02:00
}
if (*rp == r)
rp = &r->next;
}
if (run) {
perform_kick();
perform_leave();
perform_transfer();
perform_new();
perform_join();
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
void alliance_cmd(void)
2010-08-08 10:06:34 +02:00
{
static syntaxtree *stree = NULL;
if (stree == NULL) {
syntaxtree *slang = stree = stree_create();
while (slang) {
void *leaf = 0;
add_command(&leaf, NULL, LOC(slang->lang, "new"), &cmd_new);
add_command(&leaf, NULL, LOC(slang->lang, "invite"), &cmd_invite);
add_command(&leaf, NULL, LOC(slang->lang, "join"), &cmd_join);
add_command(&leaf, NULL, LOC(slang->lang, "kick"), &cmd_kick);
add_command(&leaf, NULL, LOC(slang->lang, "leave"), &cmd_leave);
add_command(&leaf, NULL, LOC(slang->lang, "command"), &cmd_transfer);
slang->root = leaf;
slang = slang->next;
}
2010-08-08 10:06:34 +02:00
}
execute(stree, K_ALLIANCE);
/* some may have been kicked, must remove f->alliance==NULL */
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void alliancejoin(void)
2010-08-08 10:06:34 +02:00
{
static syntaxtree *stree = NULL;
if (stree == NULL) {
syntaxtree *slang = stree = stree_create();
while (slang) {
void *leaf = 0;
add_command(&leaf, NULL, LOC(slang->lang, "join"), &cmd_join);
add_command(&slang->root, leaf, LOC(slang->lang, "alliance"), NULL);
slang = slang->next;
}
2010-08-08 10:06:34 +02:00
}
execute(stree, K_ALLIANCE);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void setalliance(faction * f, alliance * al)
2010-08-08 10:06:34 +02:00
{
if (f->alliance == al)
return;
if (f->alliance != NULL) {
int qi;
quicklist **flistp = &f->alliance->members;
for (qi = 0; *flistp; ql_advance(flistp, &qi, 1)) {
faction *data = (faction *)ql_get(*flistp, qi);
if (data == f) {
ql_delete(flistp, qi);
break;
}
}
2010-08-08 10:06:34 +02:00
if (f->alliance->_leader == f) {
if (f->alliance->members) {
f->alliance->_leader = (faction *)ql_get(f->alliance->members, 0);
}
else {
f->alliance->_leader = NULL;
}
}
2010-08-08 10:06:34 +02:00
}
f->alliance = al;
f->alliance_joindate = turn;
if (al != NULL) {
ql_push(&al->members, f);
if (al->_leader == NULL) {
al->_leader = f;
}
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
const char *alliancename(const alliance * al)
2010-08-08 10:06:34 +02:00
{
typedef char name[OBJECTIDSIZE + 1];
static name idbuf[8];
static int nextbuf = 0;
2010-08-08 10:06:34 +02:00
char *ibuf = idbuf[(++nextbuf) % 8];
2010-08-08 10:06:34 +02:00
if (al && al->name) {
slprintf(ibuf, sizeof(name), "%s (%s)", al->name, itoa36(al->id));
}
else {
return NULL;
}
return ibuf;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void alliancevictory(void)
2010-08-08 10:06:34 +02:00
{
const struct building_type *btype = bt_find("stronghold");
region *r = regions;
alliance *al = alliances;
if (btype == NULL)
return;
while (r != NULL) {
building *b = r->buildings;
while (b != NULL) {
if (b->type == btype) {
unit *u = building_owner(b);
if (u) {
fset(u->faction->alliance, FFL_MARK);
}
}
b = b->next;
2010-08-08 10:06:34 +02:00
}
r = r->next;
2010-08-08 10:06:34 +02:00
}
while (al != NULL) {
if (!fval(al, FFL_MARK)) {
int qi;
quicklist *flist = al->members;
for (qi = 0; flist; ql_advance(&flist, &qi, 1)) {
faction *f = (faction *)ql_get(flist, qi);
if (f->alliance == al) {
ADDMSG(&f->msgs, msg_message("alliance::lost", "alliance", al));
destroyfaction(f);
}
}
}
else {
freset(al, FFL_MARK);
2010-08-08 10:06:34 +02:00
}
al = al->next;
2010-08-08 10:06:34 +02:00
}
}
2011-03-07 08:02:35 +01:00
int victorycondition(const alliance * al, const char *name)
2010-08-08 10:06:34 +02:00
{
const char *gems[] =
2011-03-07 08:02:35 +01:00
{ "opal", "diamond", "zaphire", "topaz", "beryl", "agate", "garnet",
"emerald", NULL };
if (strcmp(name, "gems") == 0) {
const char **igem;
for (igem = gems; *igem; ++igem) {
const struct resource_type *rtype = rt_find(*igem);
quicklist *flist = al->members;
int qi;
bool found = false;
assert(rtype);
for (qi = 0; flist && !found; ql_advance(&flist, &qi, 1)) {
faction *f = (faction *)ql_get(flist, 0);
unit *u;
for (u = f->units; u; u = u->nextF) {
if (i_get(u->items, rtype->itype) > 0) {
found = true;
break;
}
}
}
if (!found)
return 0;
2010-08-08 10:06:34 +02:00
}
return 1;
2010-08-08 10:06:34 +02:00
}
else if (strcmp(name, "phoenix") == 0) {
quicklist *flist = al->members;
int qi;
for (qi = 0; flist; ql_advance(&flist, &qi, 1)) {
faction *f = (faction *)ql_get(flist, qi);
if (find_key(f->attribs, atoi36("phnx"))) {
return 1;
}
}
return 0;
2010-08-08 10:06:34 +02:00
}
else if (strcmp(name, "pyramid") == 0) {
/* Logik:
* - if (pyr > last_passed_size && pyr > all_others) {
* pyr->passed->counter++;
* for(all_other_pyrs) {
* pyr->passed->counter=0;
* }
*
* if(pyr->passed->counter >= 3) {
* set(pyr, passed);
* pyr->owner->set_attrib(pyra);
* }
* last_passed_size = pyr->size;
* }
*/
quicklist *flist = al->members;
int qi;
for (qi = 0; flist; ql_advance(&flist, &qi, 1)) {
faction *f = (faction *)ql_get(flist, qi);
if (find_key(f->attribs, atoi36("pyra"))) {
return 1;
}
}
return 0;
2010-08-08 10:06:34 +02:00
}
return -1;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void alliance_setname(alliance * self, const char *name)
2010-08-08 10:06:34 +02:00
{
free(self->name);
if (name)
self->name = _strdup(name);
else
self->name = NULL;
2010-08-08 10:06:34 +02:00
}
bool is_allied(const struct faction *f1, const struct faction *f2) {
return (f1 == f2 || (f1->alliance && f1->alliance == f2->alliance));
}