]> git.pond.sub.org Git - empserver/blob - src/lib/commands/mark.c
fd265ea9021c095f24c84078214a8c8f9171c9dd
[empserver] / src / lib / commands / mark.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  *  mark.c: Display report for commodities
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Pat Loney, 1992
32  *     Steve McClure, 1996
33  *     Markus Armbruster, 2004-2013
34  */
35
36 #include <config.h>
37
38 #include "commands.h"
39 #include "commodity.h"
40 #include "item.h"
41 #include "optlist.h"
42
43 int
44 mark(void)
45 {
46     char buf[1024];
47     char *p;
48     struct ichrstr *ip;
49
50     if (!opt_MARKET) {
51         pr("The market is disabled.\n");
52         return RET_FAIL;
53     }
54
55     if (player->argp[1]) {
56         p = getstarg(player->argp[1], "What commodity (or 'all')? ", buf);
57         if (!p)
58             return RET_SYN;
59         if (!strcmp(p, "all"))
60             return display_mark(I_NONE, 0);
61         else {
62             ip = item_by_name(p);
63             if (!ip)
64                 return RET_SYN;
65             return display_mark(ip->i_uid, 0);
66         }
67     }
68     return display_mark(I_NONE, 1);
69 }
70
71 static void
72 pr_mark(struct comstr *comm)
73 {
74     time_t now, tleft;
75
76     (void)time(&now);
77     tleft = comm->com_markettime + MARK_DELAY - now;
78     if (tleft < 0)
79         tleft = 0;
80     pr(" %3d  $%12.2f  %2d  %5.2f hrs  (%3d)   %c    %6d  ",
81        comm->com_uid, comm->com_price, comm->com_maxbidder, tleft / 3600.0,
82        comm->com_owner, ichr[comm->com_type].i_mnem, comm->com_amount);
83     if (comm->com_owner == player->cnum || player->god)
84         pr("%s", xyas(comm->sell_x, comm->sell_y, player->cnum));
85     pr("\n");
86 }
87
88 int
89 display_mark(i_type only_itype, int only_cheapest)
90 {
91     struct comstr comm;
92     struct comstr comm2;
93     int sellers = 0;
94     int cnt = 0;
95     int cheapest_items[I_MAX + 1];
96     i_type i;
97
98     /* Execute trades so report lists only lots that are still available.  */
99     check_market();
100     check_trade();
101
102     pr("\n     Empire Market Report\n   ");
103     prdate();
104     pr(" lot  high bid/unit  by  time left  owner  item  amount  sector\n");
105     pr(" ---  -------------  --  ---------  -----  ----  ------  ------\n");
106
107     if (only_cheapest) {
108         for (i = I_NONE + 1; i <= I_MAX; i++)
109             cheapest_items[i] = -1;
110         for (sellers = 0; getcomm(sellers, &comm); sellers++) {
111             if (comm.com_owner == 0)
112                 continue;
113             if (CANT_HAPPEN(comm.com_type <= I_NONE || comm.com_type > I_MAX))
114                 continue;
115             if (cheapest_items[comm.com_type] != -1) {
116                 getcomm(cheapest_items[comm.com_type], &comm2);
117                 if (comm.com_price < comm2.com_price) {
118                     cheapest_items[comm.com_type] = sellers;
119                 }
120             } else {
121                 cheapest_items[comm.com_type] = sellers;
122             }
123         }
124         CANT_HAPPEN(only_itype != I_NONE); /* not implemented */
125         for (i = I_NONE + 1; i <= I_MAX; i++) {
126             if (cheapest_items[i] == -1)
127                 continue;
128             getcomm(cheapest_items[i], &comm);
129             cnt = 1;
130             pr_mark(&comm);
131         }
132     } else {
133         for (sellers = 0; getcomm(sellers, &comm); sellers++) {
134             if (comm.com_owner == 0)
135                 continue;
136             if (only_itype != I_NONE && comm.com_type != only_itype)
137                 continue;
138             cnt = 1;
139             pr_mark(&comm);
140         }
141     }
142     if (cnt <= 0)
143         pr("\nHmmmm, the market seems to be empty today.\n");
144     else
145         pr("\nLooks just like Christmas at K-mart, doesn't it!\n");
146     return RET_OK;
147 }