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