/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure, Markus Armbruster
*
* Empire is free software: you can redistribute it and/or modify
*
* Known contributors to this file:
* Steve McClure, 1998-2000
- * Markus Armbruster, 2004-2014
+ * Markus Armbruster, 2004-2015
*/
#include <config.h>
#include <limits.h>
+#include <math.h>
#include "chance.h"
#include "commands.h"
#include "game.h"
static int build_bridge(char);
static int build_bspan(struct sctstr *sp);
static int build_btower(struct sctstr *sp);
-static void build_material_use(short[], short[], int);
static int sector_can_build(struct sctstr *, short[], int, int, char *);
static void build_charge(struct sctstr *, short[], int, double, int);
static int build_can_afford(double, int, char *);
return RET_FAIL;
}
+ if (tlev < rqtech && player->god)
+ tlev = rqtech;
if (type < 0 || tlev < rqtech) {
pr("You can't build that!\n");
pr("Use `show %s build %d' to show types you can build.\n",
build_ship(struct sctstr *sp, int type, int tlev)
{
struct mchrstr *mp = &mchr[type];
- short mat[I_MAX+1], mat_100[I_MAX+1];
+ short mat[I_MAX+1];
int work;
struct shpstr ship;
- memset(mat_100, 0, sizeof(mat_100));
- mat_100[I_LCM] = mp->m_lcm;
- mat_100[I_HCM] = mp->m_hcm;
- build_material_use(mat, mat_100, SHIP_MINEFF);
+ memset(mat, 0, sizeof(mat));
+ mat[I_LCM] = mp->m_lcm;
+ mat[I_HCM] = mp->m_hcm;
work = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
- if (sp->sct_type != SCT_HARBR) {
+ if (sp->sct_type != SCT_HARBR && !player->god) {
pr("Ships must be built in harbours.\n");
return 0;
}
build_land(struct sctstr *sp, int type, int tlev)
{
struct lchrstr *lp = &lchr[type];
- short mat[I_MAX+1], mat_100[I_MAX+1];
+ short mat[I_MAX+1];
int work;
struct lndstr land;
- memset(mat_100, 0, sizeof(mat_100));
- mat_100[I_LCM] = lp->l_lcm;
- mat_100[I_HCM] = lp->l_hcm;
- build_material_use(mat, mat_100, LAND_MINEFF);
+ memset(mat, 0, sizeof(mat));
+ mat[I_LCM] = lp->l_lcm;
+ mat[I_HCM] = lp->l_hcm;
work = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
- if (sp->sct_type != SCT_HEADQ) {
+ if (sp->sct_type != SCT_HEADQ && !player->god) {
pr("Land units must be built in headquarters.\n");
return 0;
}
static int
build_nuke(struct sctstr *sp, int type, int tlev)
{
- short *vec = sp->sct_item;
struct nchrstr *np = &nchr[type];
+ short mat[I_MAX+1];
+ int work;
struct nukstr nuke;
- int avail;
if (sp->sct_type != SCT_NUKE && !player->god) {
pr("Nuclear weapons must be built in nuclear plants.\n");
return 0;
}
- if (sp->sct_effic < 60 && !player->god) {
- pr("Sector %s is not 60%% efficient.\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum));
- return 0;
- }
- if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
- vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
- pr("Not enough materials for a %s bomb in %s\n",
- np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
- pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
- np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
- return 0;
- }
- if (!build_can_afford(np->n_cost, 100, np->n_name))
- return 0;
- avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
/*
* XXX when nukes turn into units (or whatever), then
* make them start at 20%. Since they don't have efficiency
* now, we charge all the work right away.
*/
- if (sp->sct_avail < avail) {
- pr("Not enough available work in %s to build a %s;\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
- pr(" (%d available work required)\n", avail);
+ memset(mat, 0, sizeof(mat));
+ mat[I_LCM] = np->n_lcm;
+ mat[I_HCM] = np->n_hcm;
+ mat[I_OIL] = np->n_oil;
+ mat[I_RAD] = np->n_rad;
+ work = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
+
+ if (!sector_can_build(sp, mat, work, 100, np->n_name))
return 0;
- }
- sp->sct_avail -= avail;
- player->dolcost += np->n_cost;
+ if (!build_can_afford(np->n_cost, 100, np->n_name))
+ return 0;
+ build_charge(sp, mat, work, np->n_cost, 100);
ef_blank(EF_NUKE, pick_unused_unit_uid(EF_NUKE), &nuke);
nuke.nuk_x = sp->sct_x;
nuke.nuk_tech = tlev;
unit_wipe_orders((struct empobj *)&nuke);
- vec[I_HCM] -= np->n_hcm;
- vec[I_LCM] -= np->n_lcm;
- vec[I_OIL] -= np->n_oil;
- vec[I_RAD] -= np->n_rad;
-
putnuke(nuke.nuk_uid, &nuke);
pr("%s created in %s\n", prnuke(&nuke),
xyas(sp->sct_x, sp->sct_y, player->cnum));
build_plane(struct sctstr *sp, int type, int tlev)
{
struct plchrstr *pp = &plchr[type];
- short mat[I_MAX+1], mat_100[I_MAX+1];
+ short mat[I_MAX+1];
int work;
struct plnstr plane;
- double eff = PLANE_MINEFF / 100.0;
- int mil;
-
- mil = roundavg(pp->pl_crew * eff);
- /* Always use at least 1 mil to build a plane */
- if (mil == 0 && pp->pl_crew > 0)
- mil = 1;
- memset(mat_100, 0, sizeof(mat_100));
- mat_100[I_LCM] = pp->pl_lcm;
- mat_100[I_HCM] = pp->pl_hcm;
- build_material_use(mat, mat_100, PLANE_MINEFF);
+
+ memset(mat, 0, sizeof(mat));
+ mat[I_MILIT] = pp->pl_crew;
+ mat[I_LCM] = pp->pl_lcm;
+ mat[I_HCM] = pp->pl_hcm;
work = PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm);
if (sp->sct_type != SCT_AIRPT && !player->god) {
return 0;
if (!build_can_afford(pp->pl_cost, PLANE_MINEFF, pp->pl_name))
return 0;
- if (sp->sct_item[I_MILIT] < mil) {
- pr("Not enough military for crew in %s\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum));
- return 0;
- }
- sp->sct_item[I_MILIT] -= mil;
build_charge(sp, mat, work, pp->pl_cost, PLANE_MINEFF);
ef_blank(EF_PLANE, pick_unused_unit_uid(EF_PLANE), &plane);
switch (what) {
case 'b':
- if (natp->nat_level[NAT_TLEV] < buil_bt) {
+ if (natp->nat_level[NAT_TLEV] < buil_bt && !player->god) {
pr("Building a span requires a tech of %.0f\n", buil_bt);
return RET_FAIL;
}
pr("Bridge tower building is disabled.\n");
return RET_FAIL;
}
- if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
+ if (natp->nat_level[NAT_TLEV] < buil_tower_bt && !player->god) {
pr("Building a tower requires a tech of %.0f\n",
buil_tower_bt);
return RET_FAIL;
static int
build_bspan(struct sctstr *sp)
{
- short *vec = sp->sct_item;
struct sctstr sect;
+ short mat[I_MAX+1];
+ int work;
int val;
int newx, newy;
- int avail;
- int nx, ny, i, good = 0;
char *p;
char buf[1024];
- if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
+ if (!opt_EASY_BRIDGES && !player->god) {
+ /* must have a bridge head or tower */
if (sp->sct_type != SCT_BTOWER) {
if (sp->sct_type != SCT_BHEAD)
return 0;
}
}
- if (sp->sct_effic < 60 && !player->god) {
- pr("Sector %s is not 60%% efficient.\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum));
- return 0;
- }
+ memset(mat, 0, sizeof(mat));
+ mat[I_HCM] = buil_bh;
+ work = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
- if (vec[I_HCM] < buil_bh) {
- pr("%s only has %d unit%s of hcm,\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum),
- vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
- pr("(a bridge span requires %d)\n", buil_bh);
+ if (!sector_can_build(sp, mat, work, 100, dchr[SCT_BSPAN].d_name))
return 0;
- }
-
if (!build_can_afford(buil_bc, 100, dchr[SCT_BSPAN].d_name))
return 0;
- avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
- if (sp->sct_avail < avail) {
- pr("Not enough available work in %s to build a bridge\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum));
- pr(" (%d available work required)\n", avail);
- return 0;
- }
if (!player->argp[3]) {
pr("Bridge head at %s\n",
xyas(sp->sct_x, sp->sct_y, player->cnum));
if (!p || !*p) {
return 0;
}
- /* Sanity check time */
if (!check_sect_ok(sp))
return 0;
pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
return 0;
}
- if (opt_EASY_BRIDGES) {
- good = 0;
-
- for (i = 1; i <= 6; i++) {
- struct sctstr s2;
- nx = sect.sct_x + diroff[i][0];
- ny = sect.sct_y + diroff[i][1];
- getsect(nx, ny, &s2);
- if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
- good = 1;
- }
- if (!good) {
- pr("Bridges must be built adjacent to land or bridge towers.\n");
- pr("That sector is not adjacent to land or a bridge tower.\n");
- return 0;
+ if (!bridge_support_at(§, DIR_STOP)) {
+ if (opt_EASY_BRIDGES) {
+ pr("%s is not next to land or a bridge tower",
+ xyas(newx, newy, player->cnum));
+ } else {
+ /*
+ * Note: because players need a 60% bridge head or tower,
+ * we can get here only for a deity.
+ */
+ pr("%s is not next to a supporting bridge head or tower\n",
+ xyas(newx, newy, player->cnum));
}
- } /* end EASY_BRIDGES */
- sp->sct_avail -= avail;
- player->dolcost += buil_bc;
+ return 0;
+ }
+ build_charge(sp, mat, work, buil_bc, 100);
+
sect.sct_type = SCT_BSPAN;
sect.sct_newtype = SCT_BSPAN;
sect.sct_effic = SCT_MINEFF;
putsect(§);
pr("Bridge span built over %s\n",
xyas(sect.sct_x, sect.sct_y, player->cnum));
- vec[I_HCM] -= buil_bh;
return 1;
}
static int
build_btower(struct sctstr *sp)
{
- short *vec = sp->sct_item;
struct sctstr sect;
+ short mat[I_MAX+1];
+ int work;
int val;
int newx, newy;
- int avail;
char *p;
char buf[1024];
int i;
int nx;
int ny;
- if (sp->sct_type != SCT_BSPAN) {
+ if (sp->sct_type != SCT_BSPAN && !player->god) {
pr("Bridge towers can only be built from bridge spans.\n");
return 0;
}
- if (sp->sct_effic < 60 && !player->god) {
- pr("Sector %s is not 60%% efficient.\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum));
- return 0;
- }
+ memset(mat, 0, sizeof(mat));
+ mat[I_HCM] = buil_tower_bh;
+ work = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
- if (vec[I_HCM] < buil_tower_bh) {
- pr("%s only has %d unit%s of hcm,\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum),
- vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
- pr("(a bridge tower requires %d)\n", buil_tower_bh);
+ if (!sector_can_build(sp, mat, work, 100, dchr[SCT_BTOWER].d_name))
return 0;
- }
-
if (!build_can_afford(buil_tower_bc, 100, dchr[SCT_BTOWER].d_name))
return 0;
- avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
- if (sp->sct_avail < avail) {
- pr("Not enough available work in %s to build a bridge tower\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum));
- pr(" (%d available work required)\n", avail);
- return 0;
- }
if (!player->argp[3]) {
pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
nav_map(sp->sct_x, sp->sct_y, 1);
if (!p || !*p) {
return 0;
}
- /* Sanity check time */
if (!check_sect_ok(sp))
return 0;
getsect(nx, ny, &s2);
if ((s2.sct_type != SCT_WATER) &&
(s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
- pr("Bridge towers cannot be built adjacent to land.\n");
- pr("That sector is adjacent to land.\n");
+ pr("%s is next to land, can't build bridge tower there",
+ xyas(newx, newy, player->cnum));
return 0;
}
}
- sp->sct_avail -= avail;
- player->dolcost += buil_tower_bc;
+ build_charge(sp, mat, work, buil_tower_bc, 100);
+
sect.sct_type = SCT_BTOWER;
sect.sct_newtype = SCT_BTOWER;
sect.sct_effic = SCT_MINEFF;
putsect(§);
pr("Bridge tower built in %s\n",
xyas(sect.sct_x, sect.sct_y, player->cnum));
- vec[I_HCM] -= buil_tower_bh;
return 1;
}
-static void
-build_material_use(short mat[], short mat_100[], int effic)
-{
- int i;
-
- for (i = I_MAX; i > I_NONE; i--) {
- mat[i] = mat_100[i] ? roundavg(mat_100[i] * (effic / 100.0)) : 0;
- }
-}
-
static int
sector_can_build(struct sctstr *sp, short mat[], int work,
int effic, char *what)
{
- int i, avail;
+ int i, avail, ret, req;
+ double used;
+
+ if (player->god)
+ return 1; /* Deity builds ex nihilo */
if (sp->sct_effic < 60 && !player->god) {
pr("Sector %s is not 60%% efficient.\n",
return 0;
}
- for (i = I_NONE + 1; i <= I_MAX; i++) {
- if (sp->sct_item[i] < mat[i]) {
- pr("Not enough materials in %s\n",
- xyas(sp->sct_x, sp->sct_y, player->cnum));
- return 0;
- }
- }
-
avail = (work * effic + 99) / 100;
if (sp->sct_avail < avail) {
pr("Not enough available work in %s to build a %s\n",
return 0;
}
- return 1;
+ ret = 1;
+ for (i = I_NONE + 1; i <= I_MAX; i++) {
+ used = mat[i] * effic;
+ req = (used + 99) / 100;
+ if (sp->sct_item[i] < req) {
+ pr("Not enough %s in %s (need %d more)\n",
+ ichr[i].i_name, xyas(sp->sct_x, sp->sct_y, player->cnum),
+ req - sp->sct_item[i]);
+ ret = 0;
+ }
+ mat[i] = roundavg(used / 100.0);
+ }
+
+ return ret;
}
static void
{
int i;
+ if (player->god)
+ return; /* Deity builds ex nihilo */
+
for (i = I_NONE + 1; i <= I_MAX; i++)
sp->sct_item[i] -= mat[i];
sp->sct_avail -= (work * effic + 99) / 100;