2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, 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-2014
51 static int build_ship(struct sctstr *sp, int type, int tlev);
52 static int build_land(struct sctstr *sp, int type, int tlev);
53 static int build_nuke(struct sctstr *sp, int type, int tlev);
54 static int build_plane(struct sctstr *sp, int type, int tlev);
55 static int pick_unused_unit_uid(int);
56 static int build_bridge(char);
57 static int build_bspan(struct sctstr *sp);
58 static int build_btower(struct sctstr *sp);
59 static int sector_can_build(struct sctstr *, short[], int, int, char *);
60 static void build_charge(struct sctstr *, short[], int, double, int);
61 static int build_can_afford(double, int, char *);
64 * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
69 struct natstr *natp = getnatp(player->cnum);
70 int tlev = (int)natp->nat_level[NAT_TLEV];
72 struct nstr_sect nstr;
73 int rqtech, type, number, val, gotsect;
74 char *p, *what, *prompt;
75 int (*build_it)(struct sctstr *, int, int);
78 p = getstarg(player->argp[1],
79 "Build (ship, nuke, bridge, plane, land unit, tower)? ",
86 return build_bridge(*p);
89 prompt = "Ship type? ";
90 build_it = build_ship;
94 prompt = "Plane type? ";
95 build_it = build_plane;
99 prompt = "Land unit type? ";
100 build_it = build_land;
103 if (!ef_nelem(EF_NUKE_CHR)) {
104 pr("There are no nukes in this game.\n");
107 if (drnuke_const > MIN_DRNUKE_CONST)
109 (int)(natp->nat_level[NAT_RLEV] / drnuke_const));
111 prompt = "Nuke type? ";
112 build_it = build_nuke;
115 pr("You can't build that!\n");
119 if (!snxtsct(&nstr, player->argp[2]))
122 p = getstarg(player->argp[3], prompt, buf);
129 type = ef_elt_byname(EF_PLANE_CHR, p);
131 rqtech = plchr[type].pl_tech;
134 type = ef_elt_byname(EF_SHIP_CHR, p);
136 rqtech = mchr[type].m_tech;
139 type = ef_elt_byname(EF_LAND_CHR, p);
141 rqtech = lchr[type].l_tech;
144 type = ef_elt_byname(EF_NUKE_CHR, p);
146 rqtech = nchr[type].n_tech;
153 if (type < 0 || tlev < rqtech) {
154 pr("You can't build that!\n");
155 pr("Use `show %s build %d' to show types you can build.\n",
161 if (player->argp[4]) {
162 number = atoi(player->argp[4]);
166 "Are you sure that you want to build %d of them? ",
168 p = getstarg(player->argp[6], bstr, buf);
174 if (player->argp[5]) {
175 val = atoi(player->argp[5]);
176 if (val > tlev && !player->god) {
177 pr("Your%s tech level is only %d.\n",
178 *what == 'n' && drnuke_const > MIN_DRNUKE_CONST
179 ? " effective" : "", tlev);
183 pr("Required tech is %d.\n", rqtech);
187 pr("Building with tech level %d.\n", tlev);
191 while (number-- > 0) {
192 while (nxtsct(&nstr, §)) {
196 if (build_it(§, type, tlev))
199 snxtsct_rewind(&nstr);
207 build_ship(struct sctstr *sp, int type, int tlev)
209 struct mchrstr *mp = &mchr[type];
214 memset(mat, 0, sizeof(mat));
215 mat[I_LCM] = mp->m_lcm;
216 mat[I_HCM] = mp->m_hcm;
217 work = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
219 if (sp->sct_type != SCT_HARBR) {
220 pr("Ships must be built in harbours.\n");
223 if (!sector_can_build(sp, mat, work, SHIP_MINEFF, mp->m_name))
225 if (!build_can_afford(mp->m_cost, SHIP_MINEFF, mp->m_name))
227 build_charge(sp, mat, work, mp->m_cost, SHIP_MINEFF);
229 ef_blank(EF_SHIP, pick_unused_unit_uid(EF_SHIP), &ship);
230 ship.shp_x = sp->sct_x;
231 ship.shp_y = sp->sct_y;
232 ship.shp_own = sp->sct_own;
233 ship.shp_type = mp - mchr;
234 ship.shp_effic = SHIP_MINEFF;
235 if (opt_MOB_ACCESS) {
236 game_tick_to_now(&ship.shp_access);
237 ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
241 memset(ship.shp_item, 0, sizeof(ship.shp_item));
242 ship.shp_pstage = PLG_HEALTHY;
244 ship.shp_name[0] = 0;
245 ship.shp_orig_own = sp->sct_own;
246 ship.shp_orig_x = sp->sct_x;
247 ship.shp_orig_y = sp->sct_y;
248 shp_set_tech(&ship, tlev);
249 unit_wipe_orders((struct empobj *)&ship);
251 if (sp->sct_pstage == PLG_INFECT)
252 ship.shp_pstage = PLG_EXPOSED;
253 putship(ship.shp_uid, &ship);
254 pr("%s", prship(&ship));
255 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
260 build_land(struct sctstr *sp, int type, int tlev)
262 struct lchrstr *lp = &lchr[type];
267 memset(mat, 0, sizeof(mat));
268 mat[I_LCM] = lp->l_lcm;
269 mat[I_HCM] = lp->l_hcm;
270 work = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
272 if (sp->sct_type != SCT_HEADQ) {
273 pr("Land units must be built in headquarters.\n");
276 if (!sector_can_build(sp, mat, work, LAND_MINEFF, lp->l_name))
278 if (!build_can_afford(lp->l_cost, LAND_MINEFF, lp->l_name))
280 build_charge(sp, mat, work, lp->l_cost, LAND_MINEFF);
282 ef_blank(EF_LAND, pick_unused_unit_uid(EF_LAND), &land);
283 land.lnd_x = sp->sct_x;
284 land.lnd_y = sp->sct_y;
285 land.lnd_own = sp->sct_own;
286 land.lnd_type = lp - lchr;
287 land.lnd_effic = LAND_MINEFF;
288 if (opt_MOB_ACCESS) {
289 game_tick_to_now(&land.lnd_access);
290 land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
297 memset(land.lnd_item, 0, sizeof(land.lnd_item));
298 land.lnd_pstage = PLG_HEALTHY;
300 lnd_set_tech(&land, tlev);
301 unit_wipe_orders((struct empobj *)&land);
303 if (sp->sct_pstage == PLG_INFECT)
304 land.lnd_pstage = PLG_EXPOSED;
305 putland(land.lnd_uid, &land);
306 pr("%s", prland(&land));
307 pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
312 build_nuke(struct sctstr *sp, int type, int tlev)
314 short *vec = sp->sct_item;
315 struct nchrstr *np = &nchr[type];
319 if (sp->sct_type != SCT_NUKE && !player->god) {
320 pr("Nuclear weapons must be built in nuclear plants.\n");
323 if (sp->sct_effic < 60 && !player->god) {
324 pr("Sector %s is not 60%% efficient.\n",
325 xyas(sp->sct_x, sp->sct_y, player->cnum));
328 if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
329 vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
330 pr("Not enough materials for a %s bomb in %s\n",
331 np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
332 pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
333 np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
336 if (!build_can_afford(np->n_cost, 100, np->n_name))
338 avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
340 * XXX when nukes turn into units (or whatever), then
341 * make them start at 20%. Since they don't have efficiency
342 * now, we charge all the work right away.
344 if (sp->sct_avail < avail) {
345 pr("Not enough available work in %s to build a %s;\n",
346 xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
347 pr(" (%d available work required)\n", avail);
350 sp->sct_avail -= avail;
351 player->dolcost += np->n_cost;
353 ef_blank(EF_NUKE, pick_unused_unit_uid(EF_NUKE), &nuke);
354 nuke.nuk_x = sp->sct_x;
355 nuke.nuk_y = sp->sct_y;
356 nuke.nuk_own = sp->sct_own;
357 nuke.nuk_type = np - nchr;
358 nuke.nuk_effic = 100;
360 nuke.nuk_tech = tlev;
361 unit_wipe_orders((struct empobj *)&nuke);
363 vec[I_HCM] -= np->n_hcm;
364 vec[I_LCM] -= np->n_lcm;
365 vec[I_OIL] -= np->n_oil;
366 vec[I_RAD] -= np->n_rad;
368 putnuke(nuke.nuk_uid, &nuke);
369 pr("%s created in %s\n", prnuke(&nuke),
370 xyas(sp->sct_x, sp->sct_y, player->cnum));
375 build_plane(struct sctstr *sp, int type, int tlev)
377 struct plchrstr *pp = &plchr[type];
381 double eff = PLANE_MINEFF / 100.0;
384 mil = roundavg(pp->pl_crew * eff);
385 /* Always use at least 1 mil to build a plane */
386 if (mil == 0 && pp->pl_crew > 0)
388 memset(mat, 0, sizeof(mat));
389 mat[I_LCM] = pp->pl_lcm;
390 mat[I_HCM] = pp->pl_hcm;
391 work = PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm);
393 if (sp->sct_type != SCT_AIRPT && !player->god) {
394 pr("Planes must be built in airports.\n");
397 if (!sector_can_build(sp, mat, work, PLANE_MINEFF, pp->pl_name))
399 if (!build_can_afford(pp->pl_cost, PLANE_MINEFF, pp->pl_name))
401 if (sp->sct_item[I_MILIT] < mil) {
402 pr("Not enough military for crew in %s\n",
403 xyas(sp->sct_x, sp->sct_y, player->cnum));
406 sp->sct_item[I_MILIT] -= mil;
407 build_charge(sp, mat, work, pp->pl_cost, PLANE_MINEFF);
409 ef_blank(EF_PLANE, pick_unused_unit_uid(EF_PLANE), &plane);
410 plane.pln_x = sp->sct_x;
411 plane.pln_y = sp->sct_y;
412 plane.pln_own = sp->sct_own;
413 plane.pln_type = pp - plchr;
414 plane.pln_effic = PLANE_MINEFF;
415 if (opt_MOB_ACCESS) {
416 game_tick_to_now(&plane.pln_access);
417 plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
421 plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
424 plane.pln_harden = 0;
426 pln_set_tech(&plane, tlev);
427 unit_wipe_orders((struct empobj *)&plane);
429 putplane(plane.pln_uid, &plane);
430 pr("%s built in sector %s\n", prplane(&plane),
431 xyas(sp->sct_x, sp->sct_y, player->cnum));
436 pick_unused_unit_uid(int type)
438 struct nstr_item nstr;
439 union empobj_storage unit;
441 snxtitem_all(&nstr, type);
442 while (nxtitem(&nstr, &unit)) {
451 build_bridge(char what)
453 struct natstr *natp = getnatp(player->cnum);
454 struct nstr_sect nstr;
455 int (*build_it)(struct sctstr *);
461 if (natp->nat_level[NAT_TLEV] < buil_bt) {
462 pr("Building a span requires a tech of %.0f\n", buil_bt);
465 build_it = build_bspan;
468 if (!opt_BRIDGETOWERS) {
469 pr("Bridge tower building is disabled.\n");
472 if (natp->nat_level[NAT_TLEV] < buil_tower_bt) {
473 pr("Building a tower requires a tech of %.0f\n",
477 build_it = build_btower;
484 if (!snxtsct(&nstr, player->argp[2]))
487 while (nxtsct(&nstr, §)) {
500 build_bspan(struct sctstr *sp)
502 short *vec = sp->sct_item;
507 int nx, ny, i, good = 0;
511 if (opt_EASY_BRIDGES == 0) { /* must have a bridge head or tower */
512 if (sp->sct_type != SCT_BTOWER) {
513 if (sp->sct_type != SCT_BHEAD)
515 if (sp->sct_newtype != SCT_BHEAD)
520 if (sp->sct_effic < 60 && !player->god) {
521 pr("Sector %s is not 60%% efficient.\n",
522 xyas(sp->sct_x, sp->sct_y, player->cnum));
526 if (vec[I_HCM] < buil_bh) {
527 pr("%s only has %d unit%s of hcm,\n",
528 xyas(sp->sct_x, sp->sct_y, player->cnum),
529 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
530 pr("(a bridge span requires %d)\n", buil_bh);
534 if (!build_can_afford(buil_bc, 100, dchr[SCT_BSPAN].d_name))
536 avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
537 if (sp->sct_avail < avail) {
538 pr("Not enough available work in %s to build a bridge\n",
539 xyas(sp->sct_x, sp->sct_y, player->cnum));
540 pr(" (%d available work required)\n", avail);
543 if (!player->argp[3]) {
544 pr("Bridge head at %s\n",
545 xyas(sp->sct_x, sp->sct_y, player->cnum));
546 nav_map(sp->sct_x, sp->sct_y, 1);
548 p = getstarg(player->argp[3], "build span in what direction? ", buf);
552 /* Sanity check time */
553 if (!check_sect_ok(sp))
556 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
557 pr("'%c' is not a valid direction...\n", *p);
558 direrr(NULL, NULL, NULL);
561 newx = sp->sct_x + diroff[val][0];
562 newy = sp->sct_y + diroff[val][1];
563 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
564 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
567 if (opt_EASY_BRIDGES) {
570 for (i = 1; i <= 6; i++) {
572 nx = sect.sct_x + diroff[i][0];
573 ny = sect.sct_y + diroff[i][1];
574 getsect(nx, ny, &s2);
575 if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
579 pr("Bridges must be built adjacent to land or bridge towers.\n");
580 pr("That sector is not adjacent to land or a bridge tower.\n");
583 } /* end EASY_BRIDGES */
584 sp->sct_avail -= avail;
585 player->dolcost += buil_bc;
586 sect.sct_type = SCT_BSPAN;
587 sect.sct_newtype = SCT_BSPAN;
588 sect.sct_effic = SCT_MINEFF;
591 sect.sct_defense = 0;
592 if (opt_MOB_ACCESS) {
593 game_tick_to_now(§.sct_access);
594 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
599 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
600 writemap(player->cnum);
602 pr("Bridge span built over %s\n",
603 xyas(sect.sct_x, sect.sct_y, player->cnum));
604 vec[I_HCM] -= buil_bh;
609 build_btower(struct sctstr *sp)
611 short *vec = sp->sct_item;
622 if (sp->sct_type != SCT_BSPAN) {
623 pr("Bridge towers can only be built from bridge spans.\n");
627 if (sp->sct_effic < 60 && !player->god) {
628 pr("Sector %s is not 60%% efficient.\n",
629 xyas(sp->sct_x, sp->sct_y, player->cnum));
633 if (vec[I_HCM] < buil_tower_bh) {
634 pr("%s only has %d unit%s of hcm,\n",
635 xyas(sp->sct_x, sp->sct_y, player->cnum),
636 vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
637 pr("(a bridge tower requires %d)\n", buil_tower_bh);
641 if (!build_can_afford(buil_tower_bc, 100, dchr[SCT_BTOWER].d_name))
643 avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
644 if (sp->sct_avail < avail) {
645 pr("Not enough available work in %s to build a bridge tower\n",
646 xyas(sp->sct_x, sp->sct_y, player->cnum));
647 pr(" (%d available work required)\n", avail);
650 if (!player->argp[3]) {
651 pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
652 nav_map(sp->sct_x, sp->sct_y, 1);
654 p = getstarg(player->argp[3], "build tower in what direction? ", buf);
658 /* Sanity check time */
659 if (!check_sect_ok(sp))
662 if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
663 pr("'%c' is not a valid direction...\n", *p);
664 direrr(NULL, NULL, NULL);
667 newx = sp->sct_x + diroff[val][0];
668 newy = sp->sct_y + diroff[val][1];
669 if (getsect(newx, newy, §) == 0 || sect.sct_type != SCT_WATER) {
670 pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
674 /* Now, check. You aren't allowed to build bridge towers
676 for (i = 1; i <= 6; i++) {
678 nx = sect.sct_x + diroff[i][0];
679 ny = sect.sct_y + diroff[i][1];
680 getsect(nx, ny, &s2);
681 if ((s2.sct_type != SCT_WATER) &&
682 (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
683 pr("Bridge towers cannot be built adjacent to land.\n");
684 pr("That sector is adjacent to land.\n");
689 sp->sct_avail -= avail;
690 player->dolcost += buil_tower_bc;
691 sect.sct_type = SCT_BTOWER;
692 sect.sct_newtype = SCT_BTOWER;
693 sect.sct_effic = SCT_MINEFF;
696 sect.sct_defense = 0;
697 if (opt_MOB_ACCESS) {
698 game_tick_to_now(§.sct_access);
699 sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
704 map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
705 writemap(player->cnum);
707 pr("Bridge tower built in %s\n",
708 xyas(sect.sct_x, sect.sct_y, player->cnum));
709 vec[I_HCM] -= buil_tower_bh;
714 sector_can_build(struct sctstr *sp, short mat[], int work,
715 int effic, char *what)
720 if (sp->sct_effic < 60 && !player->god) {
721 pr("Sector %s is not 60%% efficient.\n",
722 xyas(sp->sct_x, sp->sct_y, player->cnum));
726 for (i = I_NONE + 1; i <= I_MAX; i++) {
727 needed = mat[i] * (effic / 100.0);
728 if (sp->sct_item[i] < needed) {
729 pr("Not enough materials in %s\n",
730 xyas(sp->sct_x, sp->sct_y, player->cnum));
733 mat[i] = roundavg(needed);
736 avail = (work * effic + 99) / 100;
737 if (sp->sct_avail < avail) {
738 pr("Not enough available work in %s to build a %s\n",
739 xyas(sp->sct_x, sp->sct_y, player->cnum), what);
740 pr(" (%d available work required)\n", avail);
748 build_charge(struct sctstr *sp,
749 short mat[], int work, double cost, int effic)
753 for (i = I_NONE + 1; i <= I_MAX; i++)
754 sp->sct_item[i] -= mat[i];
755 sp->sct_avail -= (work * effic + 99) / 100;
756 player->dolcost += cost * effic / 100.0;
760 build_can_afford(double cost, int effic, char *what)
762 struct natstr *natp = getnatp(player->cnum);
764 if (natp->nat_money < player->dolcost + cost * effic / 100.0) {
765 pr("Not enough money left to build a %s\n", what);