]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
prod() duplicates materials_cost(), clean up
[empserver] / src / lib / commands / prod.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, 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-2013
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                 maxpop = max_population(natp->nat_level[NAT_RLEV],
139                                         type, eff);
140                 work = new_work(&sect,
141                                 total_work(sect.sct_work, etu_per_update,
142                                            civs, sect.sct_item[I_MILIT],
143                                            uws, maxpop));
144                 bwork = MIN(work / 2, bwork);
145             }
146             twork = 100 - eff;
147             if (twork > bwork) {
148                 twork = bwork;
149             }
150             if (dchr[type].d_lcms > 0) {
151                 lcms = sect.sct_item[I_LCM];
152                 lcms /= dchr[type].d_lcms;
153                 if (twork > lcms)
154                     twork = lcms;
155             }
156             if (dchr[type].d_hcms > 0) {
157                 hcms = sect.sct_item[I_HCM];
158                 hcms /= dchr[type].d_hcms;
159                 if (twork > hcms)
160                     twork = hcms;
161             }
162             bwork -= twork;
163             eff += twork;
164         } else if (eff < 100) {
165             twork = 100 - eff;
166             if (twork > bwork) {
167                 twork = bwork;
168             }
169             bwork -= twork;
170             eff += twork;
171         }
172         work = (work + 1) / 2 + bwork;
173         if (eff < 60)
174             continue;
175
176         p_e = eff / 100.0;
177         if (p_e > 1.0)
178             p_e = 1.0;
179
180         if (type == SCT_ENLIST) {
181             int maxmil;
182             int enlisted;
183
184             if (sect.sct_own != sect.sct_oldown)
185                 continue;
186             enlisted = 0;
187             maxmil = MIN(civs, maxpop) / 2 - sect.sct_item[I_MILIT];
188             if (maxmil > 0) {
189                 enlisted = (etu_per_update
190                             * (10 + sect.sct_item[I_MILIT])
191                             * 0.05);
192                 if (enlisted > maxmil)
193                     enlisted = maxmil;
194             }
195             if (enlisted < 0)
196                 enlisted = 0;
197             prprod(sect.sct_x, sect.sct_y, type, p_e, 1.0, work,
198                    ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3,
199                    "c\0\0", &enlisted, &enlisted, nsect++);
200             continue;
201         }
202
203         if (dchr[type].d_prd < 0)
204             continue;
205         pp = &pchr[dchr[type].d_prd];
206         vtype = pp->p_type;
207         if (pp->p_nrndx)
208             resource = (unsigned char *)&sect + pp->p_nrndx;
209         else
210             resource = NULL;
211         used = prod_materials_cost(pp, sect.sct_item, &unit_work);
212         /*
213          * sect p_e  (inc improvements)
214          */
215         if (resource) {
216             unit_work++;
217             p_e *= *resource / 100.0;
218         }
219         if (unit_work == 0)
220             unit_work = 1;
221         /*
222          * production effic.
223          */
224         prodeff = prod_eff(type, natp->nat_level[pp->p_nlndx]);
225         /*
226          * is production limited by resources or
227          * workforce?
228          */
229         max = (int)(work * p_e / (double)unit_work + 0.5);
230         if (resource && pp->p_nrdep != 0) {
231             if (*resource * 100 < pp->p_nrdep * max)
232                 max = *resource * 100 / pp->p_nrdep;
233         }
234         act = MIN(used, max);
235
236         real = (double)act * prodeff;
237         maxr = (double)max * prodeff;
238
239         if (vtype != I_NONE) {
240             real = MIN(999.0, real);
241             maxr = MIN(999.0, maxr);
242             if (real < 0.0)
243                 real = 0.0;
244             /* production backlog? */
245             there = MIN(ITEM_MAX, sect.sct_item[vtype]);
246             real = MIN(real, ITEM_MAX - there);
247         }
248
249         if (prodeff != 0) {
250             take = real / prodeff;
251             mtake = maxr / prodeff;
252         } else
253             mtake = take = 0.0;
254
255         cost = take * pp->p_cost;
256         if (opt_TECH_POP) {
257             if (pp->p_level == NAT_TLEV) {
258                 totpop = count_pop(sect.sct_own);
259                 if (totpop > 50000)
260                     cost *= totpop / 50000.0;
261             }
262         }
263
264         for (i = 0; i < MAXPRCON; ++i) {
265             cmnem[i] = cuse[i] = cmax[i] = 0;
266             if (!pp->p_camt[i])
267                 continue;
268             it = pp->p_ctype[i];
269             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
270                 continue;
271             cmnem[i] = ichr[it].i_mnem;
272             cuse[i] = (int)(take * pp->p_camt[i] + 0.5);
273             cmax[i] = (int)(mtake * pp->p_camt[i] + 0.5);
274         }
275
276         if (pp->p_type != I_NONE)
277             mnem = ichr[vtype].i_mnem;
278         else if (pp->p_level == NAT_TLEV || pp->p_level == NAT_RLEV)
279             mnem = '.';
280         else
281             mnem = 0;
282         prprod(sect.sct_x, sect.sct_y, type, p_e, prodeff, work,
283                mnem, real, maxr, cost,
284                cmnem, cuse, cmax, nsect++);
285     }
286     player->simulation = 0;
287     if (nsect == 0) {
288         if (player->argp[1])
289             pr("%s: No sector(s)\n", player->argp[1]);
290         else
291             pr("%s: No sector(s)\n", "");
292         return RET_FAIL;
293     } else
294         pr("%d sector%s\n", nsect, splur(nsect));
295     return RET_OK;
296 }
297
298 static void
299 prprod(coord x, coord y, int type, double p_e, double prodeff, int work,
300        char mnem, double make, double max, double cost,
301        char cmnem[], int cuse[], int cmax[], int nsect)
302 {
303     int i;
304
305     if (nsect == 0) {
306         pr("PRODUCTION SIMULATION\n");
307         pr("   sect  des eff avail  make p.e. cost   use1 use2 use3  max1 max2 max3   max\n");
308     }
309
310     prxy("%4d,%-4d", x, y);
311     pr(" %c %3.0f%% %5d", dchr[type].d_mnem, p_e * 100.0, work);
312     if (mnem == '.')
313         pr(" %5.2f", make);
314     else
315         pr(" %4.0f%c", make, mnem ? mnem : ' ');
316     pr(" %.2f $%-5.0f", prodeff, cost);
317     for (i = 0; i < 3; i++) {
318         if (i < MAXPRCON && cmnem[i])
319             pr("%4d%c", cuse[i], cmnem[i]);
320         else
321             pr("     ");
322     }
323     pr(" ");
324     for (i = 0; i < 3; i++) {
325         if (i < MAXPRCON && cmnem[i])
326             pr("%4d%c", cmax[i], cmnem[i]);
327         else
328             pr("     ");
329     }
330     if (mnem == '.')
331         pr(" %5.2f\n", max);
332     else
333         pr(" %5.0f\n", max);
334 }