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