2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * produce.c: Produce goodies
30 * Known contributors to this file:
49 static void materials_charge(struct pchrstr *, short *, int);
50 static int materials_cost(struct pchrstr *, short *, int *);
52 s_char *levelnames[] =
53 { "Technology", "Research", "Education", "Happiness" };
56 produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
57 int desig, int neweff, int *cost, int *amount)
59 register struct pchrstr *product;
73 product = &pchr[dchr[desig].d_prd];
74 if (product == &pchr[0])
76 vtype = product->p_type;
77 item = vtype & ~VT_ITEM;
81 if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0)
84 * calculate production efficiency.
87 if (product->p_nrndx != 0) {
89 resource = ((s_char *)sp) + product->p_nrndx;
90 p_e = (*resource * p_e) / 100.0;
93 * determine number that can be made with
94 * the available workforce
98 material_consume = material_limit;
99 worker_limit = roundavg(work * p_e / unit_work);
100 if (material_consume > worker_limit)
101 material_consume = worker_limit;
102 if (material_consume == 0)
105 if (product->p_nlndx >= 0) {
106 level_p_e = np->nat_level[product->p_nlndx] - product->p_nlmin;
107 if ((level_p_e < 0.0) && (!player->simulation)) {
109 "%s level too low to produce in %s (need %d)\n",
110 levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
113 level_p_e = level_p_e / (level_p_e + product->p_nllag);
116 * Adjust produced amount by commodity production ratio
118 output = roundavg(product->p_effic * 0.01 * material_consume);
119 if ((vtype == 0) && (!player->simulation)) {
120 levels[sp->sct_own][product->p_level] += output * level_p_e;
121 wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
122 product->p_name, output * level_p_e, ownxy(sp));
124 if ((actual = roundavg(level_p_e * output)) <= 0)
126 if (product->p_nrdep != 0) {
127 if (*resource * 100 < product->p_nrdep * actual)
128 actual = *resource * 100 / product->p_nrdep;
131 material_consume = roundavg(999.0 * material_consume / actual);
134 if (vec[item] + actual > ITEM_MAX) {
135 material_consume = roundavg((double)(ITEM_MAX - vec[item])
136 * material_consume / actual);
137 if (material_consume < 0)
138 material_consume = 0;
139 vec[item] = ITEM_MAX;
140 if (sp->sct_own && !player->simulation)
142 "%s production backlog in %s\n",
143 product->p_name, ownxy(sp));
148 * Reset produced amount by commodity production ratio
150 if (!player->simulation) {
151 materials_charge(product, vec, material_consume);
152 if (product->p_nrdep != 0) {
154 * lower natural resource in sector depending on
157 val = *resource - roundavg(product->p_nrdep *
158 material_consume / 100.0);
165 *cost = product->p_cost * material_consume;
168 if (product->p_level == NAT_TLEV) {
169 if (tpops[sp->sct_own] > 50000)
171 (double)*cost * (double)tpops[sp->sct_own] / 50000.0;
175 /* The min() here is to take care of integer rounding errors */
177 return min(work, (int)(unit_work * material_consume / p_e));
183 materials_cost(struct pchrstr *product, short *vec, int *costp)
186 register u_short *ap;
190 register u_char *endp;
194 ap = product->p_vamt;
195 endp = product->p_vtype + product->p_nv;
196 for (vp = product->p_vtype; vp < endp; vp++, ap++) {
199 n = vec[*vp & ~VT_ITEM] / *ap;
209 materials_charge(struct pchrstr *product, short *vec, int count)
212 register u_short *ap;
213 register u_char *endp;
217 ap = product->p_vamt;
218 endp = product->p_vtype + product->p_nv;
219 for (vp = product->p_vtype; vp < endp; vp++, ap++) {
220 item = *vp & ~VT_ITEM;
221 if (item < 0 || item > I_MAX) {
222 logerror("materials_charge: bad item %d", item);
225 if ((n = vec[item] - *ap * count) < 0) {
226 logerror("materials_charge: %d > %d item #%d",