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