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 prompt = "Ship type? ";
94 build_it = build_ship;
97 prompt = "Plane type? ";
98 build_it = build_plane;
101 prompt = "Land unit type? ";
102 build_it = build_land;
105 if (!ef_nelem(EF_NUKE_CHR)) {
106 pr("There are no nukes in this game.\n");
109 if (drnuke_const > MIN_DRNUKE_CONST)
111 (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);
129 type = ef_elt_byname(EF_PLANE_CHR, p);
131 rqtech = plchr[type].pl_tech;
136 pr("You can't build that!\n");
137 pr("Use `show plane build %d' to show types you can build.\n",
143 type = ef_elt_byname(EF_SHIP_CHR, p);
145 rqtech = mchr[type].m_tech;
148 if ((mchr[type].m_flags & M_TRADE) && !opt_TRADESHIPS)
152 pr("You can't build that!\n");
153 pr("Use `show ship build %d' to show types you can build.\n",
159 type = ef_elt_byname(EF_LAND_CHR, p);
161 rqtech = lchr[type].l_tech;
164 if ((lchr[type].l_flags & L_SPY) && !opt_LANDSPIES)
168 pr("You can't build that!\n");
169 pr("Use `show land build %d' to show types you can build.\n",
175 type = ef_elt_byname(EF_NUKE_CHR, p);
177 rqtech = nchr[type].n_tech;
182 pr("You can't build that!\n");
183 pr("Use `show nuke build %d' to show types you can build.\n",
194 if (player->argp[4]) {
195 number = atoi(player->argp[4]);
199 "Are you sure that you want to build %s of them? ",
201 p = getstarg(player->argp[6], bstr, buf);
208 if (player->argp[5]) {
209 tlev = atoi(player->argp[5]);
210 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
211 pr("Your tech level is only %d.\n",
212 (int)natp->nat_level[NAT_TLEV]);
216 pr("Required tech is %d.\n", rqtech);
219 pr("building with tech level %d.\n", tlev);
223 while (number-- > 0) {
224 while (nxtsct(&nstr, §)) {
228 if (build_it(§, type, sect.sct_item, tlev))
231 snxtsct_rewind(&nstr);
234 pr("Bad sector specification.\n");
240 build_ship(struct sctstr *sp, int type, short *vec, int tlev)
242 struct mchrstr *mp = &mchr[type];
244 struct nstr_item nstr;
247 double eff = SHIP_MINEFF / 100.0;
251 hcm = roundavg(mp->m_hcm * eff);
252 lcm = roundavg(mp->m_lcm * eff);
254 if (sp->sct_type != SCT_HARBR) {
255 pr("Ships must be built in harbours.\n");
258 if (sp->sct_effic < 60 && !player->god) {
259 pr("Sector %s is not 60%% efficient.\n",
260 xyas(sp->sct_x, sp->sct_y, player->cnum));
263 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
264 pr("Not enough materials in %s\n",
265 xyas(sp->sct_x, sp->sct_y, player->cnum));
268 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
269 if (sp->sct_avail < avail) {
270 pr("Not enough available work in %s to build a %s\n",
271 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
272 pr(" (%d available work required)\n", avail);
275 cost = mp->m_cost * SHIP_MINEFF / 100.0;
276 if (!build_can_afford(cost, mp->m_name))
278 if (!trechk(player->cnum, 0, NEWSHP))
280 if (!check_sect_ok(sp))
282 sp->sct_avail -= avail;
283 player->dolcost += cost;
284 snxtitem_all(&nstr, EF_SHIP);
285 while (nxtitem(&nstr, &ship)) {
286 if (ship.shp_own == 0) {
292 ef_extend(EF_SHIP, 50);
294 ef_blank(EF_SHIP, nstr.cur, &ship);
295 ship.shp_x = sp->sct_x;
296 ship.shp_y = sp->sct_y;
297 ship.shp_own = player->cnum;
298 ship.shp_type = mp - mchr;
299 ship.shp_effic = SHIP_MINEFF;
300 if (opt_MOB_ACCESS) {
301 game_tick_to_now(&ship.shp_access);
302 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
306 memset(ship.shp_item, 0, sizeof(ship.shp_item));
307 ship.shp_pstage = PLG_HEALTHY;
309 ship.shp_name[0] = 0;
310 ship.shp_orig_own = player->cnum;
311 ship.shp_orig_x = sp->sct_x;
312 ship.shp_orig_y = sp->sct_y;
313 shp_set_tech(&ship, tlev);
314 unit_wipe_orders((struct empobj *)&ship);
319 if (sp->sct_pstage == PLG_INFECT)
320 ship.shp_pstage = PLG_EXPOSED;
321 putship(ship.shp_uid, &ship);
322 pr("%s", prship(&ship));
323 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
328 build_land(struct sctstr *sp, int type, short *vec, int tlev)
330 struct lchrstr *lp = &lchr[type];
332 struct nstr_item nstr;
335 double eff = LAND_MINEFF / 100.0;
336 int mil, lcm, hcm, gun, shell;
340 mil = roundavg(lp->l_mil * eff);
341 shell = roundavg(lp->l_shell * eff);
342 gun = roundavg(lp->l_gun * eff);
344 mil = shell = gun = 0;
346 hcm = roundavg(lp->l_hcm * eff);
347 lcm = roundavg(lp->l_lcm * eff);
349 if (sp->sct_type != SCT_HEADQ) {
350 pr("Land units must be built in headquarters.\n");
353 if (sp->sct_effic < 60 && !player->god) {
354 pr("Sector %s is not 60%% efficient.\n",
355 xyas(sp->sct_x, sp->sct_y, player->cnum));
358 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
359 pr("Not enough materials in %s\n",
360 xyas(sp->sct_x, sp->sct_y, player->cnum));
364 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
365 pr("Not enough guns in %s\n",
366 xyas(sp->sct_x, sp->sct_y, player->cnum));
369 if (vec[I_SHELL] < shell) {
370 pr("Not enough shells in %s\n",
371 xyas(sp->sct_x, sp->sct_y, player->cnum));
374 if (vec[I_MILIT] < mil) {
375 pr("Not enough military in %s\n",
376 xyas(sp->sct_x, sp->sct_y, player->cnum));
380 if (!trechk(player->cnum, 0, NEWLND))
382 if (!check_sect_ok(sp))
384 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
385 if (sp->sct_avail < avail) {
386 pr("Not enough available work in %s to build a %s\n",
387 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
388 pr(" (%d available work required)\n", avail);
391 cost = lp->l_cost * LAND_MINEFF / 100.0;
392 if (!build_can_afford(cost, lp->l_name))
394 sp->sct_avail -= avail;
395 player->dolcost += cost;
396 snxtitem_all(&nstr, EF_LAND);
397 while (nxtitem(&nstr, &land)) {
398 if (land.lnd_own == 0) {
404 ef_extend(EF_LAND, 50);
406 ef_blank(EF_LAND, nstr.cur, &land);
407 land.lnd_x = sp->sct_x;
408 land.lnd_y = sp->sct_y;
409 land.lnd_own = player->cnum;
410 land.lnd_type = lp - lchr;
411 land.lnd_effic = LAND_MINEFF;
412 if (opt_MOB_ACCESS) {
413 game_tick_to_now(&land.lnd_access);
414 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
421 memset(land.lnd_item, 0, sizeof(land.lnd_item));
422 land.lnd_pstage = PLG_HEALTHY;
424 lnd_set_tech(&land, tlev);
425 unit_wipe_orders((struct empobj *)&land);
431 vec[I_SHELL] -= shell;
433 if (sp->sct_pstage == PLG_INFECT)
434 land.lnd_pstage = PLG_EXPOSED;
435 putland(nstr.cur, &land);
436 pr("%s", prland(&land));
437 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
442 build_nuke(struct sctstr *sp, int type, short *vec, int tlev)
444 struct nchrstr *np = &nchr[type];
446 struct nstr_item nstr;
450 if (sp->sct_type != SCT_NUKE && !player->god) {
451 pr("Nuclear weapons must be built in nuclear plants.\n");
454 if (sp->sct_effic < 60 && !player->god) {
455 pr("Sector %s is not 60%% efficient.\n",
456 xyas(sp->sct_x, sp->sct_y, player->cnum));
459 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
460 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
461 pr("Not enough materials for a %s bomb in %s\n",
462 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
463 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
464 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
467 if (!build_can_afford(np->n_cost, np->n_name))
469 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
471 * XXX when nukes turn into units (or whatever), then
472 * make them start at 20%. Since they don't have efficiency
473 * now, we charge all the work right away.
475 if (sp->sct_avail < avail) {
476 pr("Not enough available work in %s to build a %s;\n",
477 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
478 pr(" (%d available work required)\n", avail);
481 if (!trechk(player->cnum, 0, NEWNUK))
483 if (!check_sect_ok(sp))
485 sp->sct_avail -= avail;
486 player->dolcost += np->n_cost;
487 snxtitem_all(&nstr, EF_NUKE);
489 while (nxtitem(&nstr, &nuke)) {
490 if (nuke.nuk_own == 0) {
496 ef_extend(EF_NUKE, 50);
498 ef_blank(EF_NUKE, nstr.cur, &nuke);
499 nuke.nuk_x = sp->sct_x;
500 nuke.nuk_y = sp->sct_y;
501 nuke.nuk_own = sp->sct_own;
502 nuke.nuk_type = np - nchr;
503 nuke.nuk_effic = 100;
505 nuke.nuk_tech = tlev;
506 unit_wipe_orders((struct empobj *)&nuke);
508 vec[I_HCM] -= np->n_hcm;
509 vec[I_LCM] -= np->n_lcm;
510 vec[I_OIL] -= np->n_oil;
511 vec[I_RAD] -= np->n_rad;
513 putnuke(nuke.nuk_uid, &nuke);
514 pr("%s created in %s\n", prnuke(&nuke),
515 xyas(sp->sct_x, sp->sct_y, player->cnum));
520 build_plane(struct sctstr *sp, int type, short *vec, int tlev)
522 struct plchrstr *pp = &plchr[type];
524 struct nstr_item nstr;
527 double eff = PLANE_MINEFF / 100.0;
531 mil = roundavg(pp->pl_crew * eff);
532 /* Always use at least 1 mil to build a plane */
533 if (mil == 0 && pp->pl_crew > 0)
535 hcm = roundavg(pp->pl_hcm * eff);
536 lcm = roundavg(pp->pl_lcm * eff);
537 if (sp->sct_type != SCT_AIRPT && !player->god) {
538 pr("Planes must be built in airports.\n");
541 if (sp->sct_effic < 60 && !player->god) {
542 pr("Sector %s is not 60%% efficient.\n",
543 xyas(sp->sct_x, sp->sct_y, player->cnum));
546 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
547 pr("Not enough materials in %s\n",
548 xyas(sp->sct_x, sp->sct_y, player->cnum));
551 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
552 if (sp->sct_avail < avail) {
553 pr("Not enough available work in %s to build a %s\n",
554 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
555 pr(" (%d available work required)\n", avail);
558 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
559 if (!build_can_afford(cost, pp->pl_name))
561 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
562 pr("Not enough military for crew in %s\n",
563 xyas(sp->sct_x, sp->sct_y, player->cnum));
566 if (!trechk(player->cnum, 0, NEWPLN))
568 if (!check_sect_ok(sp))
570 sp->sct_avail -= avail;
571 player->dolcost += cost;
572 snxtitem_all(&nstr, EF_PLANE);
574 while (nxtitem(&nstr, &plane)) {
575 if (plane.pln_own == 0) {
580 if (freeplane == 0) {
581 ef_extend(EF_PLANE, 50);
583 ef_blank(EF_PLANE, nstr.cur, &plane);
584 plane.pln_x = sp->sct_x;
585 plane.pln_y = sp->sct_y;
586 plane.pln_own = sp->sct_own;
587 plane.pln_type = pp - plchr;
588 plane.pln_effic = PLANE_MINEFF;
589 if (opt_MOB_ACCESS) {
590 game_tick_to_now(&plane.pln_access);
591 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
595 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
598 plane.pln_harden = 0;
600 pln_set_tech(&plane, tlev);
601 unit_wipe_orders((struct empobj *)&plane);
607 putplane(plane.pln_uid, &plane);
608 pr("%s built in sector %s\n", prplane(&plane),
609 xyas(sp->sct_x, sp->sct_y, player->cnum));
614 build_bridge(char what)
616 struct natstr *natp = getnatp(player->cnum);
617 struct nstr_sect nstr;
618 int (*build_it)(struct sctstr *, short[]);
624 if (natp->nat_level[NAT_TLEV] < buil_bt) {
625 pr("Building a span requires a tech of %.0f\n", buil_bt);
628 build_it = build_bspan;
631 if (!opt_BRIDGETOWERS) {
632 pr("Bridge tower building is disabled.\n");
635 if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
636 pr("Building a tower requires a tech of %.0f\n",
640 build_it = build_btower;
647 if (!snxtsct(&nstr, player->argp[2]))
650 while (nxtsct(&nstr, §)) {
654 if (build_it(§, sect.sct_item))
658 pr("Bad sector specification.\n");
664 build_bspan(struct sctstr *sp, short *vec)
670 int nx, ny, i, good = 0;
674 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
675 if (sp->sct_type != SCT_BTOWER) {
676 if (sp->sct_type != SCT_BHEAD)
678 if (sp->sct_newtype != SCT_BHEAD)
683 if (sp->sct_effic < 60 && !player->god) {
684 pr("Sector %s is not 60%% efficient.\n",
685 xyas(sp->sct_x, sp->sct_y, player->cnum));
689 if (vec[I_HCM] < buil_bh) {
690 pr("%s only has %d unit%s of hcm,\n",
691 xyas(sp->sct_x, sp->sct_y, player->cnum),
692 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
693 pr("(a bridge span requires %d)\n", buil_bh);
697 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
699 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
700 if (sp->sct_avail < avail) {
701 pr("Not enough available work in %s to build a bridge\n",
702 xyas(sp->sct_x, sp->sct_y, player->cnum));
703 pr(" (%d available work required)\n", avail);
706 if (!player->argp[3]) {
707 pr("Bridge head at %s\n",
708 xyas(sp->sct_x, sp->sct_y, player->cnum));
709 nav_map(sp->sct_x, sp->sct_y, 1);
711 p = getstarg(player->argp[3], "build span in what direction? ", buf);
715 /* Sanity check time */
716 if (!check_sect_ok(sp))
719 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
720 pr("'%c' is not a valid direction...\n", *p);
721 direrr(NULL, NULL, NULL);
724 newx = sp->sct_x + diroff[val][0];
725 newy = sp->sct_y + diroff[val][1];
726 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
727 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
730 if (opt_EASY_BRIDGES) {
733 for (i = 1; i <= 6; i++) {
735 nx = sect.sct_x + diroff[i][0];
736 ny = sect.sct_y + diroff[i][1];
737 getsect(nx, ny, &s2);
738 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
742 pr("Bridges must be built adjacent to land or bridge towers.\n");
743 pr("That sector is not adjacent to land or a bridge tower.\n");
746 } /* end EASY_BRIDGES */
747 sp->sct_avail -= avail;
748 player->dolcost += buil_bc;
749 sect.sct_type = SCT_BSPAN;
750 sect.sct_newtype = SCT_BSPAN;
751 sect.sct_effic = SCT_MINEFF;
754 sect.sct_defense = 0;
755 if (opt_MOB_ACCESS) {
756 game_tick_to_now(§.sct_access);
757 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
762 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
763 writemap(player->cnum);
765 pr("Bridge span built over %s\n",
766 xyas(sect.sct_x, sect.sct_y, player->cnum));
767 vec[I_HCM] -= buil_bh;
772 build_btower(struct sctstr *sp, short *vec)
784 if (sp->sct_type != SCT_BSPAN) {
785 pr("Bridge towers can only be built from bridge spans.\n");
789 if (sp->sct_effic < 60 && !player->god) {
790 pr("Sector %s is not 60%% efficient.\n",
791 xyas(sp->sct_x, sp->sct_y, player->cnum));
795 if (vec[I_HCM] < buil_tower_bh) {
796 pr("%s only has %d unit%s of hcm,\n",
797 xyas(sp->sct_x, sp->sct_y, player->cnum),
798 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
799 pr("(a bridge tower requires %d)\n", buil_tower_bh);
803 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
805 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
806 if (sp->sct_avail < avail) {
807 pr("Not enough available work in %s to build a bridge tower\n",
808 xyas(sp->sct_x, sp->sct_y, player->cnum));
809 pr(" (%d available work required)\n", avail);
812 if (!player->argp[3]) {
813 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
814 nav_map(sp->sct_x, sp->sct_y, 1);
816 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
820 /* Sanity check time */
821 if (!check_sect_ok(sp))
824 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
825 pr("'%c' is not a valid direction...\n", *p);
826 direrr(NULL, NULL, NULL);
829 newx = sp->sct_x + diroff[val][0];
830 newy = sp->sct_y + diroff[val][1];
831 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
832 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
836 /* Now, check. You aren't allowed to build bridge towers
838 for (i = 1; i <= 6; i++) {
840 nx = sect.sct_x + diroff[i][0];
841 ny = sect.sct_y + diroff[i][1];
842 getsect(nx, ny, &s2);
843 if ((s2.sct_type != SCT_WATER) &&
844 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
845 pr("Bridge towers cannot be built adjacent to land.\n");
846 pr("That sector is adjacent to land.\n");
851 sp->sct_avail -= avail;
852 player->dolcost += buil_tower_bc;
853 sect.sct_type = SCT_BTOWER;
854 sect.sct_newtype = SCT_BTOWER;
855 sect.sct_effic = SCT_MINEFF;
858 sect.sct_defense = 0;
859 if (opt_MOB_ACCESS) {
860 game_tick_to_now(§.sct_access);
861 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
866 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
867 writemap(player->cnum);
869 pr("Bridge tower built in %s\n",
870 xyas(sect.sct_x, sect.sct_y, player->cnum));
871 vec[I_HCM] -= buil_tower_bh;
876 build_can_afford(double cost, char *what)
878 struct natstr *natp = getnatp(player->cnum);
879 if (natp->nat_money < player->dolcost + cost) {
880 pr("Not enough money left to build a %s\n", what);