alliance code. #define ALLIANCES to use.

requires that alliancejoin and alliancekick are called at some point in the code.
This commit is contained in:
Enno Rehling 2002-08-26 14:16:16 +00:00
parent f0040493d1
commit 86c0a65184
11 changed files with 493 additions and 26 deletions

View File

@ -0,0 +1,160 @@
/* vi: set ts=2:
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
| | Enno Rehling <enno@eressea-pbem.de>
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
| (c) 1998 - 2001 | 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 <config.h>
#include <eressea.h>
#include "alliance.h"
#include "command.h"
/* kernel includes */
#include <faction.h>
#include <message.h>
#include <region.h>
#include <unit.h>
/* util includes */
#include <umlaut.h>
/* libc includes */
#include <stdlib.h>
#include <string.h>
alliance * alliances = NULL;
alliance *
makealliance(int id, const char * name)
{
alliance * al = calloc(1, sizeof(alliance));
al->id=id;
al->name=strdup(name);
al->next=alliances;
return alliances=al;
}
alliance *
findalliance(int id)
{
alliance * al;
for (al=alliances;al;al=al->next) {
if (al->id==id) return al;
}
return NULL;
}
void destroy_kick(struct attrib * a)
{
faction_list * flist = (faction_list*)a->data.v;
freelist(flist);
}
const attrib_type at_kick = { "kick",
DEFAULT_INIT, destroy_kick
};
static attrib *
make_kick(void)
{
return a_new(&at_kick);
}
void
add_kick(attrib * a, const faction * f)
{
faction_list * flist = (faction_list*)a->data.v;
while (flist && flist->data!=f) flist = flist->next;
if (flist) return;
flist = calloc(1, sizeof(faction_list));
flist->data = (void*)f;
flist->next = (faction_list*)a->data.v;
a->data.v = flist;
}
static void
alliance_kick(const char * str, void * data, const char * cmd)
{
unit * u = (unit*)data;
faction * f = findfaction(atoi36(igetstrtoken(str)));
attrib * a;
if (f==NULL || f->alliance!=u->faction->alliance) {
/* does not belong to our alliance */
return;
}
a = a_find(f->attribs, &at_kick);
if (a==NULL) a = a_add(&f->attribs, make_kick());
add_kick(a, f);
}
static void
alliance_join(const char * str, void * data, const char * cmd)
{
unit * u = (unit*)data;
alliance * al = findalliance(atoi36(igetstrtoken(str)));
if (u->faction->alliance!=NULL || al==NULL) {
/* not found */
return;
}
u->faction->alliance = al;
/* inform the rest? */
}
static tnode * g_keys;
static void
alliance_command(const char * str, void * data, const char * cmd)
{
do_command(g_keys, data, str);
}
static void
execute(tnode * root)
{
region ** rp = &regions;
while (*rp) {
region * r = *rp;
unit **up = &r->units;
while (*up) {
unit * u = *up;
strlist * order;
for (order = u->orders; order; order = order->next) {
if (igetkeyword(order->s, u->faction->locale) == K_ALLIANCE) {
do_command(root, u, order->s);
}
}
if (u==*up) up = &u->next;
}
if (*rp==r) rp = &r->next;
}
}
void
alliancejoin(void)
{
tnode root;
add_command(&root, "alliance", &alliance_command);
g_keys = calloc(1, sizeof(tnode));
add_command(g_keys, "join", &alliance_join);
execute(&root);
free(g_keys);
}
void
alliancekick(void)
{
tnode root;
add_command(&root, "alliance", &alliance_command);
g_keys = calloc(1, sizeof(tnode));
add_command(g_keys, "kick", &alliance_kick);
execute(&root);
free(g_keys);
}

View File

@ -0,0 +1,34 @@
/* vi: set ts=2:
*
*
* Eressea PB(E)M host Copyright (C) 1998-2000
* Christian Schlittchen (corwin@amber.kn-bremen.de)
* Katja Zedel (katze@felidae.kn-bremen.de)
* Henning Peters (faroul@beyond.kn-bremen.de)
* Enno Rehling (enno@eressea-pbem.de)
* Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
*
* This program may not be used, modified or distributed without
* prior permission by the authors of Eressea.
*/
struct plane;
struct attrib;
struct unit;
struct faction;
struct region;
typedef struct alliance {
struct alliance * next;
int id;
char * name;
} alliance;
extern alliance * alliances;
extern alliance * findalliance(int id);
extern alliance * makealliance(int id, const char * name);
extern void alliancejoin(void);
extern void alliancekick(void);
/* execute commands */

View File

