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