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