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