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