Exemplo n.º 1
0
int
check_trade(void)
{
    int n;
    struct natstr *natp;
    struct trdstr trade;
    union empobj_storage tg;
    time_t now;
    int price;
    int saveid;
    natid seller;

    for (n = 0; gettrade(n, &trade); n++) {
	if (trade.trd_unitid < 0)
	    continue;
	if (!trade_getitem(&trade, &tg))
	    continue;
	if (tg.gen.own == 0) {
	    trade.trd_owner = 0;
	    trade.trd_unitid = -1;
	    puttrade(n, &trade);
	    continue;
	}
	if (tg.gen.own != trade.trd_owner) {
	    logerror("Something weird, tg.gen.own != trade.trd_owner!\n");
	    trade.trd_owner = 0;
	    trade.trd_unitid = -1;
	    puttrade(n, &trade);
	    continue;
	}

	if (trade.trd_owner == trade.trd_maxbidder)
	    continue;

	(void)time(&now);
	if (trade.trd_markettime + TRADE_DELAY > now)
	    continue;

	saveid = trade.trd_unitid;
	seller = trade.trd_owner;
	trade.trd_owner = 0;
	trade.trd_unitid = -1;
	if (!puttrade(n, &trade)) {
	    logerror("Couldn't save trade after purchase; get help!\n");
	    continue;
	}

	price = trade.trd_price;
	natp = getnatp(trade.trd_maxbidder);
	if (natp->nat_money < price) {
	    nreport(trade.trd_maxbidder, N_WELCH_DEAL, seller, 1);
	    wu(0, seller,
	       "%s tried to buy a %s #%d from you for $%.2f\n",
	       cname(trade.trd_maxbidder), trade_nameof(&trade, &tg.gen),
	       saveid, price * tradetax);
	    wu(0, seller, "   but couldn't afford it.\n");
	    wu(0, seller,
	       "   Your item was taken off the market.\n");
	    wu(0, trade.trd_maxbidder,
	       "You tried to buy %s #%d from %s for $%d\n",
	       trade_nameof(&trade, &tg.gen), saveid, cname(seller),
	       price);
	    wu(0, trade.trd_maxbidder, "but couldn't afford it.\n");
	    continue;
	}

/* If we get this far, the sale will go through. */

	natp->nat_money -= price;
	putnat(natp);

	natp = getnatp(seller);
	natp->nat_money += roundavg(price * tradetax);
	putnat(natp);

	switch (trade.trd_type) {
	case EF_NUKE:
	    tg.nuke.nuk_x = trade.trd_x;
	    tg.nuke.nuk_y = trade.trd_y;
	    tg.nuke.nuk_plane = -1;
	    break;
	case EF_PLANE:
	    if (!pln_is_in_orbit(&tg.plane)) {
		tg.plane.pln_x = trade.trd_x;
		tg.plane.pln_y = trade.trd_y;
	    }
	    if (opt_MOB_ACCESS) {
		tg.plane.pln_mobil = -(etu_per_update / sect_mob_neg_factor);
		game_tick_to_now(&tg.plane.pln_access);
	    } else {
		tg.plane.pln_mobil = 0;
	    }
	    tg.plane.pln_harden = 0;
	    tg.plane.pln_ship = -1;
	    tg.plane.pln_land = -1;
	    break;
	case EF_SHIP:
	    break;
	case EF_LAND:
	    tg.land.lnd_x = trade.trd_x;
	    tg.land.lnd_y = trade.trd_y;
	    if (opt_MOB_ACCESS) {
		tg.land.lnd_mobil = -(etu_per_update / sect_mob_neg_factor);
		game_tick_to_now(&tg.land.lnd_access);
	    } else {
		tg.land.lnd_mobil = 0;
	    }
	    tg.land.lnd_harden = 0;
	    unit_drop_cargo(&tg.gen, 0);
	    tg.land.lnd_ship = -1;
	    tg.land.lnd_land = -1;
	    break;
	default:
	    logerror("Bad trade type %d in trade\n", trade.trd_type);
	    break;
	}
	unit_give_away(&tg.gen, trade.trd_maxbidder, 0);
	put_empobj(trade.trd_type, saveid, &tg.gen);

	nreport(seller, N_MAKE_SALE, trade.trd_maxbidder, 1);
	wu(0, seller, "%s bought %s #%d from you for $%.2f\n",
	   cname(trade.trd_maxbidder), trade_nameof(&trade, &tg.gen),
	   saveid, price * tradetax);
	wu(0, trade.trd_maxbidder,
	   "The bidding is over & you bought %s #%d from %s for $%d\n",
	   trade_nameof(&trade, &tg.gen), saveid, cname(seller),
	   price);
    }
    return RET_OK;
}
Exemplo n.º 2
0
int
scra(void)
{
    struct nstr_item ni;
    union empobj_storage item;
    int type, n;
    struct sctstr sect;
    struct mchrstr *mp;
    struct plchrstr *pp;
    struct lchrstr *lp;
    char *p;
    i_type i;
    char prompt[128];
    char buf[1024];
    float eff;
    short *mvec;
    int amt;

    if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
	return RET_SYN;
    switch (*p) {
    case 's':
	type = EF_SHIP;
	break;
    case 'p':
	type = EF_PLANE;
	break;
    case 'l':
	type = EF_LAND;
	break;
    default:
	pr("Ships, land units, or planes only! (s, l, p)\n");
	return RET_SYN;
    }

    if (!snxtitem(&ni, type, player->argp[2], NULL))
	return RET_SYN;
    n = 0;
    while (nxtitem(&ni, &item)) {
	if (!player->owner)
	    continue;
	n++;
    }
    snprintf(prompt, sizeof(prompt), "Really scrap %d %s%s [n]? ",
			    n, ef_nameof(type), splur(n));
    if (!confirm(prompt))
	return RET_FAIL;

    snxtitem_rewind(&ni);
    while (nxtitem(&ni, &item)) {
	if (!player->owner)
	    continue;

	if (opt_MARKET) {
	    if (ontradingblock(type, &item.ship)) {
		pr("You cannot scrap an item on the trading block!\n");
		continue;
	    }
	}

	getsect(item.gen.x, item.gen.y, &sect);
	if (type == EF_SHIP) {
	    if (!player->owner
		&& relations_with(sect.sct_own, player->cnum) < FRIENDLY) {
		pr("%s is not in a friendly harbor!\n",
		   prship(&item.ship));
		continue;
	    }
	    if (sect.sct_type != SCT_HARBR || sect.sct_effic < 60) {
		pr("%s is not in a 60%% efficient harbor!\n",
		   prship(&item.ship));
		continue;
	    }
	    if (mchr[item.ship.shp_type].m_flags & M_TRADE) {
		pr("WARNING: You only collect money from trade ships if you \"scuttle\" them!\n");
		sprintf(prompt,
			"Are you really sure that you want to scrap %s (n)? ",
			prship(&item.ship));
		if (!confirm(prompt)) {
		    pr("%s not scrapped\n", prship(&item.ship));
		    continue;
		}
	    }
	} else {
	    if (!player->owner
		&& relations_with(sect.sct_own, player->cnum) != ALLIED) {
		pr("%s is not in an allied sector!\n",
		   unit_nameof(&item.gen));
		continue;
	    }
	    if (type == EF_PLANE
		&& (sect.sct_type != SCT_AIRPT || sect.sct_effic < 60)) {
		pr("%s is not in a 60%% efficient airfield!\n",
		   prplane(&item.plane));
		continue;
	    }
	}

	pr("%s scrapped in %s\n",
	   unit_nameof(&item.gen),
	   xyas(item.gen.x, item.gen.y, player->cnum));
	unit_drop_cargo(&item.gen, sect.sct_own);
	if (type == EF_SHIP) {
	    eff = item.ship.shp_effic / 100.0;
	    mp = &mchr[(int)item.ship.shp_type];
	    for (i = I_NONE + 1; i <= I_MAX; i++) {
		if (load_comm_ok(&sect, item.ship.shp_own, i,
				 -item.ship.shp_item[i]))
		    sect.sct_item[i] += item.ship.shp_item[i];
	    }
	    mvec = mp->m_mat;
	    if (item.ship.shp_pstage == PLG_INFECT
		&& sect.sct_pstage == PLG_HEALTHY)
		sect.sct_pstage = PLG_EXPOSED;
	} else if (type == EF_LAND) {
	    eff = item.land.lnd_effic / 100.0;
	    lp = &lchr[(int)item.land.lnd_type];
	    for (i = I_NONE + 1; i <= I_MAX; i++) {
		if (load_comm_ok(&sect, item.land.lnd_own, i,
				 -item.land.lnd_item[i]))
		    sect.sct_item[i] += item.land.lnd_item[i];
	    }
	    mvec = lp->l_mat;
	    if (item.land.lnd_pstage == PLG_INFECT
		&& sect.sct_pstage == PLG_HEALTHY)
		sect.sct_pstage = PLG_EXPOSED;
	} else {
	    eff = item.land.lnd_effic / 100.0;
	    pp = &plchr[(int)item.plane.pln_type];
	    mvec = pp->pl_mat;
	}
	item.gen.effic = 0;
	put_empobj(type, item.gen.uid, &item.gen);
	for (i = I_NONE + 1; i <= I_MAX; i++) {
	    if (i == I_CIVIL || i == I_MILIT || i == I_UW)
		amt = sect.sct_item[i] + mvec[i] * eff;
	    else
		amt = sect.sct_item[i] + mvec[i] * 2 / 3 * eff;
	    if (amt > ITEM_MAX)
		amt = ITEM_MAX;
	    sect.sct_item[i] = amt;
	}
	putsect(&sect);
    }
    return RET_OK;
}