]> git.pond.sub.org Git - empserver/blob - src/lib/commands/buil.c
b30049bb73221033fea775da53f12cab0e780bbd
[empserver] / src / lib / commands / buil.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, 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-2014
32  */
33
34 #include <config.h>
35
36 #include <limits.h>
37 #include <math.h>
38 #include "chance.h"
39 #include "commands.h"
40 #include "game.h"
41 #include "land.h"
42 #include "lost.h"
43 #include "map.h"
44 #include "nuke.h"
45 #include "optlist.h"
46 #include "path.h"
47 #include "plague.h"
48 #include "plane.h"
49 #include "ship.h"
50 #include "unit.h"
51
52 static int build_ship(struct sctstr *sp, int type, int tlev);
53 static int build_land(struct sctstr *sp, int type, int tlev);
54 static int build_nuke(struct sctstr *sp, int type, int tlev);
55 static int build_plane(struct sctstr *sp, int type, int tlev);
56 static int pick_unused_unit_uid(int);
57 static int build_bridge(char);
58 static int build_bspan(struct sctstr *sp);
59 static int build_btower(struct sctstr *sp);
60 static int sector_can_build(struct sctstr *, short[], int, int, char *);
61 static void build_charge(struct sctstr *, short[], int, double, int);
62 static int build_can_afford(double, int, char *);
63
64 /*
65  * build <WHAT> <SECTS> <TYPE|DIR|MEG> [NUMBER]
66  */
67 int
68 buil(void)
69 {
70     struct natstr *natp = getnatp(player->cnum);
71     int tlev = (int)natp->nat_level[NAT_TLEV];
72     struct sctstr sect;
73     struct nstr_sect nstr;
74     int rqtech, type, number, val, gotsect;
75     char *p, *what, *prompt;
76     int (*build_it)(struct sctstr *, int, int);
77     char buf[1024];
78
79     p = getstarg(player->argp[1],
80                  "Build (ship, nuke, bridge, plane, land unit, tower)? ",
81                  buf);
82     if (!p)
83         return RET_SYN;
84     switch (*p) {
85     case 'b':
86     case 't':
87         return build_bridge(*p);
88     case 's':
89         what = "ship";
90         prompt = "Ship type? ";
91         build_it = build_ship;
92         break;
93     case 'p':
94         what = "plane";
95         prompt = "Plane type? ";
96         build_it = build_plane;
97         break;
98     case 'l':
99         what = "land";
100         prompt = "Land unit type? ";
101         build_it = build_land;
102         break;
103     case 'n':
104         if (!ef_nelem(EF_NUKE_CHR)) {
105             pr("There are no nukes in this game.\n");
106             return RET_FAIL;
107         }
108         if (drnuke_const > MIN_DRNUKE_CONST)
109             tlev = MIN(tlev,
110                        (int)(natp->nat_level[NAT_RLEV] / drnuke_const));
111         what = "nuke";
112         prompt = "Nuke type? ";
113         build_it = build_nuke;
114         break;
115     default:
116         pr("You can't build that!\n");
117         return RET_SYN;
118     }
119
120     if (!snxtsct(&nstr, player->argp[2]))
121         return RET_SYN;
122
123     p = getstarg(player->argp[3], prompt, buf);
124     if (!p || !*p)
125         return RET_SYN;
126
127     rqtech = 0;
128     switch (*what) {
129     case 'p':
130         type = ef_elt_byname(EF_PLANE_CHR, p);
131         if (type >= 0)
132             rqtech = plchr[type].pl_tech;
133         break;
134     case 's':
135         type = ef_elt_byname(EF_SHIP_CHR, p);
136         if (type >= 0)
137             rqtech = mchr[type].m_tech;
138         break;
139     case 'l':
140         type = ef_elt_byname(EF_LAND_CHR, p);
141         if (type >= 0)
142             rqtech = lchr[type].l_tech;
143         break;
144     case 'n':
145         type = ef_elt_byname(EF_NUKE_CHR, p);
146         if (type >= 0)
147             rqtech = nchr[type].n_tech;
148         break;
149     default:
150         CANT_REACH();
151         return RET_FAIL;
152     }
153
154     if (tlev < rqtech && player->god)
155         tlev = rqtech;
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",
159            what, tlev);
160         return RET_FAIL;
161     }
162
163     number = 1;
164     if (player->argp[4]) {
165         number = atoi(player->argp[4]);
166         if (number > 20) {
167             char bstr[80];
168             sprintf(bstr,
169                     "Are you sure that you want to build %d of them? ",
170                     number);
171             p = getstarg(player->argp[6], bstr, buf);
172             if (!p || *p != 'y')
173                 return RET_SYN;
174         }
175     }
176
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);
183             return RET_FAIL;
184         }
185         if (rqtech > val) {
186             pr("Required tech is %d.\n", rqtech);
187             return RET_FAIL;
188         }
189         tlev = val;
190         pr("Building with tech level %d.\n", tlev);
191     }
192
193     gotsect = 0;
194     while (number-- > 0) {
195         while (nxtsct(&nstr, &sect)) {
196             if (!player->owner)
197                 continue;
198             gotsect = 1;
199             if (build_it(&sect, type, tlev))
200                 putsect(&sect);
201         }
202         snxtsct_rewind(&nstr);
203     }
204     if (!gotsect)
205         pr("No sectors.\n");
206     return RET_OK;
207 }
208
209 static int
210 build_ship(struct sctstr *sp, int type, int tlev)
211 {
212     struct mchrstr *mp = &mchr[type];
213     short mat[I_MAX+1];
214     int work;
215     struct shpstr ship;
216
217     memset(mat, 0, sizeof(mat));
218     mat[I_LCM] = mp->m_lcm;
219     mat[I_HCM] = mp->m_hcm;
220     work = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
221
222     if (sp->sct_type != SCT_HARBR && !player->god) {
223         pr("Ships must be built in harbours.\n");
224         return 0;
225     }
226     if (!sector_can_build(sp, mat, work, SHIP_MINEFF, mp->m_name))
227         return 0;
228     if (!build_can_afford(mp->m_cost, SHIP_MINEFF, mp->m_name))
229         return 0;
230     build_charge(sp, mat, work, mp->m_cost, SHIP_MINEFF);
231
232     ef_blank(EF_SHIP, pick_unused_unit_uid(EF_SHIP), &ship);
233     ship.shp_x = sp->sct_x;
234     ship.shp_y = sp->sct_y;
235     ship.shp_own = sp->sct_own;
236     ship.shp_type = mp - mchr;
237     ship.shp_effic = SHIP_MINEFF;
238     if (opt_MOB_ACCESS) {
239         game_tick_to_now(&ship.shp_access);
240         ship.shp_mobil = -(etu_per_update / sect_mob_neg_factor);
241     } else {
242         ship.shp_mobil = 0;
243     }
244     memset(ship.shp_item, 0, sizeof(ship.shp_item));
245     ship.shp_pstage = PLG_HEALTHY;
246     ship.shp_ptime = 0;
247     ship.shp_name[0] = 0;
248     ship.shp_orig_own = sp->sct_own;
249     ship.shp_orig_x = sp->sct_x;
250     ship.shp_orig_y = sp->sct_y;
251     shp_set_tech(&ship, tlev);
252     unit_wipe_orders((struct empobj *)&ship);
253
254     if (sp->sct_pstage == PLG_INFECT)
255         ship.shp_pstage = PLG_EXPOSED;
256     putship(ship.shp_uid, &ship);
257     pr("%s", prship(&ship));
258     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
259     return 1;
260 }
261
262 static int
263 build_land(struct sctstr *sp, int type, int tlev)
264 {
265     struct lchrstr *lp = &lchr[type];
266     short mat[I_MAX+1];
267     int work;
268     struct lndstr land;
269
270     memset(mat, 0, sizeof(mat));
271     mat[I_LCM] = lp->l_lcm;
272     mat[I_HCM] = lp->l_hcm;
273     work = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
274
275     if (sp->sct_type != SCT_HEADQ && !player->god) {
276         pr("Land units must be built in headquarters.\n");
277         return 0;
278     }
279     if (!sector_can_build(sp, mat, work, LAND_MINEFF, lp->l_name))
280         return 0;
281     if (!build_can_afford(lp->l_cost, LAND_MINEFF, lp->l_name))
282         return 0;
283     build_charge(sp, mat, work, lp->l_cost, LAND_MINEFF);
284
285     ef_blank(EF_LAND, pick_unused_unit_uid(EF_LAND), &land);
286     land.lnd_x = sp->sct_x;
287     land.lnd_y = sp->sct_y;
288     land.lnd_own = sp->sct_own;
289     land.lnd_type = lp - lchr;
290     land.lnd_effic = LAND_MINEFF;
291     if (opt_MOB_ACCESS) {
292         game_tick_to_now(&land.lnd_access);
293         land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
294     } else {
295         land.lnd_mobil = 0;
296     }
297     land.lnd_ship = -1;
298     land.lnd_land = -1;
299     land.lnd_harden = 0;
300     memset(land.lnd_item, 0, sizeof(land.lnd_item));
301     land.lnd_pstage = PLG_HEALTHY;
302     land.lnd_ptime = 0;
303     lnd_set_tech(&land, tlev);
304     unit_wipe_orders((struct empobj *)&land);
305
306     if (sp->sct_pstage == PLG_INFECT)
307         land.lnd_pstage = PLG_EXPOSED;
308     putland(land.lnd_uid, &land);
309     pr("%s", prland(&land));
310     pr(" built in sector %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
311     return 1;
312 }
313
314 static int
315 build_nuke(struct sctstr *sp, int type, int tlev)
316 {
317     struct nchrstr *np = &nchr[type];
318     short mat[I_MAX+1];
319     int work;
320     struct nukstr nuke;
321
322     if (sp->sct_type != SCT_NUKE && !player->god) {
323         pr("Nuclear weapons must be built in nuclear plants.\n");
324         return 0;
325     }
326     /*
327      * XXX when nukes turn into units (or whatever), then
328      * make them start at 20%.  Since they don't have efficiency
329      * now, we charge all the work right away.
330      */
331     memset(mat, 0, sizeof(mat));
332     mat[I_LCM] = np->n_lcm;
333     mat[I_HCM] = np->n_hcm;
334     mat[I_OIL] = np->n_oil;
335     mat[I_RAD] = np->n_rad;
336     work = NUK_BLD_WORK(np->n_lcm, np->n_hcm, np->n_oil, np->n_rad);
337
338     if (!sector_can_build(sp, mat, work, 100, np->n_name))
339         return 0;
340     if (!build_can_afford(np->n_cost, 100, np->n_name))
341         return 0;
342     build_charge(sp, mat, work, np->n_cost, 100);
343
344     ef_blank(EF_NUKE, pick_unused_unit_uid(EF_NUKE), &nuke);
345     nuke.nuk_x = sp->sct_x;
346     nuke.nuk_y = sp->sct_y;
347     nuke.nuk_own = sp->sct_own;
348     nuke.nuk_type = np - nchr;
349     nuke.nuk_effic = 100;
350     nuke.nuk_plane = -1;
351     nuke.nuk_tech = tlev;
352     unit_wipe_orders((struct empobj *)&nuke);
353
354     putnuke(nuke.nuk_uid, &nuke);
355     pr("%s created in %s\n", prnuke(&nuke),
356        xyas(sp->sct_x, sp->sct_y, player->cnum));
357     return 1;
358 }
359
360 static int
361 build_plane(struct sctstr *sp, int type, int tlev)
362 {
363     struct plchrstr *pp = &plchr[type];
364     short mat[I_MAX+1];
365     int work;
366     struct plnstr plane;
367
368     memset(mat, 0, sizeof(mat));
369     mat[I_MILIT] = pp->pl_crew;
370     mat[I_LCM] = pp->pl_lcm;
371     mat[I_HCM] = pp->pl_hcm;
372     work = PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm);
373
374     if (sp->sct_type != SCT_AIRPT && !player->god) {
375         pr("Planes must be built in airports.\n");
376         return 0;
377     }
378     if (!sector_can_build(sp, mat, work, PLANE_MINEFF, pp->pl_name))
379         return 0;
380     if (!build_can_afford(pp->pl_cost, PLANE_MINEFF, pp->pl_name))
381         return 0;
382     build_charge(sp, mat, work, pp->pl_cost, PLANE_MINEFF);
383
384     ef_blank(EF_PLANE, pick_unused_unit_uid(EF_PLANE), &plane);
385     plane.pln_x = sp->sct_x;
386     plane.pln_y = sp->sct_y;
387     plane.pln_own = sp->sct_own;
388     plane.pln_type = pp - plchr;
389     plane.pln_effic = PLANE_MINEFF;
390     if (opt_MOB_ACCESS) {
391         game_tick_to_now(&plane.pln_access);
392         plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
393     } else {
394         plane.pln_mobil = 0;
395     }
396     plane.pln_range = UCHAR_MAX; /* will be adjusted by pln_set_tech() */
397     plane.pln_ship = -1;
398     plane.pln_land = -1;
399     plane.pln_harden = 0;
400     plane.pln_flags = 0;
401     pln_set_tech(&plane, tlev);
402     unit_wipe_orders((struct empobj *)&plane);
403
404     putplane(plane.pln_uid, &plane);
405     pr("%s built in sector %s\n", prplane(&plane),
406        xyas(sp->sct_x, sp->sct_y, player->cnum));
407     return 1;
408 }
409
410 static int
411 pick_unused_unit_uid(int type)
412 {
413     struct nstr_item nstr;
414     union empobj_storage unit;
415
416     snxtitem_all(&nstr, type);
417     while (nxtitem(&nstr, &unit)) {
418         if (!unit.gen.own)
419             return nstr.cur;
420     }
421     ef_extend(type, 50);
422     return nstr.cur;
423 }
424
425 static int
426 build_bridge(char what)
427 {
428     struct natstr *natp = getnatp(player->cnum);
429     struct nstr_sect nstr;
430     int (*build_it)(struct sctstr *);
431     int gotsect;
432     struct sctstr sect;
433
434     switch (what) {
435     case 'b':
436         if (natp->nat_level[NAT_TLEV] < buil_bt && !player->god) {
437             pr("Building a span requires a tech of %.0f\n", buil_bt);
438             return RET_FAIL;
439         }
440         build_it = build_bspan;
441         break;
442     case 't':
443         if (!opt_BRIDGETOWERS) {
444             pr("Bridge tower building is disabled.\n");
445             return RET_FAIL;
446         }
447         if (natp->nat_level[NAT_TLEV] < buil_tower_bt && !player->god) {
448             pr("Building a tower requires a tech of %.0f\n",
449                buil_tower_bt);
450             return RET_FAIL;
451         }
452         build_it = build_btower;
453         break;
454     default:
455         CANT_REACH();
456         return RET_FAIL;
457     }
458
459     if (!snxtsct(&nstr, player->argp[2]))
460         return RET_SYN;
461     gotsect = 0;
462     while (nxtsct(&nstr, &sect)) {
463         if (!player->owner)
464             continue;
465         gotsect = 1;
466         if (build_it(&sect))
467             putsect(&sect);
468     }
469     if (!gotsect)
470         pr("No sectors.\n");
471     return RET_OK;
472 }
473
474 static int
475 build_bspan(struct sctstr *sp)
476 {
477     struct sctstr sect;
478     short mat[I_MAX+1];
479     int work;
480     int val;
481     int newx, newy;
482     char *p;
483     char buf[1024];
484
485     if (!opt_EASY_BRIDGES && !player->god) {
486         /* must have a bridge head or tower */
487         if (sp->sct_type != SCT_BTOWER) {
488             if (sp->sct_type != SCT_BHEAD)
489                 return 0;
490             if (sp->sct_newtype != SCT_BHEAD)
491                 return 0;
492         }
493     }
494
495     memset(mat, 0, sizeof(mat));
496     mat[I_HCM] = buil_bh;
497     work = (SCT_BLD_WORK(0, buil_bh) * SCT_MINEFF + 99) / 100;
498
499     if (!sector_can_build(sp, mat, work, 100, dchr[SCT_BSPAN].d_name))
500         return 0;
501     if (!build_can_afford(buil_bc, 100, dchr[SCT_BSPAN].d_name))
502         return 0;
503     if (!player->argp[3]) {
504         pr("Bridge head at %s\n",
505            xyas(sp->sct_x, sp->sct_y, player->cnum));
506         nav_map(sp->sct_x, sp->sct_y, 1);
507     }
508     p = getstarg(player->argp[3], "build span in what direction? ", buf);
509     if (!p || !*p) {
510         return 0;
511     }
512     if (!check_sect_ok(sp))
513         return 0;
514
515     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
516         pr("'%c' is not a valid direction...\n", *p);
517         direrr(NULL, NULL, NULL);
518         return 0;
519     }
520     newx = sp->sct_x + diroff[val][0];
521     newy = sp->sct_y + diroff[val][1];
522     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
523         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
524         return 0;
525     }
526     if (!bridge_support_at(&sect, DIR_STOP)) {
527         if (opt_EASY_BRIDGES) {
528             pr("%s is not next to land or a bridge tower",
529                xyas(newx, newy, player->cnum));
530         } else {
531             /*
532              * Note: because players need a 60% bridge head or tower,
533              * we can get here only for a deity.
534              */
535             pr("%s is not next to a supporting bridge head or tower\n",
536                xyas(newx, newy, player->cnum));
537         }
538         return 0;
539     }
540     build_charge(sp, mat, work, buil_bc, 100);
541
542     sect.sct_type = SCT_BSPAN;
543     sect.sct_newtype = SCT_BSPAN;
544     sect.sct_effic = SCT_MINEFF;
545     sect.sct_road = 0;
546     sect.sct_rail = 0;
547     sect.sct_defense = 0;
548     if (opt_MOB_ACCESS) {
549         game_tick_to_now(&sect.sct_access);
550         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
551     } else {
552         sect.sct_mobil = 0;
553     }
554     sect.sct_mines = 0;
555     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BSPAN].d_mnem, 2);
556     writemap(player->cnum);
557     putsect(&sect);
558     pr("Bridge span built over %s\n",
559        xyas(sect.sct_x, sect.sct_y, player->cnum));
560     return 1;
561 }
562
563 static int
564 build_btower(struct sctstr *sp)
565 {
566     struct sctstr sect;
567     short mat[I_MAX+1];
568     int work;
569     int val;
570     int newx, newy;
571     char *p;
572     char buf[1024];
573     int i;
574     int nx;
575     int ny;
576
577     if (sp->sct_type != SCT_BSPAN && !player->god) {
578         pr("Bridge towers can only be built from bridge spans.\n");
579         return 0;
580     }
581
582     memset(mat, 0, sizeof(mat));
583     mat[I_HCM] = buil_tower_bh;
584     work = (SCT_BLD_WORK(0, buil_tower_bh) * SCT_MINEFF + 99) / 100;
585
586     if (!sector_can_build(sp, mat, work, 100, dchr[SCT_BTOWER].d_name))
587         return 0;
588     if (!build_can_afford(buil_tower_bc, 100, dchr[SCT_BTOWER].d_name))
589         return 0;
590     if (!player->argp[3]) {
591         pr("Building from %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
592         nav_map(sp->sct_x, sp->sct_y, 1);
593     }
594     p = getstarg(player->argp[3], "build tower in what direction? ", buf);
595     if (!p || !*p) {
596         return 0;
597     }
598     if (!check_sect_ok(sp))
599         return 0;
600
601     if ((val = chkdir(*p, DIR_FIRST, DIR_LAST)) < 0) {
602         pr("'%c' is not a valid direction...\n", *p);
603         direrr(NULL, NULL, NULL);
604         return 0;
605     }
606     newx = sp->sct_x + diroff[val][0];
607     newy = sp->sct_y + diroff[val][1];
608     if (getsect(newx, newy, &sect) == 0 || sect.sct_type != SCT_WATER) {
609         pr("%s is not a water sector\n", xyas(newx, newy, player->cnum));
610         return 0;
611     }
612
613     /* Now, check.  You aren't allowed to build bridge towers
614        next to land. */
615     for (i = 1; i <= 6; i++) {
616         struct sctstr s2;
617         nx = sect.sct_x + diroff[i][0];
618         ny = sect.sct_y + diroff[i][1];
619         getsect(nx, ny, &s2);
620         if ((s2.sct_type != SCT_WATER) &&
621             (s2.sct_type != SCT_BTOWER) && (s2.sct_type != SCT_BSPAN)) {
622             pr("%s is next to land, can't build bridge tower there",
623                xyas(newx, newy, player->cnum));
624             return 0;
625         }
626     }
627
628     build_charge(sp, mat, work, buil_tower_bc, 100);
629
630     sect.sct_type = SCT_BTOWER;
631     sect.sct_newtype = SCT_BTOWER;
632     sect.sct_effic = SCT_MINEFF;
633     sect.sct_road = 0;
634     sect.sct_rail = 0;
635     sect.sct_defense = 0;
636     if (opt_MOB_ACCESS) {
637         game_tick_to_now(&sect.sct_access);
638         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
639     } else {
640         sect.sct_mobil = 0;
641     }
642     sect.sct_mines = 0;
643     map_set(player->cnum, sect.sct_x, sect.sct_y, dchr[SCT_BTOWER].d_mnem, 2);
644     writemap(player->cnum);
645     putsect(&sect);
646     pr("Bridge tower built in %s\n",
647        xyas(sect.sct_x, sect.sct_y, player->cnum));
648     return 1;
649 }
650
651 static int
652 sector_can_build(struct sctstr *sp, short mat[], int work,
653                  int effic, char *what)
654 {
655     int i, avail, ret;
656     double needed;
657
658     if (player->god)
659         return 1;               /* Deity builds ex nihilo */
660
661     if (sp->sct_effic < 60 && !player->god) {
662         pr("Sector %s is not 60%% efficient.\n",
663            xyas(sp->sct_x, sp->sct_y, player->cnum));
664         return 0;
665     }
666
667     avail = (work * effic + 99) / 100;
668     if (sp->sct_avail < avail) {
669         pr("Not enough available work in %s to build a %s\n",
670            xyas(sp->sct_x, sp->sct_y, player->cnum), what);
671         pr(" (%d available work required)\n", avail);
672         return 0;
673     }
674
675     ret = 1;
676     for (i = I_NONE + 1; i <= I_MAX; i++) {
677         needed = mat[i] * (effic / 100.0);
678         if (sp->sct_item[i] < needed) {
679             pr("Not enough %s in %s (need %g more)\n",
680                ichr[i].i_name, xyas(sp->sct_x, sp->sct_y, player->cnum),
681                ceil(needed - sp->sct_item[i]));
682             ret = 0;
683         }
684         mat[i] = roundavg(needed);
685     }
686
687     return ret;
688 }
689
690 static void
691 build_charge(struct sctstr *sp,
692              short mat[], int work, double cost, int effic)
693 {
694     int i;
695
696     if (player->god)
697         return;                 /* Deity builds ex nihilo */
698
699     for (i = I_NONE + 1; i <= I_MAX; i++)
700         sp->sct_item[i] -= mat[i];
701     sp->sct_avail -= (work * effic + 99) / 100;
702     player->dolcost += cost * effic / 100.0;
703 }
704
705 static int
706 build_can_afford(double cost, int effic, char *what)
707 {
708     struct natstr *natp = getnatp(player->cnum);
709
710     if (natp->nat_money < player->dolcost + cost * effic / 100.0) {
711         pr("Not enough money left to build a %s\n", what);
712         return 0;
713     }
714     return 1;
715 }