]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/trad.c
Update copyright notice
[empserver] / src / lib / commands / trad.c
index abd8fc6f58ad1a686003011cd6428f9f07be010f..74fff15de85c5e623c88e7658fcb686e5aa0f5a2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
 #include <config.h>
 
 #include <ctype.h>
-#include "misc.h"
-#include "sect.h"
-#include "nat.h"
+#include "commands.h"
+#include "commodity.h"
+#include "empobj.h"
+#include "land.h"
+#include "loan.h"
 #include "news.h"
-#include "ship.h"
 #include "nuke.h"
-#include "land.h"
+#include "optlist.h"
 #include "plane.h"
+#include "ship.h"
 #include "trade.h"
-#include "xy.h"
-#include "nsc.h"
-#include "file.h"
-#include "player.h"
-#include "commodity.h"
-#include "loan.h"
-#include "commands.h"
-#include "optlist.h"
 
 /*
  * format: trade
@@ -71,7 +65,7 @@ trad(void)
     struct nstr_item ni;
     struct trdstr trade;
     struct trdstr tmpt;
-    union trdgenstr tg;
+    union empobj_storage tg;
     int plflags;
     double canspend;
     time_t now;
@@ -79,8 +73,7 @@ trad(void)
     double tleft;
     double tally;
     int q;
-    s_char buf[1024];
-
+    char buf[1024];
 
     if (!opt_MARKET) {
        pr("The market is disabled.\n");
@@ -115,8 +108,8 @@ trad(void)
            TRADE_DELAY / 3600.0 - (now - trade.trd_markettime) / 3600.0;
        if (tleft < 0.0)
            tleft = 0.0;
-       pr("$%7ld  %2d %5.2f hrs ", trade.trd_price,
-          trade.trd_maxbidder, tleft);
+       pr("$%7ld  %2d %5.2f hrs ",
+          trade.trd_price, trade.trd_maxbidder, tleft);
        (void)trade_desc(&trade, &tg);  /* XXX */
        pr("\n");
        if (trade.trd_owner == player->cnum && !player->god)
@@ -155,13 +148,6 @@ trad(void)
     }
     switch (trade.trd_type) {
     case EF_NUKE:
-       /*
-          if (!getsect(tg.gen.trg_x, tg.gen.trg_y, &sect)) {
-          return RET_FAIL;
-          }
-          trade.trd_owner = sect.sct_own;
-          break;
-        */
     case EF_PLANE:
     case EF_SHIP:
     case EF_LAND:
@@ -201,8 +187,8 @@ trad(void)
      */
     if (((trade.trd_type == EF_PLANE) || (trade.trd_type == EF_NUKE))
        && ((trade.trd_type == EF_NUKE) ||
-           !(tg.pln.pln_flags & PLN_LAUNCHED))) {
-       plflags = plchr[(int)tg.pln.pln_type].pl_flags;
+           !(tg.plane.pln_flags & PLN_LAUNCHED))) {
+       plflags = plchr[(int)tg.plane.pln_type].pl_flags;
        while (1) {
            p = getstring("Destination sector: ", buf);
            if (!trade_check_ok(&trade, &tg))
@@ -300,10 +286,8 @@ check_trade(void)
     struct lndstr land;
     struct natstr *natp;
     struct trdstr trade;
-    union trdgenstr tg;
+    union empobj_storage tg;
     time_t now;
-    double subleft;
-    double monleft;
     double tleft;
     float price;
     int saveid;
@@ -344,112 +328,98 @@ check_trade(void)
            continue;
        }
 
-       monleft = 0;
        price = trade.trd_price;
        natp = getnatp(trade.trd_maxbidder);
-       if (natp->nat_money <= 0)
-           monleft = price;
-       if (natp->nat_money < price && natp->nat_money > 0) {
-           monleft = price - (natp->nat_money - 1);
-           natp->nat_money = 1;
-           price = price - monleft;
-       } else if (natp->nat_money > 0) {
-           monleft = 0;
-           natp->nat_money -= price;
-       }
-
-       subleft = monleft;
-
-       if (monleft > 0) {
+       if (natp->nat_money < price) {
            nreport(trade.trd_maxbidder, N_WELCH_DEAL, trade.trd_owner, 1);
            wu(0, trade.trd_owner,
               "%s tried to buy a %s #%d from you for $%.2f\n",
               cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
-              saveid, (float)(price * tradetax));
+              saveid, price * tradetax);
            wu(0, trade.trd_owner, "   but couldn't afford it.\n");
            wu(0, trade.trd_owner,
               "   Your item was taken off the market.\n");
            wu(0, trade.trd_maxbidder,
               "You tried to buy %s #%d from %s for $%.2f\n",
               trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
-              (float)(price * tradetax));
+              price);
            wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
            continue;
        }
 
 /* If we get this far, the sale will go through. */
