]> git.pond.sub.org Git - empserver/blob - src/lib/commands/repo.c
Update copyright notice
[empserver] / src / lib / commands / repo.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  repo.c: Report on various levels (tech, research) of other nations
29  *
30  *  Known contributors to this file:
31  *     Keith Muller, 1983
32  *     Dave Pare, 1986 (rewrite)
33  *     Steve McClure, 2000
34  */
35
36 #include <config.h>
37
38 #include "commands.h"
39 #include "optlist.h"
40
41 static void repo_header(void);
42 static void repo_list(struct natstr *, struct natstr *);
43 static void printdiff(struct natstr *, struct natstr *, int what);
44 static int tryprdiff(double, double, double, int, int);
45
46 int
47 repo(void)
48 {
49     struct natstr *natp;
50     struct natstr nat;
51     struct nstr_item ni;
52
53     if (!snxtitem(&ni, EF_NATION, player->argp[1], NULL))
54         return RET_SYN;
55     prdate();
56     natp = getnatp(player->cnum);
57     repo_header();
58     while (nxtitem(&ni, &nat)) {
59         if (nat.nat_stat == STAT_UNUSED)
60             continue;
61         if (opt_HIDDEN) {
62             if (!player->god && !getcontact(natp, ni.cur))
63                 continue;
64         }
65         if (!player->god && nat.nat_stat != STAT_ACTIVE)
66             continue;
67         repo_list(natp, &nat);
68     }
69     return RET_OK;
70 }
71
72 static void
73 repo_header(void)
74 {
75     pr(" #    name                tech      research   education   happiness"
76        " %s\n",
77        player->god ? "capital" : " status");
78 }
79
80 static void
81 repo_list(struct natstr *plnatp, struct natstr *natp)
82 {
83     pr(" %-3d   %-14.14s ", natp->nat_cnum, natp->nat_cnam);
84     if (player->god || player->cnum == natp->nat_cnum) {
85         pr(" %7.2f     %7.2f     %7.2f     %7.2f%s",
86            natp->nat_level[NAT_TLEV], natp->nat_level[NAT_RLEV],
87            natp->nat_level[NAT_ELEV], natp->nat_level[NAT_HLEV],
88            player->god ? "" : "    ");
89     } else {
90         printdiff(plnatp, natp, NAT_TLEV);
91         printdiff(plnatp, natp, NAT_RLEV);
92         printdiff(plnatp, natp, NAT_ELEV);
93         printdiff(plnatp, natp, NAT_HLEV);
94     }
95     if (player->god) {
96         prxy("  %4d,%-4d\n", natp->nat_xcap, natp->nat_ycap, player->cnum);
97     } else {
98         if (!opt_HIDDEN && influx(natp))
99             pr("In flux\n");
100         else if (!opt_HIDDEN && natp->nat_money < 0)
101             pr("Broke\n");
102         else
103             pr("Active\n");
104     }
105 }
106
107 static void
108 printdiff(struct natstr *plnatp, struct natstr *natp, int what)
109 {
110     double ours = plnatp->nat_level[what];
111     double theirs;
112     int shift;
113     int tolerance;
114
115     if (ours
116         && plnatp->nat_stat >= STAT_ACTIVE
117         && natp->nat_stat >= STAT_ACTIVE) {
118         theirs = natp->nat_level[what];
119         if ((shift = MIN((int)theirs, (int)ours) - 100) > 0) {
120             ours -= shift;
121             theirs -= shift;
122         } else
123             shift = 0;
124         switch (what) {
125         case NAT_TLEV:
126             tolerance = 20;
127             break;
128         case NAT_RLEV:
129             tolerance = 10;
130             break;
131         default:
132             tolerance = 5;
133         }
134         if (tolerance > 2 * ours)
135             tolerance = (int)(2 * ours);
136         if (tryprdiff(theirs, 2 * ours, -1.0, shift, tolerance))
137           ;
138         else if (tryprdiff(theirs, 1.5 * ours, 2.0 * ours, shift, tolerance))
139           ;
140         else if (tryprdiff(theirs, 1.2 * ours, 1.5 * ours, shift, tolerance))
141           ;
142         else if (tryprdiff(theirs, 1.1 * ours, 1.2 * ours, shift, tolerance))
143           ;
144         else if (tryprdiff(theirs, ours / 1.1, 1.1 * ours, shift, tolerance))
145           ;
146         else if (tryprdiff(theirs, ours / 1.2, ours / 1.1, shift, tolerance))
147           ;
148         else if (tryprdiff(theirs, ours / 1.5, ours / 1.2, shift, tolerance))
149           ;
150         else if (tryprdiff(theirs, ours / 2.0, ours / 1.5, shift, tolerance))
151           ;
152         else if (tryprdiff(theirs, -1.0, ours / 2.0, shift, tolerance)) ;
153         else
154             pr("    n/a     ");
155     } else
156         pr("    n/a     ");
157 }
158
159 static int
160 tryprdiff(double theirs, double min, double max, int shift, int tolerance)
161 {
162     double shove;
163
164     if (min < 0) {
165         if (theirs <= max) {
166             if (max < tolerance)
167                 max = tolerance;
168             pr("   0 - %-4d ", (int)max + shift);
169             return 1;
170         }
171     } else if (max < 0) {
172         if (theirs >= min) {
173             pr("    >= %-4d ", (int)min + shift);
174             return 1;
175         }
176     } else if (theirs >= min && theirs <= max) {
177         if (max - min < tolerance) {
178             shove = (tolerance - (max - min)) / 2;
179             if (min + shift - shove >= 0) {
180                 min -= shove;
181                 max += shove;
182             } else {
183                 min = 0;
184                 max = tolerance;
185             }
186         }
187         pr("%4d - %-4d ", (int)min + shift, (int)max + shift);
188         return 1;
189     }
190
191     return 0;
192 }