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