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;
72 char *p, *what, *prompt;
74 int (*build_it)(struct sctstr *, int, short[], int);
78 natp = getnatp(player->cnum);
79 tlev = (int)natp->nat_level[NAT_TLEV];
81 p = getstarg(player->argp[1],
82 "Build (ship, nuke, bridge, plane, land unit, tower)? ",
89 return build_bridge(*p);
92 prompt = "Ship type? ";
93 build_it = build_ship;
97 prompt = "Plane type? ";
98 build_it = build_plane;
102 prompt = "Land unit type? ";
103 build_it = build_land;
106 if (!ef_nelem(EF_NUKE_CHR)) {
107 pr("There are no nukes in this game.\n");
110 if (drnuke_const > MIN_DRNUKE_CONST)
112 (int)(natp->nat_level[NAT_RLEV] / drnuke_const));
114 prompt = "Nuke type? ";
115 build_it = build_nuke;
118 pr("You can't build that!\n");
122 if (!snxtsct(&nstr, player->argp[2]))
125 p = getstarg(player->argp[3], prompt, buf);
132 type = ef_elt_byname(EF_PLANE_CHR, p);
134 rqtech = plchr[type].pl_tech;
137 type = ef_elt_byname(EF_SHIP_CHR, p);
139 rqtech = mchr[type].m_tech;
140 if ((mchr[type].m_flags & M_TRADE) && !opt_TRADESHIPS)
145 type = ef_elt_byname(EF_LAND_CHR, p);
147 rqtech = lchr[type].l_tech;
148 if ((lchr[type].l_flags & L_SPY) && !opt_LANDSPIES)
153 type = ef_elt_byname(EF_NUKE_CHR, p);
155 rqtech = nchr[type].n_tech;
162 if (type < 0 || tlev < rqtech) {
163 pr("You can't build that!\n");
164 pr("Use `show %s build %d' to show types you can build.\n",
170 if (player->argp[4]) {
171 number = atoi(player->argp[4]);
175 "Are you sure that you want to build %s of them? ",
177 p = getstarg(player->argp[6], bstr, buf);
183 if (player->argp[5]) {
184 val = atoi(player->argp[5]);
185 if (val > tlev && !player->god) {
186 pr("Your%s tech level is only %d.\n",
187 *what == 'n' && drnuke_const > MIN_DRNUKE_CONST
188 ? " effective" : "", tlev);
192 pr("Required tech is %d.\n", rqtech);
196 pr("Building with tech level %d.\n", tlev);
199 while (number-- > 0) {
200 while (nxtsct(&nstr, §)) {
204 if (build_it(§, type, sect.sct_item, tlev))
207 snxtsct_rewind(&nstr);
210 pr("Bad sector specification.\n");
216 build_ship(struct sctstr *sp, int type, short *vec, int tlev)
218 struct mchrstr *mp = &mchr[type];
220 struct nstr_item nstr;
223 double eff = SHIP_MINEFF / 100.0;
227 hcm = roundavg(mp->m_hcm * eff);
228 lcm = roundavg(mp->m_lcm * eff);
230 if (sp->sct_type != SCT_HARBR) {
231 pr("Ships must be built in harbours.\n");
234 if (sp->sct_effic < 60 && !player->god) {
235 pr("Sector %s is not 60%% efficient.\n",
236 xyas(sp->sct_x, sp->sct_y, player->cnum));
239 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
240 pr("Not enough materials in %s\n",
241 xyas(sp->sct_x, sp->sct_y, player->cnum));
244 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
245 if (sp->sct_avail < avail) {
246 pr("Not enough available work in %s to build a %s\n",
247 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
248 pr(" (%d available work required)\n", avail);
251 cost = mp->m_cost * SHIP_MINEFF / 100.0;
252 if (!build_can_afford(cost, mp->m_name))
254 if (!trechk(player->cnum, 0, NEWSHP))
256 if (!check_sect_ok(sp))
258 sp->sct_avail -= avail;
259 player->dolcost += cost;
260 snxtitem_all(&nstr, EF_SHIP);
261 while (nxtitem(&nstr, &ship)) {
262 if (ship.shp_own == 0) {
268 ef_extend(EF_SHIP, 50);
270 ef_blank(EF_SHIP, nstr.cur, &ship);
271 ship.shp_x = sp->sct_x;
272 ship.shp_y = sp->sct_y;
273 ship.shp_own = player->cnum;
274 ship.shp_type = mp - mchr;
275 ship.shp_effic = SHIP_MINEFF;
276 if (opt_MOB_ACCESS) {
277 game_tick_to_now(&ship.shp_access);
278 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
282 memset(ship.shp_item, 0, sizeof(ship.shp_item));
283 ship.shp_pstage = PLG_HEALTHY;
285 ship.shp_name[0] = 0;
286 ship.shp_orig_own = player->cnum;
287 ship.shp_orig_x = sp->sct_x;
288 ship.shp_orig_y = sp->sct_y;
289 shp_set_tech(&ship, tlev);
290 unit_wipe_orders((struct empobj *)&ship);
295 if (sp->sct_pstage == PLG_INFECT)
296 ship.shp_pstage = PLG_EXPOSED;
297 putship(ship.shp_uid, &ship);
298 pr("%s", prship(&ship));
299 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
304 build_land(struct sctstr *sp, int type, short *vec, int tlev)
306 struct lchrstr *lp = &lchr[type];
308 struct nstr_item nstr;
311 double eff = LAND_MINEFF / 100.0;
312 int mil, lcm, hcm, gun, shell;
316 mil = roundavg(lp->l_mil * eff);
317 shell = roundavg(lp->l_shell * eff);
318 gun = roundavg(lp->l_gun * eff);
320 mil = shell = gun = 0;
322 hcm = roundavg(lp->l_hcm * eff);
323 lcm = roundavg(lp->l_lcm * eff);
325 if (sp->sct_type != SCT_HEADQ) {
326 pr("Land units must be built in headquarters.\n");
329 if (sp->sct_effic < 60 && !player->god) {
330 pr("Sector %s is not 60%% efficient.\n",
331 xyas(sp->sct_x, sp->sct_y, player->cnum));
334 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
335 pr("Not enough materials in %s\n",
336 xyas(sp->sct_x, sp->sct_y, player->cnum));
340 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
341 pr("Not enough guns in %s\n",
342 xyas(sp->sct_x, sp->sct_y, player->cnum));
345 if (vec[I_SHELL] < shell) {
346 pr("Not enough shells in %s\n",
347 xyas(sp->sct_x, sp->sct_y, player->cnum));
350 if (vec[I_MILIT] < mil) {
351 pr("Not enough military in %s\n",
352 xyas(sp->sct_x, sp->sct_y, player->cnum));
356 if (!trechk(player->cnum, 0, NEWLND))
358 if (!check_sect_ok(sp))
360 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
361 if (sp->sct_avail < avail) {
362 pr("Not enough available work in %s to build a %s\n",
363 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
364 pr(" (%d available work required)\n", avail);
367 cost = lp->l_cost * LAND_MINEFF / 100.0;
368 if (!build_can_afford(cost, lp->l_name))
370 sp->sct_avail -= avail;
371 player->dolcost += cost;
372 snxtitem_all(&nstr, EF_LAND);
373 while (nxtitem(&nstr, &land)) {
374 if (land.lnd_own == 0) {
380 ef_extend(EF_LAND, 50);
382 ef_blank(EF_LAND, nstr.cur, &land);
383 land.lnd_x = sp->sct_x;
384 land.lnd_y = sp->sct_y;
385 land.lnd_own = player->cnum;
386 land.lnd_type = lp - lchr;
387 land.lnd_effic = LAND_MINEFF;
388 if (opt_MOB_ACCESS) {
389 game_tick_to_now(&land.lnd_access);
390 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
397 memset(land.lnd_item, 0, sizeof(land.lnd_item));
398 land.lnd_pstage = PLG_HEALTHY;
400 lnd_set_tech(&land, tlev);
401 unit_wipe_orders((struct empobj *)&land);
407 vec[I_SHELL] -= shell;
409 if (sp->sct_pstage == PLG_INFECT)
410 land.lnd_pstage = PLG_EXPOSED;
411 putland(nstr.cur, &land);
412 pr("%s", prland(&land));
413 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
418 build_nuke(struct sctstr *sp, int type, short *vec, int tlev)
420 struct nchrstr *np = &nchr[type];
422 struct nstr_item nstr;
426 if (sp->sct_type != SCT_NUKE && !player->god) {
427 pr("Nuclear weapons must be built in nuclear plants.\n");
430 if (sp->sct_effic < 60 && !player->god) {
431 pr("Sector %s is not 60%% efficient.\n",
432 xyas(sp->sct_x, sp->sct_y, player->cnum));
435 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
436 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
437 pr("Not enough materials for a %s bomb in %s\n",
438 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
439 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
440 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
443 if (!build_can_afford(np->n_cost, np->n_name))
445 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
447 * XXX when nukes turn into units (or whatever), then
448 * make them start at 20%. Since they don't have efficiency
449 * now, we charge all the work right away.
451 if (sp->sct_avail < avail) {
452 pr("Not enough available work in %s to build a %s;\n",
453 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
454 pr(" (%d available work required)\n", avail);
457 if (!trechk(player->cnum, 0, NEWNUK))
459 if (!check_sect_ok(sp))
461 sp->sct_avail -= avail;
462 player->dolcost += np->n_cost;
463 snxtitem_all(&nstr, EF_NUKE);
465 while (nxtitem(&nstr, &nuke)) {
466 if (nuke.nuk_own == 0) {
472 ef_extend(EF_NUKE, 50);
474 ef_blank(EF_NUKE, nstr.cur, &nuke);
475 nuke.nuk_x = sp->sct_x;
476 nuke.nuk_y = sp->sct_y;
477 nuke.nuk_own = sp->sct_own;
478 nuke.nuk_type = np - nchr;
479 nuke.nuk_effic = 100;
481 nuke.nuk_tech = tlev;
482 unit_wipe_orders((struct empobj *)&nuke);
484 vec[I_HCM] -= np->n_hcm;
485 vec[I_LCM] -= np->n_lcm;
486 vec[I_OIL] -= np->n_oil;
487 vec[I_RAD] -= np->n_rad;
489 putnuke(nuke.nuk_uid, &nuke);
490 pr("%s created in %s\n", prnuke(&nuke),
491 xyas(sp->sct_x, sp->sct_y, player->cnum));
496 build_plane(struct sctstr *sp, int type, short *vec, int tlev)
498 struct plchrstr *pp = &plchr[type];
500 struct nstr_item nstr;
503 double eff = PLANE_MINEFF / 100.0;
507 mil = roundavg(pp->pl_crew * eff);
508 /* Always use at least 1 mil to build a plane */
509 if (mil == 0 && pp->pl_crew > 0)
511 hcm = roundavg(pp->pl_hcm * eff);
512 lcm = roundavg(pp->pl_lcm * eff);
513 if (sp->sct_type != SCT_AIRPT && !player->god) {
514 pr("Planes must be built in airports.\n");
517 if (sp->sct_effic < 60 && !player->god) {
518 pr("Sector %s is not 60%% efficient.\n",
519 xyas(sp->sct_x, sp->sct_y, player->cnum));
522 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
523 pr("Not enough materials in %s\n",
524 xyas(sp->sct_x, sp->sct_y, player->cnum));
527 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
528 if (sp->sct_avail < avail) {
529 pr("Not enough available work in %s to build a %s\n",
530 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
531 pr(" (%d available work required)\n", avail);
534 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
535 if (!build_can_afford(cost, pp->pl_name))
537 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
538 pr("Not enough military for crew in %s\n",
539 xyas(sp->sct_x, sp->sct_y, player->cnum));
542 if (!trechk(player->cnum, 0, NEWPLN))
544 if (!check_sect_ok(sp))
546 sp->sct_avail -= avail;
547 player->dolcost += cost;
548 snxtitem_all(&nstr, EF_PLANE);
550 while (nxtitem(&nstr, &plane)) {
551 if (plane.pln_own == 0) {
556 if (freeplane == 0) {
557 ef_extend(EF_PLANE, 50);
559 ef_blank(EF_PLANE, nstr.cur, &plane);
560 plane.pln_x = sp->sct_x;
561 plane.pln_y = sp->sct_y;
562 plane.pln_own = sp->sct_own;
563 plane.pln_type = pp - plchr;
564 plane.pln_effic = PLANE_MINEFF;
565 if (opt_MOB_ACCESS) {
566 game_tick_to_now(&plane.pln_access);
567 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
571 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
574 plane.pln_harden = 0;
576 pln_set_tech(&plane, tlev);
577 unit_wipe_orders((struct empobj *)&plane);
583 putplane(plane.pln_uid, &plane);
584 pr("%s built in sector %s\n", prplane(&plane),
585 xyas(sp->sct_x, sp->sct_y, player->cnum));
590 build_bridge(char what)
592 struct natstr *natp = getnatp(player->cnum);
593 struct nstr_sect nstr;
594 int (*build_it)(struct sctstr *, short[]);
600 if (natp->nat_level[NAT_TLEV] < buil_bt) {
601 pr("Building a span requires a tech of %.0f\n", buil_bt);
604 build_it = build_bspan;
607 if (!opt_BRIDGETOWERS) {
608 pr("Bridge tower building is disabled.\n");
611 if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
612 pr("Building a tower requires a tech of %.0f\n",
616 build_it = build_btower;
623 if (!snxtsct(&nstr, player->argp[2]))
626 while (nxtsct(&nstr, §)) {
630 if (build_it(§, sect.sct_item))
634 pr("Bad sector specification.\n");
640 build_bspan(struct sctstr *sp, short *vec)
646 int nx, ny, i, good = 0;
650 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
651 if (sp->sct_type != SCT_BTOWER) {
652 if (sp->sct_type != SCT_BHEAD)
654 if (sp->sct_newtype != SCT_BHEAD)
659 if (sp->sct_effic < 60 && !player->god) {
660 pr("Sector %s is not 60%% efficient.\n",
661 xyas(sp->sct_x, sp->sct_y, player->cnum));
665 if (vec[I_HCM] < buil_bh) {
666 pr("%s only has %d unit%s of hcm,\n",
667 xyas(sp->sct_x, sp->sct_y, player->cnum),
668 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
669 pr("(a bridge span requires %d)\n", buil_bh);
673 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
675 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
676 if (sp->sct_avail < avail) {
677 pr("Not enough available work in %s to build a bridge\n",
678 xyas(sp->sct_x, sp->sct_y, player->cnum));
679 pr(" (%d available work required)\n", avail);
682 if (!player->argp[3]) {
683 pr("Bridge head at %s\n",
684 xyas(sp->sct_x, sp->sct_y, player->cnum));
685 nav_map(sp->sct_x, sp->sct_y, 1);
687 p = getstarg(player->argp[3], "build span in what direction? ", buf);
691 /* Sanity check time */
692 if (!check_sect_ok(sp))
695 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
696 pr("'%c' is not a valid direction...\n", *p);
697 direrr(NULL, NULL, NULL);
700 newx = sp->sct_x + diroff[val][0];
701 newy = sp->sct_y + diroff[val][1];
702 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
703 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
706 if (opt_EASY_BRIDGES) {
709 for (i = 1; i <= 6; i++) {
711 nx = sect.sct_x + diroff[i][0];
712 ny = sect.sct_y + diroff[i][1];
713 getsect(nx, ny, &s2);
714 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
718 pr("Bridges must be built adjacent to land or bridge towers.\n");
719 pr("That sector is not adjacent to land or a bridge tower.\n");
722 } /* end EASY_BRIDGES */
723 sp->sct_avail -= avail;
724 player->dolcost += buil_bc;
725 sect.sct_type = SCT_BSPAN;
726 sect.sct_newtype = SCT_BSPAN;
727 sect.sct_effic = SCT_MINEFF;
730 sect.sct_defense = 0;
731 if (opt_MOB_ACCESS) {
732 game_tick_to_now(§.sct_access);
733 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
738 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
739 writemap(player->cnum);
741 pr("Bridge span built over %s\n",
742 xyas(sect.sct_x, sect.sct_y, player->cnum));
743 vec[I_HCM] -= buil_bh;
748 build_btower(struct sctstr *sp, short *vec)
760 if (sp->sct_type != SCT_BSPAN) {
761 pr("Bridge towers can only be built from bridge spans.\n");
765 if (sp->sct_effic < 60 && !player->god) {
766 pr("Sector %s is not 60%% efficient.\n",
767 xyas(sp->sct_x, sp->sct_y, player->cnum));
771 if (vec[I_HCM] < buil_tower_bh) {
772 pr("%s only has %d unit%s of hcm,\n",
773 xyas(sp->sct_x, sp->sct_y, player->cnum),
774 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
775 pr("(a bridge tower requires %d)\n", buil_tower_bh);
779 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
781 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
782 if (sp->sct_avail < avail) {
783 pr("Not enough available work in %s to build a bridge tower\n",
784 xyas(sp->sct_x, sp->sct_y, player->cnum));
785 pr(" (%d available work required)\n", avail);
788 if (!player->argp[3]) {
789 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
790 nav_map(sp->sct_x, sp->sct_y, 1);
792 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
796 /* Sanity check time */
797 if (!check_sect_ok(sp))
800 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
801 pr("'%c' is not a valid direction...\n", *p);
802 direrr(NULL, NULL, NULL);
805 newx = sp->sct_x + diroff[val][0];
806 newy = sp->sct_y + diroff[val][1];
807 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
808 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
812 /* Now, check. You aren't allowed to build bridge towers
814 for (i = 1; i <= 6; i++) {
816 nx = sect.sct_x + diroff[i][0];
817 ny = sect.sct_y + diroff[i][1];
818 getsect(nx, ny, &s2);
819 if ((s2.sct_type != SCT_WATER) &&
820 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
821 pr("Bridge towers cannot be built adjacent to land.\n");
822 pr("That sector is adjacent to land.\n");
827 sp->sct_avail -= avail;
828 player->dolcost += buil_tower_bc;
829 sect.sct_type = SCT_BTOWER;
830 sect.sct_newtype = SCT_BTOWER;
831 sect.sct_effic = SCT_MINEFF;
834 sect.sct_defense = 0;
835 if (opt_MOB_ACCESS) {
836 game_tick_to_now(§.sct_access);
837 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
842 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
843 writemap(player->cnum);
845 pr("Bridge tower built in %s\n",
846 xyas(sect.sct_x, sect.sct_y, player->cnum));
847 vec[I_HCM] -= buil_tower_bh;
852 build_can_afford(double cost, char *what)
854 struct natstr *natp = getnatp(player->cnum);
855 if (natp->nat_money < player->dolcost + cost) {
856 pr("Not enough money left to build a %s\n", what);