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