]> git.pond.sub.org Git - empserver/blob - src/lib/commands/trad.c
Distinguish between planes "in orbit" and "launched"
[empserver] / src / lib / commands / trad.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  trad.c: Buy units/ships/planes/nukes from other nations.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Pat Loney, 1992
33  *     Steve McClure, 1996-2000
34  */
35
36 #include <config.h>
37
38 #include <ctype.h>
39 #include "commands.h"
40 #include "commodity.h"
41 #include "empobj.h"
42 #include "land.h"
43 #include "loan.h"
44 #include "news.h"
45 #include "nuke.h"
46 #include "optlist.h"
47 #include "plane.h"
48 #include "ship.h"
49 #include "trade.h"
50
51 /*
52  * format: trade
53  */
54 int
55 trad(void)
56 {
57     struct sctstr sect;
58     struct natstr *natp;
59     struct comstr comt;
60     int lotno;
61     float price;
62     coord sx, sy;
63     int n;
64     char *p;
65     struct nstr_item ni;
66     struct trdstr trade;
67     struct trdstr tmpt;
68     union empobj_storage tg;
69     double canspend;
70     time_t now;
71     int bid;
72     double tleft;
73     double tally;
74     int q;
75     char buf[1024];
76
77     if (!opt_MARKET) {
78         pr("The market is disabled.\n");
79         return RET_FAIL;
80     }
81     /* First, we execute all trades, so that we can only buy what is available. */
82     check_market();
83     check_trade();
84
85     pr("\n     Empire Trade Report\n  ");
86     prdate();
87     n = 0;
88     pr(" lot high bid  by time left owner  description\n");
89     pr(" --- --------  -- --------- -----  -------------------------\n");
90
91     snxtitem_all(&ni, EF_TRADE);
92     while (nxtitem(&ni, &trade)) {
93         if (trade.trd_owner == 0)
94             continue;
95         if (!trade_getitem(&trade, &tg)) {
96             continue;
97         };
98         /* fix up database if things get weird */
99         /*if (trade.trd_owner != tg.gen.own) {
100            trade.trd_unitid = -1;
101            (void) puttrade(ni.cur, &trade);
102            continue;
103            } */
104         pr(" %3d ", ni.cur);
105         (void)time(&now);
106         tleft =
107             TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
108         if (tleft < 0.0)
109             tleft = 0.0;
110         pr("$%7ld  %2d %5.2f hrs ",
111            trade.trd_price, trade.trd_maxbidder, tleft);
112         (void)trade_desc(&trade, &tg);  /* XXX */
113         pr("\n");
114         if (trade.trd_owner == player->cnum && !player->god)
115             pr(" (your own lot)\n");
116         n++;
117     }
118     if (n == 0) {
119         pr("Nothing to buy at the moment...\n");
120         return RET_OK;
121     }
122     if ((p = getstring("Which lot to buy: ", buf)) == 0 || *p == 0)
123         return RET_OK;
124     if (isdigit(*p) == 0)
125         return RET_OK;
126     lotno = atoi(p);
127     if (lotno < 0 || lotno >= ni.cur) {
128         pr("Bad lot number\n");
129         return RET_OK;
130     }
131     if (!gettrade(lotno, &trade)) {
132         pr("No such lot number\n");
133         return RET_OK;
134     }
135     if (trade.trd_unitid < 0) {
136         pr("Invalid lot number.\n");
137         return RET_OK;
138     }
139     if (!trade_getitem(&trade, &tg)) {
140         pr("Can't find trade #%d!\n", trade.trd_unitid);
141         trade.trd_unitid = -1;
142         if (!puttrade(lotno, &trade)) {
143             pr("Couldn't save after getitem failed; get help!\n");
144             return RET_SYS;
145         }
146         return RET_OK;
147     }
148     switch (trade.trd_type) {
149     case EF_NUKE:
150     case EF_PLANE:
151     case EF_SHIP:
152     case EF_LAND:
153         break;
154     default:
155         pr("Bad unit type on lot number %d\n", lotno);
156         return RET_FAIL;
157     }
158     if (trade.trd_owner == player->cnum) {
159         pr("You can't buy from yourself!\n");
160         return RET_OK;
161     }
162     price = trade.trd_price;
163     natp = getnatp(player->cnum);
164     if (natp->nat_money < price) {
165         pr("You don't have %.2f to spend!\n", price);
166         return RET_OK;
167     }
168     tally = 0.0;
169     for (q = 0; gettrade(q, &tmpt); q++) {
170         if (tmpt.trd_maxbidder == player->cnum &&
171             tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) {
172             tally += tmpt.trd_price * tradetax;
173         }
174     }
175     for (q = 0; getcomm(q, &comt); q++) {
176         if (comt.com_maxbidder == player->cnum &&
177             comt.com_owner != 0 && comt.com_owner != player->cnum) {
178             tally += (comt.com_price * comt.com_amount) * buytax;
179         }
180     }
181     canspend = natp->nat_money - tally;
182     /*
183      * Find the destination sector for the plane before the trade is
184      * actually made, except for satellites in orbit.  Must be owned
185      * and must be a 60% airfield (except for VTOL planes).
186      */
187     if (((trade.trd_type == EF_PLANE) && !pln_is_in_orbit(&tg.plane))
188         || (trade.trd_type == EF_NUKE)) {
189         while (1) {
190             p = getstring("Destination sector: ", buf);
191             if (!trade_check_ok(&trade, &tg))
192                 return RET_FAIL;
193             if (p == 0) {
194                 return RET_FAIL;
195             }
196             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
197                 pr("Bad sector designation; try again!\n");
198                 continue;
199             }
200             if (!player->owner) {
201                 pr("You don't own that sector; try again!\n");
202                 continue;
203             }
204             if (!(plchr[tg.plane.pln_type].pl_flags & P_V)) {
205                 if (!player->god && (sect.sct_type != SCT_AIRPT)) {
206                     pr("Destination sector is not an airfield!\n");
207                     continue;
208                 }
209                 if (!player->god && (sect.sct_effic < 60)) {
210                     pr("That airport still under construction!\n");
211                     continue;
212                 }
213             }
214             break;
215         }
216     }
217     if (trade.trd_type == EF_LAND) {
218         while (1) {
219             p = getstring("Destination sector: ", buf);
220             if (!trade_check_ok(&trade, &tg))
221                 return RET_FAIL;
222             if (p == 0) {
223                 return RET_FAIL;
224             }
225             if (!sarg_xy(p, &sx, &sy) || !getsect(sx, sy, &sect)) {
226                 pr("Bad sector designation; try again!\n");
227                 continue;
228             }
229             if (!player->owner) {
230                 pr("You don't own that sector; try again!\n");
231                 continue;
232             }
233             if (!player->god && (sect.sct_type != SCT_HEADQ)) {
234                 pr("Destination sector is not a headquarters!\n");
235                 continue;
236             }
237             if (!player->god && (sect.sct_effic < 60)) {
238                 pr("That headquarters still under construction!\n");
239                 continue;
240             }
241             break;
242         }
243     }
244
245     if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
246         return RET_OK;
247     if (!trade_check_ok(&trade, &tg))
248         return RET_FAIL;
249     bid = atoi(p);
250     if (bid < price)
251         bid = price;
252     if (bid > canspend) {
253         pr("You don't have %.2f to spend!\n", price);
254         return RET_OK;
255     }
256     if (bid > trade.trd_price) {
257         /* Add five minutes to the time if less than 5 minutes left. */
258         time(&now);
259         if (((TRADE_DELAY - (now - trade.trd_markettime)) < 300) &&
260             trade.trd_maxbidder != player->cnum)
261             trade.trd_markettime += 300;
262         trade.trd_price = bid;
263         trade.trd_maxbidder = player->cnum;
264         trade.trd_x = sx;
265         trade.trd_y = sy;
266         pr("Your bid on lot #%d is being considered.\n", lotno);
267         if (!puttrade(lotno, &trade))
268             pr("Problems with the trade file.  Get help\n");
269     } else
270         pr("Your bid wasn't high enough (you need to bid more than someone else.)\n");
271
272     check_trade();
273
274     return RET_OK;
275 }
276
277 int
278 check_trade(void)
279 {
280     int n;
281     struct nstr_item ni;
282     struct plnstr plane;
283     struct lndstr land;
284     struct natstr *natp;
285     struct trdstr trade;
286     union empobj_storage tg;
287     time_t now;
288     double tleft;
289     float price;
290     int saveid;
291
292 /*    logerror("Checking the trades.\n");*/
293     for (n = 0; gettrade(n, &trade); n++) {
294         if (trade.trd_unitid < 0)
295             continue;
296         if (!trade_getitem(&trade, &tg))
297             continue;
298         if (tg.gen.own == 0) {
299             trade.trd_unitid = -1;
300             puttrade(n, &trade);
301             continue;
302         }
303         if (tg.gen.own != trade.trd_owner) {
304             logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
305             trade.trd_unitid = -1;
306             puttrade(n, &trade);
307             continue;
308         }
309
310         if (trade.trd_owner == trade.trd_maxbidder)
311             continue;
312
313         (void)time(&now);
314         tleft =
315             TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
316         if (tleft < 0.0)
317             tleft = 0.0;
318         if (tleft > 0.0)
319             continue;
320
321         saveid = trade.trd_unitid;
322         trade.trd_unitid = -1;
323         if (!puttrade(n, &trade)) {
324             logerror("Couldn't save trade after purchase; get help!\n");
325             continue;
326         }
327
328         price = trade.trd_price;
329         natp = getnatp(trade.trd_maxbidder);
330         if (natp->nat_money < price) {
331             nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
332             wu(0, trade.trd_owner,
333                "%s tried to buy a %s #%d from you for $%.2f\n",
334                cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
335                saveid, price * tradetax);
336             wu(0, trade.trd_owner, "   but couldn't afford it.\n");
337             wu(0, trade.trd_owner,
338                "   Your item was taken off the market.\n");
339             wu(0, trade.trd_maxbidder,
340                "You tried to buy %s #%d from %s for $%.2f\n",
341                trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
342                price);
343             wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
344             continue;
345         }
346
347 /* If we get this far, the sale will go through. */
348
349         natp->nat_money -= price;
350         putnat(natp);
351
352         natp = getnatp(trade.trd_owner);
353         natp->nat_money += roundavg(price * tradetax);
354         putnat(natp);
355
356         switch (trade.trd_type) {
357         case EF_NUKE:
358             tg.nuke.nuk_x = trade.trd_x;
359             tg.nuke.nuk_y = trade.trd_y;
360             makelost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
361                      tg.nuke.nuk_x, tg.nuke.nuk_y);
362             tg.nuke.nuk_own = trade.trd_maxbidder;
363             makenotlost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
364                         tg.nuke.nuk_x, tg.nuke.nuk_y);
365             break;
366         case EF_PLANE:
367             if (!pln_is_in_orbit(&tg.plane)) {
368                 tg.plane.pln_x = trade.trd_x;
369                 tg.plane.pln_y = trade.trd_y;
370             }
371             makelost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
372                      tg.plane.pln_x, tg.plane.pln_y);
373             tg.plane.pln_own = trade.trd_maxbidder;
374             makenotlost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
375                         tg.plane.pln_x, tg.plane.pln_y);
376             tg.plane.pln_wing = 0;
377             /* no cheap version of fly */
378             if (opt_MOB_ACCESS) {
379                 tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
380                 game_tick_to_now(&tg.plane.pln_access);
381             } else {
382                 tg.plane.pln_mobil = 0;
383             }
384             tg.plane.pln_mission = 0;
385             tg.plane.pln_harden = 0;
386             tg.plane.pln_ship = -1;
387             tg.plane.pln_land = -1;
388             break;
389         case EF_SHIP:
390             takeover_ship(&tg.ship, trade.trd_maxbidder, 0);
391             break;
392         case EF_LAND:
393             tg.land.lnd_x = trade.trd_x;
394             tg.land.lnd_y = trade.trd_y;
395             if (tg.land.lnd_ship >= 0) {
396                 struct shpstr ship;
397                 getship(tg.land.lnd_ship, &ship);
398                 ship.shp_nland--;
399                 putship(ship.shp_uid, &ship);
400             }
401             makelost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
402                      tg.land.lnd_x, tg.land.lnd_y);
403             tg.land.lnd_own = trade.trd_maxbidder;
404             makenotlost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
405                         tg.land.lnd_x, tg.land.lnd_y);
406             tg.land.lnd_army = 0;
407             /* no cheap version of fly */
408             if (opt_MOB_ACCESS) {
409                 tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
410                 game_tick_to_now(&tg.land.lnd_access);
411             } else {
412                 tg.land.lnd_mobil = 0;
413             }
414             tg.land.lnd_harden = 0;
415             tg.land.lnd_mission = 0;
416             /* Drop any land units this unit was carrying */
417             snxtitem_xy(&ni, EF_LAND, tg.land.lnd_x, tg.land.lnd_y);
418             while (nxtitem(&ni, &land)) {
419                 if (land.lnd_land != tg.land.lnd_uid)
420                     continue;
421                 land.lnd_land = -1;
422                 wu(0, land.lnd_own, "unit #%d dropped in %s\n",
423                    land.lnd_uid,
424                    xyas(land.lnd_x, land.lnd_y, land.lnd_own));
425                 putland(land.lnd_uid, &land);
426             }
427             /* Drop any planes this unit was carrying */
428             snxtitem_xy(&ni, EF_PLANE, tg.land.lnd_x, tg.land.lnd_y);
429             while (nxtitem(&ni, &plane)) {
430                 if (plane.pln_flags & PLN_LAUNCHED)
431                     continue;
432                 if (plane.pln_land != land.lnd_uid)
433                     continue;
434                 plane.pln_land = -1;
435                 wu(0, plane.pln_own, "plane #%d dropped in %s\n",
436                    plane.pln_uid,
437                    xyas(plane.pln_x, plane.pln_y, plane.pln_own));
438                 putplane(plane.pln_uid, &plane);
439             }
440             tg.land.lnd_ship = -1;
441             tg.land.lnd_land = -1;
442             break;
443         default:
444             logerror("Bad trade type %d in trade\n", trade.trd_type);
445             break;
446         }
447         if (!ef_write(trade.trd_type, saveid, &tg)) {
448             logerror("Couldn't write unit to disk; seek help.\n");
449             continue;
450         }
451         nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
452         wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
453            cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
454            saveid, price * tradetax);
455         wu(0, trade.trd_maxbidder,
456            "The bidding is over & you bought %s #%d from %s for $%.2f\n",
457            trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
458            price);
459     }
460 /*    logerror("Done checking the trades.\n");*/
461     return RET_OK;
462 }
463
464 int
465 ontradingblock(int type, void *ptr)
466 {
467     struct trdstr trade;
468     union empobj_storage tg;
469     int n;
470
471     for (n = 0; gettrade(n, &trade); n++) {
472         if (trade.trd_unitid < 0)
473             continue;
474         if (!trade_getitem(&trade, &tg))
475             continue;
476         if (trade.trd_type != type)
477             continue;
478         if (tg.gen.uid == ((struct empobj *)ptr)->uid)
479             return 1;
480     }
481     return 0;
482 }
483
484 void
485 trdswitchown(int type, void *ptr, int newown)
486 {
487     struct trdstr trade;
488     union empobj_storage tg;
489     int n;
490
491     for (n = 0; gettrade(n, &trade); n++) {
492         if (trade.trd_unitid < 0)
493             continue;
494         if (!trade_getitem(&trade, &tg))
495             continue;
496         if (trade.trd_type != type)
497             continue;
498         if (tg.gen.uid != ((struct empobj *)ptr)->uid)
499             continue;
500         if (trade.trd_owner == trade.trd_maxbidder)
501             trade.trd_maxbidder = newown;
502         trade.trd_owner = newown;
503         if (newown == 0)
504             trade.trd_unitid = -1;
505         puttrade(n, &trade);
506         return;
507     }
508 }