2010-08-08 10:06:34 +02:00
|
|
|
|
/*
|
|
|
|
|
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 "ship.h"
|
|
|
|
|
|
|
|
|
|
/* kernel includes */
|
|
|
|
|
#include "build.h"
|
|
|
|
|
#include "unit.h"
|
|
|
|
|
#include "item.h"
|
|
|
|
|
#include "race.h"
|
|
|
|
|
#include "region.h"
|
|
|
|
|
#include "skill.h"
|
|
|
|
|
|
|
|
|
|
/* util includes */
|
|
|
|
|
#include <util/attrib.h>
|
|
|
|
|
#include <util/base36.h>
|
2012-05-18 18:19:46 +02:00
|
|
|
|
#include <util/bsdstring.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#include <util/event.h>
|
|
|
|
|
#include <util/language.h>
|
|
|
|
|
#include <util/lists.h>
|
|
|
|
|
#include <util/umlaut.h>
|
2012-05-31 04:55:17 +02:00
|
|
|
|
#include <quicklist.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#include <util/xml.h>
|
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
|
#include <storage.h>
|
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
|
/* libc includes */
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2011-03-07 06:11:17 +01:00
|
|
|
|
quicklist *shiptypes = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static local_names *snames;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const ship_type *findshiptype(const char *name, const struct locale *lang)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
local_names *sn = snames;
|
2011-03-07 06:11:17 +01:00
|
|
|
|
variant var;
|
|
|
|
|
|
|
|
|
|
while (sn) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (sn->lang == lang)
|
|
|
|
|
break;
|
|
|
|
|
sn = sn->next;
|
2011-03-07 06:11:17 +01:00
|
|
|
|
}
|
|
|
|
|
if (!sn) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
quicklist *ql;
|
2011-03-07 06:11:17 +01:00
|
|
|
|
int qi;
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
sn = (local_names *) calloc(sizeof(local_names), 1);
|
2011-03-07 06:11:17 +01:00
|
|
|
|
sn->next = snames;
|
|
|
|
|
sn->lang = lang;
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (qi = 0, ql = shiptypes; ql; ql_advance(&ql, &qi, 1)) {
|
|
|
|
|
ship_type *stype = (ship_type *) ql_get(ql, qi);
|
2011-03-09 00:25:51 +01:00
|
|
|
|
variant var2;
|
2014-06-13 07:14:07 +02:00
|
|
|
|
const char *n = locale_string(lang, stype->_name);
|
2011-03-09 00:25:51 +01:00
|
|
|
|
var2.v = (void *)stype;
|
|
|
|
|
addtoken(&sn->names, n, var2);
|
2011-03-07 06:11:17 +01:00
|
|
|
|
}
|
|
|
|
|
snames = sn;
|
|
|
|
|
}
|
2012-05-16 00:04:23 +02:00
|
|
|
|
if (findtoken(sn->names, name, &var) == E_TOK_NOMATCH)
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return NULL;
|
|
|
|
|
return (const ship_type *)var.v;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-13 07:14:07 +02:00
|
|
|
|
static ship_type *st_find_i(const char *name)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
quicklist *ql;
|
2011-03-07 06:11:17 +01:00
|
|
|
|
int qi;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
|
|
|
|
|
for (qi = 0, ql = shiptypes; ql; ql_advance(&ql, &qi, 1)) {
|
|
|
|
|
ship_type *stype = (ship_type *) ql_get(ql, qi);
|
2014-06-13 07:14:07 +02:00
|
|
|
|
if (strcmp(stype->_name, name) == 0) {
|
2011-03-07 06:11:17 +01:00
|
|
|
|
return stype;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-13 07:14:07 +02:00
|
|
|
|
const ship_type *st_find(const char *name) {
|
|
|
|
|
return st_find_i(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ship_type *st_get_or_create(const char * name) {
|
|
|
|
|
ship_type * st = st_find_i(name);
|
|
|
|
|
if (!st) {
|
|
|
|
|
st = (ship_type *)calloc(sizeof(ship_type), 1);
|
|
|
|
|
st->_name = _strdup(name);
|
|
|
|
|
ql_push(&shiptypes, (void *)st);
|
|
|
|
|
}
|
|
|
|
|
return st;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-12 04:20:20 +02:00
|
|
|
|
#define MAXSHIPHASH 7919
|
|
|
|
|
ship *shiphash[MAXSHIPHASH];
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void shash(ship * s)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2014-04-12 04:20:20 +02:00
|
|
|
|
ship *old = shiphash[s->no % MAXSHIPHASH];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2014-04-12 04:20:20 +02:00
|
|
|
|
shiphash[s->no % MAXSHIPHASH] = s;
|
2011-03-07 06:11:17 +01:00
|
|
|
|
s->nexthash = old;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void sunhash(ship * s)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 06:11:17 +01:00
|
|
|
|
ship **show;
|
|
|
|
|
|
2014-04-12 04:20:20 +02:00
|
|
|
|
for (show = &shiphash[s->no % MAXSHIPHASH]; *show; show = &(*show)->nexthash) {
|
2011-03-07 06:11:17 +01:00
|
|
|
|
if ((*show)->no == s->no)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (*show) {
|
|
|
|
|
assert(*show == s);
|
|
|
|
|
*show = (*show)->nexthash;
|
|
|
|
|
s->nexthash = 0;
|
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static ship *sfindhash(int i)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 06:11:17 +01:00
|
|
|
|
ship *old;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2014-04-12 04:20:20 +02:00
|
|
|
|
for (old = shiphash[i % MAXSHIPHASH]; old; old = old->nexthash)
|
2011-03-07 06:11:17 +01:00
|
|
|
|
if (old->no == i)
|
|
|
|
|
return old;
|
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct ship *findship(int i)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 06:11:17 +01:00
|
|
|
|
return sfindhash(i);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct ship *findshipr(const region * r, int n)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
ship *sh;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
for (sh = r->ships; sh; sh = sh->next) {
|
|
|
|
|
if (sh->no == n) {
|
|
|
|
|
assert(sh->region == r);
|
|
|
|
|
return sh;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void damage_ship(ship * sh, double percent)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
double damage =
|
|
|
|
|
DAMAGE_SCALE * sh->type->damage * percent * sh->size + sh->damage;
|
2011-03-07 06:11:17 +01:00
|
|
|
|
sh->damage = (int)damage;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Alte Schiffstypen: */
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static ship *deleted_ships;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2012-05-17 09:13:30 +02:00
|
|
|
|
ship *new_ship(const ship_type * stype, region * r, const struct locale *lang)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2012-05-18 18:19:46 +02:00
|
|
|
|
static char buffer[32];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
ship *sh = (ship *) calloc(1, sizeof(ship));
|
2012-05-18 18:19:46 +02:00
|
|
|
|
const char *sname = 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
assert(stype);
|
|
|
|
|
sh->no = newcontainerid();
|
|
|
|
|
sh->coast = NODIRECTION;
|
|
|
|
|
sh->type = stype;
|
|
|
|
|
sh->region = r;
|
|
|
|
|
|
2014-06-13 07:14:07 +02:00
|
|
|
|
sname = LOC(lang, stype->_name);
|
2012-05-18 18:19:46 +02:00
|
|
|
|
if (!sname) {
|
|
|
|
|
sname = LOC(lang, parameters[P_SHIP]);
|
|
|
|
|
if (!sname) {
|
|
|
|
|
sname = parameters[P_SHIP];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert(sname);
|
|
|
|
|
slprintf(buffer, sizeof(buffer), "%s %s", sname, shipid(sh));
|
2013-12-29 14:38:58 +01:00
|
|
|
|
sh->name = _strdup(buffer);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
shash(sh);
|
2012-07-02 03:00:31 +02:00
|
|
|
|
if (r) {
|
|
|
|
|
addlist(&r->ships, sh);
|
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return sh;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void remove_ship(ship ** slist, ship * sh)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *r = sh->region;
|
|
|
|
|
unit *u = r->units;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
handle_event(sh->attribs, "destroy", sh);
|
|
|
|
|
while (u) {
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
leave_ship(u);
|
|
|
|
|
}
|
|
|
|
|
u = u->next;
|
|
|
|
|
}
|
|
|
|
|
sunhash(sh);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (*slist && *slist != sh)
|
|
|
|
|
slist = &(*slist)->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
assert(*slist);
|
|
|
|
|
*slist = sh->next;
|
|
|
|
|
sh->next = deleted_ships;
|
|
|
|
|
deleted_ships = sh;
|
|
|
|
|
sh->region = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void free_ship(ship * s)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (s->attribs)
|
|
|
|
|
a_remove(&s->attribs, s->attribs);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
free(s->name);
|
|
|
|
|
free(s->display);
|
|
|
|
|
free(s);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void free_ships(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
while (deleted_ships) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
ship *s = deleted_ships;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
deleted_ships = s->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *write_shipname(const ship * sh, char *ibuf, size_t size)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2012-05-29 21:17:25 +02:00
|
|
|
|
slprintf(ibuf, size, "%s (%s)", sh->name, itoa36(sh->no));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return ibuf;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *shipname(const ship * sh)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
typedef char name[OBJECTIDSIZE + 1];
|
|
|
|
|
static name idbuf[8];
|
|
|
|
|
static int nextbuf = 0;
|
|
|
|
|
char *ibuf = idbuf[(++nextbuf) % 8];
|
|
|
|
|
return write_shipname(sh, ibuf, sizeof(name));
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int shipcapacity(const ship * sh)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
int i = sh->type->cargo;
|
|
|
|
|
|
|
|
|
|
/* sonst ist construction:: size nicht ship_type::maxsize */
|
2011-03-07 08:02:35 +01:00
|
|
|
|
assert(!sh->type->construction
|
|
|
|
|
|| sh->type->construction->improvement == NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (sh->type->construction && sh->size != sh->type->construction->maxsize)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
#ifdef SHIPDAMAGE
|
|
|
|
|
if (sh->damage) {
|
|
|
|
|
i = (int)ceil(i * (1.0 - sh->damage / sh->size / (double)DAMAGE_SCALE));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void getshipweight(const ship * sh, int *sweight, int *scabins)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
unit *u;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
*sweight = 0;
|
|
|
|
|
*scabins = 0;
|
|
|
|
|
|
|
|
|
|
for (u = sh->region->units; u; u = u->next) {
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
*sweight += weight(u);
|
|
|
|
|
if (sh->type->cabins) {
|
2013-05-02 03:42:20 +02:00
|
|
|
|
int pweight = u->number * u_race(u)->weight;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
/* weight goes into number of cabins, not cargo */
|
|
|
|
|
*scabins += pweight;
|
|
|
|
|
*sweight -= pweight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 19:43:05 +02:00
|
|
|
|
void ship_set_owner(unit * u) {
|
|
|
|
|
assert(u && u->ship);
|
|
|
|
|
u->ship->_owner = u;
|
2012-05-17 21:23:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-05-19 08:20:38 +02:00
|
|
|
|
static unit * ship_owner_ex(const ship * sh, const struct faction * last_owner)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2012-05-19 08:20:38 +02:00
|
|
|
|
unit *u, *heir = 0;
|
|
|
|
|
|
|
|
|
|
/* Eigent<6E>mer tot oder kein Eigent<6E>mer vorhanden. Erste lebende Einheit
|
|
|
|
|
* nehmen. */
|
|
|
|
|
for (u = sh->region->units; u; u = u->next) {
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
if (u->number > 0) {
|
|
|
|
|
if (heir && last_owner && heir->faction!=last_owner && u->faction==last_owner) {
|
|
|
|
|
heir = u;
|
|
|
|
|
break; /* we found someone from the same faction who is not dead. let's take this guy */
|
|
|
|
|
}
|
|
|
|
|
else if (!heir) {
|
|
|
|
|
heir = u; /* you'll do in an emergency */
|
2012-05-17 10:14:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-07 06:11:17 +01:00
|
|
|
|
}
|
2012-05-19 08:20:38 +02:00
|
|
|
|
return heir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ship_update_owner(ship * sh) {
|
|
|
|
|
unit * owner = sh->_owner;
|
|
|
|
|
sh->_owner = ship_owner_ex(sh, owner?owner->faction:0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unit *ship_owner(const ship * sh)
|
|
|
|
|
{
|
|
|
|
|
unit *owner = sh->_owner;
|
|
|
|
|
if (!owner || (owner->ship!=sh || owner->number<=0)) {
|
|
|
|
|
unit * heir = ship_owner_ex(sh, owner?owner->faction:0);
|
|
|
|
|
return (heir && heir->number>0) ? heir : 0;
|
|
|
|
|
}
|
2012-05-17 10:14:43 +02:00
|
|
|
|
return owner;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void write_ship_reference(const struct ship *sh, struct storage *store)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2013-12-31 10:06:28 +01:00
|
|
|
|
WRITE_INT(store, (sh && sh->region) ? sh->no : 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void ship_setname(ship * self, const char *name)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
free(self->name);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (name)
|
2013-12-29 14:38:58 +01:00
|
|
|
|
self->name = _strdup(name);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
else
|
|
|
|
|
self->name = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *ship_getname(const ship * self)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
return self->name;
|
|
|
|
|
}
|