(trad, set): Treat trade file slots with zero owner as unused, so that

freshly extended trade file makes sense.  When set() removes a lot,
simply zero the slot.

(set): Initialize trd_uid.

(trade_check_ok): Use check_land_ok() & friends instead of doing it by
hand, but wrong.  This fixes bying planes and land units loaded on
ships that moved after loading.

(check_trade_ok): New.
(trade_check_ok): Use it.  Remove unused argument lot.  Callers
changed.
This commit is contained in:
Markus Armbruster 2004-08-28 15:22:50 +00:00
parent 133c1754f9
commit e16e38dfab
6 changed files with 52 additions and 47 deletions

View file

@ -111,6 +111,7 @@ extern int check_nuke_ok(struct nukstr *);
extern int check_plane_ok(struct plnstr *);
extern int check_comm_ok(struct comstr *);
extern int check_loan_ok(struct lonstr *);
extern int check_trade_ok(struct trdstr *);
/* fsize.c */
extern int fsize(int);
extern int blksize(int);

View file

@ -73,7 +73,7 @@ union trdgenstr {
struct shpstr shp;
};
extern int trade_check_ok(int, struct trdstr *, union trdgenstr *);
extern int trade_check_ok(struct trdstr *, union trdgenstr *);
extern s_char *trade_nameof(struct trdstr *, union trdgenstr *);
extern int trade_desc(struct trdstr *, union trdgenstr *);
extern int trade_getitem(struct trdstr *, union trdgenstr *);

View file

@ -116,34 +116,21 @@ set(void)
freeslot = -1;
snxtitem_all(&ni_trade, EF_TRADE);
while (nxtitem(&ni_trade, (char *)&trade)) {
if (trade.trd_unitid < 0)
if (trade.trd_owner == 0)
freeslot = ni_trade.cur;
if (trade.trd_unitid == ni.cur && trade.trd_type == type) {
foundslot = ni_trade.cur;
break;
}
}
if (price == 0 && foundslot >= 0) {
if (price <= 0) {
if (foundslot >= 0) {
pr("%s #%d (lot #%d) removed from trading\n",
trade_nameof(&trade, &item), ni.cur, foundslot);
trade.trd_type = 0;
trade.trd_owner = 0;
trade.trd_unitid = -1;
trade.trd_price = 0;
(void)time(&now);
trade.trd_markettime = now;
trade.trd_maxbidder = player->cnum;
memset(&trade, 0, sizeof(trade));
puttrade(ni_trade.cur, &trade);
} else if (price > 0) {
trade.trd_x = item.gen.trg_x;
trade.trd_y = item.gen.trg_x;
trade.trd_type = type;
trade.trd_owner = player->cnum;
trade.trd_unitid = ni.cur;
trade.trd_price = price;
(void)time(&now);
trade.trd_markettime = now;
trade.trd_maxbidder = player->cnum;
}
} else {
if (foundslot >= 0)
id = foundslot;
else if (freeslot >= 0)
@ -152,6 +139,16 @@ set(void)
ef_extend(EF_TRADE, 1);
id = ni_trade.cur;
}
trade.trd_x = item.gen.trg_x;
trade.trd_y = item.gen.trg_x;
trade.trd_type = type;
trade.trd_owner = player->cnum;
trade.trd_uid = id;
trade.trd_unitid = ni.cur;
trade.trd_price = price;
(void)time(&now);
trade.trd_markettime = now;
trade.trd_maxbidder = player->cnum;
puttrade(id, &trade);
pr("%s #%d (lot #%d) price %s to $%d\n",
trade_nameof(&trade, &item), ni.cur,

View file

@ -98,7 +98,7 @@ trad(void)
snxtitem_all(&ni, EF_TRADE);
while (nxtitem(&ni, (char *)&trade)) {
if (trade.trd_unitid < 0)
if (trade.trd_owner == 0)
continue;
if (!trade_getitem(&trade, &tg)) {
continue;
@ -205,7 +205,7 @@ trad(void)
plflags = plchr[(int)tg.pln.pln_type].pl_flags;
while (1) {
p = getstring("Destination sector: ", buf);
if (!trade_check_ok(lotno, &trade, &tg))
if (!trade_check_ok(&trade, &tg))
return RET_FAIL;
if (p == 0) {
return RET_FAIL;
@ -234,7 +234,7 @@ trad(void)
if (trade.trd_type == EF_LAND) {
while (1) {
p = getstring("Destination sector: ", buf);
if (!trade_check_ok(lotno, &trade, &tg))
if (!trade_check_ok(&trade, &tg))
return RET_FAIL;
if (p == 0) {
return RET_FAIL;
@ -266,7 +266,7 @@ trad(void)
if ((p = getstring("How much do you bid: ", buf)) == 0 || *p == 0)
return RET_OK;
if (!trade_check_ok(lotno, &trade, &tg))
if (!trade_check_ok(&trade, &tg))
return RET_FAIL;
bid = atoi(p);
if (bid < price)

View file

@ -161,3 +161,16 @@ check_comm_ok(struct comstr *commp)
}
return 1;
}
int
check_trade_ok(struct trdstr *tp)
{
struct trdstr chktrade;
gettrade(tp->trd_uid, &chktrade);
if (memcmp(tp, &chktrade, sizeof(struct trdstr))) {
pr("Trade lot #%d has changed!\n", tp->trd_uid);
return 0;
}
return 1;
}

View file

@ -52,32 +52,26 @@
#include "optlist.h"
int
trade_check_ok(int lot, struct trdstr *tp, union trdgenstr *tgp)
trade_check_ok(struct trdstr *tp, union trdgenstr *tgp)
{
union trdgenstr check;
struct trdstr trade;
int result = 0;
if (!check_trade_ok(tp))
return 0;
trade_getitem(tp, &check);
if (tp->trd_type == EF_LAND)
result = memcmp(&(check.lnd), &(tgp->lnd), sizeof(struct lndstr));
else if (tp->trd_type == EF_PLANE)
result = memcmp(&(check.pln), &(tgp->pln), sizeof(struct plnstr));
else if (tp->trd_type == EF_SHIP)
result = memcmp(&(check.shp), &(tgp->shp), sizeof(struct shpstr));
else
result = memcmp(&(check.nuk), &(tgp->nuk), sizeof(struct nukstr));
if (result) {
pr("That item has changed!\n");
return check_land_ok(&tgp->lnd);
if (tp->trd_type == EF_PLANE)
return check_plane_ok(&tgp->pln);
if (tp->trd_type == EF_SHIP)
return check_ship_ok(&tgp->shp);
if (tp->trd_type == EF_NUKE)
return check_nuke_ok(&tgp->nuk);
CANT_HAPPEN("Bad TRD_TYPE");
pr("Trade lot #%d went bad!\n", tp->trd_uid);
return 0;
}
gettrade(lot, &trade);
if (memcmp((s_char *)&trade, (s_char *)tp, sizeof(struct trdstr))) {
pr("That item has changed!\n");
return 0;
}
return 1;
}
s_char *
trade_nameof(struct trdstr *tp, union trdgenstr *tgp)