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