(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_plane_ok(struct plnstr *);
extern int check_comm_ok(struct comstr *); extern int check_comm_ok(struct comstr *);
extern int check_loan_ok(struct lonstr *); extern int check_loan_ok(struct lonstr *);
extern int check_trade_ok(struct trdstr *);
/* fsize.c */ /* fsize.c */
extern int fsize(int); extern int fsize(int);
extern int blksize(int); extern int blksize(int);

View file

@ -73,7 +73,7 @@ union trdgenstr {
struct shpstr shp; 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 s_char *trade_nameof(struct trdstr *, union trdgenstr *);
extern int trade_desc(struct trdstr *, union trdgenstr *); extern int trade_desc(struct trdstr *, union trdgenstr *);
extern int trade_getitem(struct trdstr *, union trdgenstr *); extern int trade_getitem(struct trdstr *, union trdgenstr *);

View file

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

View file

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

View file

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