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_plane(struct sctstr *sp,
58 struct plchrstr *pp, short *vec, int tlev);
59 static int build_bridge(char);
60 static int build_bspan(struct sctstr *sp, short *vec);
61 static int build_btower(struct sctstr *sp, short *vec);
62 static int build_can_afford(double, char *);
65 * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
71 struct nstr_sect nstr;
88 natp = getnatp(player->cnum);
89 p = getstarg(player->argp[1],
90 "Build (ship, nuke, bridge, plane, land unit, tower)? ",
98 return build_bridge(what);
104 if (!ef_nelem(EF_NUKE_CHR)) {
105 pr("There are no nukes in this game.\n");
110 pr("You can't build that!\n");
114 if (!snxtsct(&nstr, player->argp[2]))
116 tlev = (int)natp->nat_level[NAT_TLEV];
117 rlev = (int)natp->nat_level[NAT_RLEV];
121 p = getstarg(player->argp[3], "Plane type? ", buf);
124 type = ef_elt_byname(EF_PLANE_CHR, p);
127 rqtech = pp->pl_tech;
132 pr("You can't build that!\n");
133 pr("Use `show plane build %d' to show types you can build.\n",
139 p = getstarg(player->argp[3], "Ship type? ", buf);
142 type = ef_elt_byname(EF_SHIP_CHR, p);
148 if ((mp->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 p = getstarg(player->argp[3], "Land unit type? ", buf);
162 type = ef_elt_byname(EF_LAND_CHR, p);
168 if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
172 pr("You can't build that!\n");
173 pr("Use `show land build %d' to show types you can build.\n",
179 p = getstarg(player->argp[3], "Nuke type? ", buf);
182 type = ef_elt_byname(EF_NUKE_CHR, p);
187 || (drnuke_const > MIN_DRNUKE_CONST &&
188 np->n_tech * drnuke_const > rlev))
193 if (drnuke_const > MIN_DRNUKE_CONST)
194 tt = (tlev < (rlev / drnuke_const) ? (int)tlev :
195 (int)(rlev / drnuke_const));
196 pr("You can't build that!\n");
197 pr("Use `show nuke build %d' to show types you can build.\n",
208 if (player->argp[4]) {
209 number = atoi(player->argp[4]);
213 "Are you sure that you want to build %s of them? ",
215 p = getstarg(player->argp[6], bstr, buf);
222 if (player->argp[5]) {
223 tlev = atoi(player->argp[5]);
224 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
225 pr("Your tech level is only %d.\n",
226 (int)natp->nat_level[NAT_TLEV]);
230 pr("Required tech is %d.\n", rqtech);
233 pr("building with tech level %d.\n", tlev);
237 while (number-- > 0) {
238 while (nxtsct(&nstr, §)) {
244 built = build_land(§, lp, sect.sct_item, tlev);
247 built = build_ship(§, mp, sect.sct_item, tlev);
250 built = build_nuke(§, np, sect.sct_item, tlev);
253 built = build_plane(§, pp, sect.sct_item, tlev);
263 snxtsct_rewind(&nstr);
266 pr("Bad sector specification.\n");
272 build_ship(struct sctstr *sp, struct mchrstr *mp, short *vec, int tlev)
275 struct nstr_item nstr;
278 double eff = SHIP_MINEFF / 100.0;
282 hcm = roundavg(mp->m_hcm * eff);
283 lcm = roundavg(mp->m_lcm * eff);
285 if (sp->sct_type != SCT_HARBR) {
286 pr("Ships must be built in harbours.\n");
289 if (sp->sct_effic < 60 && !player->god) {
290 pr("Sector %s is not 60%% efficient.\n",
291 xyas(sp->sct_x, sp->sct_y, player->cnum));
294 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
295 pr("Not enough materials in %s\n",
296 xyas(sp->sct_x, sp->sct_y, player->cnum));
299 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
300 if (sp->sct_avail < avail) {
301 pr("Not enough available work in %s to build a %s\n",
302 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
303 pr(" (%d available work required)\n", avail);
306 cost = mp->m_cost * SHIP_MINEFF / 100.0;
307 if (!build_can_afford(cost, mp->m_name))
309 if (!trechk(player->cnum, 0, NEWSHP))
311 if (!check_sect_ok(sp))
313 sp->sct_avail -= avail;
314 player->dolcost += cost;
315 snxtitem_all(&nstr, EF_SHIP);
316 while (nxtitem(&nstr, &ship)) {
317 if (ship.shp_own == 0) {
323 ef_extend(EF_SHIP, 50);
325 ef_blank(EF_SHIP, nstr.cur, &ship);
326 ship.shp_x = sp->sct_x;
327 ship.shp_y = sp->sct_y;
328 ship.shp_own = player->cnum;
329 ship.shp_type = mp - mchr;
330 ship.shp_effic = SHIP_MINEFF;
331 if (opt_MOB_ACCESS) {
332 game_tick_to_now(&ship.shp_access);
333 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
337 memset(ship.shp_item, 0, sizeof(ship.shp_item));
338 ship.shp_pstage = PLG_HEALTHY;
340 ship.shp_name[0] = 0;
341 ship.shp_orig_own = player->cnum;
342 ship.shp_orig_x = sp->sct_x;
343 ship.shp_orig_y = sp->sct_y;
344 shp_set_tech(&ship, tlev);
345 unit_wipe_orders((struct empobj *)&ship);
350 if (sp->sct_pstage == PLG_INFECT)
351 ship.shp_pstage = PLG_EXPOSED;
352 putship(ship.shp_uid, &ship);
353 pr("%s", prship(&ship));
354 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
359 build_land(struct sctstr *sp, struct lchrstr *lp, short *vec, int tlev)
362 struct nstr_item nstr;
365 double eff = LAND_MINEFF / 100.0;
366 int mil, lcm, hcm, gun, shell;
370 mil = roundavg(lp->l_mil * eff);
371 shell = roundavg(lp->l_shell * eff);
372 gun = roundavg(lp->l_gun * eff);
374 mil = shell = gun = 0;
376 hcm = roundavg(lp->l_hcm * eff);
377 lcm = roundavg(lp->l_lcm * eff);
379 if (sp->sct_type != SCT_HEADQ) {
380 pr("Land units must be built in headquarters.\n");
383 if (sp->sct_effic < 60 && !player->god) {
384 pr("Sector %s is not 60%% efficient.\n",
385 xyas(sp->sct_x, sp->sct_y, player->cnum));
388 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
389 pr("Not enough materials in %s\n",
390 xyas(sp->sct_x, sp->sct_y, player->cnum));
394 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
395 pr("Not enough guns in %s\n",
396 xyas(sp->sct_x, sp->sct_y, player->cnum));
399 if (vec[I_SHELL] < shell) {
400 pr("Not enough shells in %s\n",
401 xyas(sp->sct_x, sp->sct_y, player->cnum));
404 if (vec[I_MILIT] < mil) {
405 pr("Not enough military in %s\n",
406 xyas(sp->sct_x, sp->sct_y, player->cnum));
410 if (!trechk(player->cnum, 0, NEWLND))
412 if (!check_sect_ok(sp))
414 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
415 if (sp->sct_avail < avail) {
416 pr("Not enough available work in %s to build a %s\n",
417 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
418 pr(" (%d available work required)\n", avail);
421 cost = lp->l_cost * LAND_MINEFF / 100.0;
422 if (!build_can_afford(cost, lp->l_name))
424 sp->sct_avail -= avail;
425 player->dolcost += cost;
426 snxtitem_all(&nstr, EF_LAND);
427 while (nxtitem(&nstr, &land)) {
428 if (land.lnd_own == 0) {
434 ef_extend(EF_LAND, 50);
436 ef_blank(EF_LAND, nstr.cur, &land);
437 land.lnd_x = sp->sct_x;
438 land.lnd_y = sp->sct_y;
439 land.lnd_own = player->cnum;
440 land.lnd_type = lp - lchr;
441 land.lnd_effic = LAND_MINEFF;
442 if (opt_MOB_ACCESS) {
443 game_tick_to_now(&land.lnd_access);
444 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
451 memset(land.lnd_item, 0, sizeof(land.lnd_item));
452 land.lnd_pstage = PLG_HEALTHY;
454 lnd_set_tech(&land, tlev);
455 unit_wipe_orders((struct empobj *)&land);
461 vec[I_SHELL] -= shell;
463 if (sp->sct_pstage == PLG_INFECT)
464 land.lnd_pstage = PLG_EXPOSED;
465 putland(nstr.cur, &land);
466 pr("%s", prland(&land));
467 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
472 build_nuke(struct sctstr *sp, struct nchrstr *np, short *vec, int tlev)
475 struct nstr_item nstr;
479 if (sp->sct_type != SCT_NUKE && !player->god) {
480 pr("Nuclear weapons must be built in nuclear plants.\n");
483 if (sp->sct_effic < 60 && !player->god) {
484 pr("Sector %s is not 60%% efficient.\n",
485 xyas(sp->sct_x, sp->sct_y, player->cnum));
488 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
489 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
490 pr("Not enough materials for a %s bomb in %s\n",
491 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
492 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
493 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
496 if (!build_can_afford(np->n_cost, np->n_name))
498 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
500 * XXX when nukes turn into units (or whatever), then
501 * make them start at 20%. Since they don't have efficiency
502 * now, we charge all the work right away.
504 if (sp->sct_avail < avail) {
505 pr("Not enough available work in %s to build a %s;\n",
506 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
507 pr(" (%d available work required)\n", avail);
510 if (!trechk(player->cnum, 0, NEWNUK))
512 if (!check_sect_ok(sp))
514 sp->sct_avail -= avail;
515 player->dolcost += np->n_cost;
516 snxtitem_all(&nstr, EF_NUKE);
518 while (nxtitem(&nstr, &nuke)) {
519 if (nuke.nuk_own == 0) {
525 ef_extend(EF_NUKE, 50);
527 ef_blank(EF_NUKE, nstr.cur, &nuke);
528 nuke.nuk_x = sp->sct_x;
529 nuke.nuk_y = sp->sct_y;
530 nuke.nuk_own = sp->sct_own;
531 nuke.nuk_type = np - nchr;
532 nuke.nuk_effic = 100;
534 nuke.nuk_tech = tlev;
535 unit_wipe_orders((struct empobj *)&nuke);
537 vec[I_HCM] -= np->n_hcm;
538 vec[I_LCM] -= np->n_lcm;
539 vec[I_OIL] -= np->n_oil;
540 vec[I_RAD] -= np->n_rad;
542 putnuke(nuke.nuk_uid, &nuke);
543 pr("%s created in %s\n", prnuke(&nuke),
544 xyas(sp->sct_x, sp->sct_y, player->cnum));
549 build_plane(struct sctstr *sp, struct plchrstr *pp, short *vec, int tlev)
552 struct nstr_item nstr;
555 double eff = PLANE_MINEFF / 100.0;
559 mil = roundavg(pp->pl_crew * eff);
560 /* Always use at least 1 mil to build a plane */
561 if (mil == 0 && pp->pl_crew > 0)
563 hcm = roundavg(pp->pl_hcm * eff);
564 lcm = roundavg(pp->pl_lcm * eff);
565 if (sp->sct_type != SCT_AIRPT && !player->god) {
566 pr("Planes must be built in airports.\n");
569 if (sp->sct_effic < 60 && !player->god) {
570 pr("Sector %s is not 60%% efficient.\n",
571 xyas(sp->sct_x, sp->sct_y, player->cnum));
574 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
575 pr("Not enough materials in %s\n",
576 xyas(sp->sct_x, sp->sct_y, player->cnum));
579 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
580 if (sp->sct_avail < avail) {
581 pr("Not enough available work in %s to build a %s\n",
582 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
583 pr(" (%d available work required)\n", avail);
586 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
587 if (!build_can_afford(cost, pp->pl_name))
589 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
590 pr("Not enough military for crew in %s\n",
591 xyas(sp->sct_x, sp->sct_y, player->cnum));
594 if (!trechk(player->cnum, 0, NEWPLN))
596 if (!check_sect_ok(sp))
598 sp->sct_avail -= avail;
599 player->dolcost += cost;
600 snxtitem_all(&nstr, EF_PLANE);
602 while (nxtitem(&nstr, &plane)) {
603 if (plane.pln_own == 0) {
608 if (freeplane == 0) {
609 ef_extend(EF_PLANE, 50);
611 ef_blank(EF_PLANE, nstr.cur, &plane);
612 plane.pln_x = sp->sct_x;
613 plane.pln_y = sp->sct_y;
614 plane.pln_own = sp->sct_own;
615 plane.pln_type = pp - plchr;
616 plane.pln_effic = PLANE_MINEFF;
617 if (opt_MOB_ACCESS) {
618 game_tick_to_now(&plane.pln_access);
619 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
623 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
626 plane.pln_harden = 0;
628 pln_set_tech(&plane, tlev);
629 unit_wipe_orders((struct empobj *)&plane);
635 putplane(plane.pln_uid, &plane);
636 pr("%s built in sector %s\n", prplane(&plane),
637 xyas(sp->sct_x, sp->sct_y, player->cnum));
642 build_bridge(char what)
644 struct natstr *natp = getnatp(player->cnum);
645 struct nstr_sect nstr;
646 int (*build_it)(struct sctstr *, short[]);
652 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_bt) {
653 pr("Building a span requires a tech of %.0f\n", buil_bt);
656 build_it = build_bspan;
659 if (!opt_BRIDGETOWERS) {
660 pr("Bridge tower building is disabled.\n");
663 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_tower_bt) {
664 pr("Building a tower requires a tech of %.0f\n",
668 build_it = build_btower;
675 if (!snxtsct(&nstr, player->argp[2]))
678 while (nxtsct(&nstr, §)) {
682 if (build_it(§, sect.sct_item))
686 pr("Bad sector specification.\n");
692 build_bspan(struct sctstr *sp, short *vec)
698 int nx, ny, i, good = 0;
702 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
703 if (sp->sct_type != SCT_BTOWER) {
704 if (sp->sct_type != SCT_BHEAD)
706 if (sp->sct_newtype != SCT_BHEAD)
711 if (sp->sct_effic < 60 && !player->god) {
712 pr("Sector %s is not 60%% efficient.\n",
713 xyas(sp->sct_x, sp->sct_y, player->cnum));
717 if (vec[I_HCM] < buil_bh) {
718 pr("%s only has %d unit%s of hcm,\n",
719 xyas(sp->sct_x, sp->sct_y, player->cnum),
720 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
721 pr("(a bridge span requires %d)\n", buil_bh);
725 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
727 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
728 if (sp->sct_avail < avail) {
729 pr("Not enough available work in %s to build a bridge\n",
730 xyas(sp->sct_x, sp->sct_y, player->cnum));
731 pr(" (%d available work required)\n", avail);
734 if (!player->argp[3]) {
735 pr("Bridge head at %s\n",
736 xyas(sp->sct_x, sp->sct_y, player->cnum));
737 nav_map(sp->sct_x, sp->sct_y, 1);
739 p = getstarg(player->argp[3], "build span in what direction? ", buf);
743 /* Sanity check time */
744 if (!check_sect_ok(sp))
747 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
748 pr("'%c' is not a valid direction...\n", *p);
749 direrr(NULL, NULL, NULL);
752 newx = sp->sct_x + diroff[val][0];
753 newy = sp->sct_y + diroff[val][1];
754 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
755 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
758 if (opt_EASY_BRIDGES) {
761 for (i = 1; i <= 6; i++) {
763 nx = sect.sct_x + diroff[i][0];
764 ny = sect.sct_y + diroff[i][1];
765 getsect(nx, ny, &s2);
766 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
770 pr("Bridges must be built adjacent to land or bridge towers.\n");
771 pr("That sector is not adjacent to land or a bridge tower.\n");
774 } /* end EASY_BRIDGES */
775 sp->sct_avail -= avail;
776 player->dolcost += buil_bc;
777 sect.sct_type = SCT_BSPAN;
778 sect.sct_newtype = SCT_BSPAN;
779 sect.sct_effic = SCT_MINEFF;
782 sect.sct_defense = 0;
783 if (opt_MOB_ACCESS) {
784 game_tick_to_now(§.sct_access);
785 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
790 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
791 writemap(player->cnum);
793 pr("Bridge span built over %s\n",
794 xyas(sect.sct_x, sect.sct_y, player->cnum));
795 vec[I_HCM] -= buil_bh;
800 build_btower(struct sctstr *sp, short *vec)
812 if (sp->sct_type != SCT_BSPAN) {
813 pr("Bridge towers can only be built from bridge spans.\n");
817 if (sp->sct_effic < 60 && !player->god) {
818 pr("Sector %s is not 60%% efficient.\n",
819 xyas(sp->sct_x, sp->sct_y, player->cnum));
823 if (vec[I_HCM] < buil_tower_bh) {
824 pr("%s only has %d unit%s of hcm,\n",
825 xyas(sp->sct_x, sp->sct_y, player->cnum),
826 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
827 pr("(a bridge tower requires %d)\n", buil_tower_bh);
831 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
833 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
834 if (sp->sct_avail < avail) {
835 pr("Not enough available work in %s to build a bridge tower\n",
836 xyas(sp->sct_x, sp->sct_y, player->cnum));
837 pr(" (%d available work required)\n", avail);
840 if (!player->argp[3]) {
841 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
842 nav_map(sp->sct_x, sp->sct_y, 1);
844 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
848 /* Sanity check time */
849 if (!check_sect_ok(sp))
852 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
853 pr("'%c' is not a valid direction...\n", *p);
854 direrr(NULL, NULL, NULL);
857 newx = sp->sct_x + diroff[val][0];
858 newy = sp->sct_y + diroff[val][1];
859 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
860 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
864 /* Now, check. You aren't allowed to build bridge towers
866 for (i = 1; i <= 6; i++) {
868 nx = sect.sct_x + diroff[i][0];
869 ny = sect.sct_y + diroff[i][1];
870 getsect(nx, ny, &s2);
871 if ((s2.sct_type != SCT_WATER) &&
872 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
873 pr("Bridge towers cannot be built adjacent to land.\n");
874 pr("That sector is adjacent to land.\n");
879 sp->sct_avail -= avail;
880 player->dolcost += buil_tower_bc;
881 sect.sct_type = SCT_BTOWER;
882 sect.sct_newtype = SCT_BTOWER;
883 sect.sct_effic = SCT_MINEFF;
886 sect.sct_defense = 0;
887 if (opt_MOB_ACCESS) {
888 game_tick_to_now(§.sct_access);
889 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
894 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
895 writemap(player->cnum);
897 pr("Bridge tower built in %s\n",
898 xyas(sect.sct_x, sect.sct_y, player->cnum));
899 vec[I_HCM] -= buil_tower_bh;
904 build_can_afford(double cost, char *what)
906 struct natstr *natp = getnatp(player->cnum);
907 if (natp->nat_money < player->dolcost + cost) {
908 pr("Not enough money left to build a %s\n", what);