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