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