]> git.pond.sub.org Git - empserver/blob - src/lib/commands/mult.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / mult.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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  *  mult.c: Set per-nation list of price multipliers
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "var.h"
37 #include "nat.h"
38 #include "file.h"
39 #include "xy.h"
40 #include "nsc.h"
41 #include "commands.h"
42 #include "optlist.h"
43 #include <math.h>               /* bailey@math-cs.kent.edu */
44
45 static void multsingle(natid us, natid them, struct natstr *natp);
46
47 int
48 mult(void)
49 {
50     struct nstr_item ni;
51     struct natstr nat;
52     int nats;
53
54     if (!opt_MARKET) {
55         pr("The market is disabled.\n");
56         return RET_FAIL;
57     }
58     pr("The mult command is no longer used.\n");
59     if (!snxtitem(&ni, EF_NATION, player->argp[1]))
60         return RET_SYN;
61     if (commread() < 0) {
62         pr("Unable to read commodity file; get help!\n");
63         return RET_SYS;
64     }
65     nats = 0;
66     while (!player->aborted && nxtitem(&ni, (s_char *)&nat)) {
67         if ((nat.nat_stat & STAT_NORM) == 0)
68             continue;
69         multsingle(player->cnum, (natid)ni.cur, &nat);
70         nats++;
71     }
72     pr("%d nation multipliers changed\n", nats);
73     return RET_OK;
74 }
75
76 /*
77  * Set the multipler for a single country.
78  */
79 static void
80 multsingle(natid us, natid them, struct natstr *natp)
81 {
82     extern double minmult;
83     extern double maxmult;
84     double price;
85     s_char *p;
86     s_char prompt[128];
87     s_char buf[1024];
88
89     sprintf(prompt, "%s (%7.3f) : ", natp->nat_cnam, multread(us, them));
90     p = getstarg(player->argp[2], prompt, buf);
91     if (p == 0 || *p == 0)
92         return;
93     if ((price = atof(p)) == 0.0)
94         return;
95 #if defined(HUGE)
96     if ((price == HUGE) || (price > 1000000.0)) /* Inf causes overflow. */
97 #else
98     if (price > 1000000.0)
99 #endif
100         price = 1000000.0;      /* bailey@math-cs.kent.edu */
101     /*
102      * no free lunches!
103      */
104     if (price <= minmult)
105         price = minmult;
106     if (price >= maxmult)
107         price = maxmult;
108     if (!commlock()) {
109         pr("Unable to lock commodity file; get help!\n");
110         return;
111     }
112     if (commread() < 0) {
113         (void)communlock();
114         pr("Unable to re-read commodity file; get help!\n");
115         return;
116     }
117     multset(them, price);
118     if (commwrite() < 0) {
119         pr("Unable to write out commodity file; get help!\n");
120     }
121     (void)communlock();
122 }