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]
66 struct natstr *natp = getnatp(player->cnum);
67 int tlev = (int)natp->nat_level[NAT_TLEV];
69 struct nstr_sect nstr;
70 int rqtech, type, number, val, gotsect;
71 char *p, *what, *prompt;
72 int (*build_it)(struct sctstr *, int, short[], int);
75 p = getstarg(player->argp[1],
76 "Build (ship, nuke, bridge, plane, land unit, tower)? ",
83 return build_bridge(*p);
86 prompt = "Ship type? ";
87 build_it = build_ship;
91 prompt = "Plane type? ";
92 build_it = build_plane;
96 prompt = "Land unit type? ";
97 build_it = build_land;
100 if (!ef_nelem(EF_NUKE_CHR)) {
101 pr("There are no nukes in this game.\n");
104 if (drnuke_const > MIN_DRNUKE_CONST)
106 (int)(natp->nat_level[NAT_RLEV] / drnuke_const));
108 prompt = "Nuke type? ";
109 build_it = build_nuke;
112 pr("You can't build that!\n");
116 if (!snxtsct(&nstr, player->argp[2]))
119 p = getstarg(player->argp[3], prompt, buf);
126 type = ef_elt_byname(EF_PLANE_CHR, p);
128 rqtech = plchr[type].pl_tech;
131 type = ef_elt_byname(EF_SHIP_CHR, p);
133 rqtech = mchr[type].m_tech;
134 if ((mchr[type].m_flags & M_TRADE) && !opt_TRADESHIPS)
139 type = ef_elt_byname(EF_LAND_CHR, p);
141 rqtech = lchr[type].l_tech;
142 if ((lchr[type].l_flags & L_SPY) && !opt_LANDSPIES)
147 type = ef_elt_byname(EF_NUKE_CHR, p);
149 rqtech = nchr[type].n_tech;
156 if (type < 0 || tlev < rqtech) {
157 pr("You can't build that!\n");
158 pr("Use `show %s build %d' to show types you can build.\n",
164 if (player->argp[4]) {
165 number = atoi(player->argp[4]);
169 "Are you sure that you want to build %s of them? ",
171 p = getstarg(player->argp[6], bstr, buf);
177 if (player->argp[5]) {
178 val = atoi(player->argp[5]);
179 if (val > tlev && !player->god) {
180 pr("Your%s tech level is only %d.\n",
181 *what == 'n' && drnuke_const > MIN_DRNUKE_CONST
182 ? " effective" : "", tlev);
186 pr("Required tech is %d.\n", rqtech);
190 pr("Building with tech level %d.\n", tlev);
194 while (number-- > 0) {
195 while (nxtsct(&nstr, §)) {
199 if (build_it(§, type, sect.sct_item, tlev))
202 snxtsct_rewind(&nstr);
205 pr("Bad sector specification.\n");
211 build_ship(struct sctstr *sp, int type, short *vec, int tlev)
213 struct mchrstr *mp = &mchr[type];
215 struct nstr_item nstr;
218 double eff = SHIP_MINEFF / 100.0;
222 hcm = roundavg(mp->m_hcm * eff);
223 lcm = roundavg(mp->m_lcm * eff);
225 if (sp->sct_type != SCT_HARBR) {
226 pr("Ships must be built in harbours.\n");
229 if (sp->sct_effic < 60 && !player->god) {
230 pr("Sector %s is not 60%% efficient.\n",
231 xyas(sp->sct_x, sp->sct_y, player->cnum));
234 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
235 pr("Not enough materials in %s\n",
236 xyas(sp->sct_x, sp->sct_y, player->cnum));
239 avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
240 if (sp->sct_avail < avail) {
241 pr("Not enough available work in %s to build a %s\n",
242 xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
243 pr(" (%d available work required)\n", avail);
246 cost = mp->m_cost * SHIP_MINEFF / 100.0;
247 if (!build_can_afford(cost, mp->m_name))
249 if (!trechk(player->cnum, 0, NEWSHP))
251 if (!check_sect_ok(sp))
253 sp->sct_avail -= avail;
254 player->dolcost += cost;
255 snxtitem_all(&nstr, EF_SHIP);
256 while (nxtitem(&nstr, &ship)) {
257 if (ship.shp_own == 0) {
263 ef_extend(EF_SHIP, 50);
265 ef_blank(EF_SHIP, nstr.cur, &ship);
266 ship.shp_x = sp->sct_x;
267 ship.shp_y = sp->sct_y;
268 ship.shp_own = player->cnum;
269 ship.shp_type = mp - mchr;
270 ship.shp_effic = SHIP_MINEFF;
271 if (opt_MOB_ACCESS) {
272 game_tick_to_now(&ship.shp_access);
273 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
277 memset(ship.shp_item, 0, sizeof(ship.shp_item));
278 ship.shp_pstage = PLG_HEALTHY;
280 ship.shp_name[0] = 0;
281 ship.shp_orig_own = player->cnum;
282 ship.shp_orig_x = sp->sct_x;
283 ship.shp_orig_y = sp->sct_y;
284 shp_set_tech(&ship, tlev);
285 unit_wipe_orders((struct empobj *)&ship);
290 if (sp->sct_pstage == PLG_INFECT)
291 ship.shp_pstage = PLG_EXPOSED;
292 putship(ship.shp_uid, &ship);
293 pr("%s", prship(&ship));
294 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
299 build_land(struct sctstr *sp, int type, short *vec, int tlev)
301 struct lchrstr *lp = &lchr[type];
303 struct nstr_item nstr;
306 double eff = LAND_MINEFF / 100.0;
307 int mil, lcm, hcm, gun, shell;
311 mil = roundavg(lp->l_mil * eff);
312 shell = roundavg(lp->l_shell * eff);
313 gun = roundavg(lp->l_gun * eff);
315 mil = shell = gun = 0;
317 hcm = roundavg(lp->l_hcm * eff);
318 lcm = roundavg(lp->l_lcm * eff);
320 if (sp->sct_type != SCT_HEADQ) {
321 pr("Land units must be built in headquarters.\n");
324 if (sp->sct_effic < 60 && !player->god) {
325 pr("Sector %s is not 60%% efficient.\n",
326 xyas(sp->sct_x, sp->sct_y, player->cnum));
329 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
330 pr("Not enough materials in %s\n",
331 xyas(sp->sct_x, sp->sct_y, player->cnum));
335 if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
336 pr("Not enough guns in %s\n",
337 xyas(sp->sct_x, sp->sct_y, player->cnum));
340 if (vec[I_SHELL] < shell) {
341 pr("Not enough shells in %s\n",
342 xyas(sp->sct_x, sp->sct_y, player->cnum));
345 if (vec[I_MILIT] < mil) {
346 pr("Not enough military in %s\n",
347 xyas(sp->sct_x, sp->sct_y, player->cnum));
351 if (!trechk(player->cnum, 0, NEWLND))
353 if (!check_sect_ok(sp))
355 avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
356 if (sp->sct_avail < avail) {
357 pr("Not enough available work in %s to build a %s\n",
358 xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
359 pr(" (%d available work required)\n", avail);
362 cost = lp->l_cost * LAND_MINEFF / 100.0;
363 if (!build_can_afford(cost, lp->l_name))
365 sp->sct_avail -= avail;
366 player->dolcost += cost;
367 snxtitem_all(&nstr, EF_LAND);
368 while (nxtitem(&nstr, &land)) {
369 if (land.lnd_own == 0) {
375 ef_extend(EF_LAND, 50);
377 ef_blank(EF_LAND, nstr.cur, &land);
378 land.lnd_x = sp->sct_x;
379 land.lnd_y = sp->sct_y;
380 land.lnd_own = player->cnum;
381 land.lnd_type = lp - lchr;
382 land.lnd_effic = LAND_MINEFF;
383 if (opt_MOB_ACCESS) {
384 game_tick_to_now(&land.lnd_access);
385 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
392 memset(land.lnd_item, 0, sizeof(land.lnd_item));
393 land.lnd_pstage = PLG_HEALTHY;
395 lnd_set_tech(&land, tlev);
396 unit_wipe_orders((struct empobj *)&land);
402 vec[I_SHELL] -= shell;
404 if (sp->sct_pstage == PLG_INFECT)
405 land.lnd_pstage = PLG_EXPOSED;
406 putland(nstr.cur, &land);
407 pr("%s", prland(&land));
408 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
413 build_nuke(struct sctstr *sp, int type, short *vec, int tlev)
415 struct nchrstr *np = &nchr[type];
417 struct nstr_item nstr;
421 if (sp->sct_type != SCT_NUKE && !player->god) {
422 pr("Nuclear weapons must be built in nuclear plants.\n");
425 if (sp->sct_effic < 60 && !player->god) {
426 pr("Sector %s is not 60%% efficient.\n",
427 xyas(sp->sct_x, sp->sct_y, player->cnum));
430 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
431 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
432 pr("Not enough materials for a %s bomb in %s\n",
433 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
434 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
435 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
438 if (!build_can_afford(np->n_cost, np->n_name))
440 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
442 * XXX when nukes turn into units (or whatever), then
443 * make them start at 20%. Since they don't have efficiency
444 * now, we charge all the work right away.
446 if (sp->sct_avail < avail) {
447 pr("Not enough available work in %s to build a %s;\n",
448 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
449 pr(" (%d available work required)\n", avail);
452 if (!trechk(player->cnum, 0, NEWNUK))
454 if (!check_sect_ok(sp))
456 sp->sct_avail -= avail;
457 player->dolcost += np->n_cost;
458 snxtitem_all(&nstr, EF_NUKE);
460 while (nxtitem(&nstr, &nuke)) {
461 if (nuke.nuk_own == 0) {
467 ef_extend(EF_NUKE, 50);
469 ef_blank(EF_NUKE, nstr.cur, &nuke);
470 nuke.nuk_x = sp->sct_x;
471 nuke.nuk_y = sp->sct_y;
472 nuke.nuk_own = sp->sct_own;
473 nuke.nuk_type = np - nchr;
474 nuke.nuk_effic = 100;
476 nuke.nuk_tech = tlev;
477 unit_wipe_orders((struct empobj *)&nuke);
479 vec[I_HCM] -= np->n_hcm;
480 vec[I_LCM] -= np->n_lcm;
481 vec[I_OIL] -= np->n_oil;
482 vec[I_RAD] -= np->n_rad;
484 putnuke(nuke.nuk_uid, &nuke);
485 pr("%s created in %s\n", prnuke(&nuke),
486 xyas(sp->sct_x, sp->sct_y, player->cnum));
491 build_plane(struct sctstr *sp, int type, short *vec, int tlev)
493 struct plchrstr *pp = &plchr[type];
495 struct nstr_item nstr;
498 double eff = PLANE_MINEFF / 100.0;
502 mil = roundavg(pp->pl_crew * eff);
503 /* Always use at least 1 mil to build a plane */
504 if (mil == 0 && pp->pl_crew > 0)
506 hcm = roundavg(pp->pl_hcm * eff);
507 lcm = roundavg(pp->pl_lcm * eff);
508 if (sp->sct_type != SCT_AIRPT && !player->god) {
509 pr("Planes must be built in airports.\n");
512 if (sp->sct_effic < 60 && !player->god) {
513 pr("Sector %s is not 60%% efficient.\n",
514 xyas(sp->sct_x, sp->sct_y, player->cnum));
517 if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
518 pr("Not enough materials in %s\n",
519 xyas(sp->sct_x, sp->sct_y, player->cnum));
522 avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
523 if (sp->sct_avail < avail) {
524 pr("Not enough available work in %s to build a %s\n",
525 xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
526 pr(" (%d available work required)\n", avail);
529 cost = pp->pl_cost * PLANE_MINEFF / 100.0;
530 if (!build_can_afford(cost, pp->pl_name))
532 if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
533 pr("Not enough military for crew in %s\n",
534 xyas(sp->sct_x, sp->sct_y, player->cnum));
537 if (!trechk(player->cnum, 0, NEWPLN))
539 if (!check_sect_ok(sp))
541 sp->sct_avail -= avail;
542 player->dolcost += cost;
543 snxtitem_all(&nstr, EF_PLANE);
545 while (nxtitem(&nstr, &plane)) {
546 if (plane.pln_own == 0) {
551 if (freeplane == 0) {
552 ef_extend(EF_PLANE, 50);
554 ef_blank(EF_PLANE, nstr.cur, &plane);
555 plane.pln_x = sp->sct_x;
556 plane.pln_y = sp->sct_y;
557 plane.pln_own = sp->sct_own;
558 plane.pln_type = pp - plchr;
559 plane.pln_effic = PLANE_MINEFF;
560 if (opt_MOB_ACCESS) {
561 game_tick_to_now(&plane.pln_access);
562 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
566 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
569 plane.pln_harden = 0;
571 pln_set_tech(&plane, tlev);
572 unit_wipe_orders((struct empobj *)&plane);
578 putplane(plane.pln_uid, &plane);
579 pr("%s built in sector %s\n", prplane(&plane),
580 xyas(sp->sct_x, sp->sct_y, player->cnum));
585 build_bridge(char what)
587 struct natstr *natp = getnatp(player->cnum);
588 struct nstr_sect nstr;
589 int (*build_it)(struct sctstr *, short[]);
595 if (natp->nat_level[NAT_TLEV] < buil_bt) {
596 pr("Building a span requires a tech of %.0f\n", buil_bt);
599 build_it = build_bspan;
602 if (!opt_BRIDGETOWERS) {
603 pr("Bridge tower building is disabled.\n");
606 if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
607 pr("Building a tower requires a tech of %.0f\n",
611 build_it = build_btower;
618 if (!snxtsct(&nstr, player->argp[2]))
621 while (nxtsct(&nstr, §)) {
625 if (build_it(§, sect.sct_item))
629 pr("Bad sector specification.\n");
635 build_bspan(struct sctstr *sp, short *vec)
641 int nx, ny, i, good = 0;
645 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
646 if (sp->sct_type != SCT_BTOWER) {
647 if (sp->sct_type != SCT_BHEAD)
649 if (sp->sct_newtype != SCT_BHEAD)
654 if (sp->sct_effic < 60 && !player->god) {
655 pr("Sector %s is not 60%% efficient.\n",
656 xyas(sp->sct_x, sp->sct_y, player->cnum));
660 if (vec[I_HCM] < buil_bh) {
661 pr("%s only has %d unit%s of hcm,\n",
662 xyas(sp->sct_x, sp->sct_y, player->cnum),
663 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
664 pr("(a bridge span requires %d)\n", buil_bh);
668 if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
670 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
671 if (sp->sct_avail < avail) {
672 pr("Not enough available work in %s to build a bridge\n",
673 xyas(sp->sct_x, sp->sct_y, player->cnum));
674 pr(" (%d available work required)\n", avail);
677 if (!player->argp[3]) {
678 pr("Bridge head at %s\n",
679 xyas(sp->sct_x, sp->sct_y, player->cnum));
680 nav_map(sp->sct_x, sp->sct_y, 1);
682 p = getstarg(player->argp[3], "build span in what direction? ", buf);
686 /* Sanity check time */
687 if (!check_sect_ok(sp))
690 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
691 pr("'%c' is not a valid direction...\n", *p);
692 direrr(NULL, NULL, NULL);
695 newx = sp->sct_x + diroff[val][0];
696 newy = sp->sct_y + diroff[val][1];
697 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
698 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
701 if (opt_EASY_BRIDGES) {
704 for (i = 1; i <= 6; i++) {
706 nx = sect.sct_x + diroff[i][0];
707 ny = sect.sct_y + diroff[i][1];
708 getsect(nx, ny, &s2);
709 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
713 pr("Bridges must be built adjacent to land or bridge towers.\n");
714 pr("That sector is not adjacent to land or a bridge tower.\n");
717 } /* end EASY_BRIDGES */
718 sp->sct_avail -= avail;
719 player->dolcost += buil_bc;
720 sect.sct_type = SCT_BSPAN;
721 sect.sct_newtype = SCT_BSPAN;
722 sect.sct_effic = SCT_MINEFF;
725 sect.sct_defense = 0;
726 if (opt_MOB_ACCESS) {
727 game_tick_to_now(§.sct_access);
728 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
733 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
734 writemap(player->cnum);
736 pr("Bridge span built over %s\n",
737 xyas(sect.sct_x, sect.sct_y, player->cnum));
738 vec[I_HCM] -= buil_bh;
743 build_btower(struct sctstr *sp, short *vec)
755 if (sp->sct_type != SCT_BSPAN) {
756 pr("Bridge towers can only be built from bridge spans.\n");
760 if (sp->sct_effic < 60 && !player->god) {
761 pr("Sector %s is not 60%% efficient.\n",
762 xyas(sp->sct_x, sp->sct_y, player->cnum));
766 if (vec[I_HCM] < buil_tower_bh) {
767 pr("%s only has %d unit%s of hcm,\n",
768 xyas(sp->sct_x, sp->sct_y, player->cnum),
769 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
770 pr("(a bridge tower requires %d)\n", buil_tower_bh);
774 if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
776 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
777 if (sp->sct_avail < avail) {
778 pr("Not enough available work in %s to build a bridge tower\n",
779 xyas(sp->sct_x, sp->sct_y, player->cnum));
780 pr(" (%d available work required)\n", avail);
783 if (!player->argp[3]) {
784 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
785 nav_map(sp->sct_x, sp->sct_y, 1);
787 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
791 /* Sanity check time */
792 if (!check_sect_ok(sp))
795 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
796 pr("'%c' is not a valid direction...\n", *p);
797 direrr(NULL, NULL, NULL);
800 newx = sp->sct_x + diroff[val][0];
801 newy = sp->sct_y + diroff[val][1];
802 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
803 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
807 /* Now, check. You aren't allowed to build bridge towers
809 for (i = 1; i <= 6; i++) {
811 nx = sect.sct_x + diroff[i][0];
812 ny = sect.sct_y + diroff[i][1];
813 getsect(nx, ny, &s2);
814 if ((s2.sct_type != SCT_WATER) &&
815 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
816 pr("Bridge towers cannot be built adjacent to land.\n");
817 pr("That sector is adjacent to land.\n");
822 sp->sct_avail -= avail;
823 player->dolcost += buil_tower_bc;
824 sect.sct_type = SCT_BTOWER;
825 sect.sct_newtype = SCT_BTOWER;
826 sect.sct_effic = SCT_MINEFF;
829 sect.sct_defense = 0;
830 if (opt_MOB_ACCESS) {
831 game_tick_to_now(§.sct_access);
832 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
837 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
838 writemap(player->cnum);
840 pr("Bridge tower built in %s\n",
841 xyas(sect.sct_x, sect.sct_y, player->cnum));
842 vec[I_HCM] -= buil_tower_bh;
847 build_can_afford(double cost, char *what)
849 struct natstr *natp = getnatp(player->cnum);
850 if (natp->nat_money < player->dolcost + cost) {
851 pr("Not enough money left to build a %s\n", what);