]> git.pond.sub.org Git - empserver/blob - src/lib/common/btu.c
ebf93684e96f154043cb7e326255dcc154518349
[empserver] / src / lib / common / btu.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  *  btu.c: Dealing with BTUs
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2007
32  */
33
34 #include <config.h>
35
36 #include "file.h"
37 #include "nat.h"
38 #include "optlist.h"
39 #include "prototypes.h"
40 #include "sect.h"
41
42 /*
43  * Return BTUs produced by CAP in ETU ETUs.
44  */
45 static int
46 accrued_btus(struct sctstr *cap, int etu)
47 {
48     double eff, civ;
49
50     switch (cap->sct_type) {
51     case SCT_CAPIT:
52     case SCT_SANCT:
53         eff = cap->sct_effic;
54         break;
55     case SCT_MOUNT:
56         eff = 0;
57         break;
58     default:
59         return 0;
60     }
61
62     eff *= cap->sct_work / 100.0;
63     if (eff < 0.5)
64         eff = 0.5;
65
66     civ = cap->sct_item[I_CIVIL];
67     if (civ > 999)
68         civ = 999;
69
70     return roundavg(etu * civ * eff * btu_build_rate);
71 }
72
73 /*
74  * Grant nation NP the BTUs produced by its capital in ETU ETUs.
75  * Return whether it has a capital.
76  */
77 int
78 grant_btus(struct natstr *np, int etu)
79 {
80     int has_cap, delta;
81     struct sctstr sect;
82
83     getsect(np->nat_xcap, np->nat_ycap, &sect);
84     has_cap = np->nat_stat >= STAT_ACTIVE && !influx(np);
85
86     if (has_cap) {
87         delta = accrued_btus(&sect, etu);
88         if (delta + np->nat_btu > max_btus)
89             np->nat_btu = max_btus;
90         else
91             np->nat_btu += delta;
92     }
93     if (np->nat_stat == STAT_VIS || np->nat_stat == STAT_GOD)
94         np->nat_btu = max_btus;
95
96     return has_cap;
97 }