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_nuke(struct sctstr *sp,
52 struct nchrstr *np, short *vec, int tlev);
53 static int build_ship(struct sctstr *sp,
54 struct mchrstr *mp, short *vec, int tlev);
55 static int build_land(struct sctstr *sp,
56 struct lchrstr *lp, short *vec, int tlev);
57 static int build_bridge(struct sctstr *sp, short *vec);
58 static int build_tower(struct sctstr *sp, short *vec);
59 static int build_plane(struct sctstr *sp,
60 struct plchrstr *pp, short *vec, int tlev);
61 static int build_can_afford(double, char *);
64 * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
70 struct nstr_sect nstr;
87 natp = getnatp(player->cnum);
88 p = getstarg(player->argp[1],
89 "Build (ship, nuke, bridge, plane, land unit, tower)? ",
95 if (!snxtsct(&nstr, player->argp[2]))
97 tlev = (int)natp->nat_level[NAT_TLEV];
98 rlev = (int)natp->nat_level[NAT_RLEV];
102 p = getstarg(player->argp[3], "Plane type? ", buf);
105 type = ef_elt_byname(EF_PLANE_CHR, p);
108 rqtech = pp->pl_tech;
113 pr("You can't build that!\n");
114 pr("Use `show plane build %d' to show types you can build.\n",
120 p = getstarg(player->argp[3], "Ship type? ", buf);
123 type = ef_elt_byname(EF_SHIP_CHR, p);
129 if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
133 pr("You can't build that!\n");
134 pr("Use `show ship build %d' to show types you can build.\n",
140 p = getstarg(player->argp[3], "Land unit type? ", buf);
143 type = ef_elt_byname(EF_LAND_CHR, p);
149 if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
153 pr("You can't build that!\n");
154 pr("Use `show land build %d' to show types you can build.\n",
160 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_bt) {
161 pr("Building a span requires a tech of %.0f\n", buil_bt);
166 if (!opt_BRIDGETOWERS) {
167 pr("Bridge tower building is disabled.\n");
170 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_tower_bt) {
171 pr("Building a tower requires a tech of %.0f\n",
177 if (!ef_nelem(EF_NUKE_CHR)) {
178 pr("There are no nukes in this game.\n");
181 p = getstarg(player->argp[3], "Nuke type? ", buf);
184 type = ef_elt_byname(EF_NUKE_CHR, p);
189 || (drnuke_const > MIN_DRNUKE_CONST &&
190 np->n_tech * drnuke_const > rlev))
195 if (drnuke_const > MIN_DRNUKE_CONST)
196 tt = (tlev < (rlev / drnuke_const) ? (int)tlev :
197 (int)(rlev / drnuke_const));
198 pr("You can't build that!\n");
199 pr("Use `show nuke build %d' to show types you can build.\n",
205 pr("You can't build that!\n");
210 if (what != 'b' && what != 't') {
211 if (player->argp[4]) {
212 number = atoi(player->argp[4]);
216 "Are you sure that you want to build %s of them? ",
218 p = getstarg(player->argp[6], bstr, buf);
225 if (what != 'b' && what != 'n' && what != 't') {
226 if (player->argp[5]) {
227 tlev = atoi(player->argp[5]);
228 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
229 pr("Your tech level is only %d.\n",
230 (int)natp->nat_level[NAT_TLEV]);
234 pr("Required tech is %d.\n", rqtech);
237 pr("building with tech level %d.\n", tlev);
241 while (number-- > 0) {
242 while (nxtsct(&nstr, §)) {
248 built = build_land(§, lp, sect.sct_item, tlev);
251 built = build_ship(§, mp, sect.sct_item, tlev);
254 built = build_bridge(§, sect.sct_item);
257 built = build_tower(§, sect.sct_item);
260 built = build_nuke(§, np, sect.sct_item, tlev);
263 built = build_plane(§, pp, sect.sct_item, tlev);
273 snxtsct_rewind(&nstr);
276 pr("Bad sector specification.\n");
282 build_ship(struct sctstr *sp, struct mchrstr *mp, short *vec, int tlev)
285 struct nstr_item nstr;
288 double eff = SHIP_MINEFF / 100.0;
292 hcm = roundavg(mp->m_hcm * eff);
293 lcm = roundavg(mp->m_lcm * eff);
295 if (sp->sct_type != SCT_HARBR) {
296 pr("Ships must be built in harbours.\n");
299 if (sp->sct_effic < 60 && !player->god) {
300 pr("Sector %s is not 60%% efficient.\n",
301 xyas(sp->sct_x, sp->sct_y, player->cnum));
304 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
305 pr("Not enough materials in %s\n",
306 xyas(sp->sct_x, sp->sct_y, player->cnum));
309 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
310 if (sp->sct_avail < avail) {
311 pr("Not enough available work in %s to build a %s\n",
312 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
313 pr(" (%d available work required)\n", avail);
316 cost = mp->m_cost * SHIP_MINEFF / 100.0;
317 if (!build_can_afford(cost, mp->m_name))
319 if (!trechk(player->cnum, 0, NEWSHP))
321 if (!check_sect_ok(sp))
323 sp->sct_avail -= avail;
324 player->dolcost += cost;
325 snxtitem_all(&nstr, EF_SHIP);
326 while (nxtitem(&nstr, &ship)) {
327 if (ship.shp_own == 0) {
333 ef_extend(EF_SHIP, 50);
335 ef_blank(EF_SHIP, nstr.cur, &ship);
336 ship.shp_x = sp->sct_x;
337 ship.shp_y = sp->sct_y;
338 ship.shp_own = player->cnum;
339 ship.shp_type = mp - mchr;
340 ship.shp_effic = SHIP_MINEFF;
341 if (opt_MOB_ACCESS) {
342 game_tick_to_now(&ship.shp_access);
343 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
347 memset(ship.shp_item, 0, sizeof(ship.shp_item));
348 ship.shp_pstage = PLG_HEALTHY;
350 ship.shp_name[0] = 0;
351 ship.shp_orig_own = player->cnum;
352 ship.shp_orig_x = sp->sct_x;
353 ship.shp_orig_y = sp->sct_y;
354 shp_set_tech(&ship, tlev);
355 unit_wipe_orders((struct empobj *)&ship);
360 if (sp->sct_pstage == PLG_INFECT)
361 ship.shp_pstage = PLG_EXPOSED;
362 putship(ship.shp_uid, &ship);
363 pr("%s", prship(&ship));
364 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
369 build_land(struct sctstr *sp, struct lchrstr *lp, short *vec, int tlev)
372 struct nstr_item nstr;
375 double eff = LAND_MINEFF / 100.0;
376 int mil, lcm, hcm, gun, shell;
380 mil = roundavg(lp->l_mil * eff);
381 shell = roundavg(lp->l_shell * eff);
382 gun = roundavg(lp->l_gun * eff);
384 mil = shell = gun = 0;
386 hcm = roundavg(lp->l_hcm * eff);
387 lcm = roundavg(lp->l_lcm * eff);
389 if (sp->sct_type != SCT_HEADQ) {
390 pr("Land units must be built in headquarters.\n");
393 if (sp->sct_effic < 60 && !player->god) {
394 pr("Sector %s is not 60%% efficient.\n",
395 xyas(sp->sct_x, sp->sct_y, player->cnum));
398 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
399 pr("Not enough materials in %s\n",
400 xyas(sp->sct_x, sp->sct_y, player->cnum));
404 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
405 pr("Not enough guns in %s\n",
406 xyas(sp->sct_x, sp->sct_y, player->cnum));
409 if (vec[I_SHELL] < shell) {
410 pr("Not enough shells in %s\n",
411 xyas(sp->sct_x, sp->sct_y, player->cnum));
414 if (vec[I_MILIT] < mil) {
415 pr("Not enough military in %s\n",
416 xyas(sp->sct_x, sp->sct_y, player->cnum));
420 if (!trechk(player->cnum, 0, NEWLND))
422 if (!check_sect_ok(sp))
424 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
425 if (sp->sct_avail < avail) {
426 pr("Not enough available work in %s to build a %s\n",
427 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
428 pr(" (%d available work required)\n", avail);
431 cost = lp->l_cost * LAND_MINEFF / 100.0;
432 if (!build_can_afford(cost, lp->l_name))
434 sp->sct_avail -= avail;
435 player->dolcost += cost;
436 snxtitem_all(&nstr, EF_LAND);
437 while (nxtitem(&nstr, &land)) {
438 if (land.lnd_own == 0) {
444 ef_extend(EF_LAND, 50);
446 ef_blank(EF_LAND, nstr.cur, &land);
447 land.lnd_x = sp->sct_x;
448 land.lnd_y = sp->sct_y;
449 land.lnd_own = player->cnum;
450 land.lnd_type = lp - lchr;
451 land.lnd_effic = LAND_MINEFF;
452 if (opt_MOB_ACCESS) {
453 game_tick_to_now(&land.lnd_access);
454 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
461 memset(land.lnd_item, 0, sizeof(land.lnd_item));
462 land.lnd_pstage = PLG_HEALTHY;
464 lnd_set_tech(&land, tlev);
465 unit_wipe_orders((struct empobj *)&land);
471 vec[I_SHELL] -= shell;
473 if (sp->sct_pstage == PLG_INFECT)
474 land.lnd_pstage = PLG_EXPOSED;
475 putland(nstr.cur, &land);
476 pr("%s", prland(&land));
477 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
482 build_bridge(struct sctstr *sp, short *vec)
488 int nx, ny, i, good = 0;
492 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
493 if (sp->sct_type != SCT_BTOWER) {
494 if (sp->sct_type != SCT_BHEAD)
496 if (sp->sct_newtype != SCT_BHEAD)
501 if (sp->sct_effic < 60 && !player->god) {
502 pr("Sector %s is not 60%% efficient.\n",
503 xyas(sp->sct_x, sp->sct_y, player->cnum));
507 if (vec[I_HCM] < buil_bh) {
508 pr("%s only has %d unit%s of hcm,\n",
509 xyas(sp->sct_x, sp->sct_y, player->cnum),
510 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
511 pr("(a bridge span requires %d)\n", buil_bh);
515 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
517 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
518 if (sp->sct_avail < avail) {
519 pr("Not enough available work in %s to build a bridge\n",
520 xyas(sp->sct_x, sp->sct_y, player->cnum));
521 pr(" (%d available work required)\n", avail);
524 if (!player->argp[3]) {
525 pr("Bridge head at %s\n",
526 xyas(sp->sct_x, sp->sct_y, player->cnum));
527 nav_map(sp->sct_x, sp->sct_y, 1);
529 p = getstarg(player->argp[3], "build span in what direction? ", buf);
533 /* Sanity check time */
534 if (!check_sect_ok(sp))
537 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
538 pr("'%c' is not a valid direction...\n", *p);
539 direrr(NULL, NULL, NULL);
542 newx = sp->sct_x + diroff[val][0];
543 newy = sp->sct_y + diroff[val][1];
544 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
545 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
548 if (opt_EASY_BRIDGES) {
551 for (i = 1; i <= 6; i++) {
553 nx = sect.sct_x + diroff[i][0];
554 ny = sect.sct_y + diroff[i][1];
555 getsect(nx, ny, &s2);
556 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
560 pr("Bridges must be built adjacent to land or bridge towers.\n");
561 pr("That sector is not adjacent to land or a bridge tower.\n");
564 } /* end EASY_BRIDGES */
565 sp->sct_avail -= avail;
566 player->dolcost += buil_bc;
567 sect.sct_type = SCT_BSPAN;
568 sect.sct_newtype = SCT_BSPAN;
569 sect.sct_effic = SCT_MINEFF;
572 sect.sct_defense = 0;
573 if (opt_MOB_ACCESS) {
574 game_tick_to_now(§.sct_access);
575 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
580 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
581 writemap(player->cnum);
583 pr("Bridge span built over %s\n",
584 xyas(sect.sct_x, sect.sct_y, player->cnum));
585 vec[I_HCM] -= buil_bh;
590 build_nuke(struct sctstr *sp, struct nchrstr *np, short *vec, int tlev)
593 struct nstr_item nstr;
597 if (sp->sct_type != SCT_NUKE && !player->god) {
598 pr("Nuclear weapons must be built in nuclear plants.\n");
601 if (sp->sct_effic < 60 && !player->god) {
602 pr("Sector %s is not 60%% efficient.\n",
603 xyas(sp->sct_x, sp->sct_y, player->cnum));
606 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
607 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
608 pr("Not enough materials for a %s bomb in %s\n",
609 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
610 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
611 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
614 if (!build_can_afford(np->n_cost, np->n_name))
616 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
618 * XXX when nukes turn into units (or whatever), then
619 * make them start at 20%. Since they don't have efficiency
620 * now, we charge all the work right away.
622 if (sp->sct_avail < avail) {
623 pr("Not enough available work in %s to build a %s;\n",
624 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
625 pr(" (%d available work required)\n", avail);
628 if (!trechk(player->cnum, 0, NEWNUK))
630 if (!check_sect_ok(sp))
632 sp->sct_avail -= avail;
633 player->dolcost += np->n_cost;
634 snxtitem_all(&nstr, EF_NUKE);
636 while (nxtitem(&nstr, &nuke)) {
637 if (nuke.nuk_own == 0) {
643 ef_extend(EF_NUKE, 50);
645 ef_blank(EF_NUKE, nstr.cur, &nuke);
646 nuke.nuk_x = sp->sct_x;
647 nuke.nuk_y = sp->sct_y;
648 nuke.nuk_own = sp->sct_own;
649 nuke.nuk_type = np - nchr;
650 nuke.nuk_effic = 100;
652 nuke.nuk_tech = tlev;
653 unit_wipe_orders((struct empobj *)&nuke);
655 vec[I_HCM] -= np->n_hcm;
656 vec[I_LCM] -= np->n_lcm;
657 vec[I_OIL] -= np->n_oil;
658 vec[I_RAD] -= np->n_rad;
660 putnuke(nuke.nuk_uid, &nuke);
661 pr("%s created in %s\n", prnuke(&nuke),
662 xyas(sp->sct_x, sp->sct_y, player->cnum));
667 build_plane(struct sctstr *sp, struct plchrstr *pp, short *vec, int tlev)
670 struct nstr_item nstr;
673 double eff = PLANE_MINEFF / 100.0;
677 mil = roundavg(pp->pl_crew * eff);
678 /* Always use at least 1 mil to build a plane */
679 if (mil == 0 && pp->pl_crew > 0)
681 hcm = roundavg(pp->pl_hcm * eff);
682 lcm = roundavg(pp->pl_lcm * eff);
683 if (sp->sct_type != SCT_AIRPT && !player->god) {
684 pr("Planes must be built in airports.\n");
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));
692 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
693 pr("Not enough materials in %s\n",
694 xyas(sp->sct_x, sp->sct_y, player->cnum));
697 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
698 if (sp->sct_avail < avail) {
699 pr("Not enough available work in %s to build a %s\n",
700 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
701 pr(" (%d available work required)\n", avail);
704 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
705 if (!build_can_afford(cost, pp->pl_name))
707 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
708 pr("Not enough military for crew in %s\n",
709 xyas(sp->sct_x, sp->sct_y, player->cnum));
712 if (!trechk(player->cnum, 0, NEWPLN))
714 if (!check_sect_ok(sp))
716 sp->sct_avail -= avail;
717 player->dolcost += cost;
718 snxtitem_all(&nstr, EF_PLANE);
720 while (nxtitem(&nstr, &plane)) {
721 if (plane.pln_own == 0) {
726 if (freeplane == 0) {
727 ef_extend(EF_PLANE, 50);
729 ef_blank(EF_PLANE, nstr.cur, &plane);
730 plane.pln_x = sp->sct_x;
731 plane.pln_y = sp->sct_y;
732 plane.pln_own = sp->sct_own;
733 plane.pln_type = pp - plchr;
734 plane.pln_effic = PLANE_MINEFF;
735 if (opt_MOB_ACCESS) {
736 game_tick_to_now(&plane.pln_access);
737 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
741 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
744 plane.pln_harden = 0;
746 pln_set_tech(&plane, tlev);
747 unit_wipe_orders((struct empobj *)&plane);
753 putplane(plane.pln_uid, &plane);
754 pr("%s built in sector %s\n", prplane(&plane),
755 xyas(sp->sct_x, sp->sct_y, player->cnum));
760 build_tower(struct sctstr *sp, short *vec)
772 if (sp->sct_type != SCT_BSPAN) {
773 pr("Bridge towers can only be built from bridge spans.\n");
777 if (sp->sct_effic < 60 && !player->god) {
778 pr("Sector %s is not 60%% efficient.\n",
779 xyas(sp->sct_x, sp->sct_y, player->cnum));
783 if (vec[I_HCM] < buil_tower_bh) {
784 pr("%s only has %d unit%s of hcm,\n",
785 xyas(sp->sct_x, sp->sct_y, player->cnum),
786 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
787 pr("(a bridge tower requires %d)\n", buil_tower_bh);
791 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
793 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
794 if (sp->sct_avail < avail) {
795 pr("Not enough available work in %s to build a bridge tower\n",
796 xyas(sp->sct_x, sp->sct_y, player->cnum));
797 pr(" (%d available work required)\n", avail);
800 if (!player->argp[3]) {
801 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
802 nav_map(sp->sct_x, sp->sct_y, 1);
804 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
808 /* Sanity check time */
809 if (!check_sect_ok(sp))
812 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
813 pr("'%c' is not a valid direction...\n", *p);
814 direrr(NULL, NULL, NULL);
817 newx = sp->sct_x + diroff[val][0];
818 newy = sp->sct_y + diroff[val][1];
819 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
820 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
824 /* Now, check. You aren't allowed to build bridge towers
826 for (i = 1; i <= 6; i++) {
828 nx = sect.sct_x + diroff[i][0];
829 ny = sect.sct_y + diroff[i][1];
830 getsect(nx, ny, &s2);
831 if ((s2.sct_type != SCT_WATER) &&
832 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
833 pr("Bridge towers cannot be built adjacent to land.\n");
834 pr("That sector is adjacent to land.\n");
839 sp->sct_avail -= avail;
840 player->dolcost += buil_tower_bc;
841 sect.sct_type = SCT_BTOWER;
842 sect.sct_newtype = SCT_BTOWER;
843 sect.sct_effic = SCT_MINEFF;
846 sect.sct_defense = 0;
847 if (opt_MOB_ACCESS) {
848 game_tick_to_now(§.sct_access);
849 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
854 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
855 writemap(player->cnum);
857 pr("Bridge tower built in %s\n",
858 xyas(sect.sct_x, sect.sct_y, player->cnum));
859 vec[I_HCM] -= buil_tower_bh;
864 build_can_afford(double cost, char *what)
866 struct natstr *natp = getnatp(player->cnum);
867 if (natp->nat_money < player->dolcost + cost) {
868 pr("Not enough money left to build a %s\n", what);