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
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.
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, see <http://www.gnu.org/licenses/>.
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.
27 * produce.c: Produce goodies
29 * Known contributors to this file:
30 * Markus Armbruster, 2004-2013
41 static void materials_charge(struct pchrstr *, short *, int);
43 static char *levelnames[] = {
44 "Technology", "Research", "Education", "Happiness"
48 produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
49 int desig, int neweff, int *cost, int *amount)
51 struct pchrstr *product;
54 unsigned char *resource;
60 int material_limit, res_limit;
64 if (dchr[desig].d_prd < 0)
66 product = &pchr[dchr[desig].d_prd];
67 item = product->p_type;
69 resource = (unsigned char *)sp + product->p_nrndx;
75 material_limit = prod_materials_cost(product, vec, &unit_work);
76 if (material_limit <= 0)
83 p_e *= *resource / 100.0;
88 worker_limit = roundavg(work * p_e / unit_work);
89 res_limit = prod_resource_limit(product, resource);
91 material_consume = res_limit;
92 if (material_consume > worker_limit)
93 material_consume = worker_limit;
94 if (material_consume > material_limit)
95 material_consume = material_limit;
96 if (material_consume == 0)
99 prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]);
100 if (prodeff <= 0.0 && !player->simulation) {
102 "%s level too low to produce in %s (need %d)\n",
103 levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
107 * Adjust produced amount by commodity production ratio
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));
118 actual = roundavg(output);
123 material_consume = roundavg(actual / prodeff);
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)
132 "%s production backlog in %s\n",
133 product->p_name, ownxy(sp));
138 * Reset produced amount by commodity production ratio
140 if (!player->simulation) {
141 materials_charge(product, vec, material_consume);
142 if (resource && product->p_nrdep != 0) {
144 * lower natural resource in sector depending on
147 val = *resource - roundavg(product->p_nrdep *
148 material_consume / 100.0);
155 *cost = product->p_cost * material_consume;
158 if (product->p_level == NAT_TLEV) {
159 if (tpops[sp->sct_own] > 50000)
160 *cost *= tpops[sp->sct_own] / 50000.0;
164 /* The MIN() here is to take care of integer rounding errors */
166 return MIN(work, (int)(unit_work * material_consume / p_e));
172 * Return how much of product PP can be made from materials VEC[].
173 * Store amount of work per unit in *COSTP.
176 prod_materials_cost(struct pchrstr *pp, short vec[], int *costp)
184 for (i = 0; i < MAXPRCON; ++i) {
187 if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
189 n = vec[pp->p_ctype[i]] / pp->p_camt[i];
192 cost += pp->p_camt[i];
199 materials_charge(struct pchrstr *pp, short *vec, int count)
204 for (i = 0; i < MAXPRCON; ++i) {
205 item = pp->p_ctype[i];
208 if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
210 n = vec[item] - pp->p_camt[i] * count;
211 if (CANT_HAPPEN(n < 0))
218 * Return how much of product PP can be made from its resource.
219 * If PP depletes a resource, RESOURCE must point to its value.
222 prod_resource_limit(struct pchrstr *pp, unsigned char *resource)
224 if (CANT_HAPPEN(pp->p_nrndx && !resource))
226 if (resource && pp->p_nrdep != 0)
227 return *resource * 100 / pp->p_nrdep;
232 * Return p.e. for sector type TYPE.
233 * Zero means level is too low for production.
234 * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
237 prod_eff(int type, float level)
240 struct dchrstr *dp = &dchr[type];
241 struct pchrstr *pp = &pchr[dp->d_prd];
243 if (CANT_HAPPEN(dp->d_prd < 0))
249 double delta = (double)level - (double)pp->p_nlmin;
253 if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
255 level_p_e = delta / (delta + pp->p_nllag);
258 return level_p_e * dp->d_peffic * 0.01;