]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
Fix and clean up some comments
[empserver] / src / lib / update / produce.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, 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-2013
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, short *vec, 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, vec, &unit_work);
76     if (material_limit <= 0)
77         return 0;
78
79     /* sector p.e. */
80     p_e = neweff / 100.0;
81     if (resource) {
82         unit_work++;
83         p_e *= *resource / 100.0;
84     }
85     if (unit_work == 0)
86         unit_work = 1;
87
88     worker_limit = work * p_e / unit_work;
89     res_limit = prod_resource_limit(product, resource);
90
91     material_consume = res_limit;
92     if (material_consume > worker_limit)
93         material_consume = (int)worker_limit;
94     if (material_consume > material_limit)
95         material_consume = material_limit;
96     if (material_consume == 0)
97         return 0;
98
99     prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]);
100     if (prodeff <= 0.0 && !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 (vec[item] + actual > ITEM_MAX) {
126             actual = ITEM_MAX - vec[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         vec[item] += actual;
136     }
137     /*
138      * Reset produced amount by commodity production ratio
139      */
140     if (!player->simulation) {
141         materials_charge(product, vec, material_consume);
142         if (resource && product->p_nrdep != 0) {
143             /*
144              * lower natural resource in sector depending on
145              * amount produced
146              */
147             val = *resource - roundavg(product->p_nrdep *
148                                        material_consume / 100.0);
149             if (val < 0)
150                 val = 0;
151             *resource = val;
152         }
153     }
154     *amount = actual;
155     *cost = product->p_cost * material_consume;
156
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     if (CANT_HAPPEN(p_e <= 0.0))
165         return 0;
166     work_used = roundavg(unit_work * material_consume / p_e);
167     if (CANT_HAPPEN(work_used > work))
168         return work;
169     return work_used;
170 }
171
172 /*
173  * Return how much of product @pp can be made from materials @vec[].
174  * Store amount of work per unit in *@costp.
175  */
176 int
177 prod_materials_cost(struct pchrstr *pp, short vec[], int *costp)
178 {
179     int count;
180     int cost;
181     int i, n;
182
183     count = ITEM_MAX;
184     cost = 0;
185     for (i = 0; i < MAXPRCON; ++i) {
186         if (!pp->p_camt[i])
187             continue;
188         if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
189             continue;
190         n = vec[pp->p_ctype[i]] / pp->p_camt[i];
191         if (n < count)
192             count = n;
193         cost += pp->p_camt[i];
194     }
195     *costp = cost;
196     return count;
197 }
198
199 static void
200 materials_charge(struct pchrstr *pp, short *vec, int count)
201 {
202     int i, n;
203     i_type item;
204
205     for (i = 0; i < MAXPRCON; ++i) {
206         item = pp->p_ctype[i];
207         if (!pp->p_camt[i])
208             continue;
209         if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
210             continue;
211         n = vec[item] - pp->p_camt[i] * count;
212         if (CANT_HAPPEN(n < 0))
213             n = 0;
214         vec[item] = n;
215     }
216 }
217
218 /*
219  * Return how much of product @pp can be made from its resource.
220  * If @pp depletes a resource, @resource must point to its value.
221  */
222 int
223 prod_resource_limit(struct pchrstr *pp, unsigned char *resource)
224 {
225     if (CANT_HAPPEN(pp->p_nrndx && !resource))
226         return 0;
227     if (resource && pp->p_nrdep != 0)
228         return *resource * 100 / pp->p_nrdep;
229     return ITEM_MAX;
230 }
231
232 /*
233  * Return p.e. for sector type @type.
234  * Zero means level is too low for production.
235  * @level is the level affecting production.
236  */
237 double
238 prod_eff(int type, float level)
239 {
240     double level_p_e;
241     struct dchrstr *dp = &dchr[type];
242     struct pchrstr *pp = &pchr[dp->d_prd];
243
244     if (CANT_HAPPEN(dp->d_prd < 0))
245         return 0.0;
246
247     if (pp->p_nlndx < 0)
248         level_p_e = 1.0;
249     else {
250         double delta = (double)level - (double)pp->p_nlmin;
251
252         if (delta < 0.0)
253             return 0.0;
254         if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
255             return 0.0;
256         level_p_e = delta / (delta + pp->p_nllag);
257     }
258
259     return level_p_e * dp->d_peffic * 0.01;
260 }