]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
a9607bad8ade11efa776d9b5149e49a4d1fce9a9
[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 <math.h>
38 #include "commands.h"
39 #include "item.h"
40 #include "optlist.h"
41 #include "product.h"
42 #include "update.h"
43
44 static void prprod(struct sctstr *, double, double, char,
45                    double, double, double, char[], int[], int[], int);
46
47 int
48 count_pop(int n)
49 {
50     int i;
51     int pop = 0;
52     struct sctstr *sp;
53
54     for (i = 0; NULL != (sp = getsectid(i)); i++) {
55         if (sp->sct_own != n)
56             continue;
57         if (sp->sct_oldown != n)
58             continue;
59         pop += sp->sct_item[I_CIVIL];
60     }
61     return pop;
62 }
63
64 int
65 prod(void)
66 {
67     struct natstr *natp;
68     struct sctstr sect;
69     struct nstr_sect nstr;
70     struct pchrstr *pp;
71     double p_e;
72     double prodeff;
73     int totpop;
74     double material_consume;    /* actual production */
75     double cost;
76     int i;
77     double max_consume;         /* production w/infinite materials */
78     int nsect;
79     double real, maxr;
80     double take, mtake;
81     int there;
82     int unit_work;              /* sum of component amounts */
83     double mat_limit, worker_limit, res_limit;
84     i_type it;
85     i_type vtype;
86     unsigned char *resource;
87     char cmnem[MAXPRCON];
88     int cuse[MAXPRCON], cmax[MAXPRCON];
89     char mnem;
90
91     if (!snxtsct(&nstr, player->argp[1]))
92         return RET_SYN;
93     player->simulation = 1;
94     prdate();
95     nsect = 0;
96     while (nxtsct(&nstr, &sect)) {
97         if (!player->owner)
98             continue;
99         if (sect.sct_off)
100             continue;
101
102         natp = getnatp(sect.sct_own);
103         do_feed(&sect, natp, etu_per_update, 1);
104         buildeff(&sect);
105         if (sect.sct_effic < 60)
106             continue;
107
108         if (sect.sct_type == SCT_ENLIST) {
109             int maxmil;
110             int enlisted;
111
112             if (sect.sct_own != sect.sct_oldown)
113                 continue;
114             enlisted = 0;
115             maxmil = sect.sct_item[I_CIVIL] / 2 - sect.sct_item[I_MILIT];
116             if (maxmil > 0) {
117                 enlisted = (etu_per_update
118                             * (10 + sect.sct_item[I_MILIT])
119                             * 0.05);
120                 if (enlisted > maxmil)
121                     enlisted = maxmil;
122             }
123             if (enlisted < 0)
124                 enlisted = 0;
125             prprod(&sect, sect.sct_effic / 100.0, 1.0,
126                    ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3,
127                    "c\0\0", &enlisted, &enlisted, nsect++);
128             continue;
129         }
130
131         if (dchr[sect.sct_type].d_prd < 0)
132             continue;
133         pp = &pchr[dchr[sect.sct_type].d_prd];
134         vtype = pp->p_type;
135         if (pp->p_nrndx)
136             resource = (unsigned char *)&sect + pp->p_nrndx;
137         else
138             resource = NULL;
139
140         mat_limit = prod_materials_cost(pp, sect.sct_item, &unit_work);
141
142         /* sector p.e. */
143         p_e = sect.sct_effic / 100.0;
144         if (resource) {
145             unit_work++;
146             p_e *= *resource / 100.0;
147         }
148         if (unit_work == 0)
149             unit_work = 1;
150
151         worker_limit = sect.sct_avail * p_e / (double)unit_work;
152         res_limit = prod_resource_limit(pp, resource);
153
154         max_consume = res_limit;
155         if (max_consume > worker_limit)
156             max_consume = worker_limit;
157         material_consume = MIN(max_consume, mat_limit);
158
159         prodeff = prod_eff(sect.sct_type, natp->nat_level[pp->p_nlndx]);
160         real = material_consume * prodeff;
161         maxr = max_consume * prodeff;
162
163         if (vtype != I_NONE) {
164             real = floor(real);
165             maxr = floor(maxr);
166             real = MIN(999.0, real);
167             maxr = MIN(999.0, maxr);
168             if (CANT_HAPPEN(real < 0.0))
169                 real = 0;
170             if (CANT_HAPPEN(maxr < 0.0))
171                 maxr = 0;
172             /* production backlog? */
173             there = MIN(ITEM_MAX, sect.sct_item[vtype]);
174             real = MIN(real, ITEM_MAX - there);
175         }
176
177         if (prodeff != 0) {
178             take = real / prodeff;
179             mtake = maxr / prodeff;
180         } else
181             mtake = take = 0.0;
182
183         cost = take * pp->p_cost;
184         if (opt_TECH_POP) {
185             if (pp->p_level == NAT_TLEV) {
186                 totpop = count_pop(sect.sct_own);
187                 if (totpop > 50000)
188                     cost *= totpop / 50000.0;
189             }
190         }
191
192         for (i = 0; i < MAXPRCON; ++i) {
193             cmnem[i] = cuse[i] = cmax[i] = 0;
194             if (!pp->p_camt[i])
195                 continue;
196             it = pp->p_ctype[i];
197             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
198                 continue;
199             cmnem[i] = ichr[it].i_mnem;
200             cuse[i] = (int)ceil(take * pp->p_camt[i]);
201             cmax[i] = (int)ceil(mtake * pp->p_camt[i]);
202         }
203
204         if (pp->p_type != I_NONE)
205             mnem = ichr[vtype].i_mnem;
206         else if (pp->p_level == NAT_TLEV || pp->p_level == NAT_RLEV)
207             mnem = '.';
208         else
209             mnem = 0;
210         prprod(&sect, p_e, prodeff, mnem, real, maxr, cost,
211                cmnem, cuse, cmax, nsect++);
212     }
213     player->simulation = 0;
214     if (nsect == 0) {
215         if (player->argp[1])
216             pr("%s: No sector(s)\n", player->argp[1]);
217         else
218             pr("%s: No sector(s)\n", "");
219         return RET_FAIL;
220     } else
221         pr("%d sector%s\n", nsect, splur(nsect));
222     return RET_OK;
223 }
224
225 static void
226 prprod(struct sctstr *sp, double p_e, double prodeff,
227        char mnem, double make, double max, double cost,
228        char cmnem[], int cuse[], int cmax[], int nsect)
229 {
230     int i;
231
232     if (nsect == 0) {
233         pr("PRODUCTION SIMULATION\n");
234         pr("   sect  des eff avail  make p.e. cost   use1 use2 use3  max1 max2 max3   max\n");
235     }
236
237     prxy("%4d,%-4d", sp->sct_x, sp->sct_y);
238     pr(" %c %3.0f%% %5d",
239        dchr[sp->sct_type].d_mnem, p_e * 100.0, sp->sct_avail);
240     if (mnem == '.')
241         pr(" %5.2f", make);
242     else
243         pr(" %4.0f%c", make, mnem ? mnem : ' ');
244     pr(" %.2f $%-5.0f", prodeff, cost);
245     for (i = 0; i < 3; i++) {
246         if (i < MAXPRCON && cmnem[i])
247             pr("%4d%c", cuse[i], cmnem[i]);
248         else
249             pr("     ");
250     }
251     pr(" ");
252     for (i = 0; i < 3; i++) {
253         if (i < MAXPRCON && cmnem[i])
254             pr("%4d%c", cmax[i], cmnem[i]);
255         else
256             pr("     ");
257     }
258     if (mnem == '.')
259         pr(" %5.2f\n", max);
260     else
261         pr(" %5.0f\n", max);
262 }