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