]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
Update copyright notice
[empserver] / src / lib / update / produce.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  produce.c: Produce goodies
29  * 
30  *  Known contributors to this file:
31  *    
32  */
33
34 #include <config.h>
35
36 #include "budg.h"
37 #include "player.h"
38 #include "product.h"
39 #include "update.h"
40
41 static void materials_charge(struct pchrstr *, short *, int);
42 static int materials_cost(struct pchrstr *, short *, int *);
43
44 static char *levelnames[] = {
45     "Technology", "Research", "Education", "Happiness"
46 };
47
48 int
49 produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
50         int desig, int neweff, int *cost, int *amount)
51 {
52     struct pchrstr *product;
53     double p_e;
54     double prodeff;
55     unsigned char *resource;
56     double output;
57     int actual;
58     int unit_work;
59     i_type item;
60     int worker_limit;
61     int material_limit;
62     int material_consume;
63     int val;
64
65     if (dchr[desig].d_prd < 0)
66         return 0;
67     product = &pchr[dchr[desig].d_prd];
68     item = product->p_type;
69     *amount = 0;
70     *cost = 0;
71
72     if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0)
73         return 0;
74     /*
75      * calculate production efficiency.
76      */
77     p_e = neweff / 100.0;
78     if (product->p_nrndx != 0) {
79         unit_work++;
80         resource = (unsigned char *)sp + product->p_nrndx;
81         p_e = (*resource * p_e) / 100.0;
82     }
83     /*
84      * determine number that can be made with
85      * the available workforce
86      */
87     if (unit_work == 0)
88         unit_work = 1;
89     material_consume = material_limit;
90     worker_limit = roundavg(work * p_e / unit_work);
91     if (material_consume > worker_limit)
92         material_consume = worker_limit;
93     if (material_consume == 0)
94         return 0;
95     prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]);
96     if (prodeff <= 0.0 && !player->simulation) {
97         wu(0, sp->sct_own,
98            "%s level too low to produce in %s (need %d)\n",
99            levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
100         return 0;
101     }
102     /*
103      * Adjust produced amount by commodity production ratio
104      */
105     output = material_consume * prodeff;
106     if (item == I_NONE) {
107         actual = ldround(output, 1);
108         if (!player->simulation) {
109             levels[sp->sct_own][product->p_level] += output;
110             wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
111                product->p_name, output, ownxy(sp));
112         }
113     } else {
114         actual = roundavg(output);
115         if (actual <= 0)
116             return 0;
117         if (product->p_nrdep != 0) {
118             if (*resource * 100 < product->p_nrdep * actual)
119                 actual = *resource * 100 / product->p_nrdep;
120         }
121         if (actual > 999) {
122             material_consume = roundavg(999.0 * material_consume / actual);
123             actual = 999;
124         }
125         if (vec[item] + actual > ITEM_MAX) {
126             material_consume = roundavg((double)(ITEM_MAX - vec[item])
127                                         * material_consume / actual);
128             if (material_consume < 0)
129                 material_consume = 0;
130             actual = ITEM_MAX - vec[item];
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         vec[item] += actual;
137     }
138     /*
139      * Reset produced amount by commodity production ratio
140      */
141     if (!player->simulation) {
142         materials_charge(product, vec, material_consume);
143         if (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     /* The MIN() here is to take care of integer rounding errors */
166     if (p_e > 0.0) {
167         return MIN(work, (int)(unit_work * material_consume / p_e));
168     }
169     return 0;
170 }
171
172 static int
173 materials_cost(struct pchrstr *pp, short *vec, int *costp)
174 {
175     int count;
176     int cost;
177     int i, n;
178
179     count = 9999;
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 = 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, int count)
197 {
198     int i, n;
199     i_type item;
200
201     for (i = 0; i < MAXPRCON; ++i) {
202         item = pp->p_ctype[i];
203         if (!pp->p_camt[i])
204             continue;
205         if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
206             continue;
207         n = vec[item] - pp->p_camt[i] * count;
208         if (CANT_HAPPEN(n < 0))
209             n = 0;
210         vec[item] = n;
211     }
212 }
213
214 /*
215  * Return level p.e. for sector type TYPE.
216  * Zero means level is too low for production.
217  * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
218  */
219 double
220 prod_eff(int type, float level)
221 {
222     double level_p_e;
223     struct dchrstr *dp = &dchr[type];
224     struct pchrstr *pp = &pchr[dp->d_prd];
225
226     if (CANT_HAPPEN(dp->d_prd < 0))
227         return 0.0;
228
229     if (pp->p_nlndx < 0)
230         level_p_e = 1.0;
231     else {
232         double delta = (double)level - (double)pp->p_nlmin;
233
234         if (delta < 0.0)
235             return 0.0;
236         if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
237             return 0.0;
238         level_p_e = delta / (delta + pp->p_nllag);
239     }
240
241     return level_p_e * dp->d_peffic * 0.01;
242 }