2017-12-29 17:00:16 +01:00
|
|
|
#ifdef _MSC_VER
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
2017-12-29 17:00:16 +01:00
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "build.h"
|
|
|
|
|
|
|
|
#include "alchemy.h"
|
2014-06-16 03:34:39 +02:00
|
|
|
#include "direction.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "move.h"
|
2016-03-08 14:09:22 +01:00
|
|
|
#include "study.h"
|
2016-11-01 22:11:10 +01:00
|
|
|
#include "guard.h"
|
2014-11-01 12:09:56 +01:00
|
|
|
#include "laws.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "skill.h"
|
2014-12-17 17:22:26 +01:00
|
|
|
#include "lighthouse.h"
|
2014-11-01 12:09:56 +01:00
|
|
|
|
|
|
|
/* kernel includes */
|
2015-11-24 18:52:09 +01:00
|
|
|
#include <kernel/ally.h>
|
2014-11-01 12:09:56 +01:00
|
|
|
#include <kernel/alliance.h>
|
2018-09-29 19:32:39 +02:00
|
|
|
#include <kernel/attrib.h>
|
2014-11-01 12:09:56 +01:00
|
|
|
#include <kernel/building.h>
|
2017-12-29 17:00:16 +01:00
|
|
|
#include <kernel/config.h>
|
2018-09-29 19:32:39 +02:00
|
|
|
#include <kernel/connection.h>
|
2014-11-01 12:09:56 +01:00
|
|
|
#include <kernel/curse.h>
|
2018-09-29 19:32:39 +02:00
|
|
|
#include <kernel/event.h>
|
2014-11-01 12:09:56 +01:00
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/group.h>
|
|
|
|
#include <kernel/item.h>
|
|
|
|
#include <kernel/messages.h>
|
|
|
|
#include <kernel/order.h>
|
|
|
|
#include <kernel/pool.h>
|
|
|
|
#include <kernel/race.h>
|
|
|
|
#include <kernel/region.h>
|
2017-04-02 14:43:53 +02:00
|
|
|
#include <kernel/resources.h>
|
2014-11-01 12:09:56 +01:00
|
|
|
#include <kernel/ship.h>
|
|
|
|
#include <kernel/terrain.h>
|
|
|
|
#include <kernel/terrainid.h>
|
|
|
|
#include <kernel/unit.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
/* from libutil */
|
|
|
|
#include <util/goodies.h>
|
|
|
|
#include <util/language.h>
|
|
|
|
#include <util/log.h>
|
2018-09-29 19:32:39 +02:00
|
|
|
#include <util/param.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/parser.h>
|
|
|
|
#include <util/resolve.h>
|
2018-10-20 19:56:38 +02:00
|
|
|
#include <util/strings.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
/* from libc */
|
|
|
|
#include <assert.h>
|
2018-10-20 20:28:05 +02:00
|
|
|
#include <errno.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
struct building *getbuilding(const struct region *r)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
building *b = findbuilding(getid());
|
|
|
|
if (b == NULL || r != b->region)
|
|
|
|
return NULL;
|
|
|
|
return b;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
ship *getship(const struct region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2018-09-29 13:35:49 +02:00
|
|
|
ship *sh, *sx = findship(getid());
|
2014-12-22 16:28:17 +01:00
|
|
|
for (sh = r->ships; sh; sh = sh->next) {
|
|
|
|
if (sh == sx)
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
return NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void destroy_road(unit * u, int nmax, struct order *ord)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
char token[128];
|
|
|
|
const char *s = gettoken(token, sizeof(token));
|
2014-09-21 16:43:17 +02:00
|
|
|
direction_t d = s ? get_direction(s, u->faction->locale) : NODIRECTION;
|
|
|
|
if (d == NODIRECTION) {
|
|
|
|
/* Die Richtung wurde nicht erkannt */
|
|
|
|
cmistake(u, ord, 71, MSG_PRODUCE);
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-09-21 16:43:17 +02:00
|
|
|
unit *u2;
|
|
|
|
region *r = u->region;
|
2015-05-15 19:08:44 +02:00
|
|
|
int road, n = nmax;
|
2014-12-22 16:28:17 +01:00
|
|
|
|
2014-09-21 16:43:17 +02:00
|
|
|
if (nmax > SHRT_MAX) {
|
|
|
|
n = SHRT_MAX;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
else if (nmax < 0) {
|
2014-09-21 16:43:17 +02:00
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u2 = r->units; u2; u2 = u2->next) {
|
2016-10-31 07:45:06 +01:00
|
|
|
if (u2->faction != u->faction && is_guard(u2)
|
2014-09-21 16:43:17 +02:00
|
|
|
&& cansee(u2->faction, u->region, u, 0)
|
|
|
|
&& !alliedunit(u, u2->faction, HELP_GUARD)) {
|
|
|
|
cmistake(u, ord, 70, MSG_EVENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
road = rroad(r, d);
|
2017-12-29 17:00:16 +01:00
|
|
|
if (n > road) n = road;
|
|
|
|
|
2014-09-21 16:43:17 +02:00
|
|
|
if (n != 0) {
|
|
|
|
region *r2 = rconnect(r, d);
|
2019-02-02 20:36:23 +01:00
|
|
|
int willdo = effskill(u, SK_ROAD_BUILDING, NULL) * u->number;
|
2017-12-29 17:00:16 +01:00
|
|
|
if (willdo > n) willdo = n;
|
2014-09-21 16:43:17 +02:00
|
|
|
if (willdo == 0) {
|
|
|
|
/* TODO: error message */
|
|
|
|
}
|
2016-08-28 15:31:25 +02:00
|
|
|
else if (willdo > SHRT_MAX)
|
2014-12-22 16:28:17 +01:00
|
|
|
road = 0;
|
2014-09-21 16:43:17 +02:00
|
|
|
else
|
2015-05-15 19:08:44 +02:00
|
|
|
road = (short)(road - willdo);
|
2014-09-21 16:43:17 +02:00
|
|
|
rsetroad(r, d, road);
|
2016-08-28 17:31:29 +02:00
|
|
|
if (willdo > 0) {
|
|
|
|
ADDMSG(&u->faction->msgs, msg_message("destroy_road",
|
|
|
|
"unit from to", u, r, r2));
|
|
|
|
}
|
2014-09-21 16:43:17 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 15:32:06 +02:00
|
|
|
static int recycle(unit *u, construction *con, int size) {
|
2019-02-09 13:39:28 +01:00
|
|
|
/* TODO: Nicht an ZERSTOERE mit Punktangabe angepasst! */
|
2018-05-01 15:32:06 +02:00
|
|
|
int c;
|
|
|
|
for (c = 0; con->materials[c].number; ++c) {
|
|
|
|
const requirement *rq = con->materials + c;
|
|
|
|
int num = (rq->number * size / con->reqsize) / 2;
|
|
|
|
if (num) {
|
|
|
|
change_resource(u, rq->rtype, num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int destroy_cmd(unit * u, struct order *ord)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
char token[128];
|
|
|
|
ship *sh;
|
|
|
|
unit *u2;
|
|
|
|
region *r = u->region;
|
|
|
|
int size = 0;
|
|
|
|
const char *s;
|
|
|
|
int n = INT_MAX;
|
|
|
|
|
|
|
|
if (u->number < 1)
|
2015-08-04 22:47:55 +02:00
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-07-03 13:04:11 +02:00
|
|
|
if (fval(u, UFL_LONGACTION)) {
|
|
|
|
cmistake(u, ord, 52, MSG_PRODUCE);
|
2015-08-04 22:47:55 +02:00
|
|
|
return 52;
|
2015-07-03 13:04:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-09 20:33:47 +02:00
|
|
|
init_order_depr(ord);
|
2014-12-22 16:28:17 +01:00
|
|
|
s = gettoken(token, sizeof(token));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-12-22 16:28:17 +01:00
|
|
|
if (s && *s) {
|
2018-10-20 20:10:11 +02:00
|
|
|
ERRNO_CHECK();
|
|
|
|
n = atoi(s);
|
|
|
|
errno = 0;
|
2014-12-22 16:28:17 +01:00
|
|
|
if (n <= 0) {
|
2015-07-02 11:42:18 +02:00
|
|
|
n = INT_MAX;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
2016-08-28 17:48:06 +02:00
|
|
|
else {
|
|
|
|
s = gettoken(token, sizeof(token));
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 21:57:50 +02:00
|
|
|
if (s && isparam(s, u->faction->locale, P_ROAD)) {
|
2014-12-22 16:28:17 +01:00
|
|
|
destroy_road(u, n, ord);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-12-22 16:28:17 +01:00
|
|
|
if (u->building) {
|
|
|
|
building *b = u->building;
|
|
|
|
|
|
|
|
if (u != building_owner(b)) {
|
|
|
|
cmistake(u, ord, 138, MSG_PRODUCE);
|
2015-08-04 22:47:55 +02:00
|
|
|
return 138;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
if (fval(b->type, BTF_INDESTRUCTIBLE)) {
|
|
|
|
cmistake(u, ord, 138, MSG_PRODUCE);
|
2015-08-04 22:47:55 +02:00
|
|
|
return 138;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
if (n >= b->size) {
|
2018-05-01 15:32:06 +02:00
|
|
|
building_stage *stage;
|
2014-12-22 16:28:17 +01:00
|
|
|
/* destroy completly */
|
|
|
|
/* all units leave the building */
|
|
|
|
for (u2 = r->units; u2; u2 = u2->next) {
|
|
|
|
if (u2->building == b) {
|
|
|
|
leave_building(u2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ADDMSG(&u->faction->msgs, msg_message("destroy", "building unit", b, u));
|
2018-05-01 15:32:06 +02:00
|
|
|
for (stage = b->type->stages; stage; stage = stage->next) {
|
|
|
|
size = recycle(u, stage->construction, size);
|
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
remove_building(&r->buildings, b);
|
|
|
|
}
|
|
|
|
else {
|
2018-05-01 15:32:06 +02:00
|
|
|
/* TODO: partial destroy does not recycle */
|
2014-12-22 16:28:17 +01:00
|
|
|
b->size -= n;
|
|
|
|
ADDMSG(&u->faction->msgs, msg_message("destroy_partial",
|
|
|
|
"building unit", b, u));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (u->ship) {
|
|
|
|
sh = u->ship;
|
|
|
|
|
|
|
|
if (u != ship_owner(sh)) {
|
|
|
|
cmistake(u, ord, 138, MSG_PRODUCE);
|
2015-08-04 22:47:55 +02:00
|
|
|
return 138;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
if (fval(r->terrain, SEA_REGION)) {
|
|
|
|
cmistake(u, ord, 14, MSG_EVENT);
|
2015-08-04 22:47:55 +02:00
|
|
|
return 14;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 20:04:32 +02:00
|
|
|
if (n >= (sh->size * 100) / ship_maxsize(sh)) {
|
2014-12-22 16:28:17 +01:00
|
|
|
/* destroy completly */
|
|
|
|
/* all units leave the ship */
|
|
|
|
for (u2 = r->units; u2; u2 = u2->next) {
|
|
|
|
if (u2->ship == sh) {
|
|
|
|
leave_ship(u2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ADDMSG(&u->faction->msgs, msg_message("shipdestroy",
|
|
|
|
"unit region ship", u, r, sh));
|
2018-05-01 15:32:06 +02:00
|
|
|
size = recycle(u, sh->type->construction, size);
|
2014-12-22 16:28:17 +01:00
|
|
|
remove_ship(&sh->region->ships, sh);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* partial destroy */
|
2019-10-22 18:46:00 +02:00
|
|
|
sh->size -= (ship_maxsize(sh) * n) / 100;
|
2014-12-22 16:28:17 +01:00
|
|
|
ADDMSG(&u->faction->msgs, msg_message("shipdestroy_partial",
|
|
|
|
"unit region ship", u, r, sh));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cmistake(u, ord, 138, MSG_PRODUCE);
|
2015-08-04 22:47:55 +02:00
|
|
|
return 138;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
2015-08-27 16:59:39 +02:00
|
|
|
void build_road(unit * u, int size, direction_t d)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-08-27 16:59:39 +02:00
|
|
|
region *r = u->region;
|
2015-08-27 16:16:55 +02:00
|
|
|
int n, left, effsk;
|
2014-12-22 16:28:17 +01:00
|
|
|
region *rn = rconnect(r, d);
|
|
|
|
|
|
|
|
assert(u->number);
|
2019-02-02 20:36:23 +01:00
|
|
|
effsk = effskill(u, SK_ROAD_BUILDING, NULL);
|
2015-08-27 16:16:55 +02:00
|
|
|
if (!effsk) {
|
2014-12-22 16:28:17 +01:00
|
|
|
cmistake(u, u->thisorder, 103, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rn == NULL || rn->terrain->max_road < 0) {
|
|
|
|
cmistake(u, u->thisorder, 94, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->terrain->max_road < 0) {
|
|
|
|
cmistake(u, u->thisorder, 94, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->terrain == newterrain(T_SWAMP)) {
|
|
|
|
/* wenn kein Damm existiert */
|
|
|
|
const struct building_type *bt_dam = bt_find("dam");
|
|
|
|
if (!bt_dam || !buildingtype_exists(r, bt_dam, true)) {
|
|
|
|
cmistake(u, u->thisorder, 132, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (r->terrain == newterrain(T_DESERT)) {
|
|
|
|
const struct building_type *bt_caravan = bt_find("caravan");
|
|
|
|
/* wenn keine Karawanserei existiert */
|
|
|
|
if (!bt_caravan || !buildingtype_exists(r, bt_caravan, true)) {
|
|
|
|
cmistake(u, u->thisorder, 133, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (r->terrain == newterrain(T_GLACIER)) {
|
|
|
|
const struct building_type *bt_tunnel = bt_find("tunnel");
|
|
|
|
/* wenn kein Tunnel existiert */
|
|
|
|
if (!bt_tunnel || !buildingtype_exists(r, bt_tunnel, true)) {
|
|
|
|
cmistake(u, u->thisorder, 131, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* left kann man noch bauen */
|
|
|
|
left = r->terrain->max_road - rroad(r, d);
|
|
|
|
|
|
|
|
/* hoffentlich ist r->road <= r->terrain->max_road, n also >= 0 */
|
|
|
|
if (left <= 0) {
|
|
|
|
ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder,
|
|
|
|
"error_roads_finished", ""));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-29 17:00:16 +01:00
|
|
|
if (size > 0 && left > size) {
|
|
|
|
left = size;
|
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
/* baumaximum anhand der rohstoffe */
|
|
|
|
if (u_race(u) == get_race(RC_STONEGOLEM)) {
|
|
|
|
n = u->number * GOLEM_STONE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = get_pooled(u, get_resourcetype(R_STONE), GET_DEFAULT, left);
|
|
|
|
if (n == 0) {
|
|
|
|
cmistake(u, u->thisorder, 151, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-12-29 17:00:16 +01:00
|
|
|
if (n < left) left = n;
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
/* n = maximum by skill. try to maximize it */
|
2015-08-27 16:16:55 +02:00
|
|
|
n = u->number * effsk;
|
2014-12-22 16:28:17 +01:00
|
|
|
if (n < left) {
|
|
|
|
const resource_type *ring = get_resourcetype(R_RING_OF_NIMBLEFINGER);
|
|
|
|
item *itm = ring ? *i_find(&u->items, ring->itype) : 0;
|
|
|
|
if (itm != NULL && itm->number > 0) {
|
2017-12-29 17:00:16 +01:00
|
|
|
int rings = (u->number < itm->number) ? u->number : itm->number;
|
2014-12-22 16:28:17 +01:00
|
|
|
n = n * ((roqf_factor() - 1) * rings + u->number) / u->number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n < left) {
|
|
|
|
int dm = get_effect(u, oldpotiontype[P_DOMORE]);
|
|
|
|
if (dm != 0) {
|
2015-08-27 16:16:55 +02:00
|
|
|
int todo = (left - n + effsk - 1) / effsk;
|
2017-12-29 17:00:16 +01:00
|
|
|
if (todo > u->number) todo = u->number;
|
|
|
|
if (dm > todo) dm = todo;
|
2014-12-22 16:28:17 +01:00
|
|
|
change_effect(u, oldpotiontype[P_DOMORE], -dm);
|
2015-08-27 16:16:55 +02:00
|
|
|
n += dm * effsk;
|
2014-12-22 16:28:17 +01:00
|
|
|
} /* Auswirkung Schaffenstrunk */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make minimum of possible and available: */
|
2017-12-29 17:00:16 +01:00
|
|
|
if (n > left) n = left;
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
/* n is now modified by several special effects, so we have to
|
|
|
|
* minimize it again to make sure the road will not grow beyond
|
|
|
|
* maximum. */
|
2015-05-15 19:08:44 +02:00
|
|
|
rsetroad(r, d, rroad(r, d) + n);
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
if (u_race(u) == get_race(RC_STONEGOLEM)) {
|
|
|
|
int golemsused = n / GOLEM_STONE;
|
|
|
|
if (n % GOLEM_STONE != 0) {
|
|
|
|
++golemsused;
|
|
|
|
}
|
|
|
|
scale_number(u, u->number - golemsused);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
use_pooled(u, get_resourcetype(R_STONE), GET_DEFAULT, n);
|
|
|
|
/* Nur soviel PRODUCEEXP wie auch tatsaechlich gemacht wurde */
|
2017-12-29 17:00:16 +01:00
|
|
|
produceexp(u, SK_ROAD_BUILDING, (n < u->number) ? n : u->number);
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
ADDMSG(&u->faction->msgs, msg_message("buildroad",
|
|
|
|
"region unit size", r, u, n));
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* ** ** ** ** ** ** *
|
|
|
|
* new build rules *
|
|
|
|
* ** ** ** ** ** ** */
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int required(int size, int msize, int maxneed)
|
2014-12-22 16:28:17 +01:00
|
|
|
/* um size von msize Punkten zu bauen,
|
|
|
|
* braucht man required von maxneed resourcen */
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
int used;
|
2017-02-15 17:09:23 +01:00
|
|
|
assert(msize > 0);
|
2014-12-22 16:28:17 +01:00
|
|
|
used = size * maxneed / msize;
|
|
|
|
if (size * maxneed % msize)
|
|
|
|
++used;
|
|
|
|
return used;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2017-04-02 14:43:53 +02:00
|
|
|
static int matmod(const unit * u, const resource_type * rtype, int value)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2017-04-02 14:43:53 +02:00
|
|
|
if (rtype->modifiers) {
|
2017-10-29 18:55:43 +01:00
|
|
|
variant save = frac_one;
|
2017-04-02 14:43:53 +02:00
|
|
|
const struct building_type *btype = NULL;
|
|
|
|
const struct race *rc = u_race(u);
|
|
|
|
resource_mod *mod;
|
|
|
|
if (u->building && inside_building(u)) {
|
|
|
|
btype = u->building->type;
|
|
|
|
}
|
|
|
|
for (mod = rtype->modifiers; mod->type != RMT_END; ++mod) {
|
|
|
|
if (mod->type == RMT_USE_SAVE) {
|
|
|
|
if (!mod->btype || mod->btype == btype) {
|
2018-04-29 13:46:17 +02:00
|
|
|
if (!mod->race_mask || (mod->race_mask & rc->mask_item)) {
|
2017-04-02 14:43:53 +02:00
|
|
|
save = frac_mul(save, mod->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value * save.sa[0] / save.sa[1];
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
return value;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int roqf_factor(void)
|
|
|
|
{
|
2016-09-11 12:48:00 +02:00
|
|
|
static int config;
|
|
|
|
static int value;
|
|
|
|
if (config_changed(&config)) {
|
2015-11-22 10:44:46 +01:00
|
|
|
value = config_get_int("rules.economy.roqf", 10);
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
return value;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2017-02-14 21:01:22 +01:00
|
|
|
static int use_materials(unit *u, const construction *type, int n, int completed) {
|
|
|
|
if (type->materials) {
|
|
|
|
int c;
|
|
|
|
for (c = 0; type->materials[c].number; c++) {
|
|
|
|
const struct resource_type *rtype = type->materials[c].rtype;
|
|
|
|
int prebuilt =
|
|
|
|
required(completed, type->reqsize, type->materials[c].number);
|
|
|
|
int need =
|
|
|
|
required(completed + n, type->reqsize, type->materials[c].number);
|
2018-02-25 18:30:20 +01:00
|
|
|
int multi, canuse = 100; /* normalization */
|
2017-04-02 14:43:53 +02:00
|
|
|
canuse = matmod(u, rtype, canuse);
|
|
|
|
assert(canuse >= 0);
|
2017-02-14 21:01:22 +01:00
|
|
|
assert(canuse % 100 == 0
|
|
|
|
|| !"only constant multipliers are implemented in build()");
|
|
|
|
multi = canuse / 100;
|
|
|
|
if (canuse < 0) {
|
|
|
|
return canuse; /* pass errors to caller */
|
|
|
|
}
|
|
|
|
use_pooled(u, rtype, GET_DEFAULT,
|
|
|
|
(need - prebuilt + multi - 1) / multi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int count_materials(unit *u, const construction *type, int n, int completed)
|
|
|
|
{
|
|
|
|
if (type->materials) {
|
|
|
|
int c;
|
|
|
|
for (c = 0; n > 0 && type->materials[c].number; c++) {
|
|
|
|
const struct resource_type *rtype = type->materials[c].rtype;
|
|
|
|
int canuse = get_pooled(u, rtype, GET_DEFAULT, INT_MAX);
|
2017-04-02 14:43:53 +02:00
|
|
|
canuse = matmod(u, rtype, canuse);
|
2017-02-14 21:01:22 +01:00
|
|
|
|
2017-04-02 14:43:53 +02:00
|
|
|
assert(canuse >= 0);
|
2017-02-14 21:01:22 +01:00
|
|
|
if (type->reqsize > 1) {
|
2018-02-25 20:06:47 +01:00
|
|
|
int prebuilt = required(completed, type->reqsize,
|
|
|
|
type->materials[c].number);
|
|
|
|
while (n > 0) {
|
|
|
|
int need =
|
2017-02-14 21:01:22 +01:00
|
|
|
required(completed + n, type->reqsize, type->materials[c].number);
|
2018-02-25 20:06:47 +01:00
|
|
|
if (need - prebuilt <= canuse) {
|
2017-02-14 21:01:22 +01:00
|
|
|
break;
|
2018-02-25 20:06:47 +01:00
|
|
|
}
|
2017-02-14 21:01:22 +01:00
|
|
|
--n; /* TODO: optimieren? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int maxn = canuse / type->materials[c].number;
|
|
|
|
if (maxn < n) {
|
|
|
|
n = maxn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2018-10-28 21:28:05 +01:00
|
|
|
int build_skill(unit *u, int basesk, int skill_mod) {
|
|
|
|
int effsk, skills;
|
|
|
|
int dm = get_effect(u, oldpotiontype[P_DOMORE]);
|
|
|
|
|
|
|
|
effsk = basesk + skill_mod;
|
|
|
|
assert(effsk >= 0);
|
|
|
|
|
|
|
|
skills = effsk * u->number;
|
|
|
|
|
|
|
|
/* technically, nimblefinge and domore should be in a global set of
|
|
|
|
* "game"-attributes, (as at_skillmod) but for a while, we're leaving
|
|
|
|
* them in here. */
|
|
|
|
|
|
|
|
if (dm != 0) {
|
|
|
|
/* Auswirkung Schaffenstrunk */
|
|
|
|
if (dm > u->number) dm = u->number;
|
|
|
|
change_effect(u, oldpotiontype[P_DOMORE], -dm);
|
|
|
|
skills += dm * effsk;
|
|
|
|
}
|
|
|
|
return skills;
|
|
|
|
}
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
/** Use up resources for building an object.
|
|
|
|
* Build up to 'size' points of 'type', where 'completed'
|
|
|
|
* of the first object have already been finished. return the
|
|
|
|
* actual size that could be built.
|
|
|
|
*/
|
2019-10-21 20:04:32 +02:00
|
|
|
static int build_limited(unit * u, const construction * con, int completed, int number, int want, int basesk, int *skill_total) {
|
2018-10-29 19:35:49 +01:00
|
|
|
int skills = *skill_total;
|
2019-11-18 18:48:28 +01:00
|
|
|
int made = 0, maxsize;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2019-11-18 18:48:28 +01:00
|
|
|
assert(con);
|
2018-10-29 19:35:49 +01:00
|
|
|
if (want <= 0) {
|
2014-12-22 16:28:17 +01:00
|
|
|
return 0;
|
2018-10-29 19:35:49 +01:00
|
|
|
}
|
2019-11-18 18:48:28 +01:00
|
|
|
maxsize = con->maxsize * number;
|
2019-10-21 20:04:32 +02:00
|
|
|
if (completed == maxsize) {
|
2011-03-07 08:02:35 +01:00
|
|
|
return ECOMPLETE;
|
2018-04-30 23:11:13 +02:00
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
for (; want > 0 && skills > 0;) {
|
2017-02-14 21:01:22 +01:00
|
|
|
int err, n;
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
/* Hier ist entweder maxsize == -1, oder completed < maxsize.
|
|
|
|
* Andernfalls ist das Datenfile oder sonstwas kaputt...
|
2019-02-09 13:39:28 +01:00
|
|
|
* (enno): Nein, das ist fuer Dinge, bei denen die naechste Ausbaustufe
|
|
|
|
* die gleiche wie die vorherige ist. z.b. Gegenstaende.
|
2014-12-22 16:28:17 +01:00
|
|
|
*/
|
2019-10-21 20:04:32 +02:00
|
|
|
if (maxsize > 0) {
|
|
|
|
completed = completed % (maxsize);
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
completed = 0;
|
2017-04-26 21:44:24 +02:00
|
|
|
assert(con->reqsize >= 1);
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2017-04-26 21:44:24 +02:00
|
|
|
if (basesk < con->minskill) {
|
2014-12-22 16:28:17 +01:00
|
|
|
if (made == 0)
|
2015-11-06 21:04:35 +01:00
|
|
|
return ELOWSKILL;
|
|
|
|
else
|
|
|
|
break; /* not good enough to go on */
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-12-22 16:28:17 +01:00
|
|
|
/* n = maximum buildable size */
|
2017-04-26 21:44:24 +02:00
|
|
|
if (con->minskill > 1) {
|
|
|
|
n = skills / con->minskill;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = skills;
|
|
|
|
}
|
|
|
|
/* Flinkfingerring wirkt nicht auf Mengenbegrenzte (magische)
|
|
|
|
* Talente */
|
2019-05-04 10:45:51 +02:00
|
|
|
if (faction_skill_limit(u->faction, con->skill) == INT_MAX) {
|
2014-12-22 16:28:17 +01:00
|
|
|
const resource_type *ring = get_resourcetype(R_RING_OF_NIMBLEFINGER);
|
|
|
|
item *itm = ring ? *i_find(&u->items, ring->itype) : 0;
|
|
|
|
int i = itm ? itm->number : 0;
|
|
|
|
if (i > 0) {
|
2017-12-29 17:00:16 +01:00
|
|
|
int rings = (u->number < i) ? u->number : i;
|
2014-12-22 16:28:17 +01:00
|
|
|
n = n * ((roqf_factor() - 1) * rings + u->number) / u->number;
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-12-22 16:28:17 +01:00
|
|
|
if (want < n) n = want;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2019-10-21 20:04:32 +02:00
|
|
|
if (maxsize > 0) {
|
|
|
|
int req = maxsize - completed;
|
2017-12-29 17:00:16 +01:00
|
|
|
if (req < n) n = req;
|
2018-04-30 23:17:48 +02:00
|
|
|
want = n;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
|
2017-04-26 21:44:24 +02:00
|
|
|
n = count_materials(u, con, n, completed);
|
2014-12-22 16:28:17 +01:00
|
|
|
if (n <= 0) {
|
|
|
|
if (made == 0)
|
|
|
|
return ENOMATERIALS;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2017-04-26 21:44:24 +02:00
|
|
|
err = use_materials(u, con, n, completed);
|
2017-02-14 21:01:22 +01:00
|
|
|
if (err < 0) {
|
|
|
|
return err;
|
2017-02-14 20:51:03 +01:00
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
made += n;
|
2017-04-26 21:44:24 +02:00
|
|
|
skills -= n * con->minskill;
|
2014-12-22 16:28:17 +01:00
|
|
|
want -= n;
|
|
|
|
completed = completed + n;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2018-10-29 19:35:49 +01:00
|
|
|
*skill_total = skills;
|
|
|
|
return made;
|
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
|
2019-10-21 20:04:32 +02:00
|
|
|
int build(unit * u, int number, const construction * con, int completed, int want, int skill_mod)
|
2018-10-29 19:35:49 +01:00
|
|
|
{
|
|
|
|
int skills = INT_MAX; /* number of skill points remainig */
|
|
|
|
int made, basesk = 0;
|
|
|
|
|
2019-10-21 20:04:32 +02:00
|
|
|
assert(number >= 1);
|
2018-10-29 19:35:49 +01:00
|
|
|
assert(con->skill != NOSKILL);
|
2019-02-02 20:36:23 +01:00
|
|
|
basesk = effskill(u, con->skill, NULL);
|
2018-10-29 19:35:49 +01:00
|
|
|
if (basesk == 0) {
|
|
|
|
return ENEEDSKILL;
|
|
|
|
}
|
|
|
|
|
|
|
|
skills = build_skill(u, basesk, skill_mod);
|
2019-10-21 20:04:32 +02:00
|
|
|
made = build_limited(u, con, completed, number, want, basesk, &skills);
|
2018-10-29 19:35:49 +01:00
|
|
|
/* Nur soviel PRODUCEEXP wie auch tatsaechlich gemacht wurde */
|
|
|
|
if (made > 0) {
|
|
|
|
produceexp(u, con->skill, (made < u->number) ? made : u->number);
|
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
return made;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
message *msg_materials_required(unit * u, order * ord,
|
2014-12-22 16:28:17 +01:00
|
|
|
const construction * ctype, int multi)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
int c;
|
2015-10-14 20:41:42 +02:00
|
|
|
message *msg;
|
2014-12-22 16:28:17 +01:00
|
|
|
/* something missing from the list of materials */
|
|
|
|
resource *reslist = NULL;
|
|
|
|
|
|
|
|
if (multi <= 0 || multi == INT_MAX)
|
|
|
|
multi = 1;
|
|
|
|
for (c = 0; ctype && ctype->materials[c].number; ++c) {
|
|
|
|
resource *res = malloc(sizeof(resource));
|
2018-12-15 20:01:51 +01:00
|
|
|
if (!res) abort();
|
2014-12-22 16:28:17 +01:00
|
|
|
res->number = multi * ctype->materials[c].number / ctype->reqsize;
|
|
|
|
res->type = ctype->materials[c].rtype;
|
|
|
|
res->next = reslist;
|
|
|
|
reslist = res;
|
|
|
|
}
|
2015-10-14 20:41:42 +02:00
|
|
|
msg = msg_feedback(u, ord, "build_required", "required", reslist);
|
|
|
|
while (reslist) {
|
|
|
|
resource *res = reslist->next;
|
|
|
|
free(reslist);
|
|
|
|
reslist = res;
|
|
|
|
}
|
|
|
|
return msg;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int maxbuild(const unit * u, const construction * cons)
|
2014-12-22 16:28:17 +01:00
|
|
|
/* calculate maximum size that can be built from available material */
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
int c;
|
|
|
|
int maximum = INT_MAX;
|
|
|
|
for (c = 0; cons->materials[c].number; c++) {
|
|
|
|
const resource_type *rtype = cons->materials[c].rtype;
|
|
|
|
int have = get_pooled(u, rtype, GET_DEFAULT, INT_MAX);
|
|
|
|
int need = required(1, cons->reqsize, cons->materials[c].number);
|
|
|
|
if (have < need) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-29 17:00:16 +01:00
|
|
|
else {
|
|
|
|
int b = have / need;
|
|
|
|
if (maximum > b) maximum = b;
|
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
return maximum;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 22:52:38 +02:00
|
|
|
static int build_failure(unit *u, order *ord, const building_type *btype, int want, int err) {
|
|
|
|
switch (err) {
|
|
|
|
case ECOMPLETE:
|
|
|
|
/* the building is already complete */
|
|
|
|
cmistake(u, ord, 4, MSG_PRODUCE);
|
|
|
|
break;
|
|
|
|
case ENOMATERIALS:
|
|
|
|
ADDMSG(&u->faction->msgs, msg_materials_required(u, ord,
|
2018-05-01 15:32:06 +02:00
|
|
|
btype->stages->construction, want));
|
2018-04-30 22:52:38 +02:00
|
|
|
break;
|
|
|
|
case ELOWSKILL:
|
|
|
|
case ENEEDSKILL:
|
|
|
|
/* no skill, or not enough skill points to build */
|
|
|
|
cmistake(u, ord, 50, MSG_PRODUCE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:35:49 +01:00
|
|
|
static int build_stages(unit *u, const building_type *btype, int built, int n, int basesk, int *skill_total) {
|
2018-05-01 15:32:06 +02:00
|
|
|
|
|
|
|
const building_stage *stage;
|
2018-04-30 22:52:38 +02:00
|
|
|
int made = 0;
|
2018-05-01 15:32:06 +02:00
|
|
|
|
|
|
|
for (stage = btype->stages; stage; stage = stage->next) {
|
|
|
|
const construction * con = stage->construction;
|
2018-04-30 22:52:38 +02:00
|
|
|
if (con->maxsize < 0 || con->maxsize > built) {
|
|
|
|
int err, want = INT_MAX;
|
|
|
|
if (n < INT_MAX) {
|
|
|
|
/* do not build more than n total */
|
|
|
|
want = n - made;
|
|
|
|
}
|
|
|
|
if (con->maxsize > 0) {
|
|
|
|
/* do not build more than the rest of the stage */
|
|
|
|
int todo = con->maxsize - built;
|
|
|
|
if (todo < want) {
|
|
|
|
want = todo;
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 20:04:32 +02:00
|
|
|
err = build_limited(u, con, 1, built, want, basesk, skill_total);
|
2018-04-30 22:52:38 +02:00
|
|
|
if (err < 0) {
|
|
|
|
if (made == 0) {
|
|
|
|
/* could not make any part at all */
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* could not build any part of this stage (low skill, etc). */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* err is the amount we built of this stage */
|
2019-05-11 20:51:42 +02:00
|
|
|
built += err;
|
2018-04-30 22:52:38 +02:00
|
|
|
made += err;
|
2019-05-11 20:51:42 +02:00
|
|
|
if (con->maxsize > 0 && built < con->maxsize) {
|
2018-04-30 22:52:38 +02:00
|
|
|
/* we did not finish the stage, can quit here */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* build the next stage: */
|
|
|
|
if (built >= con->maxsize && con->maxsize > 0) {
|
|
|
|
built -= con->maxsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return made;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-06-29 01:19:46 +02:00
|
|
|
int
|
|
|
|
build_building(unit * u, const building_type * btype, int id, int want, order * ord)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
region *r = u->region;
|
|
|
|
int n = want, built = 0;
|
|
|
|
building *b = NULL;
|
|
|
|
/* einmalige Korrektur */
|
|
|
|
const char *btname;
|
|
|
|
order *new_order = NULL;
|
|
|
|
const struct locale *lang = u->faction->locale;
|
2018-10-29 19:35:49 +01:00
|
|
|
int skills, basesk; /* number of skill points remainig */
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
assert(u->number);
|
2018-05-01 15:32:06 +02:00
|
|
|
assert(btype->stages && btype->stages->construction);
|
2018-10-29 19:35:49 +01:00
|
|
|
|
2019-02-02 20:36:23 +01:00
|
|
|
basesk = effskill(u, SK_BUILDING, NULL);
|
2018-10-29 19:35:49 +01:00
|
|
|
skills = build_skill(u, basesk, 0);
|
|
|
|
if (skills == 0) {
|
2014-12-22 16:28:17 +01:00
|
|
|
cmistake(u, ord, 101, MSG_PRODUCE);
|
2014-06-29 01:19:46 +02:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-12-22 16:28:17 +01:00
|
|
|
/* Falls eine Nummer angegeben worden ist, und ein Gebaeude mit der
|
|
|
|
* betreffenden Nummer existiert, ist b nun gueltig. Wenn keine Burg
|
|
|
|
* gefunden wurde, dann wird nicht einfach eine neue erbaut. Ansonsten
|
|
|
|
* baut man an der eigenen burg weiter. */
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2018-02-25 20:06:47 +01:00
|
|
|
/* Wenn die angegebene Nummer falsch ist, KEINE Burg bauen! */
|
2014-12-22 16:28:17 +01:00
|
|
|
if (id > 0) { /* eine Nummer angegeben, keine neue Burg bauen */
|
|
|
|
b = findbuilding(id);
|
|
|
|
if (!b || b->region != u->region) { /* eine Burg mit dieser Nummer gibt es hier nicht */
|
|
|
|
/* vieleicht Tippfehler und die eigene Burg ist gemeint? */
|
|
|
|
if (u->building && u->building->type == btype) {
|
|
|
|
b = u->building;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* keine neue Burg anfangen wenn eine Nummer angegeben war */
|
|
|
|
cmistake(u, ord, 6, MSG_PRODUCE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (u->building && u->building->type == btype) {
|
|
|
|
b = u->building;
|
|
|
|
}
|
|
|
|
|
2017-07-17 14:01:08 +02:00
|
|
|
if (b) {
|
2014-12-22 16:28:17 +01:00
|
|
|
btype = b->type;
|
2017-07-17 14:01:08 +02:00
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
if (fval(btype, BTF_UNIQUE) && buildingtype_exists(r, btype, false)) {
|
|
|
|
/* only one of these per region */
|
|
|
|
cmistake(u, ord, 93, MSG_PRODUCE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (btype->flags & BTF_NOBUILD) {
|
|
|
|
/* special building, cannot be built */
|
|
|
|
cmistake(u, ord, 221, MSG_PRODUCE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((r->terrain->flags & LAND_REGION) == 0) {
|
|
|
|
/* special terrain, cannot build */
|
|
|
|
cmistake(u, ord, 221, MSG_PRODUCE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (btype->flags & BTF_ONEPERTURN) {
|
|
|
|
if (b && fval(b, BLD_EXPANDED)) {
|
|
|
|
cmistake(u, ord, 318, MSG_PRODUCE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
n = 1;
|
|
|
|
}
|
|
|
|
if (b) {
|
2015-11-22 16:14:27 +01:00
|
|
|
bool rule_other = config_get_int("rules.build.other_buildings", 1) != 0;
|
2014-12-22 16:28:17 +01:00
|
|
|
if (!rule_other) {
|
|
|
|
unit *owner = building_owner(b);
|
|
|
|
if (!owner || owner->faction != u->faction) {
|
|
|
|
cmistake(u, ord, 1222, MSG_PRODUCE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
built = b->size;
|
2016-11-23 19:19:04 +01:00
|
|
|
}
|
2020-01-26 00:34:31 +01:00
|
|
|
|
|
|
|
if (btype->maxsize > 0) {
|
|
|
|
int remain = btype->maxsize - built;
|
|
|
|
if (remain < n) {
|
|
|
|
n = remain;
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-29 19:35:49 +01:00
|
|
|
built = build_stages(u, btype, built, n, basesk, &skills);
|
2018-04-30 22:52:38 +02:00
|
|
|
|
|
|
|
if (built < 0) {
|
|
|
|
return build_failure(u, ord, btype, want, built);
|
2014-12-22 16:28:17 +01:00
|
|
|
}
|
2018-04-30 22:52:38 +02:00
|
|
|
|
|
|
|
if (b) {
|
|
|
|
b->size += built;
|
|
|
|
} else {
|
2014-12-22 16:28:17 +01:00
|
|
|
/* build a new building */
|
|
|
|
b = new_building(btype, r, lang);
|
2018-04-30 22:52:38 +02:00
|
|
|
b->size = built;
|
2014-12-22 16:28:17 +01:00
|
|
|
b->type = btype;
|
2016-08-21 20:12:28 +02:00
|
|
|
fset(b, BLD_MAINTAINED);
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
/* Die Einheit befindet sich automatisch im Inneren der neuen Burg. */
|
|
|
|
if (u->number && leave(u, false)) {
|
|
|
|
u_set_building(u, b);
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-12-22 16:28:17 +01:00
|
|
|
btname = LOC(lang, btype->_name);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2018-04-30 22:52:38 +02:00
|
|
|
if (want <= built) {
|
2019-02-09 13:39:28 +01:00
|
|
|
/* gebaeude fertig */
|
2014-12-22 16:28:17 +01:00
|
|
|
new_order = default_order(lang);
|
|
|
|
}
|
2015-11-07 23:34:51 +01:00
|
|
|
else if (want != INT_MAX && btname) {
|
2019-02-09 13:39:28 +01:00
|
|
|
/* reduzierte restgroesse */
|
2014-12-22 16:28:17 +01:00
|
|
|
const char *hasspace = strchr(btname, ' ');
|
|
|
|
if (hasspace) {
|
|
|
|
new_order =
|
|
|
|
create_order(K_MAKE, lang, "%d \"%s\" %i", n - built, btname, b->no);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_order =
|
|
|
|
create_order(K_MAKE, lang, "%d %s %i", n - built, btname, b->no);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (btname) {
|
2019-02-09 13:39:28 +01:00
|
|
|
/* Neues Haus, Befehl mit Gebaeudename */
|
2014-12-22 16:28:17 +01:00
|
|
|
const char *hasspace = strchr(btname, ' ');
|
|
|
|
if (hasspace) {
|
|
|
|
new_order = create_order(K_MAKE, lang, "\"%s\" %i", btname, b->no);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_order = create_order(K_MAKE, lang, "%s %i", btname, b->no);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_order) {
|
|
|
|
replace_order(&u->orders, ord, new_order);
|
|
|
|
free_order(new_order);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2018-04-30 22:52:38 +02:00
|
|
|
ADDMSG(&u->faction->msgs, msg_message("buildbuilding",
|
|
|
|
"building unit size", b, u, built));
|
|
|
|
|
2016-07-31 18:48:27 +02:00
|
|
|
if (b->type->maxsize > 0 && b->size > b->type->maxsize) {
|
|
|
|
log_error("build: %s has size=%d, maxsize=%d", buildingname(b), b->size, b->type->maxsize);
|
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
fset(b, BLD_EXPANDED);
|
|
|
|
|
2018-07-17 20:53:34 +02:00
|
|
|
if (is_lighthouse(btype)) {
|
|
|
|
update_lighthouse(b);
|
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
return built;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void build_ship(unit * u, ship * sh, int want)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
const construction *construction = sh->type->construction;
|
|
|
|
int size = (sh->size * DAMAGE_SCALE - sh->damage) / DAMAGE_SCALE;
|
|
|
|
int n;
|
2019-10-21 20:04:32 +02:00
|
|
|
int can = build(u, sh->number, construction, size, want, 0);
|
2014-12-22 16:28:17 +01:00
|
|
|
|
2019-10-21 20:04:32 +02:00
|
|
|
if ((n = ship_maxsize(sh) - sh->size) > 0 && can > 0) {
|
2014-12-22 16:28:17 +01:00
|
|
|
if (can >= n) {
|
|
|
|
sh->size += n;
|
|
|
|
can -= n;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sh->size += can;
|
|
|
|
n = can;
|
|
|
|
can = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sh->damage && can) {
|
2017-12-29 17:00:16 +01:00
|
|
|
int repair = can * DAMAGE_SCALE;
|
|
|
|
if (repair > sh->damage) repair = sh->damage;
|
2014-12-22 16:28:17 +01:00
|
|
|
n += repair / DAMAGE_SCALE;
|
|
|
|
if (repair % DAMAGE_SCALE)
|
|
|
|
++n;
|
|
|
|
sh->damage = sh->damage - repair;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n)
|
|
|
|
ADDMSG(&u->faction->msgs,
|
2018-02-25 20:06:47 +01:00
|
|
|
msg_message("buildship", "ship unit size", sh, u, n));
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 21:45:49 +02:00
|
|
|
void create_ship(unit * u, const struct ship_type *newtype, int want,
|
2018-02-25 20:06:47 +01:00
|
|
|
order * ord)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
ship *sh;
|
|
|
|
int msize;
|
|
|
|
const construction *cons = newtype->construction;
|
|
|
|
order *new_order;
|
2015-08-27 16:59:39 +02:00
|
|
|
region * r = u->region;
|
2014-12-22 16:28:17 +01:00
|
|
|
|
2019-02-02 20:36:23 +01:00
|
|
|
if (!effskill(u, SK_SHIPBUILDING, NULL)) {
|
2014-12-22 16:28:17 +01:00
|
|
|
cmistake(u, ord, 100, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if skill and material for 1 size is available */
|
2019-02-02 20:36:23 +01:00
|
|
|
if (effskill(u, cons->skill, NULL) < cons->minskill) {
|
2014-12-22 16:28:17 +01:00
|
|
|
ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder,
|
|
|
|
"error_build_skill_low", "value", cons->minskill));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msize = maxbuild(u, cons);
|
|
|
|
if (msize == 0) {
|
|
|
|
cmistake(u, ord, 88, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 17:00:16 +01:00
|
|
|
if (want <= 0 || want > msize) {
|
2014-12-22 16:28:17 +01:00
|
|
|
want = msize;
|
2017-12-29 17:00:16 +01:00
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
sh = new_ship(newtype, r, u->faction->locale);
|
|
|
|
|
|
|
|
if (leave(u, false)) {
|
|
|
|
if (fval(u_race(u), RCF_CANSAIL)) {
|
|
|
|
u_set_ship(u, sh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_order =
|
|
|
|
create_order(K_MAKE, u->faction->locale, "%s %i", LOC(u->faction->locale,
|
2018-02-25 20:06:47 +01:00
|
|
|
parameters[P_SHIP]), sh->no);
|
2014-12-22 16:28:17 +01:00
|
|
|
replace_order(&u->orders, ord, new_order);
|
|
|
|
free_order(new_order);
|
|
|
|
|
|
|
|
build_ship(u, sh, want);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2015-08-27 16:59:39 +02:00
|
|
|
void continue_ship(unit * u, int want)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-12-22 16:28:17 +01:00
|
|
|
const construction *cons;
|
|
|
|
ship *sh;
|
|
|
|
int msize;
|
2015-08-27 16:59:39 +02:00
|
|
|
region * r = u->region;
|
2014-12-22 16:28:17 +01:00
|
|
|
|
2019-02-02 20:36:23 +01:00
|
|
|
if (!effskill(u, SK_SHIPBUILDING, NULL)) {
|
2014-12-22 16:28:17 +01:00
|
|
|
cmistake(u, u->thisorder, 100, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Die Schiffsnummer bzw der Schiffstyp wird eingelesen */
|
|
|
|
sh = getship(r);
|
|
|
|
|
|
|
|
if (!sh)
|
|
|
|
sh = u->ship;
|
|
|
|
|
|
|
|
if (!sh) {
|
|
|
|
cmistake(u, u->thisorder, 20, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
2019-10-21 20:04:32 +02:00
|
|
|
msize = ship_maxsize(sh);
|
|
|
|
if (sh->size >= msize && !sh->damage) {
|
2014-12-22 16:28:17 +01:00
|
|
|
cmistake(u, u->thisorder, 16, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
2019-10-21 20:04:32 +02:00
|
|
|
cons = sh->type->construction;
|
2019-02-02 20:36:23 +01:00
|
|
|
if (effskill(u, cons->skill, NULL) < cons->minskill) {
|
2014-12-22 16:28:17 +01:00
|
|
|
ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder,
|
|
|
|
"error_build_skill_low", "value", cons->minskill));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
msize = maxbuild(u, cons);
|
|
|
|
if (msize == 0) {
|
|
|
|
cmistake(u, u->thisorder, 88, MSG_PRODUCE);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 17:00:16 +01:00
|
|
|
if (want <= 0 || want > msize) {
|
2014-12-22 16:28:17 +01:00
|
|
|
want = msize;
|
2017-12-29 17:00:16 +01:00
|
|
|
}
|
2014-12-22 16:28:17 +01:00
|
|
|
|
|
|
|
build_ship(u, sh, want);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2015-10-13 22:56:16 +02:00
|
|
|
void free_construction(struct construction *cons)
|
|
|
|
{
|
2018-05-01 15:32:06 +02:00
|
|
|
free(cons->materials);
|
|
|
|
free(cons);
|
2015-10-13 22:56:16 +02:00
|
|
|
}
|