]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
update: Give a few helpers internal linkage
[empserver] / src / lib / update / produce.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, 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  *  produce.c: Produce goodies
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2021
31  */
32
33 #include <config.h>
34
35 #include <math.h>
36 #include "chance.h"
37 #include "nat.h"
38 #include "optlist.h"
39 #include "player.h"
40 #include "product.h"
41 #include "prototypes.h"
42 #include "update.h"
43
44 static double prod_materials_cost(struct pchrstr *, short[], int *);
45 static void materials_charge(struct pchrstr *, short *, double);
46 static double prod_resource_limit(struct pchrstr *, unsigned char *);
47
48 static char *levelnames[] = {
49     "Technology", "Research", "Education", "Happiness"
50 };
51
52 void
53 produce(struct natstr *np, struct sctstr *sp)
54 {
55     struct budget *budget = &nat_budget[sp->sct_own];
56     struct pchrstr *product;
57     double prodeff;
58     double output;
59     double cost;
60
61     if (dchr[sp->sct_type].d_prd < 0)
62         return;
63     product = &pchr[dchr[sp->sct_type].d_prd];
64
65     prodeff = prod_eff(sp->sct_type, np->nat_level[product->p_nlndx]);
66     output = prod_output(sp, prodeff);
67     if (!output)
68         return;
69
70     cost = product->p_cost * output / prodeff;
71     if (opt_TECH_POP) {
72         if (product->p_level == NAT_TLEV) {
73             if (budget->oldowned_civs > 50000)
74                 cost *= budget->oldowned_civs / 50000.0;
75         }
76     }
77
78     if (product->p_level >= 0)
79         budget->level[product->p_level] += output;
80     budget->prod[sp->sct_type].count += ldround(output, 1);
81     budget->prod[sp->sct_type].money -= cost;
82     budget->money -= cost;
83 }
84
85 double
86 prod_output(struct sctstr *sp, double prodeff)
87 {
88     struct pchrstr *product = &pchr[dchr[sp->sct_type].d_prd];
89     i_type item = product->p_type;
90     unsigned char *resource;
91     double p_e;
92     double material_limit, worker_limit, res_limit;
93     double material_consume, output;
94     int unit_work, work_used;
95     int val;
96
97     if (product->p_nrndx)
98         resource = (unsigned char *)sp + product->p_nrndx;
99     else
100         resource = NULL;
101
102     material_limit = prod_materials_cost(product, sp->sct_item,
103                                          &unit_work);
104
105     /* sector p.e. */
106     p_e = sp->sct_effic / 100.0;
107     if (resource) {
108         unit_work++;
109         p_e *= *resource / 100.0;
110     }
111     if (unit_work == 0)
112         unit_work = 1;
113
114     worker_limit = sp->sct_avail * p_e / unit_work;
115     res_limit = prod_resource_limit(product, resource);
116
117     material_consume = res_limit;
118     if (material_consume > worker_limit)
119         material_consume = worker_limit;
120     if (material_consume > material_limit)
121         material_consume = material_limit;
122     if (CANT_HAPPEN(material_consume < 0.0))
123         material_consume = 0.0;
124     if (material_consume == 0.0)
125         return 0.0;
126
127     if (prodeff <= 0.0) {
128         if (!player->simulation)
129             wu(0, sp->sct_own,
130                "%s level too low to produce in %s (need %d)\n",
131                levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
132         return 0.0;
133     }
134
135     /*
136      * Adjust produced amount by commodity production ratio
137      */
138     output = material_consume * prodeff;
139     if (item == I_NONE) {
140         if (!player->simulation) {
141             wu(0, sp->sct_own, "%s (%.2f) produced in %s\n",
142                product->p_name, output, ownxy(sp));
143         }
144     } else {
145         output = floor(output);
146         if (output > 999.0)
147             output = 999.0;
148         if (sp->sct_item[item] + output > ITEM_MAX) {
149             output = ITEM_MAX - sp->sct_item[item];
150             if (sp->sct_own && !player->simulation)
151                 wu(0, sp->sct_own,
152                    "%s production backlog in %s\n",
153                    product->p_name, ownxy(sp));
154         }
155         material_consume = output / prodeff;
156         sp->sct_item[item] += output;
157     }
158
159     /*
160      * Reset produced amount by commodity production ratio
161      */
162     materials_charge(product, sp->sct_item, material_consume);
163     if (resource && product->p_nrdep != 0) {
164         /*
165          * lower natural resource in sector depending on
166          * amount produced
167          */
168         val = *resource - roundavg(product->p_nrdep *
169                                    material_consume / 100.0);
170         if (val < 0)
171             val = 0;
172         *resource = val;
173     }
174
175     if (CANT_HAPPEN(p_e <= 0.0))
176         return 0.0;
177     work_used = roundavg(unit_work * material_consume / p_e);
178     if (CANT_HAPPEN(work_used > sp->sct_avail))
179         work_used = sp->sct_avail;
180     sp->sct_avail -= work_used;
181
182     return output;
183 }
184
185 /*
186  * Return how much of product @pp can be made from materials @vec[].
187  * Store amount of work per unit in *@costp.
188  */
189 static double
190 prod_materials_cost(struct pchrstr *pp, short vec[], int *costp)
191 {
192     double count, n;
193     int cost, i;
194
195     count = ITEM_MAX;
196     cost = 0;
197     for (i = 0; i < MAXPRCON; ++i) {
198         if (!pp->p_camt[i])
199             continue;
200         if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
201             continue;
202         n = (double)vec[pp->p_ctype[i]] / pp->p_camt[i];
203         if (n < count)
204             count = n;
205         cost += pp->p_camt[i];
206     }
207     *costp = cost;
208     return count;
209 }
210
211 static void
212 materials_charge(struct pchrstr *pp, short *vec, double count)
213 {
214     int i;
215     i_type item;
216     double n;
217
218     for (i = 0; i < MAXPRCON; ++i) {
219         item = pp->p_ctype[i];
220         if (!pp->p_camt[i])
221             continue;
222         if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
223             continue;
224         n = vec[item] - pp->p_camt[i] * count;
225         if (CANT_HAPPEN(n < 0.0))
226             n = 0.0;
227         vec[item] = roundavg(n);
228     }
229 }
230
231 /*
232  * Return how much of product @pp can be made from its resource.
233  * If @pp depletes a resource, @resource must point to its value.
234  */
235 static double
236 prod_resource_limit(struct pchrstr *pp, unsigned char *resource)
237 {
238     if (CANT_HAPPEN(pp->p_nrndx && !resource))
239         return 0;
240     if (resource && pp->p_nrdep != 0)
241         return *resource * 100.0 / pp->p_nrdep;
242     return ITEM_MAX;
243 }
244
245 /*
246  * Return p.e. for sector type @type.
247  * Zero means level is too low for production.
248  * @level is the level affecting production.
249  */
250 double
251 prod_eff(int type, float level)
252 {
253     double level_p_e;
254     struct dchrstr *dp = &dchr[type];
255     struct pchrstr *pp = &pchr[dp->d_prd];
256
257     if (CANT_HAPPEN(dp->d_prd < 0))
258         return 0.0;
259
260     if (pp->p_nlndx < 0)
261         level_p_e = 1.0;
262     else {
263         double delta = (double)level - (double)pp->p_nlmin;
264
265         if (delta < 0.0)
266             return 0.0;
267         if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
268             return 0.0;
269         level_p_e = delta / (delta + pp->p_nllag);
270     }
271
272     return level_p_e * dp->d_peffic * 0.01;
273 }