2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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-2013
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 build_can_afford(double, char *);
63 * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
68 struct natstr *natp = getnatp(player->cnum);
69 int tlev = (int)natp->nat_level[NAT_TLEV];
71 struct nstr_sect nstr;
72 int rqtech, type, number, val, gotsect;
73 char *p, *what, *prompt;
74 int (*build_it)(struct sctstr *, int, int);
77 p = getstarg(player->argp[1],
78 "Build (ship, nuke, bridge, plane, land unit, tower)? ",
85 return build_bridge(*p);
88 prompt = "Ship type? ";
89 build_it = build_ship;
93 prompt = "Plane type? ";
94 build_it = build_plane;
98 prompt = "Land unit type? ";
99 build_it = build_land;
102 if (!ef_nelem(EF_NUKE_CHR)) {
103 pr("There are no nukes in this game.\n");
106 if (drnuke_const > MIN_DRNUKE_CONST)
108 (int)(natp->nat_level[NAT_RLEV] / drnuke_const));
110 prompt = "Nuke type? ";
111 build_it = build_nuke;
114 pr("You can't build that!\n");
118 if (!snxtsct(&nstr, player->argp[2]))
121 p = getstarg(player->argp[3], prompt, buf);
128 type = ef_elt_byname(EF_PLANE_CHR, p);
130 rqtech = plchr[type].pl_tech;
133 type = ef_elt_byname(EF_SHIP_CHR, p);
135 rqtech = mchr[type].m_tech;
138 type = ef_elt_byname(EF_LAND_CHR, p);
140 rqtech = lchr[type].l_tech;
143 type = ef_elt_byname(EF_NUKE_CHR, p);
145 rqtech = nchr[type].n_tech;
152 if (type < 0 || tlev < rqtech) {
153 pr("You can't build that!\n");
154 pr("Use `show %s build %d' to show types you can build.\n",
160 if (player->argp[4]) {
161 number = atoi(player->argp[4]);
165 "Are you sure that you want to build %d of them? ",
167 p = getstarg(player->argp[6], bstr, buf);
173 if (player->argp[5]) {
174 val = atoi(player->argp[5]);
175 if (val > tlev && !player->god) {
176 pr("Your%s tech level is only %d.\n",
177 *what == 'n' && drnuke_const > MIN_DRNUKE_CONST
178 ? " effective" : "", tlev);
182 pr("Required tech is %d.\n", rqtech);
186 pr("Building with tech level %d.\n", tlev);
190 while (number-- > 0) {
191 while (nxtsct(&nstr, §)) {
195 if (build_it(§, type, tlev))
198 snxtsct_rewind(&nstr);
206 build_ship(struct sctstr *sp, int type, int tlev)
208 short *vec = sp->sct_item;
209 struct mchrstr *mp = &mchr[type];
213 double eff = SHIP_MINEFF / 100.0;
216 hcm = roundavg(mp->m_hcm * eff);
217 lcm = roundavg(mp->m_lcm * eff);
219 if (sp->sct_type != SCT_HARBR) {
220 pr("Ships must be built in harbours.\n");
223 if (sp->sct_effic < 60 && !player->god) {
224 pr("Sector %s is not 60%% efficient.\n",
225 xyas(sp->sct_x, sp->sct_y, player->cnum));
228 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
229 pr("Not enough materials in %s\n",
230 xyas(sp->sct_x, sp->sct_y, player->cnum));
233 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
234 if (sp->sct_avail < avail) {
235 pr("Not enough available work in %s to build a %s\n",
236 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
237 pr(" (%d available work required)\n", avail);
240 cost = mp->m_cost * SHIP_MINEFF / 100.0;
241 if (!build_can_afford(cost, mp->m_name))
243 if (!trechk(player->cnum, 0, NEWSHP))
245 if (!check_sect_ok(sp))
247 sp->sct_avail -= avail;
248 player->dolcost += cost;
249 ef_blank(EF_SHIP, pick_unused_unit_uid(EF_SHIP), &ship);
250 ship.shp_x = sp->sct_x;
251 ship.shp_y = sp->sct_y;
252 ship.shp_own = sp->sct_own;
253 ship.shp_type = mp - mchr;
254 ship.shp_effic = SHIP_MINEFF;
255 if (opt_MOB_ACCESS) {
256 game_tick_to_now(&ship.shp_access);
257 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
261 memset(ship.shp_item, 0, sizeof(ship.shp_item));
262 ship.shp_pstage = PLG_HEALTHY;
264 ship.shp_name[0] = 0;
265 ship.shp_orig_own = sp->sct_own;
266 ship.shp_orig_x = sp->sct_x;
267 ship.shp_orig_y = sp->sct_y;
268 shp_set_tech(&ship, tlev);
269 unit_wipe_orders((struct empobj *)&ship);
274 if (sp->sct_pstage == PLG_INFECT)
275 ship.shp_pstage = PLG_EXPOSED;
276 putship(ship.shp_uid, &ship);
277 pr("%s", prship(&ship));
278 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
283 build_land(struct sctstr *sp, int type, int tlev)
285 short *vec = sp->sct_item;
286 struct lchrstr *lp = &lchr[type];
290 double eff = LAND_MINEFF / 100.0;
291 int mil, lcm, hcm, gun, shell;
294 mil = roundavg(lp->l_mil * eff);
295 shell = roundavg(lp->l_shell * eff);
296 gun = roundavg(lp->l_gun * eff);
298 mil = shell = gun = 0;
300 hcm = roundavg(lp->l_hcm * eff);
301 lcm = roundavg(lp->l_lcm * eff);
303 if (sp->sct_type != SCT_HEADQ) {
304 pr("Land units must be built in headquarters.\n");
307 if (sp->sct_effic < 60 && !player->god) {
308 pr("Sector %s is not 60%% efficient.\n",
309 xyas(sp->sct_x, sp->sct_y, player->cnum));
312 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
313 pr("Not enough materials in %s\n",
314 xyas(sp->sct_x, sp->sct_y, player->cnum));
318 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
319 pr("Not enough guns in %s\n",
320 xyas(sp->sct_x, sp->sct_y, player->cnum));
323 if (vec[I_SHELL] < shell) {
324 pr("Not enough shells in %s\n",
325 xyas(sp->sct_x, sp->sct_y, player->cnum));
328 if (vec[I_MILIT] < mil) {
329 pr("Not enough military in %s\n",
330 xyas(sp->sct_x, sp->sct_y, player->cnum));
334 if (!trechk(player->cnum, 0, NEWLND))
336 if (!check_sect_ok(sp))
338 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
339 if (sp->sct_avail < avail) {
340 pr("Not enough available work in %s to build a %s\n",
341 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
342 pr(" (%d available work required)\n", avail);
345 cost = lp->l_cost * LAND_MINEFF / 100.0;
346 if (!build_can_afford(cost, lp->l_name))
348 sp->sct_avail -= avail;
349 player->dolcost += cost;
350 ef_blank(EF_LAND, pick_unused_unit_uid(EF_LAND), &land);
351 land.lnd_x = sp->sct_x;
352 land.lnd_y = sp->sct_y;
353 land.lnd_own = sp->sct_own;
354 land.lnd_type = lp - lchr;
355 land.lnd_effic = LAND_MINEFF;
356 if (opt_MOB_ACCESS) {
357 game_tick_to_now(&land.lnd_access);
358 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
365 memset(land.lnd_item, 0, sizeof(land.lnd_item));
366 land.lnd_pstage = PLG_HEALTHY;
368 lnd_set_tech(&land, tlev);
369 unit_wipe_orders((struct empobj *)&land);
375 vec[I_SHELL] -= shell;
377 if (sp->sct_pstage == PLG_INFECT)
378 land.lnd_pstage = PLG_EXPOSED;
379 putland(land.lnd_uid, &land);
380 pr("%s", prland(&land));
381 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
386 build_nuke(struct sctstr *sp, int type, int tlev)
388 short *vec = sp->sct_item;
389 struct nchrstr *np = &nchr[type];
393 if (sp->sct_type != SCT_NUKE && !player->god) {
394 pr("Nuclear weapons must be built in nuclear plants.\n");
397 if (sp->sct_effic < 60 && !player->god) {
398 pr("Sector %s is not 60%% efficient.\n",
399 xyas(sp->sct_x, sp->sct_y, player->cnum));
402 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
403 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
404 pr("Not enough materials for a %s bomb in %s\n",
405 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
406 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
407 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
410 if (!build_can_afford(np->n_cost, np->n_name))
412 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
414 * XXX when nukes turn into units (or whatever), then
415 * make them start at 20%. Since they don't have efficiency
416 * now, we charge all the work right away.
418 if (sp->sct_avail < avail) {
419 pr("Not enough available work in %s to build a %s;\n",
420 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
421 pr(" (%d available work required)\n", avail);
424 if (!trechk(player->cnum, 0, NEWNUK))
426 if (!check_sect_ok(sp))
428 sp->sct_avail -= avail;
429 player->dolcost += np->n_cost;
430 ef_blank(EF_NUKE, pick_unused_unit_uid(EF_NUKE), &nuke);
431 nuke.nuk_x = sp->sct_x;
432 nuke.nuk_y = sp->sct_y;
433 nuke.nuk_own = sp->sct_own;
434 nuke.nuk_type = np - nchr;
435 nuke.nuk_effic = 100;
437 nuke.nuk_tech = tlev;
438 unit_wipe_orders((struct empobj *)&nuke);
440 vec[I_HCM] -= np->n_hcm;
441 vec[I_LCM] -= np->n_lcm;
442 vec[I_OIL] -= np->n_oil;
443 vec[I_RAD] -= np->n_rad;
445 putnuke(nuke.nuk_uid, &nuke);
446 pr("%s created in %s\n", prnuke(&nuke),
447 xyas(sp->sct_x, sp->sct_y, player->cnum));
452 build_plane(struct sctstr *sp, int type, int tlev)
454 short *vec = sp->sct_item;
455 struct plchrstr *pp = &plchr[type];
459 double eff = PLANE_MINEFF / 100.0;
462 mil = roundavg(pp->pl_crew * eff);
463 /* Always use at least 1 mil to build a plane */
464 if (mil == 0 && pp->pl_crew > 0)
466 hcm = roundavg(pp->pl_hcm * eff);
467 lcm = roundavg(pp->pl_lcm * eff);
468 if (sp->sct_type != SCT_AIRPT && !player->god) {
469 pr("Planes must be built in airports.\n");
472 if (sp->sct_effic < 60 && !player->god) {
473 pr("Sector %s is not 60%% efficient.\n",
474 xyas(sp->sct_x, sp->sct_y, player->cnum));
477 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
478 pr("Not enough materials in %s\n",
479 xyas(sp->sct_x, sp->sct_y, player->cnum));
482 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
483 if (sp->sct_avail < avail) {
484 pr("Not enough available work in %s to build a %s\n",
485 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
486 pr(" (%d available work required)\n", avail);
489 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
490 if (!build_can_afford(cost, pp->pl_name))
492 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
493 pr("Not enough military for crew in %s\n",
494 xyas(sp->sct_x, sp->sct_y, player->cnum));
497 if (!trechk(player->cnum, 0, NEWPLN))
499 if (!check_sect_ok(sp))
501 sp->sct_avail -= avail;
502 player->dolcost += cost;
503 ef_blank(EF_PLANE, pick_unused_unit_uid(EF_PLANE), &plane);
504 plane.pln_x = sp->sct_x;
505 plane.pln_y = sp->sct_y;
506 plane.pln_own = sp->sct_own;
507 plane.pln_type = pp - plchr;
508 plane.pln_effic = PLANE_MINEFF;
509 if (opt_MOB_ACCESS) {
510 game_tick_to_now(&plane.pln_access);
511 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
515 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
518 plane.pln_harden = 0;
520 pln_set_tech(&plane, tlev);
521 unit_wipe_orders((struct empobj *)&plane);
527 putplane(plane.pln_uid, &plane);
528 pr("%s built in sector %s\n", prplane(&plane),
529 xyas(sp->sct_x, sp->sct_y, player->cnum));
534 pick_unused_unit_uid(int type)
536 struct nstr_item nstr;
537 union empobj_storage unit;
539 snxtitem_all(&nstr, type);
540 while (nxtitem(&nstr, &unit)) {
549 build_bridge(char what)
551 struct natstr *natp = getnatp(player->cnum);
552 struct nstr_sect nstr;
553 int (*build_it)(struct sctstr *);
559 if (natp->nat_level[NAT_TLEV] < buil_bt) {
560 pr("Building a span requires a tech of %.0f\n", buil_bt);
563 build_it = build_bspan;
566 if (!opt_BRIDGETOWERS) {
567 pr("Bridge tower building is disabled.\n");
570 if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
571 pr("Building a tower requires a tech of %.0f\n",
575 build_it = build_btower;
582 if (!snxtsct(&nstr, player->argp[2]))
585 while (nxtsct(&nstr, §)) {
598 build_bspan(struct sctstr *sp)
600 short *vec = sp->sct_item;
605 int nx, ny, i, good = 0;
609 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
610 if (sp->sct_type != SCT_BTOWER) {
611 if (sp->sct_type != SCT_BHEAD)
613 if (sp->sct_newtype != SCT_BHEAD)
618 if (sp->sct_effic < 60 && !player->god) {
619 pr("Sector %s is not 60%% efficient.\n",
620 xyas(sp->sct_x, sp->sct_y, player->cnum));
624 if (vec[I_HCM] < buil_bh) {
625 pr("%s only has %d unit%s of hcm,\n",
626 xyas(sp->sct_x, sp->sct_y, player->cnum),
627 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
628 pr("(a bridge span requires %d)\n", buil_bh);
632 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
634 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
635 if (sp->sct_avail < avail) {
636 pr("Not enough available work in %s to build a bridge\n",
637 xyas(sp->sct_x, sp->sct_y, player->cnum));
638 pr(" (%d available work required)\n", avail);
641 if (!player->argp[3]) {
642 pr("Bridge head at %s\n",
643 xyas(sp->sct_x, sp->sct_y, player->cnum));
644 nav_map(sp->sct_x, sp->sct_y, 1);
646 p = getstarg(player->argp[3], "build span in what direction? ", buf);
650 /* Sanity check time */
651 if (!check_sect_ok(sp))
654 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
655 pr("'%c' is not a valid direction...\n", *p);
656 direrr(NULL, NULL, NULL);
659 newx = sp->sct_x + diroff[val][0];
660 newy = sp->sct_y + diroff[val][1];
661 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
662 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
665 if (opt_EASY_BRIDGES) {
668 for (i = 1; i <= 6; i++) {
670 nx = sect.sct_x + diroff[i][0];
671 ny = sect.sct_y + diroff[i][1];
672 getsect(nx, ny, &s2);
673 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
677 pr("Bridges must be built adjacent to land or bridge towers.\n");
678 pr("That sector is not adjacent to land or a bridge tower.\n");
681 } /* end EASY_BRIDGES */
682 sp->sct_avail -= avail;
683 player->dolcost += buil_bc;
684 sect.sct_type = SCT_BSPAN;
685 sect.sct_newtype = SCT_BSPAN;
686 sect.sct_effic = SCT_MINEFF;
689 sect.sct_defense = 0;
690 if (opt_MOB_ACCESS) {
691 game_tick_to_now(§.sct_access);
692 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
697 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
698 writemap(player->cnum);
700 pr("Bridge span built over %s\n",
701 xyas(sect.sct_x, sect.sct_y, player->cnum));
702 vec[I_HCM] -= buil_bh;
707 build_btower(struct sctstr *sp)
709 short *vec = sp->sct_item;
720 if (sp->sct_type != SCT_BSPAN) {
721 pr("Bridge towers can only be built from bridge spans.\n");
725 if (sp->sct_effic < 60 && !player->god) {
726 pr("Sector %s is not 60%% efficient.\n",
727 xyas(sp->sct_x, sp->sct_y, player->cnum));
731 if (vec[I_HCM] < buil_tower_bh) {
732 pr("%s only has %d unit%s of hcm,\n",
733 xyas(sp->sct_x, sp->sct_y, player->cnum),
734 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
735 pr("(a bridge tower requires %d)\n", buil_tower_bh);
739 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
741 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
742 if (sp->sct_avail < avail) {
743 pr("Not enough available work in %s to build a bridge tower\n",
744 xyas(sp->sct_x, sp->sct_y, player->cnum));
745 pr(" (%d available work required)\n", avail);
748 if (!player->argp[3]) {
749 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
750 nav_map(sp->sct_x, sp->sct_y, 1);
752 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
756 /* Sanity check time */
757 if (!check_sect_ok(sp))
760 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
761 pr("'%c' is not a valid direction...\n", *p);
762 direrr(NULL, NULL, NULL);
765 newx = sp->sct_x + diroff[val][0];
766 newy = sp->sct_y + diroff[val][1];
767 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
768 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
772 /* Now, check. You aren't allowed to build bridge towers
774 for (i = 1; i <= 6; i++) {
776 nx = sect.sct_x + diroff[i][0];
777 ny = sect.sct_y + diroff[i][1];
778 getsect(nx, ny, &s2);
779 if ((s2.sct_type != SCT_WATER) &&
780 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
781 pr("Bridge towers cannot be built adjacent to land.\n");
782 pr("That sector is adjacent to land.\n");
787 sp->sct_avail -= avail;
788 player->dolcost += buil_tower_bc;
789 sect.sct_type = SCT_BTOWER;
790 sect.sct_newtype = SCT_BTOWER;
791 sect.sct_effic = SCT_MINEFF;
794 sect.sct_defense = 0;
795 if (opt_MOB_ACCESS) {
796 game_tick_to_now(§.sct_access);
797 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
802 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
803 writemap(player->cnum);
805 pr("Bridge tower built in %s\n",
806 xyas(sect.sct_x, sect.sct_y, player->cnum));
807 vec[I_HCM] -= buil_tower_bh;
812 build_can_afford(double cost, char *what)
814 struct natstr *natp = getnatp(player->cnum);
815 if (natp->nat_money < player->dolcost + cost) {
816 pr("Not enough money left to build a %s\n", what);