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