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