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