server/src/kernel/terrain.c

159 lines
3.8 KiB
C
Raw Normal View History

/*
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 <attributes/racename.h>
#include "terrain.h"
#include "terrainid.h"
/* kernel includes */
#include "curse.h"
#include "region.h"
#include "resources.h"
#include <util/log.h>
#include <util/attrib.h>
/* libc includes */
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#define MAXTERRAINS 20
const char * terraindata[MAXTERRAINS] = {
"ocean",
"plain",
"swamp",
"desert",
"highland",
"mountain",
"glacier",
"firewall",
NULL, /* dungeon module */
NULL, /* former grassland */
"fog",
"thickfog",
"volcano",
"activevolcano",
"iceberg_sleep",
"iceberg",
NULL, /* museum module */
NULL, /* museum module */
NULL, /* former magicstorm */
NULL /* museum module */
};
static terrain_type * registered_terrains;
const terrain_type *
terrains(void)
{
return registered_terrains;
}
static const char *
plain_name(const struct region * r)
{
/* TODO: xml defined */
if (r_isforest(r)) return "forest";
return r->terrain->_name;
}
void
register_terrain(struct terrain_type * terrain)
{
assert(terrain->next==NULL),
terrain->next = registered_terrains;
registered_terrains = terrain;
if (strcmp("plain", terrain->_name)==0)
terrain->name = &plain_name;
}
const struct terrain_type *
get_terrain(const char * name)
{
const struct terrain_type * terrain;
for (terrain=registered_terrains;terrain;terrain=terrain->next) {
if (strcmp(terrain->_name, name)==0) break;
}
return terrain;
}
static const terrain_type * newterrains[MAXTERRAINS];
const struct terrain_type *
newterrain(terrain_t t)
{
if (t==NOTERRAIN) return NULL;
assert(t>=0);
assert(t<MAXTERRAINS);
return newterrains[t];
}
terrain_t
oldterrain(const struct terrain_type * terrain)
{
terrain_t t;
if (terrain==NULL) return NOTERRAIN;
for (t=0;t!=MAXTERRAINS;++t) {
if (newterrains[t]==terrain) return t;
}
log_warning(("%s is not a classic terrain.\n", terrain->_name));
return NOTERRAIN;
}
const char *
terrain_name(const struct region * r)
{
if (r->attribs) {
attrib * a = a_find(r->attribs, &at_racename);
if (a) {
const char * str = get_racename(a);
if (str) return str;
}
}
if (r->terrain->name!=NULL) {
return r->terrain->name(r);
} else if (fval(r->terrain, SEA_REGION)) {
if (curse_active(get_curse(r->attribs, ct_find("maelstrom")))) {
return "maelstrom";
}
}
return r->terrain->_name;
}
void
init_terrains(void)
{
terrain_t t;
for (t=0;t!=MAXTERRAINS;++t) {
const terrain_type * newterrain = newterrains[t];
if (newterrain!=NULL) continue;
if (terraindata[t]!=NULL) {
newterrain = get_terrain(terraindata[t]);
if (newterrain!=NULL) {
newterrains[t] = newterrain;
}
}
}
}