@ -336,16 +336,13 @@ set_enemy(side * as, side * ds, boolean attacking)
if (attacking) as->enemy[ds->index] |= E_ATTACKING;
}
extern int alliance(const ally * sf, const faction * f, int mode);
#ifdef ALLIANCES
static int
allysf(side * s, faction * f)
allysfm(side * s, faction * f, int mode)
{
if (s->bf->faction==f) return true;
if (s->group) return alliance(s->group->allies, f, HELP_FIGHT);
return alliance(s->bf->faction->allies, f, HELP_FIGHT);
return isallied(s->battle->plane, s->faction, f, mode);
}
#else
static int
allysfm(side * s, faction * f, int mode)
{
@ -353,6 +350,14 @@ allysfm(side * s, faction * f, int mode)
if (s->group) return alliance(s->group->allies, f, mode);
return alliance(s->bf->faction->allies, f, mode);
}
#endif
static int
allysf(side * s, faction * f)
{
return allysfm(s, f, HELP_FIGHT);
}
troop
select_corpse(battle * b, fighter * af)

View File

@ -725,6 +725,24 @@ unit_has_cursed_item(unit *u)
return false;
}
#ifdef ALLIANCES
int
allied(const unit * u, const faction * f2, int mode)
{
if (u->faction->alliance==f2->alliance) return mode;
return 0;
}
int
isallied(const plane * pl, const faction * f, const faction * f2, int mode)
{
unused(pl);
if (f->alliance==f2->alliance) return mode;
return 0;
}
#else
/* f hat zu f2 HELFE mode gesetzt */
int
isallied(const plane * pl, const faction * f, const faction * f2, int mode)
@ -751,7 +769,7 @@ isallied(const plane * pl, const faction * f, const faction * f2, int mode)
return 0;
}
int
static int
alliance(const ally * sf, const faction * f, int mode)
{
while (sf) {
@ -792,6 +810,7 @@ allied(const unit * u, const faction * f2, int mode)
if (a) sf = ((group*)a->data.v)->allies;
return alliance(sf, f2, mode);
}
#endif
boolean
seefaction(const faction * f, const region * r, const unit * u, int modifier)

View File

@ -154,9 +154,10 @@ struct xml_stack;
#define WATCHERS_VERSION 310
#define OVERRIDE_VERSION 311
#define CURSETYPE_VERSION 312
#define ALLIANCES_VERSION 313
#define UGROUPS_VERSION 400 /* nicht aktivieren, nicht fertig */
#define RELEASE_VERSION CURSETYPE_VERSION
#define RELEASE_VERSION ALLIANCES_VERSION
/*
#if RELEASE_VERSION >= UGROUPS_VERSION
@ -440,6 +441,7 @@ enum {
K_PFLANZE,
K_WEREWOLF,
K_XE,
K_ALLIANCE,
MAXKEYWORDS,
NOKEYWORD = (keyword_t) - 1
};

View File

@ -14,6 +14,7 @@
#define FACTION_H
struct player;
struct alliance;
typedef struct faction {
struct faction *next;
@ -55,6 +56,9 @@ typedef struct faction {
int number; /* enno: unterschied zu num_people ? */
int money;
int score;
#ifdef ALLIANCES
struct alliance * alliance;
#endif
#ifdef VICTORY_DELAY
unsigned char victory_delay;
#endif

View File

@ -50,6 +50,11 @@
#include <attributes/ugroup.h>
#endif
/* modules include */
#ifdef ALLIANCES
# include <modules/alliance.h>
#endif
/* attributes includes */
#include <attributes/key.h>
@ -717,6 +722,20 @@ read_ugroups(FILE *file)
}
#endif
#ifdef ALLIANCES
void
read_alliances(FILE * F)
{
char pbuf[32];
rns(F, pbuf, sizeof(pbuf));
while (strcmp(pbuf, "end")!=0) {
rs(F, buf);
makealliance(atoi36(pbuf), buf);
rns(F, pbuf, sizeof(pbuf));
}
}
#endif
int
readgame(boolean backup)
{
@ -807,7 +826,11 @@ readgame(boolean backup)
}
/* Read factions */
#ifdef ALLIANCES
if (global.data_version>ALLIANCES_VERSION) {
read_alliances(F);
}
#endif
n = ri(F);
printf(" - Einzulesende Parteien: %d\n", n);
fp = &factions;
@ -1114,6 +1137,22 @@ void wi36(FILE * F, int n)
fprintf(F, "%s ", itoa36(n));
}
#ifdef ALLIANCES
void
write_alliances(FILE * F)
{
alliance * al = alliances;
while (al) {
ws(F, itoa36(al->id));
ws(F, al->name);
al = al->next;
wnl(F);
}
ws(F, "end");
wnl(F);
}
#endif
void
write_items(FILE *F, item *ilist)
{
@ -1263,7 +1302,9 @@ writegame(char *path, char quiet)
/* Write factions */
#if defined(ALLIANCES) && RELEASE_VERSION>=ALLIANCES_VERSION
write_alliances(F);
#endif
n=listlen(factions);
wi(F, n);
wnl(F);

View File

@ -0,0 +1,160 @@
/* vi: set ts=2:
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
| | Enno Rehling <enno@eressea-pbem.de>
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
| (c) 1998 - 2001 | 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 <config.h>
#include <eressea.h>
#include "alliance.h"
#include "command.h"
/* kernel includes */
#include <faction.h>
#include <message.h>
#include <region.h>
#include <unit.h>
/* util includes */
#include <umlaut.h>
/* libc includes */
#include <stdlib.h>
#include <string.h>
alliance * alliances = NULL;
alliance *
makealliance(int id, const char * name)
{
alliance * al = calloc(1, sizeof(alliance));
al->id=id;
al->name=strdup(name);
al->next=alliances;
return alliances=al;
}
alliance *
findalliance(int id)
{
alliance * al;
for (al=alliances;al;al=al->next) {
if (al->id==id) return al;
}
return NULL;
}
void destroy_kick(struct attrib * a)
{
faction_list * flist = (faction_list*)a->data.v;
freelist(flist);
}
const attrib_type at_kick = { "kick",
DEFAULT_INIT, destroy_kick
};
static attrib *
make_kick(void)
{
return a_new(&at_kick);
}
void
add_kick(attrib * a, const faction * f)
{
faction_list * flist = (faction_list*)a->data.v;
while (flist && flist->data!=f) flist = flist->next;
if (flist) return;
flist = calloc(1, sizeof(faction_list));
flist->data = (void*)f;
flist->next = (faction_list*)a->data.v;
a->data.v = flist;
}
static void
alliance_kick(const char * str, void * data, const char * cmd)
{
unit * u = (unit*)data;
faction * f = findfaction(atoi36(igetstrtoken(str)));
attrib * a;
if (f==NULL || f->alliance!=u->faction->alliance) {
/* does not belong to our alliance */
return;
}
a = a_find(f->attribs, &at_kick);
if (a==NULL) a = a_add(&f->attribs, make_kick());
add_kick(a, f);
}
static void
alliance_join(const char * str, void * data, const char * cmd)
{
unit * u = (unit*)data;
alliance * al = findalliance(atoi36(igetstrtoken(str)));
if (u->faction->alliance!=NULL || al==NULL) {
/* not found */
return;
}
u->faction->alliance = al;
/* inform the rest? */
}
static tnode * g_keys;
static void
alliance_command(const char * str, void * data, const char * cmd)
{
do_command(g_keys, data, str);
}
static void
execute(tnode * root)
{
region ** rp = &regions;
while (*rp) {
region * r = *rp;
unit **up = &r->units;
while (*up) {
unit * u = *up;
strlist * order;
for (order = u->orders; order; order = order->next) {
if (igetkeyword(order->s, u->faction->locale) == K_ALLIANCE) {
do_command(root, u, order->s);
}
}
if (u==*up) up = &u->next;
}
if (*rp==r) rp = &r->next;
}
}
void
alliancejoin(void)
{
tnode root;
add_command(&root, "alliance", &alliance_command);
g_keys = calloc(1, sizeof(tnode));
add_command(g_keys, "join", &alliance_join);
execute(&root);
free(g_keys);
}
void
alliancekick(void)
{
tnode root;
add_command(&root, "alliance", &alliance_command);
g_keys = calloc(1, sizeof(tnode));
add_command(g_keys, "kick", &alliance_kick);
execute(&root);
free(g_keys);
}

View File

@ -0,0 +1,34 @@
/* vi: set ts=2:
*
*
* Eressea PB(E)M host Copyright (C) 1998-2000
* Christian Schlittchen (corwin@amber.kn-bremen.de)
* Katja Zedel (katze@felidae.kn-bremen.de)
* Henning Peters (faroul@beyond.kn-bremen.de)
* Enno Rehling (enno@eressea-pbem.de)
* Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
*
* This program may not be used, modified or distributed without
* prior permission by the authors of Eressea.
*/
struct plane;
struct attrib;
struct unit;
struct faction;
struct region;
typedef struct alliance {
struct alliance * next;
int id;
char * name;
} alliance;
extern alliance * alliances;
extern alliance * findalliance(int id);
extern alliance * makealliance(int id, const char * name);
extern void alliancejoin(void);
extern void alliancekick(void);
/* execute commands */

View File

@ -86,6 +86,10 @@ LIB32=link.exe -lib
# PROP Default_Filter "*.h"
# Begin Source File
SOURCE=.\alliance.h
# End Source File
# Begin Source File
SOURCE=.\arena.h
# End Source File
# Begin Source File
@ -135,6 +139,10 @@ SOURCE=.\xmas2001.h
# End Group
# Begin Source File
SOURCE=.\alliance.c
# End Source File
# Begin Source File
SOURCE=.\arena.c
# End Source File
# Begin Source File