]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
Update known contributors comments
[empserver] / src / lib / update / produce.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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-2010
31  */
32
33 #include <config.h>
34
35 #include "budg.h"
36 #include "player.h"
37 #include "product.h"
38 #include "update.h"
39
40 static void materials_charge(struct pchrstr *, short *, int);
41 static int materials_cost(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     if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0)
76         return 0;
77     /*
78      * calculate production efficiency.
79      */
80     p_e = neweff / 100.0;
81     if (resource) {
82         unit_work++;
83         p_e *= *resource / 100.0;
84     }
85     /*
86      * determine number that can be made with
87      * the available workforce
88      */
89     if (unit_work == 0)
90         unit_work = 1;
91     material_consume = material_limit;
92     worker_limit = roundavg(work * p_e / unit_work);
93     if (material_consume > worker_limit)
94         material_consume = worker_limit;
95     if (resource && product->p_nrdep != 0) {
96         if (*resource * 100 < product->p_nrdep * material_consume)
97             material_consume = *resource * 100 / product->p_nrdep;
98     }
99     if (material_consume == 0)
100         return 0;
101     prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]);
102     if (prodeff <= 0.0 && !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 0;
107     }
108     /*
109      * Adjust produced amount by commodity production ratio
110      */
111     output = material_consume * prodeff;
112     if (item == I_NONE) {
113         actual = ldround(output, 1);
114         if (!player->simulation) {
115             levels[sp->sct_own][product->p_level] += output;
116             wu(0, sp->sct_own, "%s (%.2f) produced in %s\n",
117                product->p_name, output, ownxy(sp));
118         }
119     } else {
120         actual = roundavg(output);
121         if (actual <= 0)
122             return 0;
123         if (actual > 999) {
124             material_consume = roundavg(999.0 * material_consume / actual);
125             actual = 999;
126         }
127         if (vec[item] + actual > ITEM_MAX) {
128             material_consume = roundavg((double)(ITEM_MAX - vec[item])
129                                         * material_consume / actual);
130             if (material_consume < 0)
131                 material_consume = 0;
132             actual = ITEM_MAX - vec[item];
133             if (sp->sct_own && !player->simulation)
134                 wu(0, sp->sct_own,
135                    "%s production backlog in %s\n",
136                    product->p_name, ownxy(sp));
137         }
138         vec[item] += actual;
139     }
140     /*
141      * Reset produced amount by commodity production ratio
142      */
143     if (!player->simulation) {
144         materials_charge(product, vec, material_consume);
145         if (resource && product->p_nrdep != 0) {
146             /*
147              * lower natural resource in sector depending on
148              * amount produced
149              */
150             val = *resource - roundavg(product->p_nrdep *
151                                        material_consume / 100.0);
152             if (val < 0)
153                 val = 0;
154             *resource = val;
155         }
156     }
157     *amount = actual;
158     *cost = product->p_cost * material_consume;
159
160     if (opt_TECH_POP) {
161         if (product->p_level == NAT_TLEV) {
162             if (tpops[sp->sct_own] > 50000)
163                 *cost *= tpops[sp->sct_own] / 50000.0;
164         }
165     }
166
167     /* The MIN() here is to take care of integer rounding errors */
168     if (p_e > 0.0) {
169         return MIN(work, (int)(unit_work * material_consume / p_e));
170     }
171     return 0;
172 }
173
174 static int
175 materials_cost(struct pchrstr *pp, short *vec, int *costp)
176 {
177     int count;
178     int cost;
179     int i, n;
180
181     count = 9999;
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 level p.e. for sector type TYPE.
218  * Zero means level is too low for production.
219  * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
220  */
221 double
222 prod_eff(int type, float level)
223 {
224     double level_p_e;
225     struct dchrstr *dp = &dchr[type];
226     struct pchrstr *pp = &pchr[dp->d_prd];
227
228     if (CANT_HAPPEN(dp->d_prd < 0))
229         return 0.0;
230
231     if (pp->p_nlndx < 0)
232         level_p_e = 1.0;
233     else {
234         double delta = (double)level - (double)pp->p_nlmin;
235
236         if (delta < 0.0)
237             return 0.0;
238         if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
239             return 0.0;
240         level_p_e = delta / (delta + pp->p_nllag);
241     }
242
243     return level_p_e * dp->d_peffic * 0.01;
244 }