CWeapon* CUnitLoader::LoadWeapon(WeaponDef *weapondef, CUnit* owner,UnitDef::UnitDefWeapon* udw)
{
	CWeapon* weapon;

	if(!weapondef){
		info->AddLine("Error: No weapon def?");
	}

	if(udw->name=="NOWEAPON"){
		weapon=new CNoWeapon(owner);
	} else if(weapondef->type=="Cannon"){
		weapon=new CCannon(owner);
		((CCannon*)weapon)->selfExplode=weapondef->selfExplode;
	} else if(weapondef->type=="Rifle"){
		weapon=new CRifle(owner);
	} else if(weapondef->type=="Melee"){
		weapon=new CMeleeWeapon(owner);
	} else if(weapondef->type=="AircraftBomb"){
		weapon=new CBombDropper(owner,false);
	} else if(weapondef->type=="PlasmaRepulser"){
		weapon=new CPlasmaRepulser(owner);
	} else if(weapondef->type=="flame"){
		weapon=new CFlameThrower(owner);
	} else if(weapondef->type=="MissileLauncher"){
		weapon=new CMissileLauncher(owner);
	} else if(weapondef->type=="TorpedoLauncher"){
		if(weapondef->onlyForward){	//assume aircraft
			weapon=new CBombDropper(owner,true);
			if(weapondef->tracks)
				((CBombDropper*)weapon)->tracking=weapondef->turnrate;
			((CBombDropper*)weapon)->bombMoveRange=weapondef->range;
		} else {
			weapon=new CTorpedoLauncher(owner);
			if(weapondef->tracks)
				((CTorpedoLauncher*)weapon)->tracking=weapondef->turnrate;
		}
	} else if(weapondef->type=="LaserCannon"){
		weapon=new CLaserCannon(owner);
		((CLaserCannon*)weapon)->color=weapondef->visuals.color;
	} else if(weapondef->type=="BeamLaser"){
		weapon=new CBeamLaser(owner);
		((CBeamLaser*)weapon)->color=weapondef->visuals.color;
	} else if(weapondef->type=="LightingCannon"){
		weapon=new CLightingCannon(owner);
		((CLightingCannon*)weapon)->color=weapondef->visuals.color;
	} else if(weapondef->type=="EmgCannon"){
		weapon=new CEmgCannon(owner);
	} else if(weapondef->type=="DGun"){
		weapon=new CDGunWeapon(owner);
	} else if(weapondef->type=="StarburstLauncher"){
		weapon=new CStarburstLauncher(owner);
		if(weapondef->tracks)
			((CStarburstLauncher*)weapon)->tracking=weapondef->turnrate;
		else
			((CStarburstLauncher*)weapon)->tracking=0;
		((CStarburstLauncher*)weapon)->uptime=weapondef->uptime*30;
	}else {
		(*info) << "Unknown weapon type " << weapondef->type.c_str() << "\n";
		return 0;
	}
	weapon->weaponDef = weapondef;

	weapon->reloadTime= (int) (weapondef->reload*GAME_SPEED);
	if(weapon->reloadTime==0)
		weapon->reloadTime=1;
	weapon->range=weapondef->range;
//	weapon->baseRange=weapondef->range;
	weapon->heightMod=weapondef->heightmod;
	weapon->projectileSpeed=weapondef->projectilespeed;
//	weapon->baseProjectileSpeed=weapondef->projectilespeed/GAME_SPEED;

	weapon->damages = weapondef->damages; //not needed anymore, to be removed

	weapon->areaOfEffect=weapondef->areaOfEffect;
	weapon->accuracy=weapondef->accuracy;
	weapon->sprayangle=weapondef->sprayangle;

	weapon->salvoSize=weapondef->salvosize;
	weapon->salvoDelay=(int) (weapondef->salvodelay*GAME_SPEED);

	weapon->metalFireCost=weapondef->metalcost;
	weapon->energyFireCost=weapondef->energycost;

	/*CFileHandler ffile("sounds/"+weapondef->sfiresound);
	if(ffile.FileExists())
	{
		weapondef->firesound = sound->GetWaveId(weapondef->sfiresound);
		weapon->fireSoundId = weapondef->firesoundId;
	}
	else
	{
		weapondef->firesoundId = 0;
		weapon->fireSoundId = 0;
	}
	if(weapondef->ssoundhit!="none.wav")
	{
		weapondef->soundhitId = sound->GetWaveId(weapondef->ssoundhit);
	}
	else
	{
		weapondef->soundhitId = 0;
	}*/
	CWeaponDefHandler::LoadSound(weapondef->firesound);
	CWeaponDefHandler::LoadSound(weapondef->soundhit);

	if(weapon->damages[0]>50){
		float soundVolume=sqrt(weapon->damages[0]*0.5);
		if(weapondef->type=="LaserCannon")
			soundVolume*=0.5;
		float hitSoundVolume=soundVolume;
		if((weapondef->type=="MissileLauncher" || weapondef->type=="StarburstLauncher") && soundVolume>100)
			soundVolume=10*sqrt(soundVolume);
		weapondef->firesound.volume=soundVolume;
		soundVolume=hitSoundVolume;
		if(weapon->areaOfEffect>8)
			soundVolume*=2;
		if(weapondef->type=="DGun")
			soundVolume*=0.15;
		weapondef->soundhit.volume=soundVolume;
	}
	weapon->fireSoundId = weapondef->firesound.id;
	weapon->fireSoundVolume=weapondef->firesound.volume;

	weapon->onlyForward=weapondef->onlyForward;
	if(owner->unitDef->type=="Fighter" && !owner->unitDef->hoverAttack)		//fighter aircrafts have too big tolerance in ta
		weapon->maxAngleDif=cos(weapondef->maxAngle*0.4/180*PI);
	else
		weapon->maxAngleDif=cos(weapondef->maxAngle/180*PI);

	weapon->weaponNum=owner->weapons.size();

	weapon->badTargetCategory=udw->badTargetCat;
	weapon->onlyTargetCategory=weapondef->onlyTargetCategory & udw->onlyTargetCat;

	if(udw->slavedTo)
		weapon->slavedTo=owner->weapons[udw->slavedTo-1];

	weapon->mainDir=udw->mainDir;
	weapon->maxMainDirAngleDif=udw->maxAngleDif;

	weapon->Init();

	return weapon;
}
Exemple #2
0
CWeapon* CUnitLoader::LoadWeapon(const WeaponDef *weapondef, CUnit* owner, const UnitDef::UnitDefWeapon* udw)
{
	CWeapon* weapon;

	if (!weapondef) {
		logOutput.Print("Error: No weapon def?");
	}

	if (udw->name == "NOWEAPON") {
		weapon = new CNoWeapon(owner);
	} else if (weapondef->type == "Cannon") {
		weapon = new CCannon(owner);
		((CCannon*)weapon)->selfExplode = weapondef->selfExplode;
	} else if (weapondef->type == "Rifle") {
		weapon = new CRifle(owner);
	} else if (weapondef->type == "Melee") {
		weapon = new CMeleeWeapon(owner);
	} else if (weapondef->type == "AircraftBomb") {
		weapon = new CBombDropper(owner, false);
	} else if (weapondef->type == "Shield") {
		weapon = new CPlasmaRepulser(owner);
	} else if (weapondef->type == "Flame") {
		weapon = new CFlameThrower(owner);
	} else if (weapondef->type == "MissileLauncher") {
		weapon = new CMissileLauncher(owner);
	} else if (weapondef->type == "TorpedoLauncher") {
		if (owner->unitDef->canfly && !weapondef->submissile) {
			weapon = new CBombDropper(owner, true);
			if (weapondef->tracks)
				((CBombDropper*) weapon)->tracking = weapondef->turnrate;
			((CBombDropper*) weapon)->bombMoveRange = weapondef->range;
		} else {
			weapon = new CTorpedoLauncher(owner);
			if (weapondef->tracks)
				((CTorpedoLauncher*) weapon)->tracking = weapondef->turnrate;
		}
	} else if (weapondef->type == "LaserCannon") {
		weapon = new CLaserCannon(owner);
		((CLaserCannon*) weapon)->color = weapondef->visuals.color;
	} else if (weapondef->type == "BeamLaser") {
		weapon = new CBeamLaser(owner);
		((CBeamLaser*) weapon)->color = weapondef->visuals.color;
	} else if (weapondef->type == "LightningCannon") {
		weapon = new CLightningCannon(owner);
		((CLightningCannon*) weapon)->color = weapondef->visuals.color;
	} else if (weapondef->type == "EmgCannon") {
		weapon = new CEmgCannon(owner);
	} else if (weapondef->type == "DGun") {
		weapon = new CDGunWeapon(owner);
	} else if (weapondef->type == "StarburstLauncher"){
		weapon = new CStarburstLauncher(owner);
		if (weapondef->tracks)
			((CStarburstLauncher*) weapon)->tracking = weapondef->turnrate;
		else
			((CStarburstLauncher*) weapon)->tracking = 0;
		((CStarburstLauncher*) weapon)->uptime = weapondef->uptime * GAME_SPEED;
	} else {
		LogObject() << "Unknown weapon type " << weapondef->type.c_str() << "\n";
		return 0;
	}
	weapon->weaponDef = weapondef;

	weapon->reloadTime = (int) (weapondef->reload * GAME_SPEED);
	if (weapon->reloadTime == 0)
		weapon->reloadTime = 1;
	weapon->range = weapondef->range;
//	weapon->baseRange = weapondef->range;
	weapon->heightMod = weapondef->heightmod;
	weapon->projectileSpeed = weapondef->projectilespeed;
//	weapon->baseProjectileSpeed = weapondef->projectilespeed / GAME_SPEED;

	weapon->areaOfEffect = weapondef->areaOfEffect;
	weapon->accuracy = weapondef->accuracy;
	weapon->sprayAngle = weapondef->sprayAngle;

	weapon->stockpileTime = (int) (weapondef->stockpileTime * GAME_SPEED);

	weapon->salvoSize = weapondef->salvosize;
	weapon->salvoDelay = (int) (weapondef->salvodelay * GAME_SPEED);
	weapon->projectilesPerShot = weapondef->projectilespershot;

	weapon->metalFireCost = weapondef->metalcost;
	weapon->energyFireCost = weapondef->energycost;

	weapon->fireSoundId = weapondef->firesound.getID(0);
	weapon->fireSoundVolume = weapondef->firesound.getVolume(0);

	weapon->onlyForward = weapondef->onlyForward;
	if (owner->unitDef->type == "Fighter" && !owner->unitDef->hoverAttack) {
		// fighter aircraft have too big tolerance in TA
		weapon->maxAngleDif = cos(weapondef->maxAngle * 0.4f / 180 * PI);
	} else {
		weapon->maxAngleDif = cos(weapondef->maxAngle / 180 * PI);
	}

	weapon->SetWeaponNum(owner->weapons.size());

	weapon->badTargetCategory = udw->badTargetCat;
	weapon->onlyTargetCategory = weapondef->onlyTargetCategory & udw->onlyTargetCat;

	if (udw->slavedTo) {
		const int index = (udw->slavedTo - 1);
		if ((index < 0) || (static_cast<size_t>(index) >= owner->weapons.size())) {
			throw content_error("Bad weapon slave in " + owner->unitDef->name);
		}
		weapon->slavedTo = owner->weapons[index];
	}

	weapon->mainDir = udw->mainDir;
	weapon->maxMainDirAngleDif = udw->maxAngleDif;

	weapon->fuelUsage = udw->fuelUsage;
	weapon->avoidFriendly = weapondef->avoidFriendly;
	weapon->avoidFeature = weapondef->avoidFeature;
	weapon->avoidNeutral = weapondef->avoidNeutral;
	weapon->targetBorder = weapondef->targetBorder;
	weapon->cylinderTargetting = weapondef->cylinderTargetting;
	weapon->minIntensity = weapondef->minIntensity;
	weapon->heightBoostFactor = weapondef->heightBoostFactor;
	weapon->collisionFlags = weapondef->collisionFlags;
	weapon->Init();

	return weapon;
}