2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * buil.c: Build ships, nukes, bridges, planes, land units, bridge towers
30 * Known contributors to this file:
31 * Steve McClure, 1998-2000
32 * Markus Armbruster, 2004-2008
52 static int build_nuke(struct sctstr *sp,
53 struct nchrstr *np, short *vec, int tlev);
54 static int build_ship(struct sctstr *sp,
55 struct mchrstr *mp, short *vec, int tlev);
56 static int build_land(struct sctstr *sp,
57 struct lchrstr *lp, short *vec, int tlev);
58 static int build_bridge(struct sctstr *sp, short *vec);
59 static int build_tower(struct sctstr *sp, short *vec);
60 static int build_plane(struct sctstr *sp,
61 struct plchrstr *pp, short *vec, int tlev);
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);
90 getstarg(player->argp[1],
91 "Build (ship, nuke, bridge, plane, land unit, tower)? ",
96 if (!snxtsct(&nstr, player->argp[2])) {
97 pr("Bad sector specification.\n");
100 tlev = (int)natp->nat_level[NAT_TLEV];
101 rlev = (int)natp->nat_level[NAT_RLEV];
105 p = getstarg(player->argp[3], "Plane type? ", buf);
106 if (p == 0 || *p == 0)
108 type = ef_elt_byname(EF_PLANE_CHR, p);
111 rqtech = pp->pl_tech;
116 pr("You can't build that!\n");
117 pr("Use `show plane build %d' to show types you can build.\n",
123 p = getstarg(player->argp[3], "Ship type? ", buf);
124 if (p == 0 || *p == 0)
126 type = ef_elt_byname(EF_SHIP_CHR, p);
132 if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
136 pr("You can't build that!\n");
137 pr("Use `show ship build %d' to show types you can build.\n",
143 p = getstarg(player->argp[3], "Land unit type? ", buf);
144 if (p == 0 || *p == 0)
146 type = ef_elt_byname(EF_LAND_CHR, p);
152 if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
156 pr("You can't build that!\n");
157 pr("Use `show land build %d' to show types you can build.\n",
163 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_bt) {
164 pr("Building a span requires a tech of %.0f\n", buil_bt);
169 if (!opt_BRIDGETOWERS) {
170 pr("Bridge tower building is disabled.\n");
173 if (natp->nat_level[NAT_TLEV] + 0.005 < buil_tower_bt) {
174 pr("Building a tower requires a tech of %.0f\n",
180 if (!ef_nelem(EF_NUKE_CHR)) {
181 pr("There are no nukes in this game.\n");
184 p = getstarg(player->argp[3], "Nuke type? ", buf);
185 if (p == 0 || *p == 0)
187 type = ef_elt_byname(EF_NUKE_CHR, p);
192 || (drnuke_const > MIN_DRNUKE_CONST &&
193 np->n_tech * drnuke_const > rlev))
198 if (drnuke_const > MIN_DRNUKE_CONST)
199 tt = (tlev < (rlev / drnuke_const) ? (int)tlev :
200 (int)(rlev / drnuke_const));
201 pr("You can't build that!\n");
202 pr("Use `show nuke build %d' to show types you can build.\n",
208 pr("You can't build that!\n");
213 if (what != 'b' && what != 't') {
214 if (player->argp[4]) {
215 number = atoi(player->argp[4]);
219 "Are you sure that you want to build %s of them? ",
221 p = getstarg(player->argp[6], bstr, buf);
222 if (p == 0 || *p != 'y')
228 if (what != 'b' && what != 'n' && what != 't') {
229 if (player->argp[5]) {
230 tlev = atoi(player->argp[5]);
231 if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
232 pr("Your tech level is only %d.\n",
233 (int)natp->nat_level[NAT_TLEV]);
237 pr("Required tech is %d.\n", rqtech);
240 pr("building with tech level %d.\n", tlev);
244 while (number-- > 0) {
245 while (nxtsct(&nstr, §)) {
251 built = build_land(§, lp, sect.sct_item, tlev);
254 built = build_ship(§, mp, sect.sct_item, tlev);
257 built = build_bridge(§, sect.sct_item);
260 built = build_tower(§, sect.sct_item);
263 built = build_nuke(§, np, sect.sct_item, tlev);
266 built = build_plane(§, pp, sect.sct_item, tlev);
276 snxtsct_rewind(&nstr);
279 pr("Bad sector specification.\n");
285 build_ship(struct sctstr *sp, struct mchrstr *mp, short *vec, int tlev)
288 struct nstr_item nstr;
291 double eff = SHIP_MINEFF / 100.0;
295 hcm = roundavg(mp->m_hcm * eff);
296 lcm = roundavg(mp->m_lcm * eff);
298 if (sp->sct_type != SCT_HARBR) {
299 pr("Ships must be built in harbours.\n");
302 if (sp->sct_effic < 60 && !player->god) {
303 pr("Sector %s is not 60%% efficient.\n",
304 xyas(sp->sct_x, sp->sct_y, player->cnum));
307 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
308 pr("Not enough materials in %s\n",
309 xyas(sp->sct_x, sp->sct_y, player->cnum));
312 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
313 if (sp->sct_avail < avail) {
314 pr("Not enough available work in %s to build a %s\n",
315 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
316 pr(" (%d available work required)\n", avail);
319 cost = mp->m_cost * SHIP_MINEFF / 100.0;
320 if (!build_can_afford(cost, mp->m_name))
322 if (!trechk(player->cnum, 0, NEWSHP))
324 if (!check_sect_ok(sp))
326 sp->sct_avail -= avail;
327 player->dolcost += cost;
328 snxtitem_all(&nstr, EF_SHIP);
329 while (nxtitem(&nstr, &ship)) {
330 if (ship.shp_own == 0) {
336 ef_extend(EF_SHIP, 50);
338 ef_blank(EF_SHIP, nstr.cur, &ship);
339 ship.shp_x = sp->sct_x;
340 ship.shp_y = sp->sct_y;
341 ship.shp_own = player->cnum;
342 ship.shp_type = mp - mchr;
343 ship.shp_effic = SHIP_MINEFF;
344 if (opt_MOB_ACCESS) {
345 game_tick_to_now(&ship.shp_access);
346 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
350 memset(ship.shp_item, 0, sizeof(ship.shp_item));
351 ship.shp_pstage = PLG_HEALTHY;
353 ship.shp_name[0] = 0;
354 ship.shp_orig_own = player->cnum;
355 ship.shp_orig_x = sp->sct_x;
356 ship.shp_orig_y = sp->sct_y;
357 shp_set_tech(&ship, tlev);
358 unit_wipe_orders((struct empobj *)&ship);
363 if (sp->sct_pstage == PLG_INFECT)
364 ship.shp_pstage = PLG_EXPOSED;
365 putship(ship.shp_uid, &ship);
366 pr("%s", prship(&ship));
367 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
372 build_land(struct sctstr *sp, struct lchrstr *lp, short *vec, int tlev)
375 struct nstr_item nstr;
378 double eff = LAND_MINEFF / 100.0;
379 int mil, lcm, hcm, gun, shell;
383 mil = roundavg(lp->l_mil * eff);
384 shell = roundavg(lp->l_shell * eff);
385 gun = roundavg(lp->l_gun * eff);
387 mil = shell = gun = 0;
389 hcm = roundavg(lp->l_hcm * eff);
390 lcm = roundavg(lp->l_lcm * eff);
392 if (sp->sct_type != SCT_HEADQ) {
393 pr("Land Units must be built in headquarters.\n");
396 if (sp->sct_effic < 60 && !player->god) {
397 pr("Sector %s is not 60%% efficient.\n",
398 xyas(sp->sct_x, sp->sct_y, player->cnum));
401 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
402 pr("Not enough materials in %s\n",
403 xyas(sp->sct_x, sp->sct_y, player->cnum));
407 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
408 pr("Not enough guns in %s\n",
409 xyas(sp->sct_x, sp->sct_y, player->cnum));
412 if (vec[I_SHELL] < shell) {
413 pr("Not enough shells in %s\n",
414 xyas(sp->sct_x, sp->sct_y, player->cnum));
417 if (vec[I_MILIT] < mil) {
418 pr("Not enough military in %s\n",
419 xyas(sp->sct_x, sp->sct_y, player->cnum));
423 if (!trechk(player->cnum, 0, NEWLND))
425 if (!check_sect_ok(sp))
427 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
428 if (sp->sct_avail < avail) {
429 pr("Not enough available work in %s to build a %s\n",
430 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
431 pr(" (%d available work required)\n", avail);
434 cost = lp->l_cost * LAND_MINEFF / 100.0;
435 if (!build_can_afford(cost, lp->l_name))
437 sp->sct_avail -= avail;
438 player->dolcost += cost;
439 snxtitem_all(&nstr, EF_LAND);
440 while (nxtitem(&nstr, &land)) {
441 if (land.lnd_own == 0) {
447 ef_extend(EF_LAND, 50);
449 ef_blank(EF_LAND, nstr.cur, &land);
450 land.lnd_x = sp->sct_x;
451 land.lnd_y = sp->sct_y;
452 land.lnd_own = player->cnum;
453 land.lnd_type = lp - lchr;
454 land.lnd_effic = LAND_MINEFF;
455 if (opt_MOB_ACCESS) {
456 game_tick_to_now(&land.lnd_access);
457 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
465 memset(land.lnd_item, 0, sizeof(land.lnd_item));
466 land.lnd_pstage = PLG_HEALTHY;
468 lnd_set_tech(&land, tlev);
469 unit_wipe_orders((struct empobj *)&land);
475 vec[I_SHELL] -= shell;
477 if (sp->sct_pstage == PLG_INFECT)
478 land.lnd_pstage = PLG_EXPOSED;
479 putland(nstr.cur, &land);
480 pr("%s", prland(&land));
481 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
486 build_bridge(struct sctstr *sp, short *vec)
492 int nx, ny, i, good = 0;
496 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
497 if (sp->sct_type != SCT_BTOWER) {
498 if (sp->sct_type != SCT_BHEAD)
500 if (sp->sct_newtype != SCT_BHEAD)
505 if (sp->sct_effic < 60 && !player->god) {
506 pr("Sector %s is not 60%% efficient.\n",
507 xyas(sp->sct_x, sp->sct_y, player->cnum));
511 if (vec[I_HCM] < buil_bh) {
512 pr("%s only has %d unit%s of hcm,\n",
513 xyas(sp->sct_x, sp->sct_y, player->cnum),
514 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
515 pr("(a bridge span requires %d)\n", buil_bh);
519 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
521 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
522 if (sp->sct_avail < avail) {
523 pr("Not enough available work in %s to build a bridge\n",
524 xyas(sp->sct_x, sp->sct_y, player->cnum));
525 pr(" (%d available work required)\n", avail);
528 if (!player->argp[3]) {
529 pr("Bridge head at %s\n",
530 xyas(sp->sct_x, sp->sct_y, player->cnum));
531 nav_map(sp->sct_x, sp->sct_y, 1);
533 p = getstarg(player->argp[3], "build span in what direction? ", buf);
537 /* Sanity check time */
538 if (!check_sect_ok(sp))
541 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
542 pr("'%c' is not a valid direction...\n", *p);
546 newx = sp->sct_x + diroff[val][0];
547 newy = sp->sct_y + diroff[val][1];
548 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
549 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
552 if (opt_EASY_BRIDGES) {
555 for (i = 1; i <= 6; i++) {
557 nx = sect.sct_x + diroff[i][0];
558 ny = sect.sct_y + diroff[i][1];
559 getsect(nx, ny, &s2);
560 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
564 pr("Bridges must be built adjacent to land or bridge towers.\n");
565 pr("That sector is not adjacent to land or a bridge tower.\n");
568 } /* end EASY_BRIDGES */
569 sp->sct_avail -= avail;
570 player->dolcost += buil_bc;
571 sect.sct_type = SCT_BSPAN;
572 sect.sct_newtype = SCT_BSPAN;
573 sect.sct_effic = SCT_MINEFF;
576 sect.sct_defense = 0;
577 if (opt_MOB_ACCESS) {
578 game_tick_to_now(§.sct_access);
579 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
584 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
585 writemap(player->cnum);
587 pr("Bridge span built over %s\n",
588 xyas(sect.sct_x, sect.sct_y, player->cnum));
589 vec[I_HCM] -= buil_bh;
594 build_nuke(struct sctstr *sp, struct nchrstr *np, short *vec, int tlev)
597 struct nstr_item nstr;
601 if (sp->sct_type != SCT_NUKE && !player->god) {
602 pr("Nuclear weapons must be built in nuclear plants.\n");
605 if (sp->sct_effic < 60 && !player->god) {
606 pr("Sector %s is not 60%% efficient.\n",
607 xyas(sp->sct_x, sp->sct_y, player->cnum));
610 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
611 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
612 pr("Not enough materials for a %s bomb in %s\n",
613 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
614 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
615 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
618 if (!build_can_afford(np->n_cost, np->n_name))
620 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
622 * XXX when nukes turn into units (or whatever), then
623 * make them start at 20%. Since they don't have efficiency
624 * now, we charge all the work right away.
626 if (sp->sct_avail < avail) {
627 pr("Not enough available work in %s to build a %s;\n",
628 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
629 pr(" (%d available work required)\n", avail);
632 if (!trechk(player->cnum, 0, NEWNUK))
634 if (!check_sect_ok(sp))
636 sp->sct_avail -= avail;
637 player->dolcost += np->n_cost;
638 snxtitem_all(&nstr, EF_NUKE);
640 while (nxtitem(&nstr, &nuke)) {
641 if (nuke.nuk_own == 0) {
647 ef_extend(EF_NUKE, 50);
649 ef_blank(EF_NUKE, nstr.cur, &nuke);
650 nuke.nuk_x = sp->sct_x;
651 nuke.nuk_y = sp->sct_y;
652 nuke.nuk_own = sp->sct_own;
653 nuke.nuk_type = np - nchr;
654 nuke.nuk_effic = 100;
655 nuke.nuk_ship = nuke.nuk_plane = nuke.nuk_land = -1;
656 nuke.nuk_tech = tlev;
657 unit_wipe_orders((struct empobj *)&nuke);
659 vec[I_HCM] -= np->n_hcm;
660 vec[I_LCM] -= np->n_lcm;
661 vec[I_OIL] -= np->n_oil;
662 vec[I_RAD] -= np->n_rad;
664 putnuke(nuke.nuk_uid, &nuke);
665 pr("%s created in %s\n", prnuke(&nuke),
666 xyas(sp->sct_x, sp->sct_y, player->cnum));
671 build_plane(struct sctstr *sp, struct plchrstr *pp, short *vec, int tlev)
674 struct nstr_item nstr;
677 double eff = PLANE_MINEFF / 100.0;
681 mil = roundavg(pp->pl_crew * eff);
682 /* Always use at least 1 mil to build a plane */
683 if (mil == 0 && pp->pl_crew > 0)
685 hcm = roundavg(pp->pl_hcm * eff);
686 lcm = roundavg(pp->pl_lcm * eff);
687 if (sp->sct_type != SCT_AIRPT && !player->god) {
688 pr("Planes must be built in airports.\n");
691 if (sp->sct_effic < 60 && !player->god) {
692 pr("Sector %s is not 60%% efficient.\n",
693 xyas(sp->sct_x, sp->sct_y, player->cnum));
696 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
697 pr("Not enough materials in %s\n",
698 xyas(sp->sct_x, sp->sct_y, player->cnum));
701 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
702 if (sp->sct_avail < avail) {
703 pr("Not enough available work in %s to build a %s\n",
704 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
705 pr(" (%d available work required)\n", avail);
708 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
709 if (!build_can_afford(cost, pp->pl_name))
711 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
712 pr("Not enough military for crew in %s\n",
713 xyas(sp->sct_x, sp->sct_y, player->cnum));
716 if (!trechk(player->cnum, 0, NEWPLN))
718 if (!check_sect_ok(sp))
720 sp->sct_avail -= avail;
721 player->dolcost += cost;
722 snxtitem_all(&nstr, EF_PLANE);
724 while (nxtitem(&nstr, &plane)) {
725 if (plane.pln_own == 0) {
730 if (freeplane == 0) {
731 ef_extend(EF_PLANE, 50);
733 ef_blank(EF_PLANE, nstr.cur, &plane);
734 plane.pln_x = sp->sct_x;
735 plane.pln_y = sp->sct_y;
736 plane.pln_own = sp->sct_own;
737 plane.pln_type = pp - plchr;
738 plane.pln_effic = PLANE_MINEFF;
739 if (opt_MOB_ACCESS) {
740 game_tick_to_now(&plane.pln_access);
741 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
745 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
748 plane.pln_harden = 0;
750 pln_set_tech(&plane, tlev);
751 unit_wipe_orders((struct empobj *)&plane);
757 putplane(plane.pln_uid, &plane);
758 pr("%s built in sector %s\n", prplane(&plane),
759 xyas(sp->sct_x, sp->sct_y, player->cnum));
764 build_tower(struct sctstr *sp, short *vec)
777 if (sp->sct_type != SCT_BSPAN) {
778 pr("Bridge towers can only be built from bridge spans.\n");
782 if (sp->sct_effic < 60 && !player->god) {
783 pr("Sector %s is not 60%% efficient.\n",
784 xyas(sp->sct_x, sp->sct_y, player->cnum));
788 if (vec[I_HCM] < buil_tower_bh) {
789 pr("%s only has %d unit%s of hcm,\n",
790 xyas(sp->sct_x, sp->sct_y, player->cnum),
791 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
792 pr("(a bridge tower requires %d)\n", buil_tower_bh);
796 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
798 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
799 if (sp->sct_avail < avail) {
800 pr("Not enough available work in %s to build a bridge tower\n",
801 xyas(sp->sct_x, sp->sct_y, player->cnum));
802 pr(" (%d available work required)\n", avail);
805 if (!player->argp[3]) {
806 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
807 nav_map(sp->sct_x, sp->sct_y, 1);
809 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
813 /* Sanity check time */
814 if (!check_sect_ok(sp))
817 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
818 pr("'%c' is not a valid direction...\n", *p);
822 newx = sp->sct_x + diroff[val][0];
823 newy = sp->sct_y + diroff[val][1];
824 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
825 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
829 /* Now, check. You aren't allowed to build bridge towers
832 for (i = 1; i <= 6; i++) {
834 nx = sect.sct_x + diroff[i][0];
835 ny = sect.sct_y + diroff[i][1];
836 getsect(nx, ny, &s2);
837 if ((s2.sct_type != SCT_WATER) &&
838 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
844 pr("Bridge towers cannot be built adjacent to land.\n");
845 pr("That sector is adjacent to land.\n");
849 sp->sct_avail -= avail;
850 player->dolcost += buil_tower_bc;
851 sect.sct_type = SCT_BTOWER;
852 sect.sct_newtype = SCT_BTOWER;
853 sect.sct_effic = SCT_MINEFF;
856 sect.sct_defense = 0;
857 if (opt_MOB_ACCESS) {
858 game_tick_to_now(§.sct_access);
859 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
864 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
865 writemap(player->cnum);
867 pr("Bridge tower built in %s\n",
868 xyas(sect.sct_x, sect.sct_y, player->cnum));
869 vec[I_HCM] -= buil_tower_bh;
874 build_can_afford(double cost, char *what)
876 struct natstr *natp = getnatp(player->cnum);
877 if (natp->nat_money < player->dolcost + cost) {
878 pr("Not enough money left to build a %s\n", what);