From 012da64223ca93847ab32297c6b98c89dac26c59 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Feb 2004 22:25:00 +0000 Subject: [PATCH] first prototype of wormholes --- src/common/gamecode/laws.c | 6 ++ src/common/kernel/building.c | 2 +- src/common/kernel/building.h | 1 + src/common/kernel/save.c | 1 + src/common/modules/modules.vcproj | 24 +++-- src/common/modules/wormhole.c | 170 ++++++++++++++++++++++++++++++ src/common/modules/wormhole.h | 30 ++++++ src/common/settings-eressea.h | 1 + src/eressea/main.c | 6 ++ src/eressea/server.cpp | 7 ++ src/mapper/mapper.c | 8 +- 11 files changed, 245 insertions(+), 11 deletions(-) create mode 100644 src/common/modules/wormhole.c create mode 100644 src/common/modules/wormhole.h diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index fbf78906b..7bac1f8ab 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -25,6 +25,9 @@ #include #include +#ifdef WORMHOLE_MODULE +#include +#endif /* gamecode includes */ #include "economy.h" @@ -3671,6 +3674,9 @@ processorders (void) puts(" - Attribute altern"); ageing(); +#ifdef WORMHOLE_MODULE + create_wormholes(); +#endif /* immer ausführen, wenn neue Sprüche dazugekommen sind, oder sich * Beschreibungen geändert haben */ update_spells(); diff --git a/src/common/kernel/building.c b/src/common/kernel/building.c index 7a78a9e10..0ea84fd77 100644 --- a/src/common/kernel/building.c +++ b/src/common/kernel/building.c @@ -70,7 +70,7 @@ bt_find(const char* name) return btl?btl->type:NULL; } -static void +void bt_register(building_type * type) { struct building_typelist * btl = malloc(sizeof(building_type)); diff --git a/src/common/kernel/building.h b/src/common/kernel/building.h index c08bf3fdb..2d42c0077 100644 --- a/src/common/kernel/building.h +++ b/src/common/kernel/building.h @@ -59,6 +59,7 @@ typedef struct building_type { extern const building_type * bt_find(const char* name); extern void register_buildings(void); +extern void bt_register(building_type * type); typedef struct building_typelist { struct building_typelist * next; diff --git a/src/common/kernel/save.c b/src/common/kernel/save.c index 053344465..734684ae1 100644 --- a/src/common/kernel/save.c +++ b/src/common/kernel/save.c @@ -860,6 +860,7 @@ readgame(const char * filename, int backup) int rmax = maxregions; sprintf(buf, "%s/%s", datapath(), filename); + log_printf("- reading game data from %s\n", filename); if (backup) create_backup(buf); F = cfopen(buf, "r"); if (F==NULL) { diff --git a/src/common/modules/modules.vcproj b/src/common/modules/modules.vcproj index 19c596708..e5ece7a33 100644 --- a/src/common/modules/modules.vcproj +++ b/src/common/modules/modules.vcproj @@ -123,18 +123,30 @@ + + + + + + + + @@ -148,15 +160,9 @@ - - - - @@ -166,15 +172,15 @@ - - + + diff --git a/src/common/modules/wormhole.c b/src/common/modules/wormhole.c new file mode 100644 index 000000000..f577ac66d --- /dev/null +++ b/src/common/modules/wormhole.c @@ -0,0 +1,170 @@ +/* vi: set ts=2: + +-------------------+ + | | Christian Schlittchen + | Eressea PBEM host | Enno Rehling + | (c) 1998 - 2004 | Katja Zedel + | | + +-------------------+ + + This program may not be used, modified or distributed + without prior permission by the authors of Eressea. +*/ + +#include +#include + +#ifdef WORMHOLE_MODULE +#include "wormhole.h" + +/* kernel includes */ +#include +#include +#include +#include +#include + +/* util includes */ +#include + +/* libc includes */ +#include + +static boolean +good_region(const region * r) +{ + return (r->units!=NULL && r->land!=NULL); +} + +static int +cmp_age(const void * v1, const void *v2) +{ + const region * r1 = (const region*)v1; + const region * r2 = (const region*)v2; + if (r1->ageage) return -1; + if (r1->age>r2->age) return 1; + return 0; +} + +static building_type bt_wormhole = { + "wormhole", BTF_NOBUILD|BTF_UNIQUE|BTF_INDESTRUCTIBLE, + 1, 4, 4, + 0, 0, 0, 0.0 +}; + +typedef struct wormhole_data { + building * entry; + building * exit; +} wormhole_data; + +static void +wormhole_init(struct attrib *a) +{ + a->data.v = calloc(1, sizeof(wormhole_data)); +} + +static void +wormhole_done(struct attrib * a) +{ + free(a->data.v); +} + +static int +wormhole_age(struct attrib * a) +{ + wormhole_data * data = (wormhole_data*)a->data.v; + destroy_building(data->entry); + /* age returns 0 if the attribute needs to be removed, !=0 otherwise */ + return -1; +} + +void +wormhole_write(const struct attrib * a, FILE* F) +{ + wormhole_data * data = (wormhole_data*)a->data.v; + write_building_reference(data->entry, F); + write_building_reference(data->exit, F); +} + +int +wormhole_read(struct attrib * a, FILE* F) +{ + wormhole_data * data = (wormhole_data*)a->data.v; + + read_building_reference(&data->entry, F); + read_building_reference(&data->exit, F); + /* return AT_READ_OK on success, AT_READ_FAIL if attrib needs removal */ + return AT_READ_OK; +} + +static attrib_type at_wormhole = { + "wormhole", + wormhole_init, + wormhole_done, + wormhole_age, + wormhole_write, + wormhole_read, + ATF_UNIQUE +}; + +static void +make_wormhole(region * r1, region * r2) +{ + building * b1 = new_building(&bt_wormhole, r1, NULL); + building * b2 = new_building(&bt_wormhole, r2, NULL); + attrib * a1 = a_add(&b1->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 * d2 = (wormhole_data*)a2->data.v; + d1->entry = d2->exit = b1; + d2->entry = d1->exit = b2; + /* ADDMSG() */ +} + +void +create_wormholes(void) +{ +#define WORMHOLE_CHANCE 10000 + region_list *rptr, * rlist = NULL; + region * r = regions; + int i = 0, count = 0; + region ** match; + + /* + * select a list of regions. we'll sort them by age later. + */ + while (r!=NULL) { + int next = rand() % (2*WORMHOLE_CHANCE); + while (r!=NULL && (next!=0 || !good_region(r))) { + if (good_region(r)) { + --next; + } + r=r->next; + } + if (r==NULL) break; + add_regionlist(&rlist, r); + ++count; + r=r->next; + } + + match = (region**)malloc(sizeof(region*) * count); + qsort(match, count, sizeof(region *), cmp_age); + rptr = rlist; + while (i!=count) { + match[i++] = rptr->data; + rptr = rptr->next; + } + free_regionlist(rlist); + + count /= 2; + for (i=0;i!=count;++i) { + make_wormhole(match[i], match[i+count]); + } +} + +void +register_wormholes(void) +{ + bt_register(&bt_wormhole); + at_register(&at_wormhole); +} +#endif diff --git a/src/common/modules/wormhole.h b/src/common/modules/wormhole.h new file mode 100644 index 000000000..8bb120210 --- /dev/null +++ b/src/common/modules/wormhole.h @@ -0,0 +1,30 @@ +/* vi: set ts=2: + * + * + * Eressea PB(E)M host Copyright (C) 1998-2003 + * 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. + */ +#ifndef H_MOD_WORMHOLE +#define H_MOD_WORMHOLE +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WORMHOLE_MODULE +#error "must define WORMHOLE_MODULE to use this module" +#endif + + extern void create_wormholes(void); + extern void register_wormholes(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/common/settings-eressea.h b/src/common/settings-eressea.h index 07dd40f86..4e77d5ed4 100644 --- a/src/common/settings-eressea.h +++ b/src/common/settings-eressea.h @@ -44,6 +44,7 @@ #define MUSEUM_MODULE #define ARENA_MODULE +#define WORMHOLE_MODULE #define MAILITPATH "/usr/sbin:$HOME/eressea/bin:/bin:/usr/bin:/usr/local/bin" diff --git a/src/eressea/main.c b/src/eressea/main.c index d0fbbf666..0f809a57f 100644 --- a/src/eressea/main.c +++ b/src/eressea/main.c @@ -44,6 +44,9 @@ #ifdef MUSEUM_MODULE #include #endif +#ifdef WORMHOLE_MODULE +#include +#endif #ifdef ARENA_MODULE #include #endif @@ -185,6 +188,9 @@ game_init(void) #ifdef ARENA_MODULE register_arena(); #endif +#ifdef WORMHOLE_MODULE + register_wormholes(); +#endif #ifdef REMOVE_THIS render_init(); { diff --git a/src/eressea/server.cpp b/src/eressea/server.cpp index 593574037..8627e80b8 100644 --- a/src/eressea/server.cpp +++ b/src/eressea/server.cpp @@ -47,6 +47,9 @@ #ifdef MUSEUM_MODULE #include #endif +#ifdef WORMHOLE_MODULE +#include +#endif #ifdef ARENA_MODULE #include #endif @@ -199,6 +202,10 @@ game_init(void) #ifdef ARENA_MODULE register_arena(); #endif +#ifdef WORMHOLE_MODULE + register_wormholes(); +#endif + #ifdef REMOVE_THIS render_init(); { diff --git a/src/mapper/mapper.c b/src/mapper/mapper.c index 9623e56d3..2d0d42b4b 100644 --- a/src/mapper/mapper.c +++ b/src/mapper/mapper.c @@ -41,6 +41,9 @@ #ifdef ARENA_MODULE #include #endif +#ifdef WORMHOLE_MODULE +#include +#endif /* kernel includes */ #include @@ -1708,7 +1711,10 @@ main(int argc, char *argv[]) register_museum(); #endif #ifdef ARENA_MODULE - register_arena(); + register_arena(); +#endif +#ifdef WORMHOLE_MODULE + register_wormholes(); #endif /* register_dungeon(); */