]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
(prod): Round the final cost instead of truncating along the way.
[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-2006
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     double 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,
124                                    maxpop));
125         bwork = work / 2;
126
127         if (sect.sct_off)
128             continue;
129         type = sect.sct_type;
130         eff = sect.sct_effic;
131         if (sect.sct_newtype != type) {
132             twork = (eff + 3) / 4;
133             if (twork > bwork) {
134                 twork = bwork;
135             }
136             bwork -= twork;
137             eff -= twork * 4;
138             otype = type;
139             if (eff <= 0) {
140                 type = sect.sct_newtype;
141                 eff = 0;
142             }
143             if (!eff && IS_BIG_CITY(otype) &&
144                 !IS_BIG_CITY(type)) {
145                 natp = getnatp(sect.sct_own);
146                 maxpop = max_population(natp->nat_level[NAT_RLEV],
147                                         type, eff);
148                 work = new_work(&sect,
149                                 total_work(sect.sct_work, etu_per_update,
150                                            civs, sect.sct_item[I_MILIT],
151                                            uws, maxpop));
152                 bwork = MIN(work / 2, bwork);
153             }
154             twork = 100 - eff;
155             if (twork > bwork) {
156                 twork = bwork;
157             }
158             if (dchr[type].d_lcms > 0) {
159                 lcms = sect.sct_item[I_LCM];
160                 lcms /= dchr[type].d_lcms;
161                 if (twork > lcms)
162                     twork = lcms;
163             }
164             if (dchr[type].d_hcms > 0) {
165                 hcms = sect.sct_item[I_HCM];
166                 hcms /= dchr[type].d_hcms;
167                 if (twork > hcms)
168                     twork = hcms;
169             }
170             bwork -= twork;
171             eff += twork;
172         } else if (eff < 100) {
173             twork = 100 - eff;
174             if (twork > bwork) {
175                 twork = bwork;
176             }
177             bwork -= twork;
178             eff += twork;
179         }
180         work = (work + 1) / 2 + bwork;
181         if (eff < 60)
182             continue;
183
184         p_e = eff / 100.0;
185         if (p_e > 1.0)
186             p_e = 1.0;
187
188         if (dchr[type].d_prd == 0 && type != SCT_ENLIST)
189             continue;
190         unit_work = 0;
191         pp = &pchr[dchr[type].d_prd];
192         vtype = pp->p_type;
193         natp = getnatp(sect.sct_own);
194         /*
195          * sect p_e  (inc improvements)
196          */
197         if (type == SCT_ENLIST && sect.sct_own != sect.sct_oldown)
198             continue;
199         if (type == SCT_ENLIST)
200             goto is_enlist;
201         if (pp->p_nrndx != 0) {
202             unit_work++;
203             resource = (unsigned char *)&sect + pp->p_nrndx;
204             p_e = (*resource * p_e) / 100.0;
205         }
206         /*
207          * production effic.
208          */
209         prodeff = prod_eff(pp, natp->nat_level[pp->p_nlndx]);
210         /*
211          * raw material limit
212          */
213         used = 9999;
214         for (i = 0; i < MAXPRCON; ++i) {
215             it = pp->p_ctype[i];
216             if (!pp->p_camt[i])
217                 continue;
218             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
219                 continue;
220             used = MIN(used, sect.sct_item[it] / pp->p_camt[i]);
221             unit_work += pp->p_camt[i];
222         }
223         if (unit_work == 0)
224             unit_work = 1;
225         /*
226          * is production limited by resources or
227          * workforce?
228          */
229         max = (int)(work * p_e / (double)unit_work + 0.5);
230         if (pp->p_nrdep != 0 && vtype != I_NONE) {
231             if (*resource * 100 < pp->p_nrdep * max)
232                 max = *resource * 100 / pp->p_nrdep;
233         }
234         act = MIN(used, max);
235
236         real = MIN(999.0, (double)act * prodeff);
237         maxr = MIN(999.0, (double)max * prodeff);
238
239         if (vtype != I_NONE) {
240             if (real < 0.0)
241                 real = 0.0;
242             /* production backlog? */
243             there = MIN(ITEM_MAX, sect.sct_item[vtype]);
244             real = MIN(real, ITEM_MAX - there);
245         }
246
247         if (prodeff != 0) {
248             take = real / prodeff;
249             mtake = maxr / prodeff;
250         } else
251             mtake = take = 0.0;
252
253         cost = take * pp->p_cost;
254         if (opt_TECH_POP) {
255             if (pp->p_level == NAT_TLEV) {
256                 totpop = count_pop(sect.sct_own);
257                 if (totpop > 50000)
258                     cost *= totpop / 50000.0;
259             }
260         }
261
262         for (i = 0; i < MAXPRCON; ++i) {
263             cmnem[i] = cuse[i] = cmax[i] = 0;
264             if (!pp->p_camt[i])
265                 continue;
266             it = pp->p_ctype[i];
267             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
268                 continue;
269             cmnem[i] = ichr[it].i_mnem;
270             cuse[i] = (int)(take * pp->p_camt[i] + 0.5);
271             cmax[i] = (int)(mtake * pp->p_camt[i] + 0.5);
272         }
273
274       is_enlist:
275
276         if (nsect++ == 0) {
277             pr("PRODUCTION SIMULATION\n");
278             pr("   sect  des eff avail  make p.e. cost   use1 use2 use3  max1 max2 max3   max\n");
279         }
280
281         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
282         pr(" %c", dchr[type].d_mnem);
283         pr(" %3.0f%%", p_e * 100.0);
284         pr(" %5d", work);
285
286         if (vtype != I_NONE) {
287             pr(" %4d%c", (int)(real + 0.5), ichr[vtype].i_mnem);
288         } else if (type != SCT_ENLIST) {
289             switch (pp->p_level) {
290             case NAT_TLEV:
291             case NAT_RLEV:
292                 pr(" %5.2f", real);
293                 break;
294             case NAT_ELEV:
295             case NAT_HLEV:
296                 pr(" %4.0f ", real);
297                 break;
298             default:
299                 CANT_REACH();
300                 pr("  ??? ");
301                 break;
302             }
303         } else {
304             int maxmil;
305             int enlisted;
306             int civs;
307
308             civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL];
309             natp = getnatp(sect.sct_own);
310             maxpop = max_pop(natp->nat_level[NAT_RLEV], &sect);
311             civs = MIN(civs, maxpop);
312             /* This isn't quite right, since research might
313                rise/fall during the update, but it's the best
314                we can really do  */
315             enlisted = 0;
316             maxmil = (civs / 2) - sect.sct_item[I_MILIT];
317             if (maxmil > 0) {
318                 enlisted = (etu_per_update
319                             * (10 + sect.sct_item[I_MILIT])
320                             * 0.05);
321                 if (enlisted > maxmil)
322                     enlisted = maxmil;
323             }
324             if (enlisted < 0)
325                 enlisted = 0;
326             if (natp->nat_priorities[type] == 0) {
327                 maxmil = 0;
328             }
329             pr(" %4d%c 1.00 $%-5d%4dc           %4dc           %5d\n",
330                enlisted, ichr[I_MILIT].i_mnem, enlisted * 3,
331                enlisted, enlisted, maxmil);
332             continue;
333         }
334
335         pr(" %.2f", prodeff);
336         pr(" $%-5.0f", cost);
337         for (i = 0; i < 3; i++) {
338             if (i < MAXPRCON && cmnem[i])
339                 pr("%4d%c", cuse[i], cmnem[i]);
340             else
341                 pr("     ");
342         }
343         pr(" ");
344         for (i = 0; i < 3; i++) {
345             if (i < MAXPRCON && cmnem[i])
346                 pr("%4d%c", cmax[i], cmnem[i]);
347             else
348                 pr("     ");
349         }
350         if (natp->nat_priorities[type] == 0) {
351             max = 0;
352             maxr = 0;
353         }
354         if (vtype != I_NONE || pp->p_level == NAT_ELEV
355             || pp->p_level == NAT_HLEV)
356             pr(" %5d\n", MIN(999, (int)(max * prodeff + 0.5)));
357         else
358             pr(" %5.2f\n", maxr);
359     }
360     player->simulation = 0;
361     if (nsect == 0) {
362         if (player->argp[1])
363             pr("%s: No sector(s)\n", player->argp[1]);
364         else
365             pr("%s: No sector(s)\n", "");
366         return RET_FAIL;
367     } else
368         pr("%d sector%s\n", nsect, splur(nsect));
369     return RET_OK;
370 }