]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
prod() duplicates materials_cost(), clean up
[empserver] / src / lib / update / produce.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, 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;
58     i_type item;
59     int worker_limit;
60     int material_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      * calculate production efficiency.
80      */
81     p_e = neweff / 100.0;
82     if (resource) {
83         unit_work++;
84         p_e *= *resource / 100.0;
85     }
86     /*
87      * determine number that can be made with
88      * the available workforce
89      */
90     if (unit_work == 0)
91         unit_work = 1;
92     material_consume = material_limit;
93     worker_limit = roundavg(work * p_e / unit_work);
94     if (material_consume > worker_limit)
95         material_consume = worker_limit;
96     if (resource && product->p_nrdep != 0) {
97         if (*resource * 100 < product->p_nrdep * material_consume)
98             material_consume = *resource * 100 / product->p_nrdep;
99     }
100     if (material_consume == 0)
101         return 0;
102     prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]);
103     if (prodeff <= 0.0 && !player->simulation) {
104         wu(0, sp->sct_own,
105            "%s level too low to produce in %s (need %d)\n",
106            levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
107         return 0;
108     }
109     /*
110      * Adjust produced amount by commodity production ratio
111      */
112     output = material_consume * prodeff;
113     if (item == I_NONE) {
114         actual = ldround(output, 1);
115         if (!player->simulation) {
116             levels[sp->sct_own][product->p_level] += output;
117             wu(0, sp->sct_own, "%s (%.2f) produced in %s\n",
118                product->p_name, output, ownxy(sp));
119         }
120     } else {
121         actual = roundavg(output);
122         if (actual <= 0)
123             return 0;
124         if (actual > 999) {
125             material_consume = roundavg(999.0 * material_consume / actual);
126             actual = 999;
127         }
128         if (vec[item] + actual > ITEM_MAX) {
129             material_consume = roundavg((double)(ITEM_MAX - vec[item])
130                                         * material_consume / actual);
131             if (material_consume < 0)
132                 material_consume = 0;
133             actual = ITEM_MAX - vec[item];
134             if (sp->sct_own && !player->simulation)
135                 wu(0, sp->sct_own,
136                    "%s production backlog in %s\n",
137                    product->p_name, ownxy(sp));
138         }
139         vec[item] += actual;
140     }
141     /*
142      * Reset produced amount by commodity production ratio
143      */
144     if (!player->simulation) {
145         materials_charge(product, vec, material_consume);
146         if (resource && product->p_nrdep != 0) {
147             /*
148              * lower natural resource in sector depending on
149              * amount produced
150              */
151             val = *resource - roundavg(product->p_nrdep *
152                                        material_consume / 100.0);
153             if (val < 0)
154                 val = 0;
155             *resource = val;
156         }
157     }
158     *amount = actual;
159     *cost = product->p_cost * material_consume;
160
161     if (opt_TECH_POP) {
162         if (product->p_level == NAT_TLEV) {
163             if (tpops[sp->sct_own] > 50000)
164                 *cost *= tpops[sp->sct_own] / 50000.0;
165         }
166     }
167
168     /* The MIN() here is to take care of integer rounding errors */
169     if (p_e > 0.0) {
170         return MIN(work, (int)(unit_work * material_consume / p_e));
171     }
172     return 0;
173 }
174
175 /*
176  * Return how much of product PP can be made from materials VEC[].
177  * Store amount of work per unit in *COSTP.
178  */
179 int
180 prod_materials_cost(struct pchrstr *pp, short vec[], int *costp)
181 {
182     int count;
183     int cost;
184     int i, n;
185
186     count = 9999;
187     cost = 0;
188     for (i = 0; i < MAXPRCON; ++i) {
189         if (!pp->p_camt[i])
190             continue;
191         if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
192             continue;
193         n = vec[pp->p_ctype[i]] / pp->p_camt[i];
194         if (n < count)
195             count = n;
196         cost += pp->p_camt[i];
197     }
198     *costp = cost;
199     return count;
200 }
201
202 static void
203 materials_charge(struct pchrstr *pp, short *vec, int count)
204 {
205     int i, n;
206     i_type item;
207
208     for (i = 0; i < MAXPRCON; ++i) {
209         item = pp->p_ctype[i];
210         if (!pp->p_camt[i])
211             continue;
212         if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
213             continue;
214         n = vec[item] - pp->p_camt[i] * count;
215         if (CANT_HAPPEN(n < 0))
216             n = 0;
217         vec[item] = n;
218     }
219 }
220
221 /*
222  * Return p.e. for sector type TYPE.
223  * Zero means level is too low for production.
224  * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
225  */
226 double
227 prod_eff(int type, float level)
228 {
229     double level_p_e;
230     struct dchrstr *dp = &dchr[type];
231     struct pchrstr *pp = &pchr[dp->d_prd];
232
233     if (CANT_HAPPEN(dp->d_prd < 0))
234         return 0.0;
235
236     if (pp->p_nlndx < 0)
237         level_p_e = 1.0;
238     else {
239         double delta = (double)level - (double)pp->p_nlmin;
240
241         if (delta < 0.0)
242             return 0.0;
243         if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
244             return 0.0;
245         level_p_e = delta / (delta + pp->p_nllag);
246     }
247
248     return level_p_e * dp->d_peffic * 0.01;
249 }