]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
update production: Make sector production a bit more predictable
[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 p_e;
56     double prodeff;
57     unsigned char *resource;
58     double output;
59     int unit_work, work_used;
60     i_type item;
61     double material_limit, worker_limit, res_limit;
62     double material_consume;
63     int val;
64     double cost;
65
66     if (dchr[sp->sct_type].d_prd < 0)
67         return;
68     product = &pchr[dchr[sp->sct_type].d_prd];
69     item = product->p_type;
70     if (product->p_nrndx)
71         resource = (unsigned char *)sp + product->p_nrndx;
72     else
73         resource = NULL;
74
75     material_limit = prod_materials_cost(product, sp->sct_item,
76                                          &unit_work);
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 = worker_limit;
93     if (material_consume > material_limit)
94         material_consume = material_limit;
95     if (CANT_HAPPEN(material_consume < 0.0))
96         material_consume = 0.0;
97     if (material_consume == 0.0)
98         return;
99
100     prodeff = prod_eff(sp->sct_type, np->nat_level[product->p_nlndx]);
101     if (prodeff <= 0.0) {
102         if (!player->simulation)
103             wu(0, sp->sct_own,
104                "%s level too low to produce in %s (need %d)\n",
105                levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
106         return;
107     }
108     /*
109      * Adjust produced amount by commodity production ratio
110      */
111     output = material_consume * prodeff;
112     if (item == I_NONE) {
113         if (!player->simulation) {
114             levels[sp->sct_own][product->p_level] += output;
115             wu(0, sp->sct_own, "%s (%.2f) produced in %s\n",
116                product->p_name, output, ownxy(sp));
117         }
118     } else {
119         output = floor(output);
120         if (output > 999.0)
121             output = 999.0;
122         if (sp->sct_item[item] + output > ITEM_MAX) {
123             output = ITEM_MAX - sp->sct_item[item];
124             if (sp->sct_own && !player->simulation)
125                 wu(0, sp->sct_own,
126                    "%s production backlog in %s\n",
127                    product->p_name, ownxy(sp));
128         }
129         material_consume = output / prodeff;
130         sp->sct_item[item] += output;
131     }
132
133     /*
134      * Reset produced amount by commodity production ratio
135      */
136     materials_charge(product, sp->sct_item, material_consume);
137     if (resource && product->p_nrdep != 0) {
138         /*
139          * lower natural resource in sector depending on
140          * amount produced
141          */
142         val = *resource - roundavg(product->p_nrdep *
143                                    material_consume / 100.0);
144         if (val < 0)
145             val = 0;
146         *resource = val;
147     }
148
149     cost = product->p_cost * material_consume;
150     if (opt_TECH_POP) {
151         if (product->p_level == NAT_TLEV) {
152             if (tpops[sp->sct_own] > 50000)
153                 cost *= tpops[sp->sct_own] / 50000.0;
154         }
155     }
156
157     budget->prod[sp->sct_type].count += ldround(output, 1);
158     budget->prod[sp->sct_type].money -= cost;
159     budget->money -= cost;
160
161     if (CANT_HAPPEN(p_e <= 0.0))
162         return;
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 }
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 double
174 prod_materials_cost(struct pchrstr *pp, short vec[], int *costp)
175 {
176     double count, n;
177     int cost, i;
178
179     count = ITEM_MAX;
180     cost = 0;
181     for (i = 0; i < MAXPRCON; ++i) {
182         if (!pp->p_camt[i])
183             continue;
184         if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
185             continue;
186         n = (double)vec[pp->p_ctype[i]] / pp->p_camt[i];
187         if (n < count)
188             count = n;
189         cost += pp->p_camt[i];
190     }
191     *costp = cost;
192     return count;
193 }
194
195 static void
196 materials_charge(struct pchrstr *pp, short *vec, double count)
197 {
198     int i;
199     i_type item;
200     double n;
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.0))
210             n = 0.0;
211         vec[item] = roundavg(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 double
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.0 / 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 }