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