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-2009
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);
89 p = getstarg(player->argp[1],
90 "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);
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);
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);
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);
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);
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);
464 memset(land.lnd_item, 0, sizeof(land.lnd_item));
465 land.lnd_pstage = PLG_HEALTHY;
467 lnd_set_tech(&land, tlev);
468 unit_wipe_orders((struct empobj *)&land);
474 vec[I_SHELL] -= shell;
476 if (sp->sct_pstage == PLG_INFECT)
477 land.lnd_pstage = PLG_EXPOSED;
478 putland(nstr.cur, &land);
479 pr("%s", prland(&land));
480 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
485 build_bridge(struct sctstr *sp, short *vec)
491 int nx, ny, i, good = 0;
495 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
496 if (sp->sct_type != SCT_BTOWER) {
497 if (sp->sct_type != SCT_BHEAD)
499 if (sp->sct_newtype != SCT_BHEAD)
504 if (sp->sct_effic < 60 && !player->god) {
505 pr("Sector %s is not 60%% efficient.\n",
506 xyas(sp->sct_x, sp->sct_y, player->cnum));
510 if (vec[I_HCM] < buil_bh) {
511 pr("%s only has %d unit%s of hcm,\n",
512 xyas(sp->sct_x, sp->sct_y, player->cnum),
513 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
514 pr("(a bridge span requires %d)\n", buil_bh);
518 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
520 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
521 if (sp->sct_avail < avail) {
522 pr("Not enough available work in %s to build a bridge\n",
523 xyas(sp->sct_x, sp->sct_y, player->cnum));
524 pr(" (%d available work required)\n", avail);
527 if (!player->argp[3]) {
528 pr("Bridge head at %s\n",
529 xyas(sp->sct_x, sp->sct_y, player->cnum));
530 nav_map(sp->sct_x, sp->sct_y, 1);
532 p = getstarg(player->argp[3], "build span in what direction? ", buf);
536 /* Sanity check time */
537 if (!check_sect_ok(sp))
540 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
541 pr("'%c' is not a valid direction...\n", *p);
542 direrr(NULL, NULL, NULL);
545 newx = sp->sct_x + diroff[val][0];
546 newy = sp->sct_y + diroff[val][1];
547 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
548 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
551 if (opt_EASY_BRIDGES) {
554 for (i = 1; i <= 6; i++) {
556 nx = sect.sct_x + diroff[i][0];
557 ny = sect.sct_y + diroff[i][1];
558 getsect(nx, ny, &s2);
559 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
563 pr("Bridges must be built adjacent to land or bridge towers.\n");
564 pr("That sector is not adjacent to land or a bridge tower.\n");
567 } /* end EASY_BRIDGES */
568 sp->sct_avail -= avail;
569 player->dolcost += buil_bc;
570 sect.sct_type = SCT_BSPAN;
571 sect.sct_newtype = SCT_BSPAN;
572 sect.sct_effic = SCT_MINEFF;
575 sect.sct_defense = 0;
576 if (opt_MOB_ACCESS) {
577 game_tick_to_now(§.sct_access);
578 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
583 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
584 writemap(player->cnum);
586 pr("Bridge span built over %s\n",
587 xyas(sect.sct_x, sect.sct_y, player->cnum));
588 vec[I_HCM] -= buil_bh;
593 build_nuke(struct sctstr *sp, struct nchrstr *np, short *vec, int tlev)
596 struct nstr_item nstr;
600 if (sp->sct_type != SCT_NUKE && !player->god) {
601 pr("Nuclear weapons must be built in nuclear plants.\n");
604 if (sp->sct_effic < 60 && !player->god) {
605 pr("Sector %s is not 60%% efficient.\n",
606 xyas(sp->sct_x, sp->sct_y, player->cnum));
609 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
610 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
611 pr("Not enough materials for a %s bomb in %s\n",
612 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
613 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
614 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
617 if (!build_can_afford(np->n_cost, np->n_name))
619 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
621 * XXX when nukes turn into units (or whatever), then
622 * make them start at 20%. Since they don't have efficiency
623 * now, we charge all the work right away.
625 if (sp->sct_avail < avail) {
626 pr("Not enough available work in %s to build a %s;\n",
627 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
628 pr(" (%d available work required)\n", avail);
631 if (!trechk(player->cnum, 0, NEWNUK))
633 if (!check_sect_ok(sp))
635 sp->sct_avail -= avail;
636 player->dolcost += np->n_cost;
637 snxtitem_all(&nstr, EF_NUKE);
639 while (nxtitem(&nstr, &nuke)) {
640 if (nuke.nuk_own == 0) {
646 ef_extend(EF_NUKE, 50);
648 ef_blank(EF_NUKE, nstr.cur, &nuke);
649 nuke.nuk_x = sp->sct_x;
650 nuke.nuk_y = sp->sct_y;
651 nuke.nuk_own = sp->sct_own;
652 nuke.nuk_type = np - nchr;
653 nuke.nuk_effic = 100;
655 nuke.nuk_tech = tlev;
656 unit_wipe_orders((struct empobj *)&nuke);
658 vec[I_HCM] -= np->n_hcm;
659 vec[I_LCM] -= np->n_lcm;
660 vec[I_OIL] -= np->n_oil;
661 vec[I_RAD] -= np->n_rad;
663 putnuke(nuke.nuk_uid, &nuke);
664 pr("%s created in %s\n", prnuke(&nuke),
665 xyas(sp->sct_x, sp->sct_y, player->cnum));
670 build_plane(struct sctstr *sp, struct plchrstr *pp, short *vec, int tlev)
673 struct nstr_item nstr;
676 double eff = PLANE_MINEFF / 100.0;
680 mil = roundavg(pp->pl_crew * eff);
681 /* Always use at least 1 mil to build a plane */
682 if (mil == 0 && pp->pl_crew > 0)
684 hcm = roundavg(pp->pl_hcm * eff);
685 lcm = roundavg(pp->pl_lcm * eff);
686 if (sp->sct_type != SCT_AIRPT && !player->god) {
687 pr("Planes must be built in airports.\n");
690 if (sp->sct_effic < 60 && !player->god) {
691 pr("Sector %s is not 60%% efficient.\n",
692 xyas(sp->sct_x, sp->sct_y, player->cnum));
695 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
696 pr("Not enough materials in %s\n",
697 xyas(sp->sct_x, sp->sct_y, player->cnum));
700 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
701 if (sp->sct_avail < avail) {
702 pr("Not enough available work in %s to build a %s\n",
703 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
704 pr(" (%d available work required)\n", avail);
707 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
708 if (!build_can_afford(cost, pp->pl_name))
710 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
711 pr("Not enough military for crew in %s\n",
712 xyas(sp->sct_x, sp->sct_y, player->cnum));
715 if (!trechk(player->cnum, 0, NEWPLN))
717 if (!check_sect_ok(sp))
719 sp->sct_avail -= avail;
720 player->dolcost += cost;
721 snxtitem_all(&nstr, EF_PLANE);
723 while (nxtitem(&nstr, &plane)) {
724 if (plane.pln_own == 0) {
729 if (freeplane == 0) {
730 ef_extend(EF_PLANE, 50);
732 ef_blank(EF_PLANE, nstr.cur, &plane);
733 plane.pln_x = sp->sct_x;
734 plane.pln_y = sp->sct_y;
735 plane.pln_own = sp->sct_own;
736 plane.pln_type = pp - plchr;
737 plane.pln_effic = PLANE_MINEFF;
738 if (opt_MOB_ACCESS) {
739 game_tick_to_now(&plane.pln_access);
740 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
744 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
747 plane.pln_harden = 0;
749 pln_set_tech(&plane, tlev);
750 unit_wipe_orders((struct empobj *)&plane);
756 putplane(plane.pln_uid, &plane);
757 pr("%s built in sector %s\n", prplane(&plane),
758 xyas(sp->sct_x, sp->sct_y, player->cnum));
763 build_tower(struct sctstr *sp, short *vec)
775 if (sp->sct_type != SCT_BSPAN) {
776 pr("Bridge towers can only be built from bridge spans.\n");
780 if (sp->sct_effic < 60 && !player->god) {
781 pr("Sector %s is not 60%% efficient.\n",
782 xyas(sp->sct_x, sp->sct_y, player->cnum));
786 if (vec[I_HCM] < buil_tower_bh) {
787 pr("%s only has %d unit%s of hcm,\n",
788 xyas(sp->sct_x, sp->sct_y, player->cnum),
789 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
790 pr("(a bridge tower requires %d)\n", buil_tower_bh);
794 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
796 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
797 if (sp->sct_avail < avail) {
798 pr("Not enough available work in %s to build a bridge tower\n",
799 xyas(sp->sct_x, sp->sct_y, player->cnum));
800 pr(" (%d available work required)\n", avail);
803 if (!player->argp[3]) {
804 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
805 nav_map(sp->sct_x, sp->sct_y, 1);
807 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
811 /* Sanity check time */
812 if (!check_sect_ok(sp))
815 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
816 pr("'%c' is not a valid direction...\n", *p);
817 direrr(NULL, NULL, NULL);
820 newx = sp->sct_x + diroff[val][0];
821 newy = sp->sct_y + diroff[val][1];
822 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
823 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
827 /* Now, check. You aren't allowed to build bridge towers
829 for (i = 1; i <= 6; i++) {
831 nx = sect.sct_x + diroff[i][0];
832 ny = sect.sct_y + diroff[i][1];
833 getsect(nx, ny, &s2);
834 if ((s2.sct_type != SCT_WATER) &&
835 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
836 pr("Bridge towers cannot be built adjacent to land.\n");
837 pr("That sector is adjacent to land.\n");
842 sp->sct_avail -= avail;
843 player->dolcost += buil_tower_bc;
844 sect.sct_type = SCT_BTOWER;
845 sect.sct_newtype = SCT_BTOWER;
846 sect.sct_effic = SCT_MINEFF;
849 sect.sct_defense = 0;
850 if (opt_MOB_ACCESS) {
851 game_tick_to_now(§.sct_access);
852 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
857 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
858 writemap(player->cnum);
860 pr("Bridge tower built in %s\n",
861 xyas(sect.sct_x, sect.sct_y, player->cnum));
862 vec[I_HCM] -= buil_tower_bh;
867 build_can_afford(double cost, char *what)
869 struct natstr *natp = getnatp(player->cnum);
870 if (natp->nat_money < player->dolcost + cost) {
871 pr("Not enough money left to build a %s\n", what);