]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / commands / buil.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  buil.c: Build ships, nukes, bridges, planes, land units, bridge towers
28  *
29  *  Known contributors to this file:
30  *      Steve McClure, 1998-2000
31  *      Markus Armbruster, 2004-2009
32  */
33
34 #include <config.h>
35
36 #include <limits.h>
37 #include "commands.h"
38 #include "game.h"
39 #include "land.h"
40 #include "lost.h"
41 #include "map.h"
42 #include "nuke.h"
43 #include "optlist.h"
44 #include "path.h"
45 #include "plague.h"
46 #include "plane.h"
47 #include "ship.h"
48 #include "treaty.h"
49 #include "unit.h"
50
51 static int build_nuke(struct sctstr *sp,
52                       struct nchrstr *np, short *vec, int tlev);
53 static int build_ship(struct sctstr *sp,
54                       struct mchrstr *mp, short *vec, int tlev);
55 static int build_land(struct sctstr *sp,
56                       struct lchrstr *lp, short *vec, int tlev);
57 static int build_bridge(struct sctstr *sp, short *vec);
58 static int build_tower(struct sctstr *sp, short *vec);
59 static int build_plane(struct sctstr *sp,
60                        struct plchrstr *pp, short *vec, int tlev);
61 static int build_can_afford(double, char *);
62
63 /*
64  * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
65  */
66 int
67 buil(void)
68 {
69     struct sctstr sect;
70     struct nstr_sect nstr;
71     struct natstr *natp;
72     int rqtech;
73     int tlev;
74     int rlev;
75     int type;
76     char what;
77     struct lchrstr *lp;
78     struct mchrstr *mp;
79     struct plchrstr *pp;
80     struct nchrstr *np;
81     char *p;
82     int gotsect = 0;
83     int built;
84     int number;
85     char buf[1024];
86
87     natp = getnatp(player->cnum);
88     p = getstarg(player->argp[1],
89                  "Build (ship, nuke, bridge, plane, land unit, tower)? ",
90                  buf);
91     if (!p)
92         return RET_SYN;
93     what = *p;
94
95     if (!snxtsct(&nstr, player->argp[2])) {
96         pr("Bad sector specification.\n");
97         return RET_SYN;
98     }
99     tlev = (int)natp->nat_level[NAT_TLEV];
100     rlev = (int)natp->nat_level[NAT_RLEV];
101
102     switch (what) {
103     case 'p':
104         p = getstarg(player->argp[3], "Plane type? ", buf);
105         if (!p || !*p)
106             return RET_SYN;
107         type = ef_elt_byname(EF_PLANE_CHR, p);
108         if (type >= 0) {
109             pp = &plchr[type];
110             rqtech = pp->pl_tech;
111             if (rqtech > tlev)
112                 type = -1;
113         }
114         if (type < 0) {
115             pr("You can't build that!\n");
116             pr("Use `show plane build %d' to show types you can build.\n",
117                tlev);
118             return RET_FAIL;
119         }
120         break;
121     case 's':
122         p = getstarg(player->argp[3], "Ship type? ", buf);
123         if (!p || !*p)
124             return RET_SYN;
125         type = ef_elt_byname(EF_SHIP_CHR, p);
126         if (type >= 0) {
127             mp = &mchr[type];
128             rqtech = mp->m_tech;
129             if (rqtech > tlev)
130                 type = -1;
131             if ((mp->m_flags & M_TRADE) && !opt_TRADESHIPS)
132                 type = -1;
133         }
134         if (type < 0) {
135             pr("You can't build that!\n");
136             pr("Use `show ship build %d' to show types you can build.\n",
137                tlev);
138             return RET_FAIL;
139         }
140         break;
141     case 'l':
142         p = getstarg(player->argp[3], "Land unit type? ", buf);
143         if (!p || !*p)
144             return RET_SYN;
145         type = ef_elt_byname(EF_LAND_CHR, p);
146         if (type >= 0) {
147             lp = &lchr[type];
148             rqtech = lp->l_tech;
149             if (rqtech > tlev)
150                 type = -1;
151             if ((lp->l_flags & L_SPY) && !opt_LANDSPIES)
152                 type = -1;
153         }
154         if (type < 0) {
155             pr("You can't build that!\n");
156             pr("Use `show land build %d' to show types you can build.\n",
157                tlev);
158             return RET_FAIL;
159         }
160         break;
161     case 'b':
162         if (natp->nat_level[NAT_TLEV] + 0.005 < buil_bt) {
163             pr("Building a span requires a tech of %.0f\n", buil_bt);
164             return RET_FAIL;
165         }
166         break;
167     case 't':
168         if (!opt_BRIDGETOWERS) {
169             pr("Bridge tower building is disabled.\n");
170             return RET_FAIL;
171         }
172         if (natp->nat_level[NAT_TLEV] + 0.005 < buil_tower_bt) {
173             pr("Building a tower requires a tech of %.0f\n",
174                buil_tower_bt);
175             return RET_FAIL;
176         }
177         break;
178     case 'n':
179         if (!ef_nelem(EF_NUKE_CHR)) {
180             pr("There are no nukes in this game.\n");
181             return RET_FAIL;
182         }
183         p = getstarg(player->argp[3], "Nuke type? ", buf);
184         if (!p || !*p)
185             return RET_SYN;
186         type = ef_elt_byname(EF_NUKE_CHR, p);
187         if (type >= 0) {
188             np = &nchr[type];
189             rqtech = np->n_tech;
190             if (rqtech > tlev
191                 || (drnuke_const > MIN_DRNUKE_CONST &&
192                     np->n_tech * drnuke_const > rlev))
193                 type = -1;
194         }
195         if (type < 0) {
196             int tt = tlev;
197             if (drnuke_const > MIN_DRNUKE_CONST)
198                 tt = (tlev < (rlev / drnuke_const) ? (int)tlev :
199                       (int)(rlev / drnuke_const));
200             pr("You can't build that!\n");
201             pr("Use `show nuke build %d' to show types you can build.\n",
202                tt);
203             return RET_FAIL;
204         }
205         break;
206     default:
207         pr("You can't build that!\n");
208         return RET_SYN;
209     }
210
211     number = 1;
212     if (what != 'b' && what != 't') {
213         if (player->argp[4]) {
214             number = atoi(player->argp[4]);
215             if (number > 20) {
216                 char bstr[80];
217                 sprintf(bstr,
218                         "Are you sure that you want to build %s of them? ",
219                         player->argp[4]);
220                 p = getstarg(player->argp[6], bstr, buf);
221                 if (!p || *p != 'y')
222                     return RET_SYN;
223             }
224         }
225     }
226
227     if (what != 'b' && what != 'n' && what != 't') {
228         if (player->argp[5]) {
229             tlev = atoi(player->argp[5]);
230             if (tlev > natp->nat_level[NAT_TLEV] && !player->god) {
231                 pr("Your tech level is only %d.\n",
232                    (int)natp->nat_level[NAT_TLEV]);
233                 return RET_FAIL;
234             }
235             if (rqtech > tlev) {
236                 pr("Required tech is %d.\n", rqtech);
237                 return RET_FAIL;
238             }
239             pr("building with tech level %d.\n", tlev);
240         }
241     }
242
243     while (number-- > 0) {
244         while (nxtsct(&nstr, &sect)) {
245             gotsect++;
246             if (!player->owner)
247                 continue;
248             switch (what) {
249             case 'l':
250                 built = build_land(&sect, lp, sect.sct_item, tlev);
251                 break;
252             case 's':
253                 built = build_ship(&sect, mp, sect.sct_item, tlev);
254                 break;
255             case 'b':
256                 built = build_bridge(&sect, sect.sct_item);
257                 break;
258             case 't':
259                 built = build_tower(&sect, sect.sct_item);
260                 break;
261             case 'n':
262                 built = build_nuke(&sect, np, sect.sct_item, tlev);
263                 break;
264             case 'p':
265                 built = build_plane(&sect, pp, sect.sct_item, tlev);
266                 break;
267             default:
268                 CANT_REACH();
269                 return RET_FAIL;
270             }
271             if (built) {
272                 putsect(&sect);
273             }
274         }
275         snxtsct_rewind(&nstr);
276     }
277     if (!gotsect) {
278         pr("Bad sector specification.\n");
279     }
280     return RET_OK;
281 }
282
283 static int
284 build_ship(struct sctstr *sp, struct mchrstr *mp, short *vec, int tlev)
285 {
286     struct shpstr ship;
287     struct nstr_item nstr;
288     int avail;
289     double cost;
290     double eff = SHIP_MINEFF / 100.0;
291     int lcm, hcm;
292     int freeship = 0;
293
294     hcm = roundavg(mp->m_hcm * eff);
295     lcm = roundavg(mp->m_lcm * eff);
296
297     if (sp->sct_type != SCT_HARBR) {
298         pr("Ships must be built in harbours.\n");
299         return 0;
300     }
301     if (sp->sct_effic < 60 && !player->god) {
302         pr("Sector %s is not 60%% efficient.\n",
303            xyas(sp->sct_x, sp->sct_y, player->cnum));
304         return 0;
305     }
306     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
307         pr("Not enough materials in %s\n",
308            xyas(sp->sct_x, sp->sct_y, player->cnum));
309         return 0;
310     }
311     avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * SHIP_MINEFF + 99) / 100;
312     if (sp->sct_avail < avail) {
313         pr("Not enough available work in %s to build a %s\n",
314            xyas(sp->sct_x, sp->sct_y, player->cnum), mp->m_name);
315         pr(" (%d available work required)\n", avail);
316         return 0;
317     }
318     cost = mp->m_cost * SHIP_MINEFF / 100.0;
319     if (!build_can_afford(cost, mp->m_name))
320         return 0;
321     if (!trechk(player->cnum, 0, NEWSHP))
322         return 0;
323     if (!check_sect_ok(sp))
324         return 0;
325     sp->sct_avail -= avail;
326     player->dolcost += cost;
327     snxtitem_all(&nstr, EF_SHIP);
328     while (nxtitem(&nstr, &ship)) {
329         if (ship.shp_own == 0) {
330             freeship++;
331             break;
332         }
333     }
334     if (freeship == 0) {
335         ef_extend(EF_SHIP, 50);
336     }
337     ef_blank(EF_SHIP, nstr.cur, &ship);
338     ship.shp_x = sp->sct_x;
339     ship.shp_y = sp->sct_y;
340     ship.shp_own = player->cnum;
341     ship.shp_type = mp - mchr;
342     ship.shp_effic = SHIP_MINEFF;
343     if (opt_MOB_ACCESS) {
344         game_tick_to_now(&ship.shp_access);
345         ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
346     } else {
347         ship.shp_mobil = 0;
348     }
349     memset(ship.shp_item, 0, sizeof(ship.shp_item));
350     ship.shp_pstage = PLG_HEALTHY;
351     ship.shp_ptime = 0;
352     ship.shp_name[0] = 0;
353     ship.shp_orig_own = player->cnum;
354     ship.shp_orig_x = sp->sct_x;
355     ship.shp_orig_y = sp->sct_y;
356     shp_set_tech(&ship, tlev);
357     unit_wipe_orders((struct empobj *)&ship);
358
359     vec[I_LCM] -= lcm;
360     vec[I_HCM] -= hcm;
361
362     if (sp->sct_pstage == PLG_INFECT)
363         ship.shp_pstage = PLG_EXPOSED;
364     putship(ship.shp_uid, &ship);
365     pr("%s", prship(&ship));
366     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
367     return 1;
368 }
369
370 static int
371 build_land(struct sctstr *sp, struct lchrstr *lp, short *vec, int tlev)
372 {
373     struct lndstr land;
374     struct nstr_item nstr;
375     int avail;
376     double cost;
377     double eff = LAND_MINEFF / 100.0;
378     int mil, lcm, hcm, gun, shell;
379     int freeland = 0;
380
381 #if 0
382     mil = roundavg(lp->l_mil * eff);
383     shell = roundavg(lp->l_shell * eff);
384     gun = roundavg(lp->l_gun * eff);
385 #else
386     mil = shell = gun = 0;
387 #endif
388     hcm = roundavg(lp->l_hcm * eff);
389     lcm = roundavg(lp->l_lcm * eff);
390
391     if (sp->sct_type != SCT_HEADQ) {
392         pr("Land units must be built in headquarters.\n");
393         return 0;
394     }
395     if (sp->sct_effic < 60 && !player->god) {
396         pr("Sector %s is not 60%% efficient.\n",
397            xyas(sp->sct_x, sp->sct_y, player->cnum));
398         return 0;
399     }
400     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
401         pr("Not enough materials in %s\n",
402            xyas(sp->sct_x, sp->sct_y, player->cnum));
403         return 0;
404     }
405 #if 0
406     if (vec[I_GUN] < gun || vec[I_GUN] == 0) {
407         pr("Not enough guns in %s\n",
408            xyas(sp->sct_x, sp->sct_y, player->cnum));
409         return 0;
410     }
411     if (vec[I_SHELL] < shell) {
412         pr("Not enough shells in %s\n",
413            xyas(sp->sct_x, sp->sct_y, player->cnum));
414         return 0;
415     }
416     if (vec[I_MILIT] < mil) {
417         pr("Not enough military in %s\n",
418            xyas(sp->sct_x, sp->sct_y, player->cnum));
419         return 0;
420     }
421 #endif
422     if (!trechk(player->cnum, 0, NEWLND))
423         return 0;
424     if (!check_sect_ok(sp))
425         return 0;
426     avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * LAND_MINEFF + 99) / 100;
427     if (sp->sct_avail < avail) {
428         pr("Not enough available work in %s to build a %s\n",
429            xyas(sp->sct_x, sp->sct_y, player->cnum), lp->l_name);
430         pr(" (%d available work required)\n", avail);
431         return 0;
432     }
433     cost = lp->l_cost * LAND_MINEFF / 100.0;
434     if (!build_can_afford(cost, lp->l_name))
435         return 0;
436     sp->sct_avail -= avail;
437     player->dolcost += cost;
438     snxtitem_all(&nstr, EF_LAND);
439     while (nxtitem(&nstr, &land)) {
440         if (land.lnd_own == 0) {
441             freeland++;
442             break;
443         }
444     }
445     if (freeland == 0) {
446         ef_extend(EF_LAND, 50);
447     }
448     ef_blank(EF_LAND, nstr.cur, &land);
449     land.lnd_x = sp->sct_x;
450     land.lnd_y = sp->sct_y;
451     land.lnd_own = player->cnum;
452     land.lnd_type = lp - lchr;
453     land.lnd_effic = LAND_MINEFF;
454     if (opt_MOB_ACCESS) {
455         game_tick_to_now(&land.lnd_access);
456         land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
457     } else {
458         land.lnd_mobil = 0;
459     }
460     land.lnd_ship = -1;
461     land.lnd_land = -1;
462     land.lnd_harden = 0;
463     memset(land.lnd_item, 0, sizeof(land.lnd_item));
464     land.lnd_pstage = PLG_HEALTHY;
465     land.lnd_ptime = 0;
466     lnd_set_tech(&land, tlev);
467     unit_wipe_orders((struct empobj *)&land);
468
469     vec[I_LCM] -= lcm;
470     vec[I_HCM] -= hcm;
471     vec[I_MILIT] -= mil;
472     vec[I_GUN] -= gun;
473     vec[I_SHELL] -= shell;
474
475     if (sp->sct_pstage == PLG_INFECT)
476         land.lnd_pstage = PLG_EXPOSED;
477     putland(nstr.cur, &land);
478     pr("%s", prland(&land));
479     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
480     return 1;
481 }
482
483 static int
484 build_bridge(struct sctstr *sp, short *vec)
485 {
486     struct sctstr sect;
487     int val;
488     int newx, newy;
489     int avail;
490     int nx, ny, i, good = 0;
491     char *p;
492     char buf[1024];
493
494     if (opt_EASY_BRIDGES == 0) {        /* must have a bridge head or tower */
495         if (sp->sct_type != SCT_BTOWER) {
496             if (sp->sct_type != SCT_BHEAD)
497                 return 0;
498             if (sp->sct_newtype != SCT_BHEAD)
499                 return 0;
500         }
501     }
502
503     if (sp->sct_effic < 60 && !player->god) {
504         pr("Sector %s is not 60%% efficient.\n",
505            xyas(sp->sct_x, sp->sct_y, player->cnum));
506         return 0;
507     }
508
509     if (vec[I_HCM] < buil_bh) {
510         pr("%s only has %d unit%s of hcm,\n",
511            xyas(sp->sct_x, sp->sct_y, player->cnum),
512            vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
513         pr("(a bridge span requires %d)\n", buil_bh);
514         return 0;
515     }
516
517     if (!build_can_afford(buil_bc, dchr[SCT_BSPAN].d_name))
518         return 0;
519     avail = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
520     if (sp->sct_avail < avail) {
521         pr("Not enough available work in %s to build a bridge\n",
522            xyas(sp->sct_x, sp->sct_y, player->cnum));
523         pr(" (%d available work required)\n", avail);
524         return 0;
525     }
526     if (!player->argp[3]) {
527         pr("Bridge head at %s\n",
528            xyas(sp->sct_x, sp->sct_y, player->cnum));
529         nav_map(sp->sct_x, sp->sct_y, 1);
530     }
531     p = getstarg(player->argp[3], "build span in what direction? ", buf);
532     if (!p || !*p) {
533         return 0;
534     }
535     /* Sanity check time */
536     if (!check_sect_ok(sp))
537         return 0;
538
539     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
540         pr("'%c' is not a valid direction...\n", *p);
541         direrr(NULL, NULL, NULL);
542         return 0;
543     }
544     newx = sp->sct_x + diroff[val][0];
545     newy = sp->sct_y + diroff[val][1];
546     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
547         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
548         return 0;
549     }
550     if (opt_EASY_BRIDGES) {
551         good = 0;
552
553         for (i = 1; i <= 6; i++) {
554             struct sctstr s2;
555             nx = sect.sct_x + diroff[i][0];
556             ny = sect.sct_y + diroff[i][1];
557             getsect(nx, ny, &s2);
558             if ((s2.sct_type != SCT_WATER) && (s2.sct_type != SCT_BSPAN))
559                 good = 1;
560         }
561         if (!good) {
562             pr("Bridges must be built adjacent to land or bridge towers.\n");
563             pr("That sector is not adjacent to land or a bridge tower.\n");
564             return 0;
565         }
566     }                           /* end EASY_BRIDGES */
567     sp->sct_avail -= avail;
568     player->dolcost += buil_bc;
569     sect.sct_type = SCT_BSPAN;
570     sect.sct_newtype = SCT_BSPAN;
571     sect.sct_effic = SCT_MINEFF;
572     sect.sct_road = 0;
573     sect.sct_rail = 0;
574     sect.sct_defense = 0;
575     if (opt_MOB_ACCESS) {
576         game_tick_to_now(&sect.sct_access);
577         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
578     } else {
579         sect.sct_mobil = 0;
580     }
581     sect.sct_mines = 0;
582     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
583     writemap(player->cnum);
584     putsect(&sect);
585     pr("Bridge span built over %s\n",
586        xyas(sect.sct_x, sect.sct_y, player->cnum));
587     vec[I_HCM] -= buil_bh;
588     return 1;
589 }
590
591 static int
592 build_nuke(struct sctstr *sp, struct nchrstr *np, short *vec, int tlev)
593 {
594     struct nukstr nuke;
595     struct nstr_item nstr;
596     int avail;
597     int freenuke;
598
599     if (sp->sct_type != SCT_NUKE && !player->god) {
600         pr("Nuclear weapons must be built in nuclear plants.\n");
601         return 0;
602     }
603     if (sp->sct_effic < 60 && !player->god) {
604         pr("Sector %s is not 60%% efficient.\n",
605            xyas(sp->sct_x, sp->sct_y, player->cnum));
606         return 0;
607     }
608     if (vec[I_HCM] < np->n_hcm || vec[I_LCM] < np->n_lcm ||
609         vec[I_OIL] < np->n_oil || vec[I_RAD] < np->n_rad) {
610         pr("Not enough materials for a %s bomb in %s\n",
611            np->n_name, xyas(sp->sct_x, sp->sct_y, player->cnum));
612         pr("(%d hcm, %d lcm, %d oil, & %d rads).\n",
613            np->n_hcm, np->n_lcm, np->n_oil, np->n_rad);
614         return 0;
615     }
616     if (!build_can_afford(np->n_cost, np->n_name))
617         return 0;
618     avail = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
619     /*
620      * XXX when nukes turn into units (or whatever), then
621      * make them start at 20%.  Since they don't have efficiency
622      * now, we charge all the work right away.
623      */
624     if (sp->sct_avail < avail) {
625         pr("Not enough available work in %s to build a %s;\n",
626            xyas(sp->sct_x, sp->sct_y, player->cnum), np->n_name);
627         pr(" (%d available work required)\n", avail);
628         return 0;
629     }
630     if (!trechk(player->cnum, 0, NEWNUK))
631         return 0;
632     if (!check_sect_ok(sp))
633         return 0;
634     sp->sct_avail -= avail;
635     player->dolcost += np->n_cost;
636     snxtitem_all(&nstr, EF_NUKE);
637     freenuke = 0;
638     while (nxtitem(&nstr, &nuke)) {
639         if (nuke.nuk_own == 0) {
640             freenuke++;
641             break;
642         }
643     }
644     if (freenuke == 0) {
645         ef_extend(EF_NUKE, 50);
646     }
647     ef_blank(EF_NUKE, nstr.cur, &nuke);
648     nuke.nuk_x = sp->sct_x;
649     nuke.nuk_y = sp->sct_y;
650     nuke.nuk_own = sp->sct_own;
651     nuke.nuk_type = np - nchr;
652     nuke.nuk_effic = 100;
653     nuke.nuk_plane = -1;
654     nuke.nuk_tech = tlev;
655     unit_wipe_orders((struct empobj *)&nuke);
656
657     vec[I_HCM] -= np->n_hcm;
658     vec[I_LCM] -= np->n_lcm;
659     vec[I_OIL] -= np->n_oil;
660     vec[I_RAD] -= np->n_rad;
661
662     putnuke(nuke.nuk_uid, &nuke);
663     pr("%s created in %s\n", prnuke(&nuke),
664        xyas(sp->sct_x, sp->sct_y, player->cnum));
665     return 1;
666 }
667
668 static int
669 build_plane(struct sctstr *sp, struct plchrstr *pp, short *vec, int tlev)
670 {
671     struct plnstr plane;
672     struct nstr_item nstr;
673     int avail;
674     double cost;
675     double eff = PLANE_MINEFF / 100.0;
676     int hcm, lcm, mil;
677     int freeplane;
678
679     mil = roundavg(pp->pl_crew * eff);
680     /* Always use at least 1 mil to build a plane */
681     if (mil == 0 && pp->pl_crew > 0)
682         mil = 1;
683     hcm = roundavg(pp->pl_hcm * eff);
684     lcm = roundavg(pp->pl_lcm * eff);
685     if (sp->sct_type != SCT_AIRPT && !player->god) {
686         pr("Planes must be built in airports.\n");
687         return 0;
688     }
689     if (sp->sct_effic < 60 && !player->god) {
690         pr("Sector %s is not 60%% efficient.\n",
691            xyas(sp->sct_x, sp->sct_y, player->cnum));
692         return 0;
693     }
694     if (vec[I_LCM] < lcm || vec[I_HCM] < hcm) {
695         pr("Not enough materials in %s\n",
696            xyas(sp->sct_x, sp->sct_y, player->cnum));
697         return 0;
698     }
699     avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * PLANE_MINEFF + 99) / 100;
700     if (sp->sct_avail < avail) {
701         pr("Not enough available work in %s to build a %s\n",
702            xyas(sp->sct_x, sp->sct_y, player->cnum), pp->pl_name);
703         pr(" (%d available work required)\n", avail);
704         return 0;
705     }
706     cost = pp->pl_cost * PLANE_MINEFF / 100.0;
707     if (!build_can_afford(cost, pp->pl_name))
708         return 0;
709     if (vec[I_MILIT] < mil || (vec[I_MILIT] == 0 && pp->pl_crew > 0)) {
710         pr("Not enough military for crew in %s\n",
711            xyas(sp->sct_x, sp->sct_y, player->cnum));
712         return 0;
713     }
714     if (!trechk(player->cnum, 0, NEWPLN))
715         return 0;
716     if (!check_sect_ok(sp))
717         return 0;
718     sp->sct_avail -= avail;
719     player->dolcost += cost;
720     snxtitem_all(&nstr, EF_PLANE);
721     freeplane = 0;
722     while (nxtitem(&nstr, &plane)) {
723         if (plane.pln_own == 0) {
724             freeplane++;
725             break;
726         }
727     }
728     if (freeplane == 0) {
729         ef_extend(EF_PLANE, 50);
730     }
731     ef_blank(EF_PLANE, nstr.cur, &plane);
732     plane.pln_x = sp->sct_x;
733     plane.pln_y = sp->sct_y;
734     plane.pln_own = sp->sct_own;
735     plane.pln_type = pp - plchr;
736     plane.pln_effic = PLANE_MINEFF;
737     if (opt_MOB_ACCESS) {
738         game_tick_to_now(&plane.pln_access);
739         plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
740     } else {
741         plane.pln_mobil = 0;
742     }
743     plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
744     plane.pln_ship = -1;
745     plane.pln_land = -1;
746     plane.pln_harden = 0;
747     plane.pln_flags = 0;
748     pln_set_tech(&plane, tlev);
749     unit_wipe_orders((struct empobj *)&plane);
750
751     vec[I_LCM] -= lcm;
752     vec[I_HCM] -= hcm;
753     vec[I_MILIT] -= mil;
754
755     putplane(plane.pln_uid, &plane);
756     pr("%s built in sector %s\n", prplane(&plane),
757        xyas(sp->sct_x, sp->sct_y, player->cnum));
758     return 1;
759 }
760
761 static int
762 build_tower(struct sctstr *sp, short *vec)
763 {
764     struct sctstr sect;
765     int val;
766     int newx, newy;
767     int avail;
768     char *p;
769     char buf[1024];
770     int i;
771     int nx;
772     int ny;
773
774     if (sp->sct_type != SCT_BSPAN) {
775         pr("Bridge towers can only be built from bridge spans.\n");
776         return 0;
777     }
778
779     if (sp->sct_effic < 60 && !player->god) {
780         pr("Sector %s is not 60%% efficient.\n",
781            xyas(sp->sct_x, sp->sct_y, player->cnum));
782         return 0;
783     }
784
785     if (vec[I_HCM] < buil_tower_bh) {
786         pr("%s only has %d unit%s of hcm,\n",
787            xyas(sp->sct_x, sp->sct_y, player->cnum),
788            vec[I_HCM], vec[I_HCM] > 1 ? "s" : "");
789         pr("(a bridge tower requires %d)\n", buil_tower_bh);
790         return 0;
791     }
792
793     if (!build_can_afford(buil_tower_bc, dchr[SCT_BTOWER].d_name))
794         return 0;
795     avail = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
796     if (sp->sct_avail < avail) {
797         pr("Not enough available work in %s to build a bridge tower\n",
798            xyas(sp->sct_x, sp->sct_y, player->cnum));
799         pr(" (%d available work required)\n", avail);
800         return 0;
801     }
802     if (!player->argp[3]) {
803         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
804         nav_map(sp->sct_x, sp->sct_y, 1);
805     }
806     p = getstarg(player->argp[3], "build tower in what direction? ", buf);
807     if (!p || !*p) {
808         return 0;
809     }
810     /* Sanity check time */
811     if (!check_sect_ok(sp))
812         return 0;
813
814     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
815         pr("'%c' is not a valid direction...\n", *p);
816         direrr(NULL, NULL, NULL);
817         return 0;
818     }
819     newx = sp->sct_x + diroff[val][0];
820     newy = sp->sct_y + diroff[val][1];
821     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
822         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
823         return 0;
824     }
825
826     /* Now, check.  You aren't allowed to build bridge towers
827        next to land. */
828     for (i = 1; i <= 6; i++) {
829         struct sctstr s2;
830         nx = sect.sct_x + diroff[i][0];
831         ny = sect.sct_y + diroff[i][1];
832         getsect(nx, ny, &s2);
833         if ((s2.sct_type != SCT_WATER) &&
834             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
835             pr("Bridge towers cannot be built adjacent to land.\n");
836             pr("That sector is adjacent to land.\n");
837             return 0;
838         }
839     }
840
841     sp->sct_avail -= avail;
842     player->dolcost += buil_tower_bc;
843     sect.sct_type = SCT_BTOWER;
844     sect.sct_newtype = SCT_BTOWER;
845     sect.sct_effic = SCT_MINEFF;
846     sect.sct_road = 0;
847     sect.sct_rail = 0;
848     sect.sct_defense = 0;
849     if (opt_MOB_ACCESS) {
850         game_tick_to_now(&sect.sct_access);
851         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
852     } else {
853         sect.sct_mobil = 0;
854     }
855     sect.sct_mines = 0;
856     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
857     writemap(player->cnum);
858     putsect(&sect);
859     pr("Bridge tower built in %s\n",
860        xyas(sect.sct_x, sect.sct_y, player->cnum));
861     vec[I_HCM] -= buil_tower_bh;
862     return 1;
863 }
864
865 static int
866 build_can_afford(double cost, char *what)
867 {
868     struct natstr *natp = getnatp(player->cnum);
869     if (natp->nat_money < player->dolcost + cost) {
870         pr("Not enough money left to build a %s\n", what);
871         return 0;
872     }
873     return 1;
874 }