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