Starsector API
Loading...
Searching...
No Matches
ShockRepeaterOnFireEffect.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.combat;
2
3import java.awt.Color;
4import java.util.Iterator;
5
6import org.lwjgl.util.vector.Vector2f;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.combat.CollisionClass;
10import com.fs.starfarer.api.combat.CombatEngineAPI;
11import com.fs.starfarer.api.combat.CombatEntityAPI;
12import com.fs.starfarer.api.combat.DamageType;
13import com.fs.starfarer.api.combat.DamagingProjectileAPI;
14import com.fs.starfarer.api.combat.EmpArcEntityAPI;
15import com.fs.starfarer.api.combat.MissileAPI;
16import com.fs.starfarer.api.combat.OnFireEffectPlugin;
17import com.fs.starfarer.api.combat.ShipAPI;
18import com.fs.starfarer.api.combat.WeaponAPI;
19import com.fs.starfarer.api.combat.WeaponAPI.AIHints;
20import com.fs.starfarer.api.impl.campaign.ids.Stats;
21import com.fs.starfarer.api.util.Misc;
22
25public class ShockRepeaterOnFireEffect implements OnFireEffectPlugin {
26
27 public static float ARC = 30f;
28
29 public void onFire(DamagingProjectileAPI projectile, WeaponAPI weapon, CombatEngineAPI engine) {
30 //ARC = 30f;
31 float emp = projectile.getEmpAmount();
32 float dam = projectile.getDamageAmount();
33
34 CombatEntityAPI target = findTarget(projectile, weapon, engine);
35 float thickness = 20f;
36 float coreWidthMult = 0.67f;
37 Color color = weapon.getSpec().getGlowColor();
38 if (target != null) {
39 EmpArcEntityAPI arc = engine.spawnEmpArc(projectile.getSource(), projectile.getLocation(), weapon.getShip(),
40 target,
41 DamageType.ENERGY,
42 dam,
43 emp, // emp
44 100000f, // max range
45 "shock_repeater_emp_impact",
46 thickness, // thickness
47 color,
48 new Color(255,255,255,255)
49 );
50 arc.setCoreWidthOverride(thickness * coreWidthMult);
51 arc.setSingleFlickerMode();
52 } else {
53 Vector2f from = new Vector2f(projectile.getLocation());
54 Vector2f to = pickNoTargetDest(projectile, weapon, engine);
55 EmpArcEntityAPI arc = engine.spawnEmpArcVisual(from, weapon.getShip(), to, weapon.getShip(), thickness, color, Color.white);
56 arc.setCoreWidthOverride(thickness * coreWidthMult);
57 arc.setSingleFlickerMode();
58 //Global.getSoundPlayer().playSound("shock_repeater_emp_impact", 1f, 1f, to, new Vector2f());
59 }
60 }
61
62 public Vector2f pickNoTargetDest(DamagingProjectileAPI projectile, WeaponAPI weapon, CombatEngineAPI engine) {
63 float spread = 50f;
64 float range = weapon.getRange() - spread;
65 Vector2f from = projectile.getLocation();
66 Vector2f dir = Misc.getUnitVectorAtDegreeAngle(weapon.getCurrAngle());
67 dir.scale(range);
68 Vector2f.add(from, dir, dir);
69 dir = Misc.getPointWithinRadius(dir, spread);
70 return dir;
71 }
72
73 public CombatEntityAPI findTarget(DamagingProjectileAPI projectile, WeaponAPI weapon, CombatEngineAPI engine) {
74 float range = weapon.getRange();
75 Vector2f from = projectile.getLocation();
76
77 Iterator<Object> iter = Global.getCombatEngine().getAllObjectGrid().getCheckIterator(from,
78 range * 2f, range * 2f);
79 int owner = weapon.getShip().getOwner();
80 CombatEntityAPI best = null;
81 float minScore = Float.MAX_VALUE;
82
83 ShipAPI ship = weapon.getShip();
84 boolean ignoreFlares = ship != null && ship.getMutableStats().getDynamic().getValue(Stats.PD_IGNORES_FLARES, 0) >= 1;
85 ignoreFlares |= weapon.hasAIHint(AIHints.IGNORES_FLARES);
86
87 while (iter.hasNext()) {
88 Object o = iter.next();
89 if (!(o instanceof MissileAPI) &&
91 !(o instanceof ShipAPI)) continue;
92 CombatEntityAPI other = (CombatEntityAPI) o;
93 if (other.getOwner() == owner) continue;
94
95 if (other instanceof ShipAPI) {
96 ShipAPI otherShip = (ShipAPI) other;
97 if (otherShip.isHulk()) continue;
98 //if (!otherShip.isAlive()) continue;
99 if (otherShip.isPhased()) continue;
100 }
101
102 if (other.getCollisionClass() == CollisionClass.NONE) continue;
103
104 if (ignoreFlares && other instanceof MissileAPI) {
105 MissileAPI missile = (MissileAPI) other;
106 if (missile.isFlare()) continue;
107 }
108
109 float radius = Misc.getTargetingRadius(from, other, false);
110 float dist = Misc.getDistance(from, other.getLocation()) - radius;
111 if (dist > range) continue;
112
113 if (!Misc.isInArc(weapon.getCurrAngle(), ARC, from, other.getLocation())) continue;
114
115 //float angleTo = Misc.getAngleInDegrees(from, other.getLocation());
116 //float score = Misc.getAngleDiff(weapon.getCurrAngle(), angleTo);
117 float score = dist;
118
119 if (score < minScore) {
120 minScore = score;
121 best = other;
122 }
123 }
124 return best;
125 }
126
127}
static CombatEngineAPI getCombatEngine()
Definition Global.java:63
CombatEntityAPI findTarget(DamagingProjectileAPI projectile, WeaponAPI weapon, CombatEngineAPI engine)
void onFire(DamagingProjectileAPI projectile, WeaponAPI weapon, CombatEngineAPI engine)
Vector2f pickNoTargetDest(DamagingProjectileAPI projectile, WeaponAPI weapon, CombatEngineAPI engine)