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-2009
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])) {
96 pr("Bad sector specification.\n");
99 tlev = (int)natp->nat_level[NAT_TLEV];
100 rlev = (int)natp->nat_level[NAT_RLEV];
104 p = getstarg(player->argp[3], "Plane type? ", buf);
107 type = ef_elt_byname(EF_PLANE_CHR, p);
110 rqtech = pp->pl_tech;
115 pr("You can't build that!\n");
116 pr("Use `show plane build %d' to show types you can build.\n",
122 p = getstarg(player->argp[3], "Ship type? ", buf);
125 type = ef_elt_byname(EF_SHIP_CHR, p);
131 if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
135 pr("You can't build that!\n");
136 pr("Use `show ship build %d' to show types you can build.\n",
142 p = getstarg(player->argp[3], "Land unit type? ", buf);
145 type = ef_elt_byname(EF_LAND_CHR, p);
151 if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
155 pr("You can't build that!\n");
156 pr("Use `show land build %d' to show types you can build.\n",
162 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_bt) {
163 pr("Building a span requires a tech of %.0f\n", buil_bt);
168 if (!opt_BRIDGETOWERS) {
169 pr("Bridge tower building is disabled.\n");
172 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_tower_bt) {
173 pr("Building a tower requires a tech of %.0f\n",
179 if (!ef_nelem(EF_NUKE_CHR)) {
180 pr("There are no nukes in this game.\n");
183 p = getstarg(player->argp[3], "Nuke type? ", buf);
186 type = ef_elt_byname(EF_NUKE_CHR, p);
191 || (drnuke_const > MIN_DRNUKE_CONST &&
192 np->n_tech * drnuke_const > rlev))
197 if (drnuke_const > MIN_DRNUKE_CONST)
198 tt = (tlev < (rlev / drnuke_const) ? (int)tlev :
199 (int)(rlev / drnuke_const));
200 pr("You can't build that!\n");
201 pr("Use `show nuke build %d' to show types you can build.\n",
207 pr("You can't build that!\n");
212 if (what != 'b' && what != 't') {
213 if (player->argp[4]) {
214 number = atoi(player->argp[4]);
218 "Are you sure that you want to build %s of them? ",
220 p = getstarg(player->argp[6], bstr, buf);
227 if (what != 'b' && what != 'n' && what != 't') {
228 if (player->argp[5]) {
229 tlev = atoi(player->argp[5]);
230 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
231 pr("Your tech level is only %d.\n",
232 (int)natp->nat_level[NAT_TLEV]);
236 pr("Required tech is %d.\n", rqtech);
239 pr("building with tech level %d.\n", tlev);
243 while (number-- > 0) {
244 while (nxtsct(&nstr, §)) {
250 built = build_land(§, lp, sect.sct_item, tlev);
253 built = build_ship(§, mp, sect.sct_item, tlev);
256 built = build_bridge(§, sect.sct_item);
259 built = build_tower(§, sect.sct_item);
262 built = build_nuke(§, np, sect.sct_item, tlev);
265 built = build_plane(§, pp, sect.sct_item, tlev);
275 snxtsct_rewind(&nstr);
278 pr("Bad sector specification.\n");
284 build_ship(struct sctstr *sp, struct mchrstr *mp, short *vec, int tlev)
287 struct nstr_item nstr;
290 double eff = SHIP_MINEFF / 100.0;
294 hcm = roundavg(mp->m_hcm * eff);
295 lcm = roundavg(mp->m_lcm * eff);
297 if (sp->sct_type != SCT_HARBR) {
298 pr("Ships must be built in harbours.\n");
301 if (sp->sct_effic < 60 && !player->god) {
302 pr("Sector %s is not 60%% efficient.\n",
303 xyas(sp->sct_x, sp->sct_y, player->cnum));
306 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
307 pr("Not enough materials in %s\n",
308 xyas(sp->sct_x, sp->sct_y, player->cnum));
311 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
312 if (sp->sct_avail < avail) {
313 pr("Not enough available work in %s to build a %s\n",
314 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
315 pr(" (%d available work required)\n", avail);
318 cost = mp->m_cost * SHIP_MINEFF / 100.0;
319 if (!build_can_afford(cost, mp->m_name))
321 if (!trechk(player->cnum, 0, NEWSHP))
323 if (!check_sect_ok(sp))
325 sp->sct_avail -= avail;
326 player->dolcost += cost;
327 snxtitem_all(&nstr, EF_SHIP);
328 while (nxtitem(&nstr, &ship)) {
329 if (ship.shp_own == 0) {
335 ef_extend(EF_SHIP, 50);
337 ef_blank(EF_SHIP, nstr.cur, &ship);
338 ship.shp_x = sp->sct_x;
339 ship.shp_y = sp->sct_y;
340 ship.shp_own = player->cnum;
341 ship.shp_type = mp - mchr;
342 ship.shp_effic = SHIP_MINEFF;
343 if (opt_MOB_ACCESS) {
344 game_tick_to_now(&ship.shp_access);
345 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
349 memset(ship.shp_item, 0, sizeof(ship.shp_item));
350 ship.shp_pstage = PLG_HEALTHY;
352 ship.shp_name[0] = 0;
353 ship.shp_orig_own = player->cnum;
354 ship.shp_orig_x = sp->sct_x;
355 ship.shp_orig_y = sp->sct_y;
356 shp_set_tech(&ship, tlev);
357 unit_wipe_orders((struct empobj *)&ship);
362 if (sp->sct_pstage == PLG_INFECT)
363 ship.shp_pstage = PLG_EXPOSED;
364 putship(ship.shp_uid, &ship);
365 pr("%s", prship(&ship));
366 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
371 build_land(struct sctstr *sp, struct lchrstr *lp, short *vec, int tlev)
374 struct nstr_item nstr;
377 double eff = LAND_MINEFF / 100.0;
378 int mil, lcm, hcm, gun, shell;
382 mil = roundavg(lp->l_mil * eff);
383 shell = roundavg(lp->l_shell * eff);
384 gun = roundavg(lp->l_gun * eff);
386 mil = shell = gun = 0;
388 hcm = roundavg(lp->l_hcm * eff);
389 lcm = roundavg(lp->l_lcm * eff);
391 if (sp->sct_type != SCT_HEADQ) {
392 pr("Land units must be built in headquarters.\n");
395 if (sp->sct_effic < 60 && !player->god) {
396 pr("Sector %s is not 60%% efficient.\n",
397 xyas(sp->sct_x, sp->sct_y, player->cnum));
400 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
401 pr("Not enough materials in %s\n",
402 xyas(sp->sct_x, sp->sct_y, player->cnum));
406 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
407 pr("Not enough guns in %s\n",
408 xyas(sp->sct_x, sp->sct_y, player->cnum));
411 if (vec[I_SHELL] < shell) {
412 pr("Not enough shells in %s\n",
413 xyas(sp->sct_x, sp->sct_y, player->cnum));
416 if (vec[I_MILIT] < mil) {
417 pr("Not enough military in %s\n",
418 xyas(sp->sct_x, sp->sct_y, player->cnum));
422 if (!trechk(player->cnum, 0, NEWLND))
424 if (!check_sect_ok(sp))
426 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
427 if (sp->sct_avail < avail) {
428 pr("Not enough available work in %s to build a %s\n",
429 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
430 pr(" (%d available work required)\n", avail);
433 cost = lp->l_cost * LAND_MINEFF / 100.0;
434 if (!build_can_afford(cost, lp->l_name))
436 sp->sct_avail -= avail;
437 player->dolcost += cost;
438 snxtitem_all(&nstr, EF_LAND);
439 while (nxtitem(&nstr, &land)) {
440 if (land.lnd_own == 0) {
446 ef_extend(EF_LAND, 50);
448 ef_blank(EF_LAND, nstr.cur, &land);
449 land.lnd_x = sp->sct_x;
450 land.lnd_y = sp->sct_y;
451 land.lnd_own = player->cnum;
452 land.lnd_type = lp - lchr;
453 land.lnd_effic = LAND_MINEFF;
454 if (opt_MOB_ACCESS) {
455 game_tick_to_now(&land.lnd_access);
456 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
463 memset(land.lnd_item, 0, sizeof(land.lnd_item));
464 land.lnd_pstage = PLG_HEALTHY;
466 lnd_set_tech(&land, tlev);
467 unit_wipe_orders((struct empobj *)&land);
473 vec[I_SHELL] -= shell;
475 if (sp->sct_pstage == PLG_INFECT)
476 land.lnd_pstage = PLG_EXPOSED;
477 putland(nstr.cur, &land);
478 pr("%s", prland(&land));
479 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
484 build_bridge(struct sctstr *sp, short *vec)
490 int nx, ny, i, good = 0;
494 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
495 if (sp->sct_type != SCT_BTOWER) {
496 if (sp->sct_type != SCT_BHEAD)
498 if (sp->sct_newtype != SCT_BHEAD)
503 if (sp->sct_effic < 60 && !player->god) {
504 pr("Sector %s is not 60%% efficient.\n",
505 xyas(sp->sct_x, sp->sct_y, player->cnum));
509 if (vec[I_HCM] < buil_bh) {
510 pr("%s only has %d unit%s of hcm,\n",
511 xyas(sp->sct_x, sp->sct_y, player->cnum),
512 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
513 pr("(a bridge span requires %d)\n", buil_bh);
517 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
519 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
520 if (sp->sct_avail < avail) {
521 pr("Not enough available work in %s to build a bridge\n",
522 xyas(sp->sct_x, sp->sct_y, player->cnum));
523 pr(" (%d available work required)\n", avail);
526 if (!player->argp[3]) {
527 pr("Bridge head at %s\n",
528 xyas(sp->sct_x, sp->sct_y, player->cnum));
529 nav_map(sp->sct_x, sp->sct_y, 1);
531 p = getstarg(player->argp[3], "build span in what direction? ", buf);
535 /* Sanity check time */
536 if (!check_sect_ok(sp))
539 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
540 pr("'%c' is not a valid direction...\n", *p);
541 direrr(NULL, NULL, NULL);
544 newx = sp->sct_x + diroff[val][0];
545 newy = sp->sct_y + diroff[val][1];
546 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
547 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
550 if (opt_EASY_BRIDGES) {
553 for (i = 1; i <= 6; i++) {
555 nx = sect.sct_x + diroff[i][0];
556 ny = sect.sct_y + diroff[i][1];
557 getsect(nx, ny, &s2);
558 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
562 pr("Bridges must be built adjacent to land or bridge towers.\n");
563 pr("That sector is not adjacent to land or a bridge tower.\n");
566 } /* end EASY_BRIDGES */
567 sp->sct_avail -= avail;
568 player->dolcost += buil_bc;
569 sect.sct_type = SCT_BSPAN;
570 sect.sct_newtype = SCT_BSPAN;
571 sect.sct_effic = SCT_MINEFF;
574 sect.sct_defense = 0;
575 if (opt_MOB_ACCESS) {
576 game_tick_to_now(§.sct_access);
577 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
582 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
583 writemap(player->cnum);
585 pr("Bridge span built over %s\n",
586 xyas(sect.sct_x, sect.sct_y, player->cnum));
587 vec[I_HCM] -= buil_bh;
592 build_nuke(struct sctstr *sp, struct nchrstr *np, short *vec, int tlev)
595 struct nstr_item nstr;
599 if (sp->sct_type != SCT_NUKE && !player->god) {
600 pr("Nuclear weapons must be built in nuclear plants.\n");
603 if (sp->sct_effic < 60 && !player->god) {
604 pr("Sector %s is not 60%% efficient.\n",
605 xyas(sp->sct_x, sp->sct_y, player->cnum));
608 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
609 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
610 pr("Not enough materials for a %s bomb in %s\n",
611 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
612 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
613 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
616 if (!build_can_afford(np->n_cost, np->n_name))
618 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
620 * XXX when nukes turn into units (or whatever), then
621 * make them start at 20%. Since they don't have efficiency
622 * now, we charge all the work right away.
624 if (sp->sct_avail < avail) {
625 pr("Not enough available work in %s to build a %s;\n",
626 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
627 pr(" (%d available work required)\n", avail);
630 if (!trechk(player->cnum, 0, NEWNUK))
632 if (!check_sect_ok(sp))
634 sp->sct_avail -= avail;
635 player->dolcost += np->n_cost;
636 snxtitem_all(&nstr, EF_NUKE);
638 while (nxtitem(&nstr, &nuke)) {
639 if (nuke.nuk_own == 0) {
645 ef_extend(EF_NUKE, 50);
647 ef_blank(EF_NUKE, nstr.cur, &nuke);
648 nuke.nuk_x = sp->sct_x;
649 nuke.nuk_y = sp->sct_y;
650 nuke.nuk_own = sp->sct_own;
651 nuke.nuk_type = np - nchr;
652 nuke.nuk_effic = 100;
654 nuke.nuk_tech = tlev;
655 unit_wipe_orders((struct empobj *)&nuke);
657 vec[I_HCM] -= np->n_hcm;
658 vec[I_LCM] -= np->n_lcm;
659 vec[I_OIL] -= np->n_oil;
660 vec[I_RAD] -= np->n_rad;
662 putnuke(nuke.nuk_uid, &nuke);
663 pr("%s created in %s\n", prnuke(&nuke),
664 xyas(sp->sct_x, sp->sct_y, player->cnum));
669 build_plane(struct sctstr *sp, struct plchrstr *pp, short *vec, int tlev)
672 struct nstr_item nstr;
675 double eff = PLANE_MINEFF / 100.0;
679 mil = roundavg(pp->pl_crew * eff);
680 /* Always use at least 1 mil to build a plane */
681 if (mil == 0 && pp->pl_crew > 0)
683 hcm = roundavg(pp->pl_hcm * eff);
684 lcm = roundavg(pp->pl_lcm * eff);
685 if (sp->sct_type != SCT_AIRPT && !player->god) {
686 pr("Planes must be built in airports.\n");
689 if (sp->sct_effic < 60 && !player->god) {
690 pr("Sector %s is not 60%% efficient.\n",
691 xyas(sp->sct_x, sp->sct_y, player->cnum));
694 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
695 pr("Not enough materials in %s\n",
696 xyas(sp->sct_x, sp->sct_y, player->cnum));
699 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 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), pp->pl_name);
703 pr(" (%d available work required)\n", avail);
706 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
707 if (!build_can_afford(cost, pp->pl_name))
709 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
710 pr("Not enough military for crew in %s\n",
711 xyas(sp->sct_x, sp->sct_y, player->cnum));
714 if (!trechk(player->cnum, 0, NEWPLN))
716 if (!check_sect_ok(sp))
718 sp->sct_avail -= avail;
719 player->dolcost += cost;
720 snxtitem_all(&nstr, EF_PLANE);
722 while (nxtitem(&nstr, &plane)) {
723 if (plane.pln_own == 0) {
728 if (freeplane == 0) {
729 ef_extend(EF_PLANE, 50);
731 ef_blank(EF_PLANE, nstr.cur, &plane);
732 plane.pln_x = sp->sct_x;
733 plane.pln_y = sp->sct_y;
734 plane.pln_own = sp->sct_own;
735 plane.pln_type = pp - plchr;
736 plane.pln_effic = PLANE_MINEFF;
737 if (opt_MOB_ACCESS) {
738 game_tick_to_now(&plane.pln_access);
739 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
743 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
746 plane.pln_harden = 0;
748 pln_set_tech(&plane, tlev);
749 unit_wipe_orders((struct empobj *)&plane);
755 putplane(plane.pln_uid, &plane);
756 pr("%s built in sector %s\n", prplane(&plane),
757 xyas(sp->sct_x, sp->sct_y, player->cnum));
762 build_tower(struct sctstr *sp, short *vec)
774 if (sp->sct_type != SCT_BSPAN) {
775 pr("Bridge towers can only be built from bridge spans.\n");
779 if (sp->sct_effic < 60 && !player->god) {
780 pr("Sector %s is not 60%% efficient.\n",
781 xyas(sp->sct_x, sp->sct_y, player->cnum));
785 if (vec[I_HCM] < buil_tower_bh) {
786 pr("%s only has %d unit%s of hcm,\n",
787 xyas(sp->sct_x, sp->sct_y, player->cnum),
788 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
789 pr("(a bridge tower requires %d)\n", buil_tower_bh);
793 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
795 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
796 if (sp->sct_avail < avail) {
797 pr("Not enough available work in %s to build a bridge tower\n",
798 xyas(sp->sct_x, sp->sct_y, player->cnum));
799 pr(" (%d available work required)\n", avail);
802 if (!player->argp[3]) {
803 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
804 nav_map(sp->sct_x, sp->sct_y, 1);
806 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
810 /* Sanity check time */
811 if (!check_sect_ok(sp))
814 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
815 pr("'%c' is not a valid direction...\n", *p);
816 direrr(NULL, NULL, NULL);
819 newx = sp->sct_x + diroff[val][0];
820 newy = sp->sct_y + diroff[val][1];
821 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
822 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
826 /* Now, check. You aren't allowed to build bridge towers
828 for (i = 1; i <= 6; i++) {
830 nx = sect.sct_x + diroff[i][0];
831 ny = sect.sct_y + diroff[i][1];
832 getsect(nx, ny, &s2);
833 if ((s2.sct_type != SCT_WATER) &&
834 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
835 pr("Bridge towers cannot be built adjacent to land.\n");
836 pr("That sector is adjacent to land.\n");
841 sp->sct_avail -= avail;
842 player->dolcost += buil_tower_bc;
843 sect.sct_type = SCT_BTOWER;
844 sect.sct_newtype = SCT_BTOWER;
845 sect.sct_effic = SCT_MINEFF;
848 sect.sct_defense = 0;
849 if (opt_MOB_ACCESS) {
850 game_tick_to_now(§.sct_access);
851 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
856 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
857 writemap(player->cnum);
859 pr("Bridge tower built in %s\n",
860 xyas(sect.sct_x, sect.sct_y, player->cnum));
861 vec[I_HCM] -= buil_tower_bh;
866 build_can_afford(double cost, char *what)
868 struct natstr *natp = getnatp(player->cnum);
869 if (natp->nat_money < player->dolcost + cost) {
870 pr("Not enough money left to build a %s\n", what);