2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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-2011
51 static int build_ship(struct sctstr *sp, int type, short *vec, int tlev);
52 static int build_land(struct sctstr *sp, int type, short *vec, int tlev);
53 static int build_nuke(struct sctstr *sp, int type, short *vec, int tlev);
54 static int build_plane(struct sctstr *sp, int type, short *vec, int tlev);
55 static int build_bridge(char);
56 static int build_bspan(struct sctstr *sp, short *vec);
57 static int build_btower(struct sctstr *sp, short *vec);
58 static int build_can_afford(double, char *);
61 * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
67 struct nstr_sect nstr;
75 int (*build_it)(struct sctstr *, int, short[], int);
79 natp = getnatp(player->cnum);
80 tlev = (int)natp->nat_level[NAT_TLEV];
82 p = getstarg(player->argp[1],
83 "Build (ship, nuke, bridge, plane, land unit, tower)? ",
91 return build_bridge(what);
93 build_it = build_ship;
96 build_it = build_plane;
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));
109 build_it = build_nuke;
112 pr("You can't build that!\n");
116 if (!snxtsct(&nstr, player->argp[2]))
121 p = getstarg(player->argp[3], "Plane type? ", buf);
124 type = ef_elt_byname(EF_PLANE_CHR, p);
126 rqtech = plchr[type].pl_tech;
131 pr("You can't build that!\n");
132 pr("Use `show plane build %d' to show types you can build.\n",
138 p = getstarg(player->argp[3], "Ship type? ", buf);
141 type = ef_elt_byname(EF_SHIP_CHR, p);
143 rqtech = mchr[type].m_tech;
146 if ((mchr[type].m_flags & M_TRADE) && !opt_TRADESHIPS)
150 pr("You can't build that!\n");
151 pr("Use `show ship build %d' to show types you can build.\n",
157 p = getstarg(player->argp[3], "Land unit type? ", buf);
160 type = ef_elt_byname(EF_LAND_CHR, p);
162 rqtech = lchr[type].l_tech;
165 if ((lchr[type].l_flags & L_SPY) && !opt_LANDSPIES)
169 pr("You can't build that!\n");
170 pr("Use `show land build %d' to show types you can build.\n",
176 p = getstarg(player->argp[3], "Nuke type? ", buf);
179 type = ef_elt_byname(EF_NUKE_CHR, p);
181 rqtech = nchr[type].n_tech;
186 pr("You can't build that!\n");
187 pr("Use `show nuke build %d' to show types you can build.\n",
198 if (player->argp[4]) {
199 number = atoi(player->argp[4]);
203 "Are you sure that you want to build %s of them? ",
205 p = getstarg(player->argp[6], bstr, buf);
212 if (player->argp[5]) {
213 tlev = atoi(player->argp[5]);
214 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
215 pr("Your tech level is only %d.\n",
216 (int)natp->nat_level[NAT_TLEV]);
220 pr("Required tech is %d.\n", rqtech);
223 pr("building with tech level %d.\n", tlev);
227 while (number-- > 0) {
228 while (nxtsct(&nstr, §)) {
232 if (build_it(§, type, sect.sct_item, tlev))
235 snxtsct_rewind(&nstr);
238 pr("Bad sector specification.\n");
244 build_ship(struct sctstr *sp, int type, short *vec, int tlev)
246 struct mchrstr *mp = &mchr[type];
248 struct nstr_item nstr;
251 double eff = SHIP_MINEFF / 100.0;
255 hcm = roundavg(mp->m_hcm * eff);
256 lcm = roundavg(mp->m_lcm * eff);
258 if (sp->sct_type != SCT_HARBR) {
259 pr("Ships must be built in harbours.\n");
262 if (sp->sct_effic < 60 && !player->god) {
263 pr("Sector %s is not 60%% efficient.\n",
264 xyas(sp->sct_x, sp->sct_y, player->cnum));
267 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
268 pr("Not enough materials in %s\n",
269 xyas(sp->sct_x, sp->sct_y, player->cnum));
272 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
273 if (sp->sct_avail < avail) {
274 pr("Not enough available work in %s to build a %s\n",
275 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
276 pr(" (%d available work required)\n", avail);
279 cost = mp->m_cost * SHIP_MINEFF / 100.0;
280 if (!build_can_afford(cost, mp->m_name))
282 if (!trechk(player->cnum, 0, NEWSHP))
284 if (!check_sect_ok(sp))
286 sp->sct_avail -= avail;
287 player->dolcost += cost;
288 snxtitem_all(&nstr, EF_SHIP);
289 while (nxtitem(&nstr, &ship)) {
290 if (ship.shp_own == 0) {
296 ef_extend(EF_SHIP, 50);
298 ef_blank(EF_SHIP, nstr.cur, &ship);
299 ship.shp_x = sp->sct_x;
300 ship.shp_y = sp->sct_y;
301 ship.shp_own = player->cnum;
302 ship.shp_type = mp - mchr;
303 ship.shp_effic = SHIP_MINEFF;
304 if (opt_MOB_ACCESS) {
305 game_tick_to_now(&ship.shp_access);
306 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
310 memset(ship.shp_item, 0, sizeof(ship.shp_item));
311 ship.shp_pstage = PLG_HEALTHY;
313 ship.shp_name[0] = 0;
314 ship.shp_orig_own = player->cnum;
315 ship.shp_orig_x = sp->sct_x;
316 ship.shp_orig_y = sp->sct_y;
317 shp_set_tech(&ship, tlev);
318 unit_wipe_orders((struct empobj *)&ship);
323 if (sp->sct_pstage == PLG_INFECT)
324 ship.shp_pstage = PLG_EXPOSED;
325 putship(ship.shp_uid, &ship);
326 pr("%s", prship(&ship));
327 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
332 build_land(struct sctstr *sp, int type, short *vec, int tlev)
334 struct lchrstr *lp = &lchr[type];
336 struct nstr_item nstr;
339 double eff = LAND_MINEFF / 100.0;
340 int mil, lcm, hcm, gun, shell;
344 mil = roundavg(lp->l_mil * eff);
345 shell = roundavg(lp->l_shell * eff);
346 gun = roundavg(lp->l_gun * eff);
348 mil = shell = gun = 0;
350 hcm = roundavg(lp->l_hcm * eff);
351 lcm = roundavg(lp->l_lcm * eff);
353 if (sp->sct_type != SCT_HEADQ) {
354 pr("Land units must be built in headquarters.\n");
357 if (sp->sct_effic < 60 && !player->god) {
358 pr("Sector %s is not 60%% efficient.\n",
359 xyas(sp->sct_x, sp->sct_y, player->cnum));
362 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
363 pr("Not enough materials in %s\n",
364 xyas(sp->sct_x, sp->sct_y, player->cnum));
368 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
369 pr("Not enough guns in %s\n",
370 xyas(sp->sct_x, sp->sct_y, player->cnum));
373 if (vec[I_SHELL] < shell) {
374 pr("Not enough shells in %s\n",
375 xyas(sp->sct_x, sp->sct_y, player->cnum));
378 if (vec[I_MILIT] < mil) {
379 pr("Not enough military in %s\n",
380 xyas(sp->sct_x, sp->sct_y, player->cnum));
384 if (!trechk(player->cnum, 0, NEWLND))
386 if (!check_sect_ok(sp))
388 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
389 if (sp->sct_avail < avail) {
390 pr("Not enough available work in %s to build a %s\n",
391 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
392 pr(" (%d available work required)\n", avail);
395 cost = lp->l_cost * LAND_MINEFF / 100.0;
396 if (!build_can_afford(cost, lp->l_name))
398 sp->sct_avail -= avail;
399 player->dolcost += cost;
400 snxtitem_all(&nstr, EF_LAND);
401 while (nxtitem(&nstr, &land)) {
402 if (land.lnd_own == 0) {
408 ef_extend(EF_LAND, 50);
410 ef_blank(EF_LAND, nstr.cur, &land);
411 land.lnd_x = sp->sct_x;
412 land.lnd_y = sp->sct_y;
413 land.lnd_own = player->cnum;
414 land.lnd_type = lp - lchr;
415 land.lnd_effic = LAND_MINEFF;
416 if (opt_MOB_ACCESS) {
417 game_tick_to_now(&land.lnd_access);
418 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
425 memset(land.lnd_item, 0, sizeof(land.lnd_item));
426 land.lnd_pstage = PLG_HEALTHY;
428 lnd_set_tech(&land, tlev);
429 unit_wipe_orders((struct empobj *)&land);
435 vec[I_SHELL] -= shell;
437 if (sp->sct_pstage == PLG_INFECT)
438 land.lnd_pstage = PLG_EXPOSED;
439 putland(nstr.cur, &land);
440 pr("%s", prland(&land));
441 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
446 build_nuke(struct sctstr *sp, int type, short *vec, int tlev)
448 struct nchrstr *np = &nchr[type];
450 struct nstr_item nstr;
454 if (sp->sct_type != SCT_NUKE && !player->god) {
455 pr("Nuclear weapons must be built in nuclear plants.\n");
458 if (sp->sct_effic < 60 && !player->god) {
459 pr("Sector %s is not 60%% efficient.\n",
460 xyas(sp->sct_x, sp->sct_y, player->cnum));
463 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
464 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
465 pr("Not enough materials for a %s bomb in %s\n",
466 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
467 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
468 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
471 if (!build_can_afford(np->n_cost, np->n_name))
473 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
475 * XXX when nukes turn into units (or whatever), then
476 * make them start at 20%. Since they don't have efficiency
477 * now, we charge all the work right away.
479 if (sp->sct_avail < avail) {
480 pr("Not enough available work in %s to build a %s;\n",
481 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
482 pr(" (%d available work required)\n", avail);
485 if (!trechk(player->cnum, 0, NEWNUK))
487 if (!check_sect_ok(sp))
489 sp->sct_avail -= avail;
490 player->dolcost += np->n_cost;
491 snxtitem_all(&nstr, EF_NUKE);
493 while (nxtitem(&nstr, &nuke)) {
494 if (nuke.nuk_own == 0) {
500 ef_extend(EF_NUKE, 50);
502 ef_blank(EF_NUKE, nstr.cur, &nuke);
503 nuke.nuk_x = sp->sct_x;
504 nuke.nuk_y = sp->sct_y;
505 nuke.nuk_own = sp->sct_own;
506 nuke.nuk_type = np - nchr;
507 nuke.nuk_effic = 100;
509 nuke.nuk_tech = tlev;
510 unit_wipe_orders((struct empobj *)&nuke);
512 vec[I_HCM] -= np->n_hcm;
513 vec[I_LCM] -= np->n_lcm;
514 vec[I_OIL] -= np->n_oil;
515 vec[I_RAD] -= np->n_rad;
517 putnuke(nuke.nuk_uid, &nuke);
518 pr("%s created in %s\n", prnuke(&nuke),
519 xyas(sp->sct_x, sp->sct_y, player->cnum));
524 build_plane(struct sctstr *sp, int type, short *vec, int tlev)
526 struct plchrstr *pp = &plchr[type];
528 struct nstr_item nstr;
531 double eff = PLANE_MINEFF / 100.0;
535 mil = roundavg(pp->pl_crew * eff);
536 /* Always use at least 1 mil to build a plane */
537 if (mil == 0 && pp->pl_crew > 0)
539 hcm = roundavg(pp->pl_hcm * eff);
540 lcm = roundavg(pp->pl_lcm * eff);
541 if (sp->sct_type != SCT_AIRPT && !player->god) {
542 pr("Planes must be built in airports.\n");
545 if (sp->sct_effic < 60 && !player->god) {
546 pr("Sector %s is not 60%% efficient.\n",
547 xyas(sp->sct_x, sp->sct_y, player->cnum));
550 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
551 pr("Not enough materials in %s\n",
552 xyas(sp->sct_x, sp->sct_y, player->cnum));
555 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
556 if (sp->sct_avail < avail) {
557 pr("Not enough available work in %s to build a %s\n",
558 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
559 pr(" (%d available work required)\n", avail);
562 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
563 if (!build_can_afford(cost, pp->pl_name))
565 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
566 pr("Not enough military for crew in %s\n",
567 xyas(sp->sct_x, sp->sct_y, player->cnum));
570 if (!trechk(player->cnum, 0, NEWPLN))
572 if (!check_sect_ok(sp))
574 sp->sct_avail -= avail;
575 player->dolcost += cost;
576 snxtitem_all(&nstr, EF_PLANE);
578 while (nxtitem(&nstr, &plane)) {
579 if (plane.pln_own == 0) {
584 if (freeplane == 0) {
585 ef_extend(EF_PLANE, 50);
587 ef_blank(EF_PLANE, nstr.cur, &plane);
588 plane.pln_x = sp->sct_x;
589 plane.pln_y = sp->sct_y;
590 plane.pln_own = sp->sct_own;
591 plane.pln_type = pp - plchr;
592 plane.pln_effic = PLANE_MINEFF;
593 if (opt_MOB_ACCESS) {
594 game_tick_to_now(&plane.pln_access);
595 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
599 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
602 plane.pln_harden = 0;
604 pln_set_tech(&plane, tlev);
605 unit_wipe_orders((struct empobj *)&plane);
611 putplane(plane.pln_uid, &plane);
612 pr("%s built in sector %s\n", prplane(&plane),
613 xyas(sp->sct_x, sp->sct_y, player->cnum));
618 build_bridge(char what)
620 struct natstr *natp = getnatp(player->cnum);
621 struct nstr_sect nstr;
622 int (*build_it)(struct sctstr *, short[]);
628 if (natp->nat_level[NAT_TLEV] < buil_bt) {
629 pr("Building a span requires a tech of %.0f\n", buil_bt);
632 build_it = build_bspan;
635 if (!opt_BRIDGETOWERS) {
636 pr("Bridge tower building is disabled.\n");
639 if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
640 pr("Building a tower requires a tech of %.0f\n",
644 build_it = build_btower;
651 if (!snxtsct(&nstr, player->argp[2]))
654 while (nxtsct(&nstr, §)) {
658 if (build_it(§, sect.sct_item))
662 pr("Bad sector specification.\n");
668 build_bspan(struct sctstr *sp, short *vec)
674 int nx, ny, i, good = 0;
678 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
679 if (sp->sct_type != SCT_BTOWER) {
680 if (sp->sct_type != SCT_BHEAD)
682 if (sp->sct_newtype != SCT_BHEAD)
687 if (sp->sct_effic < 60 && !player->god) {
688 pr("Sector %s is not 60%% efficient.\n",
689 xyas(sp->sct_x, sp->sct_y, player->cnum));
693 if (vec[I_HCM] < buil_bh) {
694 pr("%s only has %d unit%s of hcm,\n",
695 xyas(sp->sct_x, sp->sct_y, player->cnum),
696 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
697 pr("(a bridge span requires %d)\n", buil_bh);
701 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
703 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
704 if (sp->sct_avail < avail) {
705 pr("Not enough available work in %s to build a bridge\n",
706 xyas(sp->sct_x, sp->sct_y, player->cnum));
707 pr(" (%d available work required)\n", avail);
710 if (!player->argp[3]) {
711 pr("Bridge head at %s\n",
712 xyas(sp->sct_x, sp->sct_y, player->cnum));
713 nav_map(sp->sct_x, sp->sct_y, 1);
715 p = getstarg(player->argp[3], "build span in what direction? ", buf);
719 /* Sanity check time */
720 if (!check_sect_ok(sp))
723 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
724 pr("'%c' is not a valid direction...\n", *p);
725 direrr(NULL, NULL, NULL);
728 newx = sp->sct_x + diroff[val][0];
729 newy = sp->sct_y + diroff[val][1];
730 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
731 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
734 if (opt_EASY_BRIDGES) {
737 for (i = 1; i <= 6; i++) {
739 nx = sect.sct_x + diroff[i][0];
740 ny = sect.sct_y + diroff[i][1];
741 getsect(nx, ny, &s2);
742 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
746 pr("Bridges must be built adjacent to land or bridge towers.\n");
747 pr("That sector is not adjacent to land or a bridge tower.\n");
750 } /* end EASY_BRIDGES */
751 sp->sct_avail -= avail;
752 player->dolcost += buil_bc;
753 sect.sct_type = SCT_BSPAN;
754 sect.sct_newtype = SCT_BSPAN;
755 sect.sct_effic = SCT_MINEFF;
758 sect.sct_defense = 0;
759 if (opt_MOB_ACCESS) {
760 game_tick_to_now(§.sct_access);
761 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
766 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
767 writemap(player->cnum);
769 pr("Bridge span built over %s\n",
770 xyas(sect.sct_x, sect.sct_y, player->cnum));
771 vec[I_HCM] -= buil_bh;
776 build_btower(struct sctstr *sp, short *vec)
788 if (sp->sct_type != SCT_BSPAN) {
789 pr("Bridge towers can only be built from bridge spans.\n");
793 if (sp->sct_effic < 60 && !player->god) {
794 pr("Sector %s is not 60%% efficient.\n",
795 xyas(sp->sct_x, sp->sct_y, player->cnum));
799 if (vec[I_HCM] < buil_tower_bh) {
800 pr("%s only has %d unit%s of hcm,\n",
801 xyas(sp->sct_x, sp->sct_y, player->cnum),
802 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
803 pr("(a bridge tower requires %d)\n", buil_tower_bh);
807 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
809 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
810 if (sp->sct_avail < avail) {
811 pr("Not enough available work in %s to build a bridge tower\n",
812 xyas(sp->sct_x, sp->sct_y, player->cnum));
813 pr(" (%d available work required)\n", avail);
816 if (!player->argp[3]) {
817 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
818 nav_map(sp->sct_x, sp->sct_y, 1);
820 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
824 /* Sanity check time */
825 if (!check_sect_ok(sp))
828 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
829 pr("'%c' is not a valid direction...\n", *p);
830 direrr(NULL, NULL, NULL);
833 newx = sp->sct_x + diroff[val][0];
834 newy = sp->sct_y + diroff[val][1];
835 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
836 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
840 /* Now, check. You aren't allowed to build bridge towers
842 for (i = 1; i <= 6; i++) {
844 nx = sect.sct_x + diroff[i][0];
845 ny = sect.sct_y + diroff[i][1];
846 getsect(nx, ny, &s2);
847 if ((s2.sct_type != SCT_WATER) &&
848 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
849 pr("Bridge towers cannot be built adjacent to land.\n");
850 pr("That sector is adjacent to land.\n");
855 sp->sct_avail -= avail;
856 player->dolcost += buil_tower_bc;
857 sect.sct_type = SCT_BTOWER;
858 sect.sct_newtype = SCT_BTOWER;
859 sect.sct_effic = SCT_MINEFF;
862 sect.sct_defense = 0;
863 if (opt_MOB_ACCESS) {
864 game_tick_to_now(§.sct_access);
865 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
870 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
871 writemap(player->cnum);
873 pr("Bridge tower built in %s\n",
874 xyas(sect.sct_x, sect.sct_y, player->cnum));
875 vec[I_HCM] -= buil_tower_bh;
880 build_can_afford(double cost, char *what)
882 struct natstr *natp = getnatp(player->cnum);
883 if (natp->nat_money < player->dolcost + cost) {
884 pr("Not enough money left to build a %s\n", what);