2001-01-25 10:37:55 +01:00
|
|
|
|
/* vi: set ts=2:
|
|
|
|
|
*
|
2001-04-14 13:39:14 +02:00
|
|
|
|
*
|
2003-07-29 11:48:03 +02:00
|
|
|
|
* Eressea PB(E)M host Copyright (C) 1998-2003
|
2001-01-25 10:37:55 +01:00
|
|
|
|
* Christian Schlittchen (corwin@amber.kn-bremen.de)
|
|
|
|
|
* Katja Zedel (katze@felidae.kn-bremen.de)
|
|
|
|
|
* Henning Peters (faroul@beyond.kn-bremen.de)
|
2007-09-02 20:11:17 +02:00
|
|
|
|
* Enno Rehling (enno@eressea.de)
|
2001-01-25 10:37:55 +01:00
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
2008-04-20 16:48:15 +02:00
|
|
|
|
#include <kernel/eressea.h>
|
2001-01-25 10:37:55 +01:00
|
|
|
|
#include "ship.h"
|
|
|
|
|
|
|
|
|
|
/* kernel includes */
|
2003-07-29 11:48:03 +02:00
|
|
|
|
#include "build.h"
|
2001-01-25 10:37:55 +01:00
|
|
|
|
#include "unit.h"
|
|
|
|
|
#include "item.h"
|
|
|
|
|
#include "region.h"
|
2002-01-31 23:18:00 +01:00
|
|
|
|
#include "skill.h"
|
2001-01-25 10:37:55 +01:00
|
|
|
|
|
|
|
|
|
/* util includes */
|
2008-05-23 11:25:41 +02:00
|
|
|
|
#include <util/attrib.h>
|
2006-01-27 00:05:40 +01:00
|
|
|
|
#include <util/base36.h>
|
|
|
|
|
#include <util/event.h>
|
|
|
|
|
#include <util/language.h>
|
2007-06-20 02:34:02 +02:00
|
|
|
|
#include <util/lists.h>
|
2006-01-27 00:05:40 +01:00
|
|
|
|
#include <util/umlaut.h>
|
2008-04-26 16:55:22 +02:00
|
|
|
|
#include <util/storage.h>
|
2006-01-27 00:05:40 +01:00
|
|
|
|
#include <util/xml.h>
|
2001-01-25 10:37:55 +01:00
|
|
|
|
|
|
|
|
|
/* libc includes */
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ship_typelist *shiptypes = NULL;
|
|
|
|
|
|
2002-01-20 10:31:15 +01:00
|
|
|
|
static local_names * snames;
|
|
|
|
|
|
|
|
|
|
const ship_type *
|
2007-08-10 09:03:23 +02:00
|
|
|
|
findshiptype(const char * name, const struct locale * lang)
|
2002-01-20 10:31:15 +01:00
|
|
|
|
{
|
|
|
|
|
local_names * sn = snames;
|
2005-06-10 00:10:35 +02:00
|
|
|
|
variant var;
|
2002-01-20 10:31:15 +01:00
|
|
|
|
|
|
|
|
|
while (sn) {
|
|
|
|
|
if (sn->lang==lang) break;
|
|
|
|
|
sn=sn->next;
|
|
|
|
|
}
|
|
|
|
|
if (!sn) {
|
|
|
|
|
struct ship_typelist * stl = shiptypes;
|
|
|
|
|
sn = calloc(sizeof(local_names), 1);
|
|
|
|
|
sn->next = snames;
|
|
|
|
|
sn->lang = lang;
|
|
|
|
|
while (stl) {
|
2005-06-10 00:10:35 +02:00
|
|
|
|
variant var;
|
2007-08-10 09:03:23 +02:00
|
|
|
|
const char * n = locale_string(lang, stl->type->name[0]);
|
2005-06-10 00:10:35 +02:00
|
|
|
|
var.v = (void*)stl->type;
|
|
|
|
|
addtoken(&sn->names, n, var);
|
|
|
|
|
stl = stl->next;
|
2002-01-20 10:31:15 +01:00
|
|
|
|
}
|
|
|
|
|
snames = sn;
|
|
|
|
|
}
|
2005-06-10 00:10:35 +02:00
|
|
|
|
if (findtoken(&sn->names, name, &var)==E_TOK_NOMATCH) return NULL;
|
|
|
|
|
return (const ship_type*)var.v;
|
2002-01-20 10:31:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2001-01-25 10:37:55 +01:00
|
|
|
|
const ship_type *
|
|
|
|
|
st_find(const char* name)
|
|
|
|
|
{
|
|
|
|
|
const struct ship_typelist * stl = shiptypes;
|
2007-06-27 02:54:54 +02:00
|
|
|
|
while (stl && strcmp(stl->type->name[0], name)) stl = stl->next;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
return stl?stl->type:NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
st_register(const ship_type * type) {
|
|
|
|
|
struct ship_typelist * stl = malloc(sizeof(ship_type));
|
|
|
|
|
stl->type = type;
|
|
|
|
|
stl->next = shiptypes;
|
|
|
|
|
shiptypes = stl;
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-26 08:42:58 +02:00
|
|
|
|
#define SMAXHASH 7919
|
2001-01-25 10:37:55 +01:00
|
|
|
|
ship *shiphash[SMAXHASH];
|
|
|
|
|
void
|
|
|
|
|
shash(ship * s)
|
|
|
|
|
{
|
|
|
|
|
ship *old = shiphash[s->no % SMAXHASH];
|
|
|
|
|
|
|
|
|
|
shiphash[s->no % SMAXHASH] = s;
|
|
|
|
|
s->nexthash = old;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
sunhash(ship * s)
|
|
|
|
|
{
|
|
|
|
|
ship **show;
|
|
|
|
|
|
|
|
|
|
for (show = &shiphash[s->no % SMAXHASH]; *show; show = &(*show)->nexthash) {
|
|
|
|
|
if ((*show)->no == s->no)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (*show) {
|
|
|
|
|
assert(*show == s);
|
|
|
|
|
*show = (*show)->nexthash;
|
|
|
|
|
s->nexthash = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ship *
|
|
|
|
|
sfindhash(int i)
|
|
|
|
|
{
|
|
|
|
|
ship *old;
|
|
|
|
|
|
|
|
|
|
for (old = shiphash[i % SMAXHASH]; old; old = old->nexthash)
|
|
|
|
|
if (old->no == i)
|
|
|
|
|
return old;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ship *
|
|
|
|
|
findship(int i)
|
|
|
|
|
{
|
|
|
|
|
return sfindhash(i);
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-07 13:11:30 +02:00
|
|
|
|
struct ship *
|
|
|
|
|
findshipr(const region *r, int n)
|
|
|
|
|
{
|
|
|
|
|
ship * sh;
|
|
|
|
|
|
|
|
|
|
for (sh = r->ships; sh; sh = sh->next) {
|
|
|
|
|
if (sh->no == n) {
|
|
|
|
|
assert(sh->region == r);
|
|
|
|
|
return sh;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-25 10:37:55 +01:00
|
|
|
|
void
|
|
|
|
|
damage_ship(ship * sh, double percent)
|
|
|
|
|
{
|
|
|
|
|
double damage = DAMAGE_SCALE * percent * sh->size + sh->damage;
|
|
|
|
|
sh->damage = (int)damage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unit *
|
|
|
|
|
captain(ship *sh, region *r)
|
|
|
|
|
{
|
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
|
|
for(u = r->units; u; u = u->next)
|
2003-07-29 11:48:03 +02:00
|
|
|
|
if(u->ship == sh && fval(u, UFL_OWNER)) return u;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Alte Schiffstypen: */
|
2008-05-23 11:25:41 +02:00
|
|
|
|
static ship * deleted_ships;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
|
|
|
|
|
ship *
|
2004-07-12 23:10:16 +02:00
|
|
|
|
new_ship(const ship_type * stype, const struct locale * lang, region * r)
|
2001-01-25 10:37:55 +01:00
|
|
|
|
{
|
2008-05-23 11:25:41 +02:00
|
|
|
|
static char buffer[7 + IDSIZE + 1];
|
|
|
|
|
ship *sh = (ship *) calloc(1, sizeof(ship));
|
2001-01-25 10:37:55 +01:00
|
|
|
|
|
2008-05-23 11:25:41 +02:00
|
|
|
|
sh->no = newcontainerid();
|
|
|
|
|
sh->coast = NODIRECTION;
|
|
|
|
|
sh->type = stype;
|
|
|
|
|
sh->region = r;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
|
2008-05-23 11:25:41 +02:00
|
|
|
|
sprintf(buffer, "%s %s", LOC(lang, stype->name[0]), shipid(sh));
|
|
|
|
|
sh->name = strdup(buffer);
|
|
|
|
|
shash(sh);
|
2004-12-22 23:58:52 +01:00
|
|
|
|
addlist(&r->ships, sh);
|
2008-05-23 11:25:41 +02:00
|
|
|
|
return sh;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2008-05-23 11:25:41 +02:00
|
|
|
|
remove_ship(ship ** slist, ship * sh)
|
2001-01-25 10:37:55 +01:00
|
|
|
|
{
|
2004-05-31 18:21:03 +02:00
|
|
|
|
region * r = sh->region;
|
|
|
|
|
unit * u = r->units;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
|
2008-05-23 11:25:41 +02:00
|
|
|
|
handle_event(sh->attribs, "destroy", sh);
|
2004-05-31 18:21:03 +02:00
|
|
|
|
while (u) {
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
leave_ship(u);
|
|
|
|
|
}
|
|
|
|
|
u = u->next;
|
|
|
|
|
}
|
|
|
|
|
sunhash(sh);
|
2008-05-23 11:25:41 +02:00
|
|
|
|
while (*slist && *slist!=sh) slist = &(*slist)->next;
|
2008-05-24 02:20:00 +02:00
|
|
|
|
assert(*slist);
|
2008-05-23 11:25:41 +02:00
|
|
|
|
*slist = sh->next;
|
|
|
|
|
sh->next = deleted_ships;
|
|
|
|
|
deleted_ships = sh;
|
2008-05-24 02:20:00 +02:00
|
|
|
|
sh->region = NULL;
|
2008-05-23 11:25:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
free_ship(ship * s)
|
|
|
|
|
{
|
|
|
|
|
while (s->attribs) a_remove(&s->attribs, s->attribs);
|
|
|
|
|
free(s->name);
|
|
|
|
|
free(s->display);
|
|
|
|
|
free(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
free_ships(void)
|
|
|
|
|
{
|
|
|
|
|
while (deleted_ships) {
|
|
|
|
|
ship * s = deleted_ships;
|
|
|
|
|
deleted_ships = s->next;
|
|
|
|
|
}
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-10 09:03:23 +02:00
|
|
|
|
const char *
|
|
|
|
|
write_shipname(const ship * sh, char * ibuf, size_t size)
|
2007-06-28 02:39:55 +02:00
|
|
|
|
{
|
2007-08-10 09:03:23 +02:00
|
|
|
|
snprintf(ibuf, size, "%s (%s)", sh->name, itoa36(sh->no));
|
2007-06-28 02:39:55 +02:00
|
|
|
|
ibuf[size-1] = 0;
|
|
|
|
|
return ibuf;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-10 09:03:23 +02:00
|
|
|
|
const char *
|
2001-01-25 10:37:55 +01:00
|
|
|
|
shipname(const ship * sh)
|
|
|
|
|
{
|
2007-02-25 19:49:28 +01:00
|
|
|
|
typedef char name[OBJECTIDSIZE + 1];
|
|
|
|
|
static name idbuf[8];
|
|
|
|
|
static int nextbuf = 0;
|
|
|
|
|
char *ibuf = idbuf[(++nextbuf) % 8];
|
2007-08-10 09:03:23 +02:00
|
|
|
|
return write_shipname(sh, ibuf, sizeof(name));
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-25 16:30:51 +01:00
|
|
|
|
int
|
|
|
|
|
shipcapacity (const ship * sh)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* sonst ist construction:: size nicht ship_type::maxsize */
|
|
|
|
|
assert(!sh->type->construction || sh->type->construction->improvement==NULL);
|
|
|
|
|
|
|
|
|
|
if (sh->type->construction && sh->size!=sh->type->construction->maxsize)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
#ifdef SHIPDAMAGE
|
|
|
|
|
i = ((sh->size * DAMAGE_SCALE - sh->damage) / DAMAGE_SCALE)
|
|
|
|
|
* sh->type->cargo / sh->size;
|
|
|
|
|
i += ((sh->size * DAMAGE_SCALE - sh->damage) % DAMAGE_SCALE)
|
|
|
|
|
* sh->type->cargo / (sh->size*DAMAGE_SCALE);
|
|
|
|
|
#else
|
|
|
|
|
i = sh->type->cargo;
|
|
|
|
|
#endif
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
getshipweight(const ship * sh, int *sweight, int *scabins)
|
|
|
|
|
{
|
|
|
|
|
unit * u;
|
|
|
|
|
*sweight = 0;
|
|
|
|
|
*scabins = 0;
|
|
|
|
|
for (u = sh->region->units; u; u = u->next)
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
*sweight += weight(u);
|
|
|
|
|
*scabins += u->number;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-25 10:37:55 +01:00
|
|
|
|
unit *
|
2004-06-21 18:45:27 +02:00
|
|
|
|
shipowner(const ship * sh)
|
2001-01-25 10:37:55 +01:00
|
|
|
|
{
|
|
|
|
|
unit *u;
|
|
|
|
|
unit *first = NULL;
|
|
|
|
|
|
2004-06-21 18:45:27 +02:00
|
|
|
|
const region * r = sh->region;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
|
2004-06-21 18:45:27 +02:00
|
|
|
|
/* Pr<50>fen ob Eigent<6E>mer am leben. */
|
2001-01-25 10:37:55 +01:00
|
|
|
|
for (u = r->units; u; u = u->next) {
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
if (!first && u->number > 0)
|
|
|
|
|
first = u;
|
2003-07-29 11:48:03 +02:00
|
|
|
|
if (fval(u, UFL_OWNER) && u->number > 0)
|
2001-01-25 10:37:55 +01:00
|
|
|
|
return u;
|
|
|
|
|
if (u->number == 0)
|
2003-07-29 11:48:03 +02:00
|
|
|
|
freset(u, UFL_OWNER);
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Eigent<6E>mer tot oder kein Eigent<6E>mer vorhanden. Erste lebende Einheit
|
|
|
|
|
* nehmen. */
|
|
|
|
|
|
|
|
|
|
if (first)
|
2003-07-29 11:48:03 +02:00
|
|
|
|
fset(first, UFL_OWNER);
|
2001-01-25 10:37:55 +01:00
|
|
|
|
return first;
|
|
|
|
|
}
|
2002-01-31 23:18:00 +01:00
|
|
|
|
|
2008-04-26 16:55:22 +02:00
|
|
|
|
void
|
|
|
|
|
write_ship_reference(const struct ship * sh, struct storage * store)
|
|
|
|
|
{
|
2008-05-25 01:26:44 +02:00
|
|
|
|
store->w_id(store, (sh && sh->region)?sh->no:0);
|
2008-04-26 16:55:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2002-01-31 23:18:00 +01:00
|
|
|
|
void
|
|
|
|
|
register_ships(void)
|
|
|
|
|
{
|
2002-02-03 09:31:39 +01:00
|
|
|
|
}
|
|
|
|
|
|