Spoiler
package data.scripts.weapons;
import java.awt.Color;
import java.util.Iterator;
import org.lwjgl.util.vector.Vector2f;
import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.combat.*;
import com.fs.starfarer.api.impl.campaign.ids.Stats;
import com.fs.starfarer.api.util.Misc;
import org.lwjgl.util.vector.Vector2f;
import com.fs.starfarer.api.util.IntervalUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BarrageKannonOnFireEffect implements OnFireEffectPlugin
{
public static final float SPREAD = 8f;
public void onFire(DamagingProjectileAPI projectile, WeaponAPI weapon, CombatEngineAPI engine)
{
// Removes dummy shot
for (DamagingProjectileAPI p : engine.getProjectiles())
{
if (p.getWeapon() == weapon && p.getProjectileSpecId().equalsIgnoreCase("battlefleets_ork_barrage_kannon_shell"))
engine.removeEntity(p);
}
// Random number 0-5
int RndShotType = (int)Math.round(Math.random() * 5);
if (RndShotType == 0 ) // HE shot
{
float offset = ((float)Math.random() * SPREAD) - (SPREAD * 0.5f);
float angle = weapon.getCurrAngle() + offset;
String weaponId = "battlefleets_ork_barrage_kannon_he";
Vector2f muzzleLocation = calculateMuzzle(weapon);
DamagingProjectileAPI proj = (DamagingProjectileAPI)engine.spawnProjectile(weapon.getShip(),weapon,weaponId,muzzleLocation,angle,weapon.getShip().getVelocity());
float randomSpeed = (float)Math.random() * 0.4f + 0.8f;
proj.getVelocity().scale(randomSpeed);
}
if (RndShotType == 1 ) // HE shot
{
float offset = ((float)Math.random() * SPREAD) - (SPREAD * 0.5f);
float angle = weapon.getCurrAngle() + offset;
String weaponId = "battlefleets_ork_barrage_kannon_he";
Vector2f muzzleLocation = calculateMuzzle(weapon);
DamagingProjectileAPI proj = (DamagingProjectileAPI)engine.spawnProjectile(weapon.getShip(),weapon,weaponId,muzzleLocation,angle,weapon.getShip().getVelocity());
float randomSpeed = (float)Math.random() * 0.4f + 0.8f;
proj.getVelocity().scale(randomSpeed);
}
if (RndShotType == 2 ) // HE shot
{
float offset = ((float)Math.random() * SPREAD) - (SPREAD * 0.5f);
float angle = weapon.getCurrAngle() + offset;
String weaponId = "battlefleets_ork_barrage_kannon_he";
Vector2f muzzleLocation = calculateMuzzle(weapon);
DamagingProjectileAPI proj = (DamagingProjectileAPI)engine.spawnProjectile(weapon.getShip(),weapon,weaponId,muzzleLocation,angle,weapon.getShip().getVelocity());
float randomSpeed = (float)Math.random() * 0.4f + 0.8f;
proj.getVelocity().scale(randomSpeed);
}
if (RndShotType == 3) // Kinetic shot
{
float offset = ((float)Math.random() * SPREAD) - (SPREAD * 0.5f);
float angle = weapon.getCurrAngle() + offset;
String weaponId = "battlefleets_ork_barrage_kannon_kinetic";
Vector2f muzzleLocation = calculateMuzzle(weapon);
DamagingProjectileAPI proj = (DamagingProjectileAPI)engine.spawnProjectile(weapon.getShip(),weapon,weaponId,muzzleLocation,angle,weapon.getShip().getVelocity());
float randomSpeed = (float)Math.random() * 0.4f + 0.8f;
proj.getVelocity().scale(randomSpeed);
}
if (RndShotType == 4) // Kinetic shot
{
float offset = ((float)Math.random() * SPREAD) - (SPREAD * 0.5f);
float angle = weapon.getCurrAngle() + offset;
String weaponId = "battlefleets_ork_barrage_kannon_kinetic";
Vector2f muzzleLocation = calculateMuzzle(weapon);
DamagingProjectileAPI proj = (DamagingProjectileAPI)engine.spawnProjectile(weapon.getShip(),weapon,weaponId,muzzleLocation,angle,weapon.getShip().getVelocity());
float randomSpeed = (float)Math.random() * 0.4f + 0.8f;
proj.getVelocity().scale(randomSpeed);
}
if (RndShotType == 5) // Frag shot
{
float offset = ((float)Math.random() * SPREAD) - (SPREAD * 0.5f);
float angle = weapon.getCurrAngle() + offset;
String weaponId = "battlefleets_ork_barrage_kannon_frag";
Vector2f muzzleLocation = calculateMuzzle(weapon);
DamagingProjectileAPI proj = (DamagingProjectileAPI)engine.spawnProjectile(weapon.getShip(),weapon,weaponId,muzzleLocation,angle,weapon.getShip().getVelocity());
float randomSpeed = (float)Math.random() * 0.4f + 0.8f;
proj.getVelocity().scale(randomSpeed);
}
}
private Vector2f calculateMuzzle(WeaponAPI weapon)
{
float muzzle = 30;
double angle = Math.toRadians(weapon.getCurrAngle());
Vector2f dir = new Vector2f((float)Math.cos(angle),(float)Math.sin(angle));
if (dir.lengthSquared() > 0f) dir.normalise();
dir.scale(muzzle);
Vector2f loc = new Vector2f(weapon.getLocation());
return Vector2f.add(loc,dir,new Vector2f());
}
}
But couldn't get it to delete the original shell. Still unsure how to capture the proper muzzle offset with either function.