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