]> git.pond.sub.org Git - empserver/blob - src/lib/commands/payo.c
73769a6fffdecd1c75cd8c62efb25b600e8ff3b9
[empserver] / src / lib / commands / payo.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, 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 (!snxtitem(&ni, EF_SHIP, player->argp[1], NULL))
51         return RET_SYN;
52
53     nships = 0;
54     while (nxtitem(&ni, &ship)) {
55         if (!player->owner || ship.shp_own == 0)
56             continue;
57         mp = &mchr[(int)ship.shp_type];
58
59         if (!(mp->m_flags & M_TRADE))
60             continue;
61
62         if (nships++ == 0) {
63             if (player->god)
64                 pr("own ");
65             pr("shp#     ship type  orig x,y       x,y    dist $$\n");
66         }
67         if (player->god)
68             pr("%3d ", ship.shp_own);
69         pr("%4d ", ni.cur);
70         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
71         if (ship.shp_own != ship.shp_orig_own && !player->god) {
72             /* Don't disclose construction site to pirates! */
73             pr("    ?     ");
74             prxy("%4d,%-4d ", ship.shp_x, ship.shp_y);
75             pr("   ? $  ?\n");
76             continue;
77         }
78         prxy("%4d,%-4d ", ship.shp_orig_x, ship.shp_orig_y);
79         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y);
80
81         getsect(ship.shp_x, ship.shp_y, &sect);
82
83         dist = mapdist(ship.shp_x, ship.shp_y,
84                        ship.shp_orig_x, ship.shp_orig_y);
85         pr("%4d ", dist);
86
87         if (dist < trade_1_dist)
88             cash = 0;
89         else if (dist < trade_2_dist)
90             cash = 1.0 + trade_1 * dist;
91         else if (dist < trade_3_dist)
92             cash = 1.0 + trade_2 * dist;
93         else
94             cash = 1.0 + trade_3 * dist;
95
96         cash *= mp->m_cost;
97         cash *= ship.shp_effic / 100.0;
98
99         if (sect.sct_own && (sect.sct_own != ship.shp_own))
100             cash *= (1.0 + trade_ally_bonus);
101         pr("$%6.2f\n", cash);
102     }
103     if (nships == 0) {
104         if (player->argp[1])
105             pr("%s: No ship(s)\n", player->argp[1]);
106         else
107             pr("%s: No ship(s)\n", "");
108         return RET_FAIL;
109     } else
110         pr("%d ship%s\n", nships, splur(nships));
111     return RET_OK;
112 }