]> git.pond.sub.org Git - empserver/blob - src/lib/commands/payo.c
Update copyright notice.
[empserver] / src / lib / commands / payo.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  *  payo.c: Calculate trade ship payoffs
29  * 
30  *  Known contributors to this file:
31  *     Thomas Ruschak, 1992
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "xy.h"
37 #include "sect.h"
38 #include "ship.h"
39 #include "nat.h"
40 #include "nsc.h"
41 #include "file.h"
42 #include "commands.h"
43 #include "optlist.h"
44
45 int
46 payo(void)
47 {
48     struct sctstr sect;
49     int nships;
50     struct nstr_item ni;
51     struct shpstr ship;
52     struct mchrstr *mp;
53     int dist;
54     float cash = 0.0;
55
56     if (!opt_TRADESHIPS) {
57         pr("Tradeships are not enabled.\n");
58         return RET_FAIL;
59     }
60     if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
61         return RET_SYN;
62
63     nships = 0;
64     while (nxtitem(&ni, (s_char *)&ship)) {
65         if (!player->owner || ship.shp_own == 0)
66             continue;
67         if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
68             pr("bad ship type %d (#%d)\n", ship.shp_type, ni.cur);
69             continue;
70         }
71         mp = &mchr[(int)ship.shp_type];
72
73         if (!(mp->m_flags & M_TRADE))
74             continue;
75
76         if (nships++ == 0) {
77             if (player->god)
78                 pr("own ");
79             pr("shp#     ship type  orig x,y       x,y    dist $$\n");
80         }
81         if (player->god)
82             pr("%3d ", ship.shp_own);
83         pr("%4d ", ni.cur);
84         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
85         if (ship.shp_own != ship.shp_orig_own && !player->god) {
86             /* Don't disclose construction site to pirates! */
87             pr("    ?     ");
88             prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
89             pr("   ? $  ?\n");
90             continue;
91         }
92         prxy("%4d,%-4d ", ship.shp_orig_x, ship.shp_orig_y, player->cnum);
93         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
94
95         getsect(ship.shp_x, ship.shp_y, &sect);
96
97         dist = mapdist(ship.shp_x, ship.shp_y,
98                        ship.shp_orig_x, ship.shp_orig_y);
99         pr("%4d ", dist);
100
101         if (dist < trade_1_dist)
102             cash = 0;
103         else if (dist < trade_2_dist)
104             cash = (1.0 + trade_1 * ((float)dist));
105         else if (dist < trade_3_dist)
106             cash = (1.0 + trade_2 * ((float)dist));
107         else
108             cash = (1.0 + trade_3 * ((float)dist));
109
110         cash *= mp->m_cost;
111         cash *= (((float)ship.shp_effic) / 100.0);
112
113         if (sect.sct_own && (sect.sct_own != ship.shp_own))
114             cash *= (1.0 + trade_ally_bonus);
115         pr("$%6.2f\n", cash);
116     }
117     if (nships == 0) {
118         if (player->argp[1])
119             pr("%s: No ship(s)\n", player->argp[1]);
120         else
121             pr("%s: No ship(s)\n", "");
122         return RET_FAIL;
123     } else
124         pr("%d ship%s\n", nships, splur(nships));
125     return RET_OK;
126 }