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