]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
Import of Empire 4.2.12
[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 + (double)items[I_MILIT] * 2.0 / 5.0);
143         work = (double)etu_per_update * (double)wforce / 100.0;
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 = min(civs, max_pop(natp->nat_level[NAT_RLEV],0));
168                         uws = min(uws, max_pop(natp->nat_level[NAT_RLEV], 0));
169                     } else {
170                         civs = min(9999,civs);
171                         uws = min(9999,uws);
172                     }
173                     wforce = (int)((civs * sect.sct_work) / 100.0 + uws + items[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             getvec(VT_ITEM, vec, (s_char *)&sect, EF_SECTOR);
183             if (dchr[type].d_lcms>0){
184                 lcms = vec[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 = vec[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/2 + bwork;
206         if (eff < 60 || (type != SCT_ENLIST && eff < 61))
207             continue;
208         
209         effic = eff/100.0;
210         if (effic > 1.0)
211             effic = 1.0;
212         
213         if (dchr[type].d_prd == 0 && type != SCT_ENLIST)
214             continue;
215         totcomp = 0;
216         pp = &pchr[dchr[type].d_prd];
217         vtype = pp->p_type;
218         natp = getnatp(sect.sct_own);
219         /*
220          * sect effic  (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             totcomp++;
228             resource = ((s_char *) &sect) + pp->p_nrndx;
229             effic = (*resource * effic) / 100.0;
230             if (pp->p_nrdep > 0) {
231                 maxtake = (*resource * 100.0) / pp->p_nrdep;
232                 if (effic > maxtake)
233                     effic = maxtake;
234             }
235         }
236         /*
237          * production effic.
238          */
239         if (pp->p_nlndx >= 0) {
240             prodeff = natp->nat_level[pp->p_nlndx] - pp->p_nlmin;
241             if (prodeff < 0.0) {
242                 prodeff = 0.0;
243             }
244             prodeff = prodeff / (prodeff + pp->p_nllag);
245         } else {
246             prodeff = 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                 totcomp++;
255             else {
256                 used = min(used, (int)(getvar((int)*comp, (s_char *)&sect,
257                                               EF_SECTOR) / *amount));
258                 totcomp += *amount;
259             }
260             ++comp;
261             ++amount;
262         }
263         if (totcomp == 0)
264             continue;
265         /*
266          * is production limited by resources or
267          * workforce?
268          */
269         max = (int) (work * effic / (double) totcomp) + 0.5;
270         act = min(used, max);
271         /*
272          * some things are easier to make..  food,
273          * pet, etc.
274          */
275         act = (int) (((double)pp->p_effic * 0.01 * (double)act) + 0.5);
276         max = (int) (((double)pp->p_effic * 0.01 * (double)max) + 0.5);
277         
278         real = (double)act * (double)prodeff;
279         maxr = (double)max * (double)prodeff;
280         
281         if (vtype != 0) {
282             if (real < 0.0)
283                 real = 0.0;
284             /* production backlog? */
285             if ((there = getvar((int)vtype, (s_char *)&sect, EF_SECTOR)) >= 9999) {
286                 there = 9999;
287             }
288             act = min(act, (9999 - there));
289             max = min(max, (9999 - there));
290         }
291         
292         if (prodeff != 0) {
293             take = real / prodeff;
294             mtake = maxr / prodeff;
295         } else
296             mtake = take = 0.0;
297         
298         if (take > 999.0)
299             take = 999.0;
300         if (mtake > 999.0)
301             mtake = 999.0;
302         
303         take = (double)take / ((double)pp->p_effic * 0.01);
304         mtake = (double)mtake / ((double)pp->p_effic * 0.01);
305         
306         cost = (int)(take * (double)pp->p_cost);
307         if (opt_TECH_POP) {
308             if (pp->p_level == NAT_TLEV) {
309                 totpop = count_pop(sect.sct_own);
310                 if (totpop > 50000)
311                     cost = (int)((double)cost * (double)totpop / 50000.0);
312             }
313         }
314         
315         comp = pp->p_vtype;
316         amount = pp->p_vamt;
317         i = 0;
318         while (comp < endcomp) {
319             it = unitem((int) *comp);
320             if (it > 0 && it <= I_MAX && ichr[it].i_name != 0)
321                 c = ichr[it].i_name[0];
322             else
323                 c = ' ';
324             (void) sprintf(use[i], " %3d%c", (int)((take * (double)(*amount)) + 0.5), c);
325             (void) sprintf(maxc[i], " %3d%c", (int)((mtake * (double)(*amount)) + 0.5), c);
326             ++comp;
327             ++amount;
328             ++i;
329         }
330         while (i < 3) {
331             strcpy(use[i], "     ");
332             strcpy(maxc[i], "     ");
333             ++i;
334         }
335         
336     is_enlist:
337         
338         if (nsect++ == 0) {
339             pr("PRODUCTION SIMULATION\n");
340             pr("   sect  des eff wkfc will make- p.e. cost  use1 use2 use3  max1 max2 max3  max\n");
341         }
342         
343         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
344         pr(" %c", dchr[type].d_mnem);
345         pr(" %3.0f%%", effic * 100.0);
346         
347         pr(" %4d", wforce);
348         if (vtype != 0) {
349             pr(" %4d", (int)(real + 0.5));
350         } else if (type != SCT_ENLIST) {
351             switch (pp->p_level) {
352             case NAT_TLEV:
353             case NAT_RLEV:
354                 pr(" %1.2f", real);
355                 break;
356             case NAT_ELEV:
357             case NAT_HLEV:
358                 pr(" %4.0f", real);
359                 break;
360             default:
361                 pr("ERROR");
362                 break;
363             }
364         } else {
365             int maxmil;
366             int enlisted;
367             int civs;
368             
369             civs = min(999, (int) ((obrate * (double) etu_per_update + 1.0)
370                                    * (double) items[I_CIVIL]));
371             natp = getnatp(sect.sct_own);
372             maxpop = max_pop((float)natp->nat_level[NAT_RLEV], &sect);
373             civs = min(civs, maxpop);
374             /* This isn't quite right, since research might
375                rise/fall during the update, but it's the best
376                we can really do  */
377             enlisted = 0;
378             maxmil = (civs / 2) - items[I_MILIT];
379             if (maxmil > 0) {
380                 enlisted = (etu_per_update * (10 + items[I_MILIT]) * 0.05);
381                 if (enlisted > maxmil)
382                     enlisted = maxmil;
383             }
384             if (enlisted < 0)
385                 enlisted = 0;
386             if (natp->nat_priorities[type] == 0){
387                 maxmil = 0;
388             }
389             pr(" %4d mil   1.00 $%-5d%3dc",
390                enlisted, enlisted * 3, enlisted);
391             pr("            %3dc           %4d\n",
392                enlisted, maxmil, maxmil);
393             continue;
394         }
395         
396         pr(" %-5.5s", pp->p_sname);
397         prodeff = prodeff * (double)pp->p_effic * 0.01;
398         pr(" %.2f", prodeff);
399         pr(" $%-4d", cost);
400         for (i = 0; i < 3; i++) {
401             pr(use[i]);
402         }
403         pr(" ");
404         for (i = 0; i < 3; i++) {
405             pr(maxc[i]);
406         }
407         if (natp->nat_priorities[type] == 0){
408             max = 0;
409             maxr = 0;
410         }
411         if (vtype != 0 || pp->p_level == NAT_ELEV
412             || pp->p_level == NAT_HLEV)
413             pr(" %4d\n", min(999,(int)(max * prodeff + 0.05)));
414         else
415             pr(" %1.2f\n", maxr);
416     }
417     player->simulation = 0;
418     if (nsect == 0) {
419         if (player->argp[1])
420             pr("%s: No sector(s)\n", player->argp[1]);
421         else
422             pr("%s: No sector(s)\n", "");
423         return RET_FAIL;
424     } else
425         pr("%d sector%s\n", nsect, splur(nsect));
426     return RET_OK;
427 }
428