/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- * Ken Stevens, Steve McClure
+ * Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Ken Stevens, Steve McClure, Markus Armbruster
*
- * This program is free software; you can redistribute it and/or modify
+ * Empire is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
* ---
*
* produce.c: Produce goodies
- *
+ *
* Known contributors to this file:
- *
+ * Markus Armbruster, 2004-2013
*/
#include <config.h>
#include "budg.h"
+#include "chance.h"
#include "player.h"
#include "product.h"
#include "update.h"
static void materials_charge(struct pchrstr *, short *, int);
-static int materials_cost(struct pchrstr *, short *, int *);
static char *levelnames[] = {
"Technology", "Research", "Education", "Happiness"
int unit_work;
i_type item;
int worker_limit;
- int material_limit;
+ int material_limit, res_limit;
int material_consume;
int val;
return 0;
product = &pchr[dchr[desig].d_prd];
item = product->p_type;
+ if (product->p_nrndx)
+ resource = (unsigned char *)sp + product->p_nrndx;
+ else
+ resource = NULL;
*amount = 0;
*cost = 0;
- if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0)
+ material_limit = prod_materials_cost(product, vec, &unit_work);
+ if (material_limit <= 0)
return 0;
- /*
- * calculate production efficiency.
- */
+
+ /* sector p.e. */
p_e = neweff / 100.0;
- if (product->p_nrndx != 0) {
+ if (resource) {
unit_work++;
- resource = (unsigned char *)sp + product->p_nrndx;
- p_e = (*resource * p_e) / 100.0;
+ p_e *= *resource / 100.0;
}
- /*
- * determine number that can be made with
- * the available workforce
- */
if (unit_work == 0)
unit_work = 1;
- material_consume = material_limit;
+
worker_limit = roundavg(work * p_e / unit_work);
+ res_limit = prod_resource_limit(product, resource);
+
+ material_consume = res_limit;
if (material_consume > worker_limit)
material_consume = worker_limit;
+ if (material_consume > material_limit)
+ material_consume = material_limit;
if (material_consume == 0)
return 0;
+
prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]);
if (prodeff <= 0.0 && !player->simulation) {
wu(0, sp->sct_own,
actual = ldround(output, 1);
if (!player->simulation) {
levels[sp->sct_own][product->p_level] += output;
- wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
+ wu(0, sp->sct_own, "%s (%.2f) produced in %s\n",
product->p_name, output, ownxy(sp));
}
} else {
actual = roundavg(output);
if (actual <= 0)
return 0;
- if (product->p_nrdep != 0) {
- if (*resource * 100 < product->p_nrdep * actual)
- actual = *resource * 100 / product->p_nrdep;
- }
if (actual > 999) {
- material_consume = roundavg(999.0 * material_consume / actual);
actual = 999;
+ material_consume = roundavg(actual / prodeff);
}
if (vec[item] + actual > ITEM_MAX) {
- material_consume = roundavg((double)(ITEM_MAX - vec[item])
- * material_consume / actual);
+ actual = ITEM_MAX - vec[item];
+ material_consume = roundavg(actual / prodeff);
if (material_consume < 0)
material_consume = 0;
- actual = ITEM_MAX - vec[item];
if (sp->sct_own && !player->simulation)
wu(0, sp->sct_own,
"%s production backlog in %s\n",
*/
if (!player->simulation) {
materials_charge(product, vec, material_consume);
- if (product->p_nrdep != 0) {
+ if (resource && product->p_nrdep != 0) {
/*
* lower natural resource in sector depending on
* amount produced
return 0;
}
-static int
-materials_cost(struct pchrstr *pp, short *vec, int *costp)
+/*
+ * Return how much of product PP can be made from materials VEC[].
+ * Store amount of work per unit in *COSTP.
+ */
+int
+prod_materials_cost(struct pchrstr *pp, short vec[], int *costp)
{
int count;
int cost;
int i, n;
- count = 9999;
+ count = ITEM_MAX;
cost = 0;
for (i = 0; i < MAXPRCON; ++i) {
if (!pp->p_camt[i])
}
/*
- * Return level p.e. for sector type TYPE.
+ * Return how much of product PP can be made from its resource.
+ * If PP depletes a resource, RESOURCE must point to its value.
+ */
+int
+prod_resource_limit(struct pchrstr *pp, unsigned char *resource)
+{
+ if (CANT_HAPPEN(pp->p_nrndx && !resource))
+ return 0;
+ if (resource && pp->p_nrdep != 0)
+ return *resource * 100 / pp->p_nrdep;
+ return ITEM_MAX;
+}
+
+/*
+ * Return p.e. for sector type TYPE.
* Zero means level is too low for production.
* LEVEL is the affecting production of PP; it must match PP->p_nlndx.
*/