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