]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
Declare all configuration variables in optlist.h. Include that
[empserver] / src / lib / commands / prod.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  prod.c: Calculate production levels
29  * 
30  *  Known contributors to this file:
31  *     David Muir Sharnoff, 1987
32  *     Steve McClure, 1997-2000
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "var.h"
38 #include "xy.h"
39 #include "nsc.h"
40 #include "sect.h"
41 #include "product.h"
42 #include "nat.h"
43 #include "item.h"
44 #include "file.h"
45 #include "optlist.h"
46 #include "commands.h"
47
48 int
49 count_pop(register int n)
50 {
51     register int i;
52     register int pop = 0;
53     struct sctstr *sp;
54     int vec[I_MAX + 1];
55
56     for (i = 0; NULL != (sp = getsectid(i)); i++) {
57         if (sp->sct_own != n)
58             continue;
59         if (sp->sct_oldown != n)
60             continue;
61         if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
62             continue;
63         pop += vec[I_CIVIL];
64     }
65     return pop;
66 }
67
68 int
69 prod(void)
70 {
71     struct natstr *natp;
72     struct sctstr sect;
73     struct nstr_sect nstr;
74     struct pchrstr *pp;
75     double effic;
76     double maxr;                /* floating version of max */
77     double prodeff;
78     double real;                /* floating pt version of act */
79     double work;
80     int totpop;
81     int act;                    /* actual production */
82     int cost;
83     int i;
84     int max;                    /* production w/infinate materials */
85     double maxtake;
86     int nsect;
87     double take;
88     double mtake;
89     int there;
90     int totcomp;                /* sum of component amounts */
91     int used;                   /* production w/infinite workforce */
92     int wforce;
93     int it;
94     u_short *amount;            /* amount for component pointer */
95     u_char *comp;               /* component pointer */
96     u_char *endcomp;
97     u_char vtype;
98     s_char *resource;
99     int c;
100     s_char maxc[3][10];
101     s_char use[3][10];
102     int items[I_MAX + 1];
103     int vec[I_MAX + 1], lcms, hcms;
104     int civs = 0;
105     int uws = 0;
106     int bwork;
107     int twork;
108     int type;
109     int eff;
110     int maxpop;
111     u_char otype;
112
113     if (!snxtsct(&nstr, player->argp[1]))
114         return RET_SYN;
115     player->simulation = 1;
116     prdate();
117     nsect = 0;
118     while (nxtsct(&nstr, &sect)) {
119         if (!player->owner)
120             continue;
121         getvec(VT_ITEM, items, (s_char *)&sect, EF_SECTOR);
122
123         civs = min(9999, (int)((obrate * (double)etu_per_update + 1.0)
124                                * (double)items[I_CIVIL]));
125         uws = min(9999, (int)((uwbrate * (double)etu_per_update + 1.0)
126                               * (double)items[I_UW]));
127         if (opt_RES_POP) {
128             natp = getnatp(sect.sct_own);
129             maxpop = max_pop((float)natp->nat_level[NAT_RLEV], &sect);
130             civs = min(civs, maxpop);
131             uws = min(uws, maxpop);
132         } else {                /* now RES_POP */
133             civs = min(999, civs);
134             uws = min(999, uws);
135         }                       /* end RES_POP */
136
137         /* This isn't quite right, since research might rise/fall */
138         /* during the update, but it's the best we can really do  */
139         wforce = (int)(((double)civs * (double)sect.sct_work)
140                        / 100.0 + (double)uws +
141                        (double)items[I_MILIT] * 2.0 / 5.0);
142         work = (double)etu_per_update *(double)wforce / 100.0;
143         if (opt_ROLLOVER_AVAIL) {
144             if (sect.sct_type == sect.sct_newtype) {
145                 work += sect.sct_avail;
146             }
147             if (work > 999) work = 999;
148         }
149         bwork = (int)((double)work / 2.0);
150
151         if (sect.sct_off)
152             continue;
153         type = sect.sct_type;
154         eff = sect.sct_effic;
155         if (sect.sct_newtype != type) {
156             twork = (eff + 3) / 4;
157             if (twork > bwork) {
158                 twork = bwork;
159             }
160             bwork -= twork;
161             eff -= twork * 4;
162             otype = type;
163             if (eff <= 0) {
164                 type = sect.sct_newtype;
165                 eff = 0;
166             }
167             if (opt_BIG_CITY) {
168                 if (!eff && dchr[otype].d_pkg == UPKG &&
169                     dchr[type].d_pkg != UPKG) {
170                     if (opt_RES_POP) {
171                         natp = getnatp(sect.sct_own);
172                         civs =
173                             min(civs,
174                                 max_pop(natp->nat_level[NAT_RLEV], 0));
175                         uws =
176                             min(uws,
177                                 max_pop(natp->nat_level[NAT_RLEV], 0));
178                     } else {
179                         civs = min(9999, civs);
180                         uws = min(9999, uws);
181                     }
182                     wforce =
183                         (int)((civs * sect.sct_work) / 100.0 + uws +
184                               items[I_MILIT] * 2 / 5.0);
185                     work = etu_per_update * wforce / 100.0;
186                     bwork = min((int)(work / 2), bwork);
187                 }
188             }
189             twork = 100 - eff;
190             if (twork > bwork) {
191                 twork = bwork;
192             }
193             getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
194             if (dchr[type].d_lcms > 0) {
195                 lcms = vec[I_LCM];
196                 lcms /= dchr[type].d_lcms;
197                 if (twork > lcms)
198                     twork = lcms;
199             }
200             if (dchr[type].d_hcms > 0) {
201                 hcms = vec[I_HCM];
202                 hcms /= dchr[type].d_hcms;
203                 if (twork > hcms)
204                     twork = hcms;
205             }
206             bwork -= twork;
207             eff += twork;
208         } else if (eff < 100) {
209             twork = 100 - eff;
210             if (twork > bwork) {
211                 twork = bwork;
212             }
213             bwork -= twork;
214             eff += twork;
215         }
216         work = work / 2 + bwork;
217         if (eff < 60 || (type != SCT_ENLIST && eff < 61))
218             continue;
219
220         effic = eff / 100.0;
221         if (effic > 1.0)
222             effic = 1.0;
223
224         if (dchr[type].d_prd == 0 && type != SCT_ENLIST)
225             continue;
226         totcomp = 0;
227         pp = &pchr[dchr[type].d_prd];
228         vtype = pp->p_type;
229         natp = getnatp(sect.sct_own);
230         /*
231          * sect effic  (inc improvements)
232          */
233         if (type == SCT_ENLIST && sect.sct_own != sect.sct_oldown)
234             continue;
235         if (type == SCT_ENLIST)
236             goto is_enlist;
237         if (pp->p_nrndx != 0) {
238             totcomp++;
239             resource = ((s_char *)&sect) + pp->p_nrndx;
240             effic = (*resource * effic) / 100.0;
241             if (pp->p_nrdep > 0) {
242                 maxtake = (*resource * 100.0) / pp->p_nrdep;
243                 if (effic > maxtake)
244                     effic = maxtake;
245             }
246         }
247         /*
248          * production effic.
249          */
250         if (pp->p_nlndx >= 0) {
251             prodeff = natp->nat_level[pp->p_nlndx] - pp->p_nlmin;
252             if (prodeff < 0.0) {
253                 prodeff = 0.0;
254             }
255             prodeff = prodeff / (prodeff + pp->p_nllag);
256         } else {
257             prodeff = 1.0;
258         }
259         used = 999;
260         comp = pp->p_vtype;
261         endcomp = pp->p_vtype + pp->p_nv;
262         amount = pp->p_vamt;
263         while (comp < endcomp) {
264             if (*amount == 0)
265                 totcomp++;
266             else {
267                 used = min(used, (int)(getvar((int)*comp, (s_char *)&sect,
268                                               EF_SECTOR) / *amount));
269                 totcomp += *amount;
270             }
271             ++comp;
272             ++amount;
273         }
274         if (totcomp == 0)
275             continue;
276         /*
277          * is production limited by resources or
278          * workforce?
279          */
280         max = (int)(work * effic / (double)totcomp) + 0.5;
281         act = min(used, max);
282         /*
283          * some things are easier to make..  food,
284          * pet, etc.
285          */
286         act = (int)(((double)pp->p_effic * 0.01 * (double)act) + 0.5);
287         max = (int)(((double)pp->p_effic * 0.01 * (double)max) + 0.5);
288
289         real = (double)act *(double)prodeff;
290         maxr = (double)max *(double)prodeff;
291
292         if (vtype != 0) {
293             if (real < 0.0)
294                 real = 0.0;
295             /* production backlog? */
296             if ((there =
297                  getvar((int)vtype, (s_char *)&sect, EF_SECTOR)) >= 9999) {
298                 there = 9999;
299             }
300             act = min(act, (9999 - there));
301             max = min(max, (9999 - there));
302         }
303
304         if (prodeff != 0) {
305             take = real / prodeff;
306             mtake = maxr / prodeff;
307         } else
308             mtake = take = 0.0;
309
310         if (take > 999.0)
311             take = 999.0;
312         if (mtake > 999.0)
313             mtake = 999.0;
314
315         take = (double)take / ((double)pp->p_effic * 0.01);
316         mtake = (double)mtake / ((double)pp->p_effic * 0.01);
317
318         cost = (int)(take * (double)pp->p_cost);
319         if (opt_TECH_POP) {
320             if (pp->p_level == NAT_TLEV) {
321                 totpop = count_pop(sect.sct_own);
322                 if (totpop > 50000)
323                     cost = (int)((double)cost * (double)totpop / 50000.0);
324             }
325         }
326
327         comp = pp->p_vtype;
328         amount = pp->p_vamt;
329         i = 0;
330         while (comp < endcomp) {
331             it = unitem((int)*comp);
332             if (it > 0 && it <= I_MAX && ichr[it].i_name != 0)
333                 c = ichr[it].i_name[0];
334             else
335                 c = ' ';
336             (void)sprintf(use[i], " %3d%c",
337                           (int)((take * (double)(*amount)) + 0.5), c);
338             (void)sprintf(maxc[i], " %3d%c",
339                           (int)((mtake * (double)(*amount)) + 0.5), c);
340             ++comp;
341             ++amount;
342             ++i;
343         }
344         while (i < 3) {
345             strcpy(use[i], "     ");
346             strcpy(maxc[i], "     ");
347             ++i;
348         }
349
350       is_enlist:
351
352         if (nsect++ == 0) {
353             pr("PRODUCTION SIMULATION\n");
354             pr("   sect  des eff wkfc will make- p.e. cost  use1 use2 use3  max1 max2 max3  max\n");
355         }
356
357         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
358         pr(" %c", dchr[type].d_mnem);
359         pr(" %3.0f%%", effic * 100.0);
360
361         pr(" %4d", wforce);
362         if (vtype != 0) {
363             pr(" %4d", (int)(real + 0.5));
364         } else if (type != SCT_ENLIST) {
365             switch (pp->p_level) {
366             case NAT_TLEV:
367             case NAT_RLEV:
368                 pr(" %1.2f", real);
369                 break;
370             case NAT_ELEV:
371             case NAT_HLEV:
372                 pr(" %4.0f", real);
373                 break;
374             default:
375                 pr("ERROR");
376                 break;
377             }
378         } else {
379             int maxmil;
380             int enlisted;
381             int civs;
382
383             civs = min(999, (int)((obrate * (double)etu_per_update + 1.0)
384                                   * (double)items[I_CIVIL]));
385             natp = getnatp(sect.sct_own);
386             maxpop = max_pop((float)natp->nat_level[NAT_RLEV], &sect);
387             civs = min(civs, maxpop);
388             /* This isn't quite right, since research might
389                rise/fall during the update, but it's the best
390                we can really do  */
391             enlisted = 0;
392             maxmil = (civs / 2) - items[I_MILIT];
393             if (maxmil > 0) {
394                 enlisted = (etu_per_update * (10 + items[I_MILIT]) * 0.05);
395                 if (enlisted > maxmil)
396                     enlisted = maxmil;
397             }
398             if (enlisted < 0)
399                 enlisted = 0;
400             if (natp->nat_priorities[type] == 0) {
401                 maxmil = 0;
402             }
403             pr(" %4d mil   1.00 $%-5d%3dc",
404                enlisted, enlisted * 3, enlisted);
405             pr("            %3dc           %4d\n",
406                enlisted, maxmil, maxmil);
407             continue;
408         }
409
410         pr(" %-5.5s", pp->p_sname);
411         prodeff = prodeff * (double)pp->p_effic * 0.01;
412         pr(" %.2f", prodeff);
413         pr(" $%-4d", cost);
414         for (i = 0; i < 3; i++) {
415             pr(use[i]);
416         }
417         pr(" ");
418         for (i = 0; i < 3; i++) {
419             pr(maxc[i]);
420         }
421         if (natp->nat_priorities[type] == 0) {
422             max = 0;
423             maxr = 0;
424         }
425         if (vtype != 0 || pp->p_level == NAT_ELEV
426             || pp->p_level == NAT_HLEV)
427             pr(" %4d\n", min(999, (int)(max * prodeff + 0.05)));
428         else
429             pr(" %1.2f\n", maxr);
430     }
431     player->simulation = 0;
432     if (nsect == 0) {
433         if (player->argp[1])
434             pr("%s: No sector(s)\n", player->argp[1]);
435         else
436             pr("%s: No sector(s)\n", "");
437         return RET_FAIL;
438     } else
439         pr("%d sector%s\n", nsect, splur(nsect));
440     return RET_OK;
441 }