]> git.pond.sub.org Git - empserver/blob - src/lib/commands/repo.c
Remove a bunch of redundant casts.
[empserver] / src / lib / commands / repo.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
37 #include "player.h"
38 #include "sect.h"
39 #include "nat.h"
40 #include "file.h"
41 #include "xy.h"
42 #include "nsc.h"
43 #include <fcntl.h>
44 #include <ctype.h>
45 #include "commands.h"
46 #include "optlist.h"
47
48 struct stats {
49     double res;
50     double tech;
51     double edu;
52     double hap;
53     int stat;
54 };
55
56 static void printdiff(int mystat, double ours, struct natstr *natp,
57                       int what);
58 static void repo_header(void);
59 static void repo_list(struct stats *stat, natid cn, struct natstr *natp);
60
61 static int check(s_char *buf, double theirs, double min, double max,
62                  int shift, int tolerance);
63
64 int
65 repo(void)
66 {
67     struct natstr *natp;
68     struct stats mystat;
69     struct natstr nat;
70     struct nstr_item ni;
71     int first;
72
73     if (!snxtitem(&ni, EF_NATION, player->argp[1]))
74         return RET_SYN;
75     prdate();
76     natp = getnatp(player->cnum);
77     memset(&mystat, 0, sizeof(struct stats));
78     mystat.stat = natp->nat_stat;
79     if (mystat.stat & STAT_NORM) {
80         mystat.res = natp->nat_level[NAT_RLEV];
81         mystat.tech = natp->nat_level[NAT_TLEV];
82         mystat.edu = natp->nat_level[NAT_ELEV];
83         mystat.hap = natp->nat_level[NAT_HLEV];
84     }
85     if (opt_HIDDEN) {
86         repo_header();
87         first = 0;
88     } else {
89         first = 1;
90     }
91     while (nxtitem(&ni, &nat)) {
92         if (!(nat.nat_stat & STAT_INUSE))
93             continue;
94         if (opt_HIDDEN) {
95             if (!player->god && !getcontact(getnatp(player->cnum), ni.cur))
96                 continue;
97         }
98         if (first) {
99             repo_header();
100             first = 0;
101         }
102         repo_list(&mystat, (natid)ni.cur, &nat);
103     }
104     return RET_OK;
105 }
106
107 static void
108 repo_header(void)
109 {
110     pr(" #    name                tech      research   education   happiness ");
111     if (player->god)
112         pr("capital\n");
113     else {
114         if (opt_HIDDEN)
115             pr("\n");
116         else
117             pr(" status\n");
118     }
119 }
120
121 static void
122 repo_list(struct stats *stat, natid cn, struct natstr *natp)
123 {
124     struct sctstr cap;
125
126     if (player->god) {
127         pr(" %-3d   %-14.14s ", cn, natp->nat_cnam);
128         pr(" %7.2f    %7.2f      %7.2f     %7.2f",
129            natp->nat_level[NAT_TLEV],
130            natp->nat_level[NAT_RLEV],
131            natp->nat_level[NAT_ELEV], natp->nat_level[NAT_HLEV]);
132         prxy("  %4d,%-4d\n", natp->nat_xcap, natp->nat_ycap, player->cnum);
133         return;
134     }
135     switch (natp->nat_stat & (STAT_NORM | STAT_GOD | STAT_NEW | STAT_SANCT)) {
136     case STAT_NORM:
137     case (STAT_NORM | STAT_SANCT):
138         pr(" %-3d   %-14.14s ", cn, natp->nat_cnam);
139         printdiff(stat->stat, stat->tech, natp, NAT_TLEV);
140         printdiff(stat->stat, stat->res, natp, NAT_RLEV);
141         printdiff(stat->stat, stat->edu, natp, NAT_ELEV);
142         printdiff(stat->stat, stat->hap, natp, NAT_HLEV);
143         getsect(natp->nat_xcap, natp->nat_ycap, &cap);
144         if (opt_HIDDEN) {
145             pr("\n");
146         } else {
147             if ((cap.sct_own != cn) ||
148                 (cap.sct_type != SCT_CAPIT &&
149                  cap.sct_type != SCT_MOUNT) ||
150                 (natp->nat_flags & NF_SACKED))
151                 pr("In flux\n");
152             else if (natp->nat_money <= 0)
153                 pr("Broke\n");
154             else
155                 pr("Active\n");
156         }
157         break;
158     case STAT_SANCT:
159         break;
160     case STAT_NEW:
161     case 0:
162         break;
163     case STAT_SANCT | STAT_NORM | STAT_GOD:
164     case STAT_NORM | STAT_GOD:
165     case STAT_GOD:
166         break;
167     default:
168         pr("????        ????        ????        ????\n");
169         break;
170     }
171 }
172
173 static void
174 printdiff(int mystat, double ours, struct natstr *natp, int what)
175 {
176     double theirs;
177     int shift;
178     int tolerance;
179     s_char buf[128];
180
181     if (natp->nat_cnum == player->cnum) {
182         pr(" %7.2f    ", ours);
183         return;
184     }
185     if (ours && mystat & STAT_NORM && natp->nat_stat & STAT_NORM) {
186         theirs = natp->nat_level[what];
187         if ((shift = min((int)theirs, (int)ours) - 100) > 0) {
188             ours -= shift;
189             theirs -= shift;
190         } else
191             shift = 0;
192         switch (what) {
193         case NAT_TLEV:
194             tolerance = 20;
195             break;
196         case NAT_RLEV:
197             tolerance = 10;
198             break;
199         default:
200             tolerance = 5;
201         }
202         if (tolerance > 2 * ours)
203             tolerance = (int)(2 * ours);
204         if (check(buf, theirs, 2 * ours, -1.0, shift, tolerance))
205           ;
206         else if (check(buf, theirs, 1.5 * ours, 2.0 * ours, shift, tolerance))
207           ;
208         else if (check(buf, theirs, 1.2 * ours, 1.5 * ours, shift, tolerance))
209           ;
210         else if (check(buf, theirs, 1.1 * ours, 1.2 * ours, shift, tolerance))
211           ;
212         else if (check(buf, theirs, ours / 1.1, 1.1 * ours, shift, tolerance))
213           ;
214         else if (check(buf, theirs, ours / 1.2, ours / 1.1, shift, tolerance))
215           ;
216         else if (check(buf, theirs, ours / 1.5, ours / 1.2, shift, tolerance))
217           ;
218         else if (check(buf, theirs, ours / 2.0, ours / 1.5, shift, tolerance))
219           ;
220         else if (check(buf, theirs, -1.0, ours / 2.0, shift, tolerance)) ;
221         else
222             sprintf(buf, "    n/a");
223     } else
224         sprintf(buf, "    n/a");
225
226     pr("%-11s ", buf);
227 }
228
229 static int
230 check(s_char *buf, double theirs, double min, double max, int shift,
231       int tolerance)
232 {
233     double shove;
234
235     if (min < 0) {
236         if (theirs <= max) {
237             if (max < tolerance)
238                 max = tolerance;
239             sprintf(buf, "   0 - %d", (int)max + shift);
240             return 1;
241         }
242     } else if (max < 0) {
243         if (theirs >= min) {
244             sprintf(buf, "    >= %d", (int)min + shift);
245             return 1;
246         }
247     } else if (theirs >= min && theirs <= max) {
248         if (max - min < tolerance) {
249             shove = (tolerance - (max - min)) / 2;
250             if (min + shift - shove >= 0) {
251                 min -= shove;
252                 max += shove;
253             } else {
254                 min = 0;
255                 max = tolerance;
256             }
257         }
258         sprintf(buf, "%4d - %d", (int)min + shift, (int)max + shift);
259         return 1;
260     }
261
262     return 0;
263 }