empserver/include/update.h
Markus Armbruster 6013758272 update production: Make sector production a bit more predictable
Sector production computes a number of intermediate values, and rounds
many of them.  We've tinkered with the rounding a few times.  It
currently works as follows.

There are two production efficiencies, both shown by the production
command: sector p.e. (column eff) governs how efficiently work is
converted to units of production, and p.e. (column p.e.)  governs how
much product each unit of production yields.

Production is limited by available work, materials and resource
contents.  These limits are all rounded down.

Example: if a unit takes 16 work (tech or guns), then 600 work at 100%
sector p.e. can make at most 37 units, rounded down from 600 * 100% /
16 = 37.5.  76 work at 76% sector p.e. can make 3, rounded down from
76 * 76% / 16 = 3.61.

Output is units times p.e.  Level output isn't rounded.  Item output
is rounded down.

Example: a tech center making 37 units at p.e. 0.6 (edu=20) yields 37
* 0.6 = 22.2 tech (before tech log).  3 units yield 1.8 tech.

Example: a defense plant making 37 units at p.e. 0.6 (tech 35) yields
22 guns (rounded down from 22.2).  3 units yield 1.8g, randomly
rounded.

If item output needs to be adjusted downward (production backlog), the
number of units made is likewise adjusted.  It is rounded randomly.

Example: a 100% refinery with 156 work can make 156 units.  Its
p.e. at tech 30 is 5.0, so this yields 780p.  But if it already has
9500p, it can make only 499 more.  That's 99.8 units, rounded randomly
to either 99 or 100.

Materials and money consumed are a multiple of units made.  No
rounding there.

Resource depletion depends on units made and is rounded randomly.

Work consumed is units made times work per unit divided by sector
production efficiency.  Rounded randomly.  Any work left can normally
be used at the next update (it "rolls over").

Example: the tech center making 37 units consumes 37 * 16 / 100% = 592
work, with 8 work left.  It also consumes 37d 185o 370l $11100.

Example: the tech center making 3 units consumes 3 * 16 / 76% = 63.2
work, randomly rounded to 63 or 64, with 13 or 12 work left.  It also
consumes 3d 15o 30l $900.

Example: the defense plant making 3 units consumes work the same.  It
additionally consumes 1o 15l 30h $30 when it makes one gun, and twice
as much when it makes two.

Rounding intermediate values like "units of production" is awkward.
It's better to round only final results.  These are item output,
materials consumed, resource depletion and work consumed.  Round item
output down, and the rest randomly.  Don't round level output (it's a
floating-point value) and money consumed (also floating-point, since
the previous commit).

For item production, this shifts the random variations from number of
products made to materials and work consumed.

Example: the first defense plant again makes 22 guns (now rounded down
from 22.5).  The second one now always makes two guns (rounded down
from 3.61 * 0.6 = 2.166) instead of 1.8 randomly rounded.

This is nice, because budget and production can now predict the number
of items made exactly.  Before, budget fluctuated randomly just like
the update, and production rounded down.

Note that budget used to be even worse: until commit 6f7c93c
(v4.3.31), we rounded units of production randomly rather than down.
The 100% tech center randomly made 37 or 38 units, which is much more
relevant than random rounding of item output.

Furthermore, work is now fully used both for item and level
production, to the limit permitted by materials and resource contents.

Example: the first tech center now makes 37.5 units, yielding 37.5 *
0.6 = 22.5 tech.  It consumes 37.5d 187.5o 375l $11250 and all 600
work (fractions randomly rounded).

Example: the second tech center now makes 3.61 units yielding 1.805
tech, consuming 3.61d 18.05o 36.1l $1083 and all 76 work.

The production command duplicates much of the update's sector
production code, so it needs a matching update.  The next commit will
reduce the duplication.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
2017-08-06 20:08:29 +02:00

153 lines
4.7 KiB
C

/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure, Markus Armbruster
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* 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, see <http://www.gnu.org/licenses/>.
*
* ---
*
* See files README, COPYING and CREDITS in the root of the source
* tree for related information and legal notices. It is expected
* that future projects/authors will amend these files as needed.
*
* ---
*
* update.h: Definitions related to the update
*
* Known contributors to this file:
* Ville Virrankoski, 1995
* Markus Armbruster, 2004-2016
*/
#ifndef UPDATE_H
#define UPDATE_H
#include "sect.h"
#define IMPORT 0
#define EXPORT 1
enum {
BUDG_SHP_BUILD,
BUDG_SHP_MAINT,
BUDG_PLN_BUILD,
BUDG_PLN_MAINT,
BUDG_LND_BUILD,
BUDG_LND_MAINT,
BUDG_SCT_BUILD,
BUDG_SCT_MAINT,
BUDG_BLD_MAX = BUDG_SCT_MAINT
};
struct budg_item {
double money; /* money delta */
int count; /* #things making/consuming the money */
};
/* A nation's budget for an update */
struct budget {
/* production by sector type */
struct budg_item prod[SCT_TYPE_MAX + 1];
/* building and maintenance */
struct budg_item bm[BUDG_BLD_MAX + 1];
/* population, taxes, military payroll, bank interest */
struct budg_item civ, mil, uw, bars;
/* treasury */
int start_money; /* at beginning of update */
double money; /* current */
};
/* main.c */
extern struct budget nat_budget[MAXNOC];
extern int pops[MAXNOC];
extern int tpops[MAXNOC];
/* nat.c */
extern float levels[MAXNOC][4];
/* age.c */
extern int age_people(int, int);
extern void age_levels(int);
/* anno.c */
extern void delete_old_announcements(void);
/* bp.c */
extern struct bp *bp_alloc(void);
extern void bp_set_from_sect(struct bp *, struct sctstr *);
extern void bp_to_sect(struct bp *, struct sctstr *);
/* deliver.c */
extern void dodeliver(struct sctstr *);
/* distribute.c */
extern int dodistribute(struct sctstr *, int, double);
/* finish.c */
extern void finish_sects(int);
/* human.c */
extern void do_feed(struct sctstr *, struct natstr *, int, int);
extern int feed_people(short *, int);
extern double food_needed(short *, int);
extern int famine_victims(short *, int);
/* land.c */
extern void prod_land(int, int, struct bp *, int);
/* main.c */
/* in server.h */
/* material.c */
extern int get_materials(struct sctstr *, short[], int);
/* mobility.c */
extern void mob_sect(void);
extern void mob_ship(void);
extern void mob_land(void);
extern void mob_plane(void);
extern void sct_do_upd_mob(struct sctstr *sp);
extern void shp_do_upd_mob(struct shpstr *sp);
extern void lnd_do_upd_mob(struct lndstr *lp);
extern void pln_do_upd_mob(struct plnstr *pp);
/* move_sat.c */
extern void move_sat(struct plnstr *);
/* nat.c */
extern void prod_nat(int);
/* nxtitemp.c */
/* in nsc.h */
/* plague.c */
extern void do_plague(struct sctstr *, int);
extern int plague_people(struct natstr *, short *, int *, int *, int);
/* plane.c */
extern void prod_plane(int, int, struct bp *, int);
/* populace.c */
extern void populace(struct sctstr *, int);
extern int total_work(int, int, int, int, int, int);
/* prepare.c */
extern void prepare_sects(int);
extern void tax(struct sctstr *, int, int *);
extern void upd_slmilcosts(int, natid);
extern void bank_income(struct sctstr *, int);
extern void pay_reserve(struct natstr *, int);
/* produce.c */
extern void produce(struct natstr *, struct sctstr *);
extern double prod_materials_cost(struct pchrstr *, short[], int *);
extern double prod_resource_limit(struct pchrstr *, unsigned char *);
extern double prod_eff(int, float);
/* removewants.c */
extern int update_removewants(void);
/* revolt.c */
extern void revolt(struct sctstr *);
extern void guerrilla(struct sctstr *);
/* sect.c */
extern double buildeff(struct sctstr *);
extern void do_fallout(struct sctstr *, int);
extern void spread_fallout(struct sctstr *, int);
extern void decay_fallout(struct sctstr *, int);
extern void produce_sect(struct natstr *, int, struct bp *);
/* ship.c */
extern void prod_ship(int, int, struct bp *, int);
#endif