2010-08-08 10:06:34 +02:00
|
|
|
|
/*
|
2015-01-30 22:10:29 +01:00
|
|
|
|
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
|
2014-12-25 18:16:24 +01:00
|
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
2017-08-21 20:18:19 +02:00
|
|
|
|
#include <attributes/movement.h>
|
|
|
|
|
#include <spells/shipcurse.h>
|
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
|
/* kernel includes */
|
|
|
|
|
#include "build.h"
|
2014-12-25 18:16:24 +01:00
|
|
|
|
#include "curse.h"
|
|
|
|
|
#include "faction.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#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>
|
|
|
|
|
#include <util/event.h>
|
|
|
|
|
#include <util/language.h>
|
|
|
|
|
#include <util/lists.h>
|
2017-05-06 15:05:36 +02:00
|
|
|
|
#include <util/log.h>
|
2017-12-28 18:29:40 +01:00
|
|
|
|
#include <util/strings.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#include <util/umlaut.h>
|
|
|
|
|
#include <util/xml.h>
|
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
|
#include <storage.h>
|
2017-05-06 15:05:36 +02:00
|
|
|
|
#include <selist.h>
|
|
|
|
|
#include <critbit.h>
|
2013-12-31 10:06:28 +01:00
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
|
/* libc includes */
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2017-05-06 15:25:13 +02:00
|
|
|
|
selist *shiptypes = NULL; /* do not use this list for searching*/
|
|
|
|
|
static critbit_tree cb_shiptypes; /* use this trie instead */
|
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
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
local_names *sn = snames;
|
|
|
|
|
variant var;
|
2011-03-07 06:11:17 +01:00
|
|
|
|
|
2017-05-06 15:25:13 +02:00
|
|
|
|
while (sn && sn->lang != lang) {
|
2014-12-25 18:16:24 +01:00
|
|
|
|
sn = sn->next;
|
2011-03-07 06:11:17 +01:00
|
|
|
|
}
|
2014-12-25 18:16:24 +01:00
|
|
|
|
if (!sn) {
|
2017-01-26 17:41:21 +01:00
|
|
|
|
selist *ql;
|
2014-12-25 18:16:24 +01:00
|
|
|
|
int qi;
|
|
|
|
|
|
|
|
|
|
sn = (local_names *)calloc(sizeof(local_names), 1);
|
|
|
|
|
sn->next = snames;
|
|
|
|
|
sn->lang = lang;
|
|
|
|
|
|
2017-01-26 17:41:21 +01:00
|
|
|
|
for (qi = 0, ql = shiptypes; ql; selist_advance(&ql, &qi, 1)) {
|
|
|
|
|
ship_type *stype = (ship_type *)selist_get(ql, qi);
|
2014-12-25 18:16:24 +01:00
|
|
|
|
variant var2;
|
2015-01-08 20:55:29 +01:00
|
|
|
|
const char *n = LOC(lang, stype->_name);
|
2014-12-25 18:16:24 +01:00
|
|
|
|
var2.v = (void *)stype;
|
2016-09-06 20:57:07 +02:00
|
|
|
|
addtoken((struct tnode **)&sn->names, n, var2);
|
2014-12-25 18:16:24 +01:00
|
|
|
|
}
|
|
|
|
|
snames = sn;
|
|
|
|
|
}
|
|
|
|
|
if (findtoken(sn->names, name, &var) == E_TOK_NOMATCH)
|
|
|
|
|
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
|
|
|
|
{
|
2017-05-06 15:05:36 +02:00
|
|
|
|
const char *match;
|
|
|
|
|
ship_type *st = NULL;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
|
2017-05-06 15:05:36 +02:00
|
|
|
|
match = cb_find_str(&cb_shiptypes, name);
|
|
|
|
|
if (match) {
|
|
|
|
|
cb_get_kv(match, &st, sizeof(st));
|
|
|
|
|
}
|
|
|
|
|
return st;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-13 07:14:07 +02:00
|
|
|
|
const ship_type *st_find(const char *name) {
|
2017-05-22 21:35:25 +02:00
|
|
|
|
ship_type *st = st_find_i(name);
|
|
|
|
|
if (!st) {
|
|
|
|
|
log_warning("st_find: could not find ship '%s'\n", name);
|
|
|
|
|
}
|
|
|
|
|
return st;
|
2014-06-13 07:14:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-06 15:39:09 +02:00
|
|
|
|
static void st_register(ship_type *stype) {
|
|
|
|
|
size_t len;
|
|
|
|
|
char data[64];
|
|
|
|
|
|
|
|
|
|
selist_push(&shiptypes, (void *)stype);
|
|
|
|
|
|
|
|
|
|
len = cb_new_kv(stype->_name, strlen(stype->_name), &stype, sizeof(stype), data);
|
|
|
|
|
assert(len <= sizeof(data));
|
|
|
|
|
cb_insert(&cb_shiptypes, data, len);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-13 07:14:07 +02:00
|
|
|
|
ship_type *st_get_or_create(const char * name) {
|
|
|
|
|
ship_type * st = st_find_i(name);
|
2017-05-06 15:25:13 +02:00
|
|
|
|
assert(!snames);
|
2014-06-13 07:14:07 +02:00
|
|
|
|
if (!st) {
|
|
|
|
|
st = (ship_type *)calloc(sizeof(ship_type), 1);
|
2017-12-28 18:29:40 +01:00
|
|
|
|
st->_name = str_strdup(name);
|
2015-02-03 17:10:29 +01:00
|
|
|
|
st->storm = 1.0;
|
2017-05-06 15:39:09 +02:00
|
|
|
|
st_register(st);
|
2014-06-13 07:14:07 +02:00
|
|
|
|
}
|
|
|
|
|
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-12-25 18:16:24 +01:00
|
|
|
|
ship *old = shiphash[s->no % MAXSHIPHASH];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2014-12-25 18:16:24 +01:00
|
|
|
|
shiphash[s->no % MAXSHIPHASH] = s;
|
|
|
|
|
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
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
ship **show;
|
|
|
|
|
|
|
|
|
|
for (show = &shiphash[s->no % MAXSHIPHASH]; *show; show = &(*show)->nexthash) {
|
|
|
|
|
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
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
ship *old;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2014-12-25 18:16:24 +01:00
|
|
|
|
for (old = shiphash[i % MAXSHIPHASH]; old; old = old->nexthash)
|
|
|
|
|
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
|
|
|
|
{
|
2014-12-25 18:16:24 +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
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
ship *sh;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2014-12-25 18:16:24 +01:00
|
|
|
|
for (sh = r->ships; sh; sh = sh->next) {
|
|
|
|
|
if (sh->no == n) {
|
|
|
|
|
assert(sh->region == r);
|
|
|
|
|
return sh;
|
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2014-12-25 18:16:24 +01:00
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void damage_ship(ship * sh, double percent)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
double damage =
|
2015-11-15 02:20:55 +01:00
|
|
|
|
DAMAGE_SCALE * sh->type->damage * percent * sh->size + sh->damage + .000001;
|
2014-12-25 18:16:24 +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
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
static char buffer[32];
|
|
|
|
|
ship *sh = (ship *)calloc(1, sizeof(ship));
|
|
|
|
|
const char *sname = 0;
|
|
|
|
|
|
|
|
|
|
assert(stype);
|
|
|
|
|
sh->no = newcontainerid();
|
|
|
|
|
sh->coast = NODIRECTION;
|
|
|
|
|
sh->type = stype;
|
|
|
|
|
sh->region = r;
|
|
|
|
|
|
2015-08-07 11:50:49 +02:00
|
|
|
|
if (lang) {
|
|
|
|
|
sname = LOC(lang, stype->_name);
|
2014-12-25 18:16:24 +01:00
|
|
|
|
if (!sname) {
|
2015-08-07 11:50:49 +02:00
|
|
|
|
sname = LOC(lang, parameters[P_SHIP]);
|
2014-12-25 18:16:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-07 11:50:49 +02:00
|
|
|
|
if (!sname) {
|
|
|
|
|
sname = parameters[P_SHIP];
|
|
|
|
|
}
|
2014-12-25 18:16:24 +01:00
|
|
|
|
assert(sname);
|
2017-12-11 18:35:30 +01:00
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s %s", sname, itoa36(sh->no));
|
2017-12-28 18:29:40 +01:00
|
|
|
|
sh->name = str_strdup(buffer);
|
2014-12-25 18:16:24 +01:00
|
|
|
|
shash(sh);
|
|
|
|
|
if (r) {
|
|
|
|
|
addlist(&r->ships, sh);
|
2012-05-18 18:19:46 +02:00
|
|
|
|
}
|
2014-12-25 18:16:24 +01:00
|
|
|
|
return sh;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void remove_ship(ship ** slist, ship * sh)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
region *r = sh->region;
|
|
|
|
|
unit *u = r->units;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2014-12-25 18:16:24 +01:00
|
|
|
|
handle_event(sh->attribs, "destroy", sh);
|
|
|
|
|
while (u) {
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
leave_ship(u);
|
|
|
|
|
}
|
|
|
|
|
u = u->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2014-12-25 18:16:24 +01:00
|
|
|
|
sunhash(sh);
|
|
|
|
|
while (*slist && *slist != sh)
|
|
|
|
|
slist = &(*slist)->next;
|
|
|
|
|
assert(*slist);
|
|
|
|
|
*slist = sh->next;
|
|
|
|
|
sh->next = deleted_ships;
|
|
|
|
|
deleted_ships = sh;
|
|
|
|
|
sh->region = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void free_ship(ship * s)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2017-05-06 15:53:21 +02:00
|
|
|
|
while (s->attribs) {
|
2014-12-25 18:16:24 +01:00
|
|
|
|
a_remove(&s->attribs, s->attribs);
|
2017-05-06 15:53:21 +02:00
|
|
|
|
}
|
2014-12-25 18:16:24 +01:00
|
|
|
|
free(s->name);
|
|
|
|
|
free(s->display);
|
|
|
|
|
free(s);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-30 23:34:24 +01:00
|
|
|
|
static void free_shiptype(void *ptr) {
|
|
|
|
|
ship_type *stype = (ship_type *)ptr;
|
2014-12-30 01:44:28 +01:00
|
|
|
|
free(stype->_name);
|
2014-12-31 01:00:10 +01:00
|
|
|
|
free(stype->coasts);
|
2015-10-13 22:56:16 +02:00
|
|
|
|
free_construction(stype->construction);
|
2014-12-30 01:44:28 +01:00
|
|
|
|
free(stype);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-13 17:04:06 +02:00
|
|
|
|
void free_shiptypes(void) {
|
2017-05-06 15:05:36 +02:00
|
|
|
|
cb_clear(&cb_shiptypes);
|
2017-01-26 17:41:21 +01:00
|
|
|
|
selist_foreach(shiptypes, free_shiptype);
|
|
|
|
|
selist_free(shiptypes);
|
2014-06-13 17:04:06 +02:00
|
|
|
|
shiptypes = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void free_ships(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
while (deleted_ships) {
|
|
|
|
|
ship *s = deleted_ships;
|
|
|
|
|
deleted_ships = s->next;
|
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2017-12-11 18:35:30 +01:00
|
|
|
|
snprintf(ibuf, size, "%s (%s)", sh->name, itoa36(sh->no));
|
2014-12-25 18:16:24 +01:00
|
|
|
|
return ibuf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int ShipSpeedBonus(const unit * u)
|
|
|
|
|
{
|
2017-12-27 21:54:09 +01:00
|
|
|
|
const ship * sh = u->ship;
|
|
|
|
|
static int config;
|
|
|
|
|
static int bonus;
|
|
|
|
|
|
|
|
|
|
if (config_changed(&config)) {
|
|
|
|
|
bonus = config_get_int("movement.shipspeed.skillbonus", 0);
|
|
|
|
|
}
|
|
|
|
|
if (bonus > 0) {
|
2015-08-27 16:16:55 +02:00
|
|
|
|
int skl = effskill(u, SK_SAILING, 0);
|
2014-12-25 18:16:24 +01:00
|
|
|
|
int minsk = (sh->type->cptskill + 1) / 2;
|
2017-12-27 21:54:09 +01:00
|
|
|
|
return (skl - minsk) / bonus;
|
|
|
|
|
}
|
|
|
|
|
else if (sh->type->flags & SFL_SPEEDY) {
|
|
|
|
|
int base = 3;
|
|
|
|
|
int speed = 0;
|
|
|
|
|
int minsk = sh->type->cptskill * base;
|
|
|
|
|
int skl = effskill(u, SK_SAILING, 0);
|
|
|
|
|
while (skl >= minsk) {
|
|
|
|
|
++speed;
|
|
|
|
|
minsk *= base;
|
|
|
|
|
}
|
|
|
|
|
return speed;
|
2014-12-25 18:16:24 +01:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-07 09:03:19 +02:00
|
|
|
|
int crew_skill(const ship *sh) {
|
|
|
|
|
int n = 0;
|
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
|
|
for (u = sh->region->units; u; u = u->next) {
|
|
|
|
|
if (u->ship == sh) {
|
2015-08-27 16:16:55 +02:00
|
|
|
|
n += effskill(u, SK_SAILING, 0) * u->number;
|
2015-08-07 09:03:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-25 18:16:24 +01:00
|
|
|
|
int shipspeed(const ship * sh, const unit * u)
|
|
|
|
|
{
|
|
|
|
|
attrib *a;
|
|
|
|
|
struct curse *c;
|
2016-11-19 20:57:10 +01:00
|
|
|
|
int k, bonus;
|
2014-12-25 18:16:24 +01:00
|
|
|
|
|
2015-08-07 09:03:19 +02:00
|
|
|
|
assert(sh);
|
|
|
|
|
if (!u) u = ship_owner(sh);
|
|
|
|
|
if (!u) return 0;
|
|
|
|
|
assert(u->ship == sh);
|
|
|
|
|
assert(u == ship_owner(sh));
|
|
|
|
|
assert(sh->type->construction);
|
|
|
|
|
assert(sh->type->construction->improvement == NULL); /* sonst ist construction::size nicht ship_type::maxsize */
|
|
|
|
|
|
2016-11-19 20:57:10 +01:00
|
|
|
|
k = sh->type->range;
|
2014-12-25 18:16:24 +01:00
|
|
|
|
if (sh->size != sh->type->construction->maxsize)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2016-08-29 20:46:19 +02:00
|
|
|
|
if (sh->attribs) {
|
2017-08-20 19:07:52 +02:00
|
|
|
|
if (curse_active(get_curse(sh->attribs, &ct_stormwind))) {
|
2016-08-29 20:46:19 +02:00
|
|
|
|
k *= 2;
|
|
|
|
|
}
|
2017-08-21 20:18:19 +02:00
|
|
|
|
if (curse_active(get_curse(sh->attribs, &ct_nodrift))) {
|
2016-08-29 20:46:19 +02:00
|
|
|
|
k += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-25 18:16:24 +01:00
|
|
|
|
if (u->faction->race == u_race(u)) {
|
|
|
|
|
/* race bonus for this faction? */
|
|
|
|
|
if (fval(u_race(u), RCF_SHIPSPEED)) {
|
|
|
|
|
k += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-18 08:34:13 +02:00
|
|
|
|
bonus = ShipSpeedBonus(u);
|
2016-08-29 20:46:19 +02:00
|
|
|
|
if (bonus > 0 && sh->type->range_max > sh->type->range) {
|
2015-04-18 11:41:50 +02:00
|
|
|
|
int crew = crew_skill(sh);
|
|
|
|
|
int crew_bonus = (crew / sh->type->sumskill / 2) - 1;
|
2015-07-23 16:10:13 +02:00
|
|
|
|
if (crew_bonus > 0) {
|
2017-01-10 16:31:05 +01:00
|
|
|
|
bonus = MIN(bonus, crew_bonus);
|
|
|
|
|
bonus = MIN(bonus, sh->type->range_max - sh->type->range);
|
2015-07-23 16:10:13 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
bonus = 0;
|
|
|
|
|
}
|
2015-04-18 08:34:13 +02:00
|
|
|
|
}
|
|
|
|
|
k += bonus;
|
2014-12-25 18:16:24 +01:00
|
|
|
|
|
|
|
|
|
a = a_find(sh->attribs, &at_speedup);
|
|
|
|
|
while (a != NULL && a->type == &at_speedup) {
|
|
|
|
|
k += a->data.sa[0];
|
|
|
|
|
a = a->next;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-18 18:42:59 +02:00
|
|
|
|
c = get_curse(sh->attribs, &ct_shipspeedup);
|
2014-12-25 18:16:24 +01:00
|
|
|
|
while (c) {
|
2015-08-07 12:03:33 +02:00
|
|
|
|
k += curse_geteffect_int(c);
|
2014-12-25 18:16:24 +01:00
|
|
|
|
c = c->nexthash;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 20:46:19 +02:00
|
|
|
|
if (sh->damage > 0) {
|
2015-08-07 12:03:33 +02:00
|
|
|
|
int size = sh->size * DAMAGE_SCALE;
|
|
|
|
|
k *= (size - sh->damage);
|
|
|
|
|
k = (k + size - 1) / size;
|
|
|
|
|
}
|
|
|
|
|
return k;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *shipname(const ship * sh)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
typedef char name[OBJECTIDSIZE + 1];
|
|
|
|
|
static name idbuf[8];
|
|
|
|
|
static int nextbuf = 0;
|
|
|
|
|
char *ibuf = idbuf[(++nextbuf) % 8];
|
2017-12-08 21:08:11 +01:00
|
|
|
|
return write_shipname(sh, ibuf, sizeof(idbuf[0]));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int shipcapacity(const ship * sh)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
int i = sh->type->cargo;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2014-12-25 18:16:24 +01:00
|
|
|
|
/* sonst ist construction:: size nicht ship_type::maxsize */
|
|
|
|
|
assert(!sh->type->construction
|
|
|
|
|
|| sh->type->construction->improvement == NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2014-12-25 18:16:24 +01:00
|
|
|
|
if (sh->type->construction && sh->size != sh->type->construction->maxsize)
|
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2014-12-25 18:16:24 +01:00
|
|
|
|
if (sh->damage) {
|
|
|
|
|
i = (int)ceil(i * (1.0 - sh->damage / sh->size / (double)DAMAGE_SCALE));
|
|
|
|
|
}
|
|
|
|
|
return i;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
|
|
*sweight = 0;
|
|
|
|
|
*scabins = 0;
|
|
|
|
|
|
|
|
|
|
for (u = sh->region->units; u; u = u->next) {
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
*sweight += weight(u);
|
|
|
|
|
if (sh->type->cabins) {
|
|
|
|
|
int pweight = u->number * u_race(u)->weight;
|
|
|
|
|
/* weight goes into number of cabins, not cargo */
|
|
|
|
|
*scabins += pweight;
|
|
|
|
|
*sweight -= pweight;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 19:43:05 +02:00
|
|
|
|
void ship_set_owner(unit * u) {
|
2014-12-25 18:16:24 +01:00
|
|
|
|
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
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
unit *u, *heir = 0;
|
|
|
|
|
|
2016-11-19 20:57:10 +01:00
|
|
|
|
/* Eigent<6E>mer tot oder kein Eigent<6E>mer vorhanden. Erste lebende Einheit
|
2014-12-25 18:16:24 +01:00
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-25 18:16:24 +01:00
|
|
|
|
return heir;
|
2012-05-19 08:20:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ship_update_owner(ship * sh) {
|
2014-12-25 18:16:24 +01:00
|
|
|
|
unit * owner = sh->_owner;
|
|
|
|
|
sh->_owner = ship_owner_ex(sh, owner ? owner->faction : 0);
|
2012-05-19 08:20:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unit *ship_owner(const ship * sh)
|
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2014-12-25 18:16:24 +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
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
free(self->name);
|
2017-12-28 18:29:40 +01:00
|
|
|
|
self->name = name ? str_strdup(name) : 0;
|
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
|
|
|
|
{
|
2014-12-25 18:16:24 +01:00
|
|
|
|
return self->name;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2015-11-17 15:47:43 +01:00
|
|
|
|
|
|
|
|
|
int ship_damage_percent(const ship *ship) {
|
|
|
|
|
return (ship->damage * 100 + DAMAGE_SCALE - 1) / (ship->size * DAMAGE_SCALE);
|
|
|
|
|
}
|