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)? ",
96 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_bt) {
97 pr("Building a span requires a tech of %.0f\n", buil_bt);
102 if (!opt_BRIDGETOWERS) {
103 pr("Bridge tower building is disabled.\n");
106 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_tower_bt) {
107 pr("Building a tower requires a tech of %.0f\n",
117 if (!ef_nelem(EF_NUKE_CHR)) {
118 pr("There are no nukes in this game.\n");
123 pr("You can't build that!\n");
127 if (!snxtsct(&nstr, player->argp[2]))
129 tlev = (int)natp->nat_level[NAT_TLEV];
130 rlev = (int)natp->nat_level[NAT_RLEV];
134 p = getstarg(player->argp[3], "Plane type? ", buf);
137 type = ef_elt_byname(EF_PLANE_CHR, p);
140 rqtech = pp->pl_tech;
145 pr("You can't build that!\n");
146 pr("Use `show plane build %d' to show types you can build.\n",
152 p = getstarg(player->argp[3], "Ship type? ", buf);
155 type = ef_elt_byname(EF_SHIP_CHR, p);
161 if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
165 pr("You can't build that!\n");
166 pr("Use `show ship build %d' to show types you can build.\n",
172 p = getstarg(player->argp[3], "Land unit type? ", buf);
175 type = ef_elt_byname(EF_LAND_CHR, p);
181 if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
185 pr("You can't build that!\n");
186 pr("Use `show land build %d' to show types you can build.\n",
195 p = getstarg(player->argp[3], "Nuke type? ", buf);
198 type = ef_elt_byname(EF_NUKE_CHR, p);
203 || (drnuke_const > MIN_DRNUKE_CONST &&
204 np->n_tech * drnuke_const > rlev))
209 if (drnuke_const > MIN_DRNUKE_CONST)
210 tt = (tlev < (rlev / drnuke_const) ? (int)tlev :
211 (int)(rlev / drnuke_const));
212 pr("You can't build that!\n");
213 pr("Use `show nuke build %d' to show types you can build.\n",
224 if (what != 'b' && what != 't') {
225 if (player->argp[4]) {
226 number = atoi(player->argp[4]);
230 "Are you sure that you want to build %s of them? ",
232 p = getstarg(player->argp[6], bstr, buf);
239 if (what != 'b' && what != 'n' && what != 't') {
240 if (player->argp[5]) {
241 tlev = atoi(player->argp[5]);
242 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
243 pr("Your tech level is only %d.\n",
244 (int)natp->nat_level[NAT_TLEV]);
248 pr("Required tech is %d.\n", rqtech);
251 pr("building with tech level %d.\n", tlev);
255 while (number-- > 0) {
256 while (nxtsct(&nstr, §)) {
262 built = build_land(§, lp, sect.sct_item, tlev);
265 built = build_ship(§, mp, sect.sct_item, tlev);
268 built = build_bridge(§, sect.sct_item);
271 built = build_tower(§, sect.sct_item);
274 built = build_nuke(§, np, sect.sct_item, tlev);
277 built = build_plane(§, pp, sect.sct_item, tlev);
287 snxtsct_rewind(&nstr);
290 pr("Bad sector specification.\n");
296 build_ship(struct sctstr *sp, struct mchrstr *mp, short *vec, int tlev)
299 struct nstr_item nstr;
302 double eff = SHIP_MINEFF / 100.0;
306 hcm = roundavg(mp->m_hcm * eff);
307 lcm = roundavg(mp->m_lcm * eff);
309 if (sp->sct_type != SCT_HARBR) {
310 pr("Ships must be built in harbours.\n");
313 if (sp->sct_effic < 60 && !player->god) {
314 pr("Sector %s is not 60%% efficient.\n",
315 xyas(sp->sct_x, sp->sct_y, player->cnum));
318 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
319 pr("Not enough materials in %s\n",
320 xyas(sp->sct_x, sp->sct_y, player->cnum));
323 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
324 if (sp->sct_avail < avail) {
325 pr("Not enough available work in %s to build a %s\n",
326 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
327 pr(" (%d available work required)\n", avail);
330 cost = mp->m_cost * SHIP_MINEFF / 100.0;
331 if (!build_can_afford(cost, mp->m_name))
333 if (!trechk(player->cnum, 0, NEWSHP))
335 if (!check_sect_ok(sp))
337 sp->sct_avail -= avail;
338 player->dolcost += cost;
339 snxtitem_all(&nstr, EF_SHIP);
340 while (nxtitem(&nstr, &ship)) {
341 if (ship.shp_own == 0) {
347 ef_extend(EF_SHIP, 50);
349 ef_blank(EF_SHIP, nstr.cur, &ship);
350 ship.shp_x = sp->sct_x;
351 ship.shp_y = sp->sct_y;
352 ship.shp_own = player->cnum;
353 ship.shp_type = mp - mchr;
354 ship.shp_effic = SHIP_MINEFF;
355 if (opt_MOB_ACCESS) {
356 game_tick_to_now(&ship.shp_access);
357 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
361 memset(ship.shp_item, 0, sizeof(ship.shp_item));
362 ship.shp_pstage = PLG_HEALTHY;
364 ship.shp_name[0] = 0;
365 ship.shp_orig_own = player->cnum;
366 ship.shp_orig_x = sp->sct_x;
367 ship.shp_orig_y = sp->sct_y;
368 shp_set_tech(&ship, tlev);
369 unit_wipe_orders((struct empobj *)&ship);
374 if (sp->sct_pstage == PLG_INFECT)
375 ship.shp_pstage = PLG_EXPOSED;
376 putship(ship.shp_uid, &ship);
377 pr("%s", prship(&ship));
378 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
383 build_land(struct sctstr *sp, struct lchrstr *lp, short *vec, int tlev)
386 struct nstr_item nstr;
389 double eff = LAND_MINEFF / 100.0;
390 int mil, lcm, hcm, gun, shell;
394 mil = roundavg(lp->l_mil * eff);
395 shell = roundavg(lp->l_shell * eff);
396 gun = roundavg(lp->l_gun * eff);
398 mil = shell = gun = 0;
400 hcm = roundavg(lp->l_hcm * eff);
401 lcm = roundavg(lp->l_lcm * eff);
403 if (sp->sct_type != SCT_HEADQ) {
404 pr("Land units must be built in headquarters.\n");
407 if (sp->sct_effic < 60 && !player->god) {
408 pr("Sector %s is not 60%% efficient.\n",
409 xyas(sp->sct_x, sp->sct_y, player->cnum));
412 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
413 pr("Not enough materials in %s\n",
414 xyas(sp->sct_x, sp->sct_y, player->cnum));
418 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
419 pr("Not enough guns in %s\n",
420 xyas(sp->sct_x, sp->sct_y, player->cnum));
423 if (vec[I_SHELL] < shell) {
424 pr("Not enough shells in %s\n",
425 xyas(sp->sct_x, sp->sct_y, player->cnum));
428 if (vec[I_MILIT] < mil) {
429 pr("Not enough military in %s\n",
430 xyas(sp->sct_x, sp->sct_y, player->cnum));
434 if (!trechk(player->cnum, 0, NEWLND))
436 if (!check_sect_ok(sp))
438 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
439 if (sp->sct_avail < avail) {
440 pr("Not enough available work in %s to build a %s\n",
441 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
442 pr(" (%d available work required)\n", avail);
445 cost = lp->l_cost * LAND_MINEFF / 100.0;
446 if (!build_can_afford(cost, lp->l_name))
448 sp->sct_avail -= avail;
449 player->dolcost += cost;
450 snxtitem_all(&nstr, EF_LAND);
451 while (nxtitem(&nstr, &land)) {
452 if (land.lnd_own == 0) {
458 ef_extend(EF_LAND, 50);
460 ef_blank(EF_LAND, nstr.cur, &land);
461 land.lnd_x = sp->sct_x;
462 land.lnd_y = sp->sct_y;
463 land.lnd_own = player->cnum;
464 land.lnd_type = lp - lchr;
465 land.lnd_effic = LAND_MINEFF;
466 if (opt_MOB_ACCESS) {
467 game_tick_to_now(&land.lnd_access);
468 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
475 memset(land.lnd_item, 0, sizeof(land.lnd_item));
476 land.lnd_pstage = PLG_HEALTHY;
478 lnd_set_tech(&land, tlev);
479 unit_wipe_orders((struct empobj *)&land);
485 vec[I_SHELL] -= shell;
487 if (sp->sct_pstage == PLG_INFECT)
488 land.lnd_pstage = PLG_EXPOSED;
489 putland(nstr.cur, &land);
490 pr("%s", prland(&land));
491 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
496 build_bridge(struct sctstr *sp, short *vec)
502 int nx, ny, i, good = 0;
506 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
507 if (sp->sct_type != SCT_BTOWER) {
508 if (sp->sct_type != SCT_BHEAD)
510 if (sp->sct_newtype != SCT_BHEAD)
515 if (sp->sct_effic < 60 && !player->god) {
516 pr("Sector %s is not 60%% efficient.\n",
517 xyas(sp->sct_x, sp->sct_y, player->cnum));
521 if (vec[I_HCM] < buil_bh) {
522 pr("%s only has %d unit%s of hcm,\n",
523 xyas(sp->sct_x, sp->sct_y, player->cnum),
524 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
525 pr("(a bridge span requires %d)\n", buil_bh);
529 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
531 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
532 if (sp->sct_avail < avail) {
533 pr("Not enough available work in %s to build a bridge\n",
534 xyas(sp->sct_x, sp->sct_y, player->cnum));
535 pr(" (%d available work required)\n", avail);
538 if (!player->argp[3]) {
539 pr("Bridge head at %s\n",
540 xyas(sp->sct_x, sp->sct_y, player->cnum));
541 nav_map(sp->sct_x, sp->sct_y, 1);
543 p = getstarg(player->argp[3], "build span in what direction? ", buf);
547 /* Sanity check time */
548 if (!check_sect_ok(sp))
551 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
552 pr("'%c' is not a valid direction...\n", *p);
553 direrr(NULL, NULL, NULL);
556 newx = sp->sct_x + diroff[val][0];
557 newy = sp->sct_y + diroff[val][1];
558 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
559 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
562 if (opt_EASY_BRIDGES) {
565 for (i = 1; i <= 6; i++) {
567 nx = sect.sct_x + diroff[i][0];
568 ny = sect.sct_y + diroff[i][1];
569 getsect(nx, ny, &s2);
570 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
574 pr("Bridges must be built adjacent to land or bridge towers.\n");
575 pr("That sector is not adjacent to land or a bridge tower.\n");
578 } /* end EASY_BRIDGES */
579 sp->sct_avail -= avail;
580 player->dolcost += buil_bc;
581 sect.sct_type = SCT_BSPAN;
582 sect.sct_newtype = SCT_BSPAN;
583 sect.sct_effic = SCT_MINEFF;
586 sect.sct_defense = 0;
587 if (opt_MOB_ACCESS) {
588 game_tick_to_now(§.sct_access);
589 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
594 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
595 writemap(player->cnum);
597 pr("Bridge span built over %s\n",
598 xyas(sect.sct_x, sect.sct_y, player->cnum));
599 vec[I_HCM] -= buil_bh;
604 build_nuke(struct sctstr *sp, struct nchrstr *np, short *vec, int tlev)
607 struct nstr_item nstr;
611 if (sp->sct_type != SCT_NUKE && !player->god) {
612 pr("Nuclear weapons must be built in nuclear plants.\n");
615 if (sp->sct_effic < 60 && !player->god) {
616 pr("Sector %s is not 60%% efficient.\n",
617 xyas(sp->sct_x, sp->sct_y, player->cnum));
620 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
621 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
622 pr("Not enough materials for a %s bomb in %s\n",
623 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
624 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
625 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
628 if (!build_can_afford(np->n_cost, np->n_name))
630 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
632 * XXX when nukes turn into units (or whatever), then
633 * make them start at 20%. Since they don't have efficiency
634 * now, we charge all the work right away.
636 if (sp->sct_avail < avail) {
637 pr("Not enough available work in %s to build a %s;\n",
638 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
639 pr(" (%d available work required)\n", avail);
642 if (!trechk(player->cnum, 0, NEWNUK))
644 if (!check_sect_ok(sp))
646 sp->sct_avail -= avail;
647 player->dolcost += np->n_cost;
648 snxtitem_all(&nstr, EF_NUKE);
650 while (nxtitem(&nstr, &nuke)) {
651 if (nuke.nuk_own == 0) {
657 ef_extend(EF_NUKE, 50);
659 ef_blank(EF_NUKE, nstr.cur, &nuke);
660 nuke.nuk_x = sp->sct_x;
661 nuke.nuk_y = sp->sct_y;
662 nuke.nuk_own = sp->sct_own;
663 nuke.nuk_type = np - nchr;
664 nuke.nuk_effic = 100;
666 nuke.nuk_tech = tlev;
667 unit_wipe_orders((struct empobj *)&nuke);
669 vec[I_HCM] -= np->n_hcm;
670 vec[I_LCM] -= np->n_lcm;
671 vec[I_OIL] -= np->n_oil;
672 vec[I_RAD] -= np->n_rad;
674 putnuke(nuke.nuk_uid, &nuke);
675 pr("%s created in %s\n", prnuke(&nuke),
676 xyas(sp->sct_x, sp->sct_y, player->cnum));
681 build_plane(struct sctstr *sp, struct plchrstr *pp, short *vec, int tlev)
684 struct nstr_item nstr;
687 double eff = PLANE_MINEFF / 100.0;
691 mil = roundavg(pp->pl_crew * eff);
692 /* Always use at least 1 mil to build a plane */
693 if (mil == 0 && pp->pl_crew > 0)
695 hcm = roundavg(pp->pl_hcm * eff);
696 lcm = roundavg(pp->pl_lcm * eff);
697 if (sp->sct_type != SCT_AIRPT && !player->god) {
698 pr("Planes must be built in airports.\n");
701 if (sp->sct_effic < 60 && !player->god) {
702 pr("Sector %s is not 60%% efficient.\n",
703 xyas(sp->sct_x, sp->sct_y, player->cnum));
706 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
707 pr("Not enough materials in %s\n",
708 xyas(sp->sct_x, sp->sct_y, player->cnum));
711 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
712 if (sp->sct_avail < avail) {
713 pr("Not enough available work in %s to build a %s\n",
714 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
715 pr(" (%d available work required)\n", avail);
718 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
719 if (!build_can_afford(cost, pp->pl_name))
721 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
722 pr("Not enough military for crew in %s\n",
723 xyas(sp->sct_x, sp->sct_y, player->cnum));
726 if (!trechk(player->cnum, 0, NEWPLN))
728 if (!check_sect_ok(sp))
730 sp->sct_avail -= avail;
731 player->dolcost += cost;
732 snxtitem_all(&nstr, EF_PLANE);
734 while (nxtitem(&nstr, &plane)) {
735 if (plane.pln_own == 0) {
740 if (freeplane == 0) {
741 ef_extend(EF_PLANE, 50);
743 ef_blank(EF_PLANE, nstr.cur, &plane);
744 plane.pln_x = sp->sct_x;
745 plane.pln_y = sp->sct_y;
746 plane.pln_own = sp->sct_own;
747 plane.pln_type = pp - plchr;
748 plane.pln_effic = PLANE_MINEFF;
749 if (opt_MOB_ACCESS) {
750 game_tick_to_now(&plane.pln_access);
751 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
755 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
758 plane.pln_harden = 0;
760 pln_set_tech(&plane, tlev);
761 unit_wipe_orders((struct empobj *)&plane);
767 putplane(plane.pln_uid, &plane);
768 pr("%s built in sector %s\n", prplane(&plane),
769 xyas(sp->sct_x, sp->sct_y, player->cnum));
774 build_tower(struct sctstr *sp, short *vec)
786 if (sp->sct_type != SCT_BSPAN) {
787 pr("Bridge towers can only be built from bridge spans.\n");
791 if (sp->sct_effic < 60 && !player->god) {
792 pr("Sector %s is not 60%% efficient.\n",
793 xyas(sp->sct_x, sp->sct_y, player->cnum));
797 if (vec[I_HCM] < buil_tower_bh) {
798 pr("%s only has %d unit%s of hcm,\n",
799 xyas(sp->sct_x, sp->sct_y, player->cnum),
800 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
801 pr("(a bridge tower requires %d)\n", buil_tower_bh);
805 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
807 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
808 if (sp->sct_avail < avail) {
809 pr("Not enough available work in %s to build a bridge tower\n",
810 xyas(sp->sct_x, sp->sct_y, player->cnum));
811 pr(" (%d available work required)\n", avail);
814 if (!player->argp[3]) {
815 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
816 nav_map(sp->sct_x, sp->sct_y, 1);
818 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
822 /* Sanity check time */
823 if (!check_sect_ok(sp))
826 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
827 pr("'%c' is not a valid direction...\n", *p);
828 direrr(NULL, NULL, NULL);
831 newx = sp->sct_x + diroff[val][0];
832 newy = sp->sct_y + diroff[val][1];
833 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
834 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
838 /* Now, check. You aren't allowed to build bridge towers
840 for (i = 1; i <= 6; i++) {
842 nx = sect.sct_x + diroff[i][0];
843 ny = sect.sct_y + diroff[i][1];
844 getsect(nx, ny, &s2);
845 if ((s2.sct_type != SCT_WATER) &&
846 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
847 pr("Bridge towers cannot be built adjacent to land.\n");
848 pr("That sector is adjacent to land.\n");
853 sp->sct_avail -= avail;
854 player->dolcost += buil_tower_bc;
855 sect.sct_type = SCT_BTOWER;
856 sect.sct_newtype = SCT_BTOWER;
857 sect.sct_effic = SCT_MINEFF;
860 sect.sct_defense = 0;
861 if (opt_MOB_ACCESS) {
862 game_tick_to_now(§.sct_access);
863 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
868 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
869 writemap(player->cnum);
871 pr("Bridge tower built in %s\n",
872 xyas(sect.sct_x, sect.sct_y, player->cnum));
873 vec[I_HCM] -= buil_tower_bh;
878 build_can_afford(double cost, char *what)
880 struct natstr *natp = getnatp(player->cnum);
881 if (natp->nat_money < player->dolcost + cost) {
882 pr("Not enough money left to build a %s\n", what);