-/* Only pay tax on the part you actually get cash for.  As a break,
-   we don't tax the part you have to give a loan on. */
 
+       natp->nat_money -= price;
        putnat(natp);
+
        natp = getnatp(trade.trd_owner);
-       /* Make sure we subtract the extra amount */
-       natp->nat_money += (roundavg(price * tradetax) - subleft);
+       natp->nat_money += roundavg(price * tradetax);
        putnat(natp);
+
        switch (trade.trd_type) {
        case EF_NUKE:
-           tg.nuk.nuk_x = trade.trd_x;
-           tg.nuk.nuk_y = trade.trd_y;
-           makelost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid, tg.nuk.nuk_x,
-                    tg.nuk.nuk_y);
-           tg.nuk.nuk_own = trade.trd_maxbidder;
-           makenotlost(EF_NUKE, tg.nuk.nuk_own, tg.nuk.nuk_uid,
-                       tg.nuk.nuk_x, tg.nuk.nuk_y);
+           tg.nuke.nuk_x = trade.trd_x;
+           tg.nuke.nuk_y = trade.trd_y;
+           makelost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
+                    tg.nuke.nuk_x, tg.nuke.nuk_y);
+           tg.nuke.nuk_own = trade.trd_maxbidder;
+           makenotlost(EF_NUKE, tg.nuke.nuk_own, tg.nuke.nuk_uid,
+                       tg.nuke.nuk_x, tg.nuke.nuk_y);
            break;
        case EF_PLANE:
-           if ((tg.pln.pln_flags & PLN_LAUNCHED) == 0) {
-               tg.pln.pln_x = trade.trd_x;
-               tg.pln.pln_y = trade.trd_y;
+           if ((tg.plane.pln_flags & PLN_LAUNCHED) == 0) {
+               tg.plane.pln_x = trade.trd_x;
+               tg.plane.pln_y = trade.trd_y;
            }
-           makelost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
-                    tg.pln.pln_x, tg.pln.pln_y);
-           tg.pln.pln_own = trade.trd_maxbidder;
-           makenotlost(EF_PLANE, tg.pln.pln_own, tg.pln.pln_uid,
-                       tg.pln.pln_x, tg.pln.pln_y);
-           tg.pln.pln_wing = ' ';
+           makelost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
+                    tg.plane.pln_x, tg.plane.pln_y);
+           tg.plane.pln_own = trade.trd_maxbidder;
+           makenotlost(EF_PLANE, tg.plane.pln_own, tg.plane.pln_uid,
+                       tg.plane.pln_x, tg.plane.pln_y);
+           tg.plane.pln_wing = 0;
            /* no cheap version of fly */
            if (opt_MOB_ACCESS) {
-               tg.pln.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
+               tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
+               game_tick_to_now(&tg.plane.pln_access);
            } else {
-               tg.pln.pln_mobil = 0;
+               tg.plane.pln_mobil = 0;
            }
-           tg.pln.pln_mission = 0;
-           tg.pln.pln_harden = 0;
-           time(&tg.pln.pln_access);
-           tg.pln.pln_ship = -1;
-           tg.pln.pln_land = -1;
+           tg.plane.pln_mission = 0;
+           tg.plane.pln_harden = 0;
+           tg.plane.pln_ship = -1;
+           tg.plane.pln_land = -1;
            break;
        case EF_SHIP:
-           takeover_ship(&tg.shp, trade.trd_maxbidder, 0);
+           takeover_ship(&tg.ship, trade.trd_maxbidder, 0);
            break;
        case EF_LAND:
-           tg.lnd.lnd_x = trade.trd_x;
-           tg.lnd.lnd_y = trade.trd_y;
-           if (tg.lnd.lnd_ship >= 0) {
+           tg.land.lnd_x = trade.trd_x;
+           tg.land.lnd_y = trade.trd_y;
+           if (tg.land.lnd_ship >= 0) {
                struct shpstr ship;
-               getship(tg.lnd.lnd_ship, &ship);
+               getship(tg.land.lnd_ship, &ship);
                ship.shp_nland--;
                putship(ship.shp_uid, &ship);
            }
-           makelost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid, tg.lnd.lnd_x,
-                    tg.lnd.lnd_y);
-           tg.lnd.lnd_own = trade.trd_maxbidder;
-           makenotlost(EF_LAND, tg.lnd.lnd_own, tg.lnd.lnd_uid,
-                       tg.lnd.lnd_x, tg.lnd.lnd_y);
-           tg.lnd.lnd_army = ' ';
+           makelost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
+                    tg.land.lnd_x, tg.land.lnd_y);
+           tg.land.lnd_own = trade.trd_maxbidder;
+           makenotlost(EF_LAND, tg.land.lnd_own, tg.land.lnd_uid,
+                       tg.land.lnd_x, tg.land.lnd_y);
+           tg.land.lnd_army = 0;
            /* no cheap version of fly */
            if (opt_MOB_ACCESS) {
-               tg.lnd.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
+               tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
+               game_tick_to_now(&tg.land.lnd_access);
            } else {
-               tg.lnd.lnd_mobil = 0;
+               tg.land.lnd_mobil = 0;
            }
-           tg.lnd.lnd_harden = 0;
-           time(&tg.lnd.lnd_access);
-           tg.lnd.lnd_mission = 0;
+           tg.land.lnd_harden = 0;
+           tg.land.lnd_mission = 0;
            /* Drop any land units this unit was carrying */
-           snxtitem_xy(&ni, EF_LAND, tg.lnd.lnd_x, tg.lnd.lnd_y);
+           snxtitem_xy(&ni, EF_LAND, tg.land.lnd_x, tg.land.lnd_y);
            while (nxtitem(&ni, &land)) {
-               if (land.lnd_land != tg.lnd.lnd_uid)
+               if (land.lnd_land != tg.land.lnd_uid)
                    continue;
                land.lnd_land = -1;
                wu(0, land.lnd_own, "unit #%d dropped in %s\n",
@@ -458,7 +428,7 @@ check_trade(void)
                putland(land.lnd_uid, &land);
            }
            /* Drop any planes this unit was carrying */
-           snxtitem_xy(&ni, EF_PLANE, tg.lnd.lnd_x, tg.lnd.lnd_y);
+           snxtitem_xy(&ni, EF_PLANE, tg.land.lnd_x, tg.land.lnd_y);
            while (nxtitem(&ni, &plane)) {
                if (plane.pln_flags & PLN_LAUNCHED)
                    continue;
@@ -470,8 +440,8 @@ check_trade(void)
                   xyas(plane.pln_x, plane.pln_y, plane.pln_own));
                putplane(plane.pln_uid, &plane);
            }
-           tg.lnd.lnd_ship = -1;
-           tg.lnd.lnd_land = -1;
+           tg.land.lnd_ship = -1;
+           tg.land.lnd_land = -1;
            break;
        default:
            logerror("Bad trade type %d in trade\n", trade.trd_type);
@@ -484,22 +454,21 @@ check_trade(void)
        nreport(trade.trd_owner, N_MAKE_SALE, trade.trd_maxbidder, 1);
        wu(0, trade.trd_owner, "%s bought a %s #%d from you for $%.2f\n",
           cname(trade.trd_maxbidder), trade_nameof(&trade, &tg),
-          saveid, (float)(price * tradetax));
+          saveid, price * tradetax);
        wu(0, trade.trd_maxbidder,
           "The bidding is over & you bought %s #%d from %s for $%.2f\n",
           trade_nameof(&trade, &tg), saveid, cname(trade.trd_owner),
-          (float)price);
+          price);
     }
 /*    logerror("Done checking the trades.\n");*/
     return RET_OK;
 }
 
 int
-ontradingblock(int type, int *ptr)
-         /* Generic pointer */
+ontradingblock(int type, void *ptr)
 {
     struct trdstr trade;
-    union trdgenstr tg;
+    union empobj_storage tg;
     int n;
 
     for (n = 0; gettrade(n, &trade); n++) {
@@ -509,18 +478,17 @@ ontradingblock(int type, int *ptr)
            continue;
        if (trade.trd_type != type)
            continue;
-       if (tg.gen.uid == ((struct genitem *)ptr)->uid)
+       if (tg.gen.uid == ((struct empobj *)ptr)->uid)
            return 1;
     }
     return 0;
 }
 
 void
-trdswitchown(int type, int *ptr, int newown)
-         /* Generic pointer */
+trdswitchown(int type, void *ptr, int newown)
 {
     struct trdstr trade;
-    union trdgenstr tg;
+    union empobj_storage tg;
     int n;
 
     for (n = 0; gettrade(n, &trade); n++) {
@@ -530,7 +498,7 @@ trdswitchown(int type, int *ptr, int newown)
            continue;
        if (trade.trd_type != type)
            continue;
-       if (tg.gen.uid != ((struct genitem *)ptr)->uid)
+       if (tg.gen.uid != ((struct empobj *)ptr)->uid)
            continue;
        if (trade.trd_owner == trade.trd_maxbidder)
            trade.trd_maxbidder = newown;