2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * buil.c: Build ships, nukes, bridges, planes, land units, bridge towers
29 * Known contributors to this file:
30 * Steve McClure, 1998-2000
31 * Markus Armbruster, 2004-2014
52 static int build_ship(struct sctstr *sp, int type, int tlev);
53 static int build_land(struct sctstr *sp, int type, int tlev);
54 static int build_nuke(struct sctstr *sp, int type, int tlev);
55 static int build_plane(struct sctstr *sp, int type, int tlev);
56 static int pick_unused_unit_uid(int);
57 static int build_bridge(char);
58 static int build_bspan(struct sctstr *sp);
59 static int build_btower(struct sctstr *sp);
60 static int sector_can_build(struct sctstr *, short[], int, int, char *);
61 static void build_charge(struct sctstr *, short[], int, double, int);
62 static int build_can_afford(double, int, char *);
65 * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
70 struct natstr *natp = getnatp(player->cnum);
71 int tlev = (int)natp->nat_level[NAT_TLEV];
73 struct nstr_sect nstr;
74 int rqtech, type, number, val, gotsect;
75 char *p, *what, *prompt;
76 int (*build_it)(struct sctstr *, int, int);
79 p = getstarg(player->argp[1],
80 "Build (ship, nuke, bridge, plane, land unit, tower)? ",
87 return build_bridge(*p);
90 prompt = "Ship type? ";
91 build_it = build_ship;
95 prompt = "Plane type? ";
96 build_it = build_plane;
100 prompt = "Land unit type? ";
101 build_it = build_land;
104 if (!ef_nelem(EF_NUKE_CHR)) {
105 pr("There are no nukes in this game.\n");
108 if (drnuke_const > MIN_DRNUKE_CONST)
110 (int)(natp->nat_level[NAT_RLEV] / drnuke_const));
112 prompt = "Nuke type? ";
113 build_it = build_nuke;
116 pr("You can't build that!\n");
120 if (!snxtsct(&nstr, player->argp[2]))
123 p = getstarg(player->argp[3], prompt, buf);
130 type = ef_elt_byname(EF_PLANE_CHR, p);
132 rqtech = plchr[type].pl_tech;
135 type = ef_elt_byname(EF_SHIP_CHR, p);
137 rqtech = mchr[type].m_tech;
140 type = ef_elt_byname(EF_LAND_CHR, p);
142 rqtech = lchr[type].l_tech;
145 type = ef_elt_byname(EF_NUKE_CHR, p);
147 rqtech = nchr[type].n_tech;
154 if (type < 0 || tlev < rqtech) {
155 pr("You can't build that!\n");
156 pr("Use `show %s build %d' to show types you can build.\n",
162 if (player->argp[4]) {
163 number = atoi(player->argp[4]);
167 "Are you sure that you want to build %d of them? ",
169 p = getstarg(player->argp[6], bstr, buf);
175 if (player->argp[5]) {
176 val = atoi(player->argp[5]);
177 if (val > tlev && !player->god) {
178 pr("Your%s tech level is only %d.\n",
179 *what == 'n' && drnuke_const > MIN_DRNUKE_CONST
180 ? " effective" : "", tlev);
184 pr("Required tech is %d.\n", rqtech);
188 pr("Building with tech level %d.\n", tlev);
192 while (number-- > 0) {
193 while (nxtsct(&nstr, §)) {
197 if (build_it(§, type, tlev))
200 snxtsct_rewind(&nstr);
208 build_ship(struct sctstr *sp, int type, int tlev)
210 struct mchrstr *mp = &mchr[type];
215 memset(mat, 0, sizeof(mat));
216 mat[I_LCM] = mp->m_lcm;
217 mat[I_HCM] = mp->m_hcm;
218 work = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
220 if (sp->sct_type != SCT_HARBR) {
221 pr("Ships must be built in harbours.\n");
224 if (!sector_can_build(sp, mat, work, SHIP_MINEFF, mp->m_name))
226 if (!build_can_afford(mp->m_cost, SHIP_MINEFF, mp->m_name))
228 build_charge(sp, mat, work, mp->m_cost, SHIP_MINEFF);
230 ef_blank(EF_SHIP, pick_unused_unit_uid(EF_SHIP), &ship);
231 ship.shp_x = sp->sct_x;
232 ship.shp_y = sp->sct_y;
233 ship.shp_own = sp->sct_own;
234 ship.shp_type = mp - mchr;
235 ship.shp_effic = SHIP_MINEFF;
236 if (opt_MOB_ACCESS) {
237 game_tick_to_now(&ship.shp_access);
238 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
242 memset(ship.shp_item, 0, sizeof(ship.shp_item));
243 ship.shp_pstage = PLG_HEALTHY;
245 ship.shp_name[0] = 0;
246 ship.shp_orig_own = sp->sct_own;
247 ship.shp_orig_x = sp->sct_x;
248 ship.shp_orig_y = sp->sct_y;
249 shp_set_tech(&ship, tlev);
250 unit_wipe_orders((struct empobj *)&ship);
252 if (sp->sct_pstage == PLG_INFECT)
253 ship.shp_pstage = PLG_EXPOSED;
254 putship(ship.shp_uid, &ship);
255 pr("%s", prship(&ship));
256 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
261 build_land(struct sctstr *sp, int type, int tlev)
263 struct lchrstr *lp = &lchr[type];
268 memset(mat, 0, sizeof(mat));
269 mat[I_LCM] = lp->l_lcm;
270 mat[I_HCM] = lp->l_hcm;
271 work = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
273 if (sp->sct_type != SCT_HEADQ) {
274 pr("Land units must be built in headquarters.\n");
277 if (!sector_can_build(sp, mat, work, LAND_MINEFF, lp->l_name))
279 if (!build_can_afford(lp->l_cost, LAND_MINEFF, lp->l_name))
281 build_charge(sp, mat, work, lp->l_cost, LAND_MINEFF);
283 ef_blank(EF_LAND, pick_unused_unit_uid(EF_LAND), &land);
284 land.lnd_x = sp->sct_x;
285 land.lnd_y = sp->sct_y;
286 land.lnd_own = sp->sct_own;
287 land.lnd_type = lp - lchr;
288 land.lnd_effic = LAND_MINEFF;
289 if (opt_MOB_ACCESS) {
290 game_tick_to_now(&land.lnd_access);
291 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
298 memset(land.lnd_item, 0, sizeof(land.lnd_item));
299 land.lnd_pstage = PLG_HEALTHY;
301 lnd_set_tech(&land, tlev);
302 unit_wipe_orders((struct empobj *)&land);
304 if (sp->sct_pstage == PLG_INFECT)
305 land.lnd_pstage = PLG_EXPOSED;
306 putland(land.lnd_uid, &land);
307 pr("%s", prland(&land));
308 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
313 build_nuke(struct sctstr *sp, int type, int tlev)
315 struct nchrstr *np = &nchr[type];
320 if (sp->sct_type != SCT_NUKE && !player->god) {
321 pr("Nuclear weapons must be built in nuclear plants.\n");
325 * XXX when nukes turn into units (or whatever), then
326 * make them start at 20%. Since they don't have efficiency
327 * now, we charge all the work right away.
329 memset(mat, 0, sizeof(mat));
330 mat[I_LCM] = np->n_lcm;
331 mat[I_HCM] = np->n_hcm;
332 mat[I_OIL] = np->n_oil;
333 mat[I_RAD] = np->n_rad;
334 work = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
336 if (!sector_can_build(sp, mat, work, 100, np->n_name))
338 if (!build_can_afford(np->n_cost, 100, np->n_name))
340 build_charge(sp, mat, work, np->n_cost, 100);
342 ef_blank(EF_NUKE, pick_unused_unit_uid(EF_NUKE), &nuke);
343 nuke.nuk_x = sp->sct_x;
344 nuke.nuk_y = sp->sct_y;
345 nuke.nuk_own = sp->sct_own;
346 nuke.nuk_type = np - nchr;
347 nuke.nuk_effic = 100;
349 nuke.nuk_tech = tlev;
350 unit_wipe_orders((struct empobj *)&nuke);
352 putnuke(nuke.nuk_uid, &nuke);
353 pr("%s created in %s\n", prnuke(&nuke),
354 xyas(sp->sct_x, sp->sct_y, player->cnum));
359 build_plane(struct sctstr *sp, int type, int tlev)
361 struct plchrstr *pp = &plchr[type];
366 memset(mat, 0, sizeof(mat));
367 mat[I_MILIT] = pp->pl_crew;
368 mat[I_LCM] = pp->pl_lcm;
369 mat[I_HCM] = pp->pl_hcm;
370 work = PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm);
372 if (sp->sct_type != SCT_AIRPT && !player->god) {
373 pr("Planes must be built in airports.\n");
376 if (!sector_can_build(sp, mat, work, PLANE_MINEFF, pp->pl_name))
378 if (!build_can_afford(pp->pl_cost, PLANE_MINEFF, pp->pl_name))
380 build_charge(sp, mat, work, pp->pl_cost, PLANE_MINEFF);
382 ef_blank(EF_PLANE, pick_unused_unit_uid(EF_PLANE), &plane);
383 plane.pln_x = sp->sct_x;
384 plane.pln_y = sp->sct_y;
385 plane.pln_own = sp->sct_own;
386 plane.pln_type = pp - plchr;
387 plane.pln_effic = PLANE_MINEFF;
388 if (opt_MOB_ACCESS) {
389 game_tick_to_now(&plane.pln_access);
390 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
394 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
397 plane.pln_harden = 0;
399 pln_set_tech(&plane, tlev);
400 unit_wipe_orders((struct empobj *)&plane);
402 putplane(plane.pln_uid, &plane);
403 pr("%s built in sector %s\n", prplane(&plane),
404 xyas(sp->sct_x, sp->sct_y, player->cnum));
409 pick_unused_unit_uid(int type)
411 struct nstr_item nstr;
412 union empobj_storage unit;
414 snxtitem_all(&nstr, type);
415 while (nxtitem(&nstr, &unit)) {
424 build_bridge(char what)
426 struct natstr *natp = getnatp(player->cnum);
427 struct nstr_sect nstr;
428 int (*build_it)(struct sctstr *);
434 if (natp->nat_level[NAT_TLEV] < buil_bt) {
435 pr("Building a span requires a tech of %.0f\n", buil_bt);
438 build_it = build_bspan;
441 if (!opt_BRIDGETOWERS) {
442 pr("Bridge tower building is disabled.\n");
445 if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
446 pr("Building a tower requires a tech of %.0f\n",
450 build_it = build_btower;
457 if (!snxtsct(&nstr, player->argp[2]))
460 while (nxtsct(&nstr, §)) {
473 build_bspan(struct sctstr *sp)
475 short *vec = sp->sct_item;
480 int nx, ny, i, good = 0;
484 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
485 if (sp->sct_type != SCT_BTOWER) {
486 if (sp->sct_type != SCT_BHEAD)
488 if (sp->sct_newtype != SCT_BHEAD)
493 if (sp->sct_effic < 60 && !player->god) {
494 pr("Sector %s is not 60%% efficient.\n",
495 xyas(sp->sct_x, sp->sct_y, player->cnum));
499 if (vec[I_HCM] < buil_bh) {
500 pr("%s only has %d unit%s of hcm,\n",
501 xyas(sp->sct_x, sp->sct_y, player->cnum),
502 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
503 pr("(a bridge span requires %d)\n", buil_bh);
507 if (!build_can_afford(buil_bc, 100, dchr[SCT_BSPAN].d_name))
509 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
510 if (sp->sct_avail < avail) {
511 pr("Not enough available work in %s to build a bridge\n",
512 xyas(sp->sct_x, sp->sct_y, player->cnum));
513 pr(" (%d available work required)\n", avail);
516 if (!player->argp[3]) {
517 pr("Bridge head at %s\n",
518 xyas(sp->sct_x, sp->sct_y, player->cnum));
519 nav_map(sp->sct_x, sp->sct_y, 1);
521 p = getstarg(player->argp[3], "build span in what direction? ", buf);
525 /* Sanity check time */
526 if (!check_sect_ok(sp))
529 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
530 pr("'%c' is not a valid direction...\n", *p);
531 direrr(NULL, NULL, NULL);
534 newx = sp->sct_x + diroff[val][0];
535 newy = sp->sct_y + diroff[val][1];
536 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
537 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
540 if (opt_EASY_BRIDGES) {
543 for (i = 1; i <= 6; i++) {
545 nx = sect.sct_x + diroff[i][0];
546 ny = sect.sct_y + diroff[i][1];
547 getsect(nx, ny, &s2);
548 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
552 pr("Bridges must be built adjacent to land or bridge towers.\n");
553 pr("That sector is not adjacent to land or a bridge tower.\n");
556 } /* end EASY_BRIDGES */
557 sp->sct_avail -= avail;
558 player->dolcost += buil_bc;
559 sect.sct_type = SCT_BSPAN;
560 sect.sct_newtype = SCT_BSPAN;
561 sect.sct_effic = SCT_MINEFF;
564 sect.sct_defense = 0;
565 if (opt_MOB_ACCESS) {
566 game_tick_to_now(§.sct_access);
567 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
572 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
573 writemap(player->cnum);
575 pr("Bridge span built over %s\n",
576 xyas(sect.sct_x, sect.sct_y, player->cnum));
577 vec[I_HCM] -= buil_bh;
582 build_btower(struct sctstr *sp)
584 short *vec = sp->sct_item;
595 if (sp->sct_type != SCT_BSPAN) {
596 pr("Bridge towers can only be built from bridge spans.\n");
600 if (sp->sct_effic < 60 && !player->god) {
601 pr("Sector %s is not 60%% efficient.\n",
602 xyas(sp->sct_x, sp->sct_y, player->cnum));
606 if (vec[I_HCM] < buil_tower_bh) {
607 pr("%s only has %d unit%s of hcm,\n",
608 xyas(sp->sct_x, sp->sct_y, player->cnum),
609 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
610 pr("(a bridge tower requires %d)\n", buil_tower_bh);
614 if (!build_can_afford(buil_tower_bc, 100, dchr[SCT_BTOWER].d_name))
616 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
617 if (sp->sct_avail < avail) {
618 pr("Not enough available work in %s to build a bridge tower\n",
619 xyas(sp->sct_x, sp->sct_y, player->cnum));
620 pr(" (%d available work required)\n", avail);
623 if (!player->argp[3]) {
624 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
625 nav_map(sp->sct_x, sp->sct_y, 1);
627 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
631 /* Sanity check time */
632 if (!check_sect_ok(sp))
635 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
636 pr("'%c' is not a valid direction...\n", *p);
637 direrr(NULL, NULL, NULL);
640 newx = sp->sct_x + diroff[val][0];
641 newy = sp->sct_y + diroff[val][1];
642 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
643 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
647 /* Now, check. You aren't allowed to build bridge towers
649 for (i = 1; i <= 6; i++) {
651 nx = sect.sct_x + diroff[i][0];
652 ny = sect.sct_y + diroff[i][1];
653 getsect(nx, ny, &s2);
654 if ((s2.sct_type != SCT_WATER) &&
655 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
656 pr("Bridge towers cannot be built adjacent to land.\n");
657 pr("That sector is adjacent to land.\n");
662 sp->sct_avail -= avail;
663 player->dolcost += buil_tower_bc;
664 sect.sct_type = SCT_BTOWER;
665 sect.sct_newtype = SCT_BTOWER;
666 sect.sct_effic = SCT_MINEFF;
669 sect.sct_defense = 0;
670 if (opt_MOB_ACCESS) {
671 game_tick_to_now(§.sct_access);
672 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
677 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
678 writemap(player->cnum);
680 pr("Bridge tower built in %s\n",
681 xyas(sect.sct_x, sect.sct_y, player->cnum));
682 vec[I_HCM] -= buil_tower_bh;
687 sector_can_build(struct sctstr *sp, short mat[], int work,
688 int effic, char *what)
693 if (sp->sct_effic < 60 && !player->god) {
694 pr("Sector %s is not 60%% efficient.\n",
695 xyas(sp->sct_x, sp->sct_y, player->cnum));
699 avail = (work * effic + 99) / 100;
700 if (sp->sct_avail < avail) {
701 pr("Not enough available work in %s to build a %s\n",
702 xyas(sp->sct_x, sp->sct_y, player->cnum), what);
703 pr(" (%d available work required)\n", avail);
708 for (i = I_NONE + 1; i <= I_MAX; i++) {
709 needed = mat[i] * (effic / 100.0);
710 if (sp->sct_item[i] < needed) {
711 pr("Not enough %s in %s (need %g more)\n",
712 ichr[i].i_name, xyas(sp->sct_x, sp->sct_y, player->cnum),
713 ceil(needed - sp->sct_item[i]));
716 mat[i] = roundavg(needed);
723 build_charge(struct sctstr *sp,
724 short mat[], int work, double cost, int effic)
728 for (i = I_NONE + 1; i <= I_MAX; i++)
729 sp->sct_item[i] -= mat[i];
730 sp->sct_avail -= (work * effic + 99) / 100;
731 player->dolcost += cost * effic / 100.0;
735 build_can_afford(double cost, int effic, char *what)
737 struct natstr *natp = getnatp(player->cnum);
739 if (natp->nat_money < player->dolcost + cost * effic / 100.0) {
740 pr("Not enough money left to build a %s\n", what);