コード例 #1
0
ファイル: CargoHolder.cpp プロジェクト: vkholodkov/freestars
void CargoHolder::TransferCargo(CargoHolder * dest, CargoType ct, long * amount, Player * player)
{
	if (*amount == 0)
		return;
	assert(*amount > 0);

	if (GetContain(ct) < *amount) {
		Message * mess = player->AddMessage("Warning: Transfer more then carried", this);
		mess->AddLong("Attempted amount", *amount);
		*amount = GetContain(ct);
		mess->AddLong("Actual amount", *amount);
	}

	if (ct == POPULATION) {
		*amount -= *amount % Rules::PopEQ1kT;	// only transfer full groups
		if (dest->GetCargoCapacity() >= 0 && dest->GetCargoCapacity() < dest->GetCargoMass() + (*amount / Rules::PopEQ1kT)) {
			Message * mess = player->AddMessage("Warning: Transfer more then capacity", this);
			mess->AddItem("", dest);
			mess->AddLong("Attempted amount", *amount);
			*amount = (dest->GetCargoCapacity() - dest->GetCargoMass()) * Rules::PopEQ1kT;
			mess->AddLong("Actual amount", *amount);
		}
	} else if (ct == FUEL) {
		Fleet * destf = dynamic_cast<Fleet *>(dest);
		if (destf->GetFuelCapacity() >= 0 && destf->GetFuelCapacity() < destf->GetFuel() + *amount) {
			Message * mess = player->AddMessage("Warning: Transfer more then capacity", this);
			mess->AddItem("", dest);
			mess->AddLong("Attempted amount", *amount);
			*amount = destf->GetFuelCapacity() - destf->GetFuel();
			mess->AddLong("Actual amount", *amount);
		}
	} else {
		if (dest->GetCargoCapacity() >= 0 && dest->GetCargoCapacity() < dest->GetCargoMass() + *amount) {
			Message * mess = player->AddMessage("Warning: Transfer more then capacity", this);
			mess->AddItem("", dest);
			mess->AddLong("Attempted amount", *amount);
			*amount = dest->GetCargoCapacity() - dest->GetCargoMass();
			mess->AddLong("Actual amount", *amount);
		}
	}

	if (ct == POPULATION && GetOwner() != dest->GetOwner()) {
		assert(false);	// should never get here now
		Planet * destp = dynamic_cast<Planet *>(dest);
		if (destp) {
			if (destp->GetBaseNumber() >= 0) {
				player->AddMessage("Warning: Invading world with base", this);
				return;
			} else {
				AdjustAmounts(ct, -*amount);
				destp->Invade(NCGetOwner(), *amount);
				return;
			}
		} else {
			Message * mess = player->AddMessage("Warning: Transfer pop to unowned fleet", this);
			mess->AddItem("", dest);
			return;
		}
	}

	dest->AdjustAmounts(ct, *amount);
	AdjustAmounts(ct, -*amount);
}
コード例 #2
0
ファイル: CargoHolder.cpp プロジェクト: vkholodkov/freestars
void CargoHolder::ProcessLoad(CargoHolder * dest, CargoType ct, TransferType tt, long value, bool dunnage)	// value usage depends on TransferType
{
	assert(dest->IsWith(*this));
	assert(dest != this);

	if (value < 0) {
		Message * mess = NCGetOwner()->AddMessage("Error: Transfer order is negative", this);
		mess->AddLong("Amount transferred", value);
		return;
	}

	if (value > Rules::GetConstant("MaxTransfer")) {
		Message * mess = NCGetOwner()->AddMessage("Error: Transfer order is over max", this);
		mess->AddLong("Amount transferred", value);
		return;
	}

	long destAmt = dest->GetContain(ct);
	if (GetOwner() != dest->GetOwner() && (ct == POPULATION || !dest->CanLoadBy(GetOwner())))
		destAmt = 0;

	long amount = 0;	// actual amount moved;
	switch (tt) {
	case TRANSFER_DROPNLOAD:
	case TRANSFER_LOADALL:
		if (dunnage) break;
		amount = TransferAmount(ct, dest, this, destAmt);
		break;

	case TRANSFER_UNLOADALL:
		break;

	case TRANSFER_LOADAMT:
		if (dunnage) break;
		amount = TransferAmount(ct, dest, this, value);
		break;

	case TRANSFER_UNLOADAMT:
		break;

	case TRANSFER_FILLPER:
	case TRANSFER_WAITPER:	// affects movement too
		if (dunnage) break;
		if (value > 100) {
			Message * mess = NCGetOwner()->AddMessage("Error: Transfer percent over 100%", this);
			mess->AddLong("Amount transferred", value);
			return;
		}
		amount = TransferAmount(ct, dest, this, GetCargoCapacity() * value / 100);
		break;

	case TRANSFER_LOADDUNN:
		if (!dunnage) break;
		// order of loading is different, but this is basicly a loadall -- execpt fuel
		if (ct == FUEL) {
			Fleet * f = dynamic_cast<Fleet *>(this);
			int Need = f->GetFuelNeeded() - f->GetFuel();
			if (Need > 0)
				amount = TransferAmount(ct, dest, this, Need);
			else
				amount = -TransferAmount(ct, this, dest, -Need);
		} else {
			amount = TransferAmount(ct, dest, this, destAmt);
		}
		break;

	case TRANSFER_SETTOPER:
		if (value > 100) {
			Message * mess = NCGetOwner()->AddMessage("Error: Transfer percent over 100%", this);
			mess->AddLong("Amount transferred", value);
			return;
			
			
		}
		value = GetCargoCapacity() * value / 100;
		// drop to a regualr transfer amount
	case TRANSFER_AMOUNTTO:
		if (dunnage) break;
		if (GetContain(ct) < value)
			amount = TransferAmount(ct, dest, this, value - GetContain(ct));
		break;

	case TRANSFER_DESTTO:
		if (dunnage) break;
		if (destAmt > value)
			amount = TransferAmount(ct, dest, this, destAmt - value);
		break;

	default:
		Message * mess = NCGetOwner()->AddMessage("Error: Invalid transfer order", this);
		mess->AddLong("Transfer code", tt);
		return;
	}

	assert(amount >= 0 || tt == TRANSFER_LOADDUNN && ct == FUEL);

	if (amount > 0 && dest->GetOwner() != GetOwner()) {
		if (!dest->CanLoadBy(GetOwner())) {
			Message * mess = NCGetOwner()->AddMessage("Warning: No theiving component at location", this);
			mess->AddItem("", dest);
			return;
		} else if (!Rules::Stealable(ct)) {
			Message * mess = NCGetOwner()->AddMessage("Warning: Cargo cannot be stolen", this);
			mess->AddItem("", dest);
			mess->AddLong("Cargo type", ct);
			return;
		}
	}

	dest->AdjustAmounts(ct, -amount);	// remove cargo from destination
	AdjustAmounts(ct, amount);		// add it to the source
}