2019-01-24 16:34:07 +01:00
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
# include <platform.h>
|
|
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#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 */
|
2018-09-29 11:37:17 +02:00
|
|
|
|
#include <kernel/attrib.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#include <util/base36.h>
|
2018-09-29 11:37:17 +02:00
|
|
|
|
#include <kernel/event.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#include <util/language.h>
|
|
|
|
|
#include <util/lists.h>
|
2017-05-06 15:05:36 +02:00
|
|
|
|
#include <util/log.h>
|
2018-09-29 19:32:39 +02:00
|
|
|
|
#include <util/param.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>
|
|
|
|
|
|
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;
|
|
|
|
|
|
2018-12-15 19:38:40 +01:00
|
|
|
|
sn = (local_names *)calloc(1, sizeof(local_names));
|
|
|
|
|
if (!sn) abort();
|
2014-12-25 18:16:24 +01:00
|
|
|
|
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) {
|
2018-12-15 19:38:40 +01:00
|
|
|
|
st = (ship_type *)calloc(1, sizeof(ship_type));
|
|
|
|
|
if (!st) abort();
|
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;
|
2018-05-03 22:44:01 +02:00
|
|
|
|
st->tac_bonus = 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
|
|
|
|
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;
|
|
|
|
|
|
2018-12-15 19:38:40 +01:00
|
|
|
|
if (!sh) abort();
|
2014-12-25 18:16:24 +01:00
|
|
|
|
assert(stype);
|
|
|
|
|
sh->no = newcontainerid();
|
|
|
|
|
sh->coast = NODIRECTION;
|
|
|
|
|
sh->type = stype;
|
|
|
|
|
sh->region = r;
|
2019-10-06 18:11:10 +02:00
|
|
|
|
sh->number = 1;
|
2014-12-25 18:16:24 +01:00
|
|
|
|
|
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);
|
2018-05-01 15:32:06 +02:00
|
|
|
|
if (stype->construction) {
|
|
|
|
|
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) {
|
2019-02-02 20:36:23 +01:00
|
|
|
|
int skl = effskill(u, SK_SAILING, NULL);
|
2019-10-06 18:11:10 +02:00
|
|
|
|
int minsk = (ship_captain_minskill(sh) + 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;
|
2019-10-06 18:11:10 +02:00
|
|
|
|
int minsk = ship_captain_minskill(sh) * base;
|
2019-02-02 20:36:23 +01:00
|
|
|
|
int skl = effskill(u, SK_SAILING, NULL);
|
2017-12-27 21:54:09 +01:00
|
|
|
|
while (skl >= minsk) {
|
|
|
|
|
++speed;
|
|
|
|
|
minsk *= base;
|
|
|
|
|
}
|
|
|
|
|
return speed;
|
2014-12-25 18:16:24 +01:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-06 18:11:10 +02:00
|
|
|
|
int ship_captain_minskill(const ship *sh) {
|
|
|
|
|
return sh->type->cptskill;
|
2015-08-07 09:03:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2016-11-19 20:57:10 +01:00
|
|
|
|
k = sh->type->range;
|
2019-10-06 18:11:10 +02:00
|
|
|
|
if (!ship_finished(sh)) {
|
2014-12-25 18:16:24 +01:00
|
|
|
|
return 0;
|
2019-10-06 18:11:10 +02:00
|
|
|
|
}
|
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) {
|
2019-10-06 20:22:19 +02:00
|
|
|
|
int crew = crew_skill(sh);
|
2015-04-18 11:41:50 +02:00
|
|
|
|
int crew_bonus = (crew / sh->type->sumskill / 2) - 1;
|
2015-07-23 16:10:13 +02:00
|
|
|
|
if (crew_bonus > 0) {
|
2019-01-24 16:34:07 +01:00
|
|
|
|
int sbonus = sh->type->range_max - sh->type->range;
|
|
|
|
|
if (bonus > sbonus) bonus = sbonus;
|
|
|
|
|
if (bonus > crew_bonus) bonus = crew_bonus;
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:04:49 +02:00
|
|
|
|
int ship_maxsize(const ship *sh)
|
|
|
|
|
{
|
|
|
|
|
return sh->number * sh->type->construction->maxsize;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-06 18:11:10 +02:00
|
|
|
|
bool ship_finished(const ship *sh)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2019-10-06 18:11:10 +02:00
|
|
|
|
if (sh->type->construction) {
|
2019-10-20 18:04:49 +02:00
|
|
|
|
return (sh->size >= ship_maxsize(sh));
|
2019-10-06 18:11:10 +02:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2019-10-06 18:11:10 +02:00
|
|
|
|
int enoughsailors(const ship * sh, int crew_skill)
|
|
|
|
|
{
|
|
|
|
|
return crew_skill >= sh->type->sumskill * sh->number;
|
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2019-10-06 20:22:19 +02:00
|
|
|
|
int crew_skill(const ship *sh) {
|
|
|
|
|
int n = 0;
|
2019-10-06 18:11:10 +02:00
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
|
|
for (u = sh->region->units; u; u = u->next) {
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
int es = effskill(u, SK_SAILING, NULL);
|
|
|
|
|
if (es >= sh->type->minskill) {
|
|
|
|
|
n += es * u->number;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-06 20:22:19 +02:00
|
|
|
|
bool ship_crewed(const ship *sh) {
|
2019-10-27 10:56:16 +01:00
|
|
|
|
unit *u, *cap = ship_owner(sh);
|
2019-10-06 20:22:19 +02:00
|
|
|
|
int capskill = -1, sumskill = 0;
|
|
|
|
|
for (u = sh->region->units; u; u = u->next) {
|
|
|
|
|
if (u->ship == sh) {
|
|
|
|
|
int es = effskill(u, SK_SAILING, NULL);
|
2019-10-27 10:56:16 +01:00
|
|
|
|
if (es > 0) {
|
|
|
|
|
if (u == cap && u->number >= sh->number) {
|
2019-10-06 20:22:19 +02:00
|
|
|
|
capskill = es;
|
|
|
|
|
}
|
2019-10-27 10:56:16 +01:00
|
|
|
|
if (es >= sh->type->minskill) {
|
|
|
|
|
sumskill += es * u->number;
|
2019-10-06 20:22:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (capskill >= ship_captain_minskill(sh)) && (sumskill >= sh->type->sumskill * sh->number);
|
2019-10-06 18:11:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-07 21:45:49 +02:00
|
|
|
|
void scale_ship(ship *sh, int n)
|
|
|
|
|
{
|
|
|
|
|
sh->size = sh->size * n / sh->number;
|
|
|
|
|
sh->damage = sh->damage * n / sh->number;
|
|
|
|
|
sh->number = n;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-06 18:11:10 +02:00
|
|
|
|
int ship_capacity(const ship * sh)
|
|
|
|
|
{
|
|
|
|
|
if (ship_finished(sh)) {
|
|
|
|
|
int i = sh->type->cargo * sh->number;
|
|
|
|
|
if (sh->damage) {
|
|
|
|
|
i = (int)ceil(i * (1.0 - sh->damage / sh->size / (double)DAMAGE_SCALE));
|
|
|
|
|
}
|
|
|
|
|
return i;
|
2014-12-25 18:16:24 +01:00
|
|
|
|
}
|
2019-10-06 18:11:10 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ship_cabins(const ship * sh)
|
|
|
|
|
{
|
|
|
|
|
if (ship_finished(sh)) {
|
|
|
|
|
int i = sh->type->cabins * sh->number;
|
|
|
|
|
if (sh->damage) {
|
|
|
|
|
i = (int)ceil(i * (1.0 - sh->damage / sh->size / (double)DAMAGE_SCALE));
|
|
|
|
|
}
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
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;
|
|
|
|
|
|
2019-02-07 20:33:20 +01:00
|
|
|
|
/* Eigentuemer tot oder kein Eigentuemer 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
|
|
|
|
}
|
|
|
|
|
|
2019-10-05 20:31:00 +02:00
|
|
|
|
void ship_setname(ship * sh, const char *name)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2019-10-05 20:31:00 +02:00
|
|
|
|
free(sh->name);
|
|
|
|
|
sh->name = name ? str_strdup(name) : 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-05 20:31:00 +02:00
|
|
|
|
const char *ship_getname(const ship * sh)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2019-10-05 20:31:00 +02:00
|
|
|
|
return sh->name;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2015-11-17 15:47:43 +01:00
|
|
|
|
|
2019-10-05 20:31:00 +02:00
|
|
|
|
int ship_damage_percent(const ship *sh) {
|
2019-10-21 20:04:32 +02:00
|
|
|
|
/* Schaden muss granularer sein als Gr<47><72>e, deshalb ist er skaliert
|
|
|
|
|
* DAMAGE_SCALE ist der Faktor zwischen 1 Schadenspunkt und 1 Gr<EFBFBD><EFBFBD>enpunkt.
|
|
|
|
|
*/
|
|
|
|
|
return ((DAMAGE_SCALE - 1) + sh->damage * 100) / (sh->size * DAMAGE_SCALE);
|
2015-11-17 15:47:43 +01:00
|
|
|
|
}
|