Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.97a is out! (02/02/24); New blog post: New music for Galatia Academy (06/12/24)

Author Topic: Help wityh phasor effect please  (Read 361 times)

rogerbacon

  • Commander
  • ***
  • Posts: 151
    • View Profile
Help wityh phasor effect please
« on: February 18, 2023, 07:49:40 PM »

Hello all. I've made a phasor effect that when applied to a limited duration beam like the phase lance or similar weapons causes the beam to 'move' from the point of aim off to the left or right up to 10 degrees. So it strafes over the target (and some of it probably misses).
It works by changing the weapon offset when it fires so the beam moves from initial position to the new offset. After it fires I reset the offset so it's ready to fire again.
The problem occurs if the battle ends before the weapons finishes firing (including the cooldown time).  Then the default aim position is offset by whatever it was last time it fired. So, the beam will always miss. My code is below. It was originally based on another mod where they had the beam sweep back and forth, but that's not exactly what I want in mine.
If anyone can see how to make sure the offsets are always reset, regardless of when the battle ends I would appreciate it. Of course if I'm totally doing this the hard way and there is an easier way that would be great too.

Note : ANGLES is an arraylist in the code but it has only one item. It was leftover from the original SCY code that had more than one intem.
Code
package data.scripts.weapons;
import java.lang.Math;
//import com.fs.starfarer.api.combat.BeamAPI;
//import com.fs.starfarer.api.combat.BeamEffectPlugin;
import com.fs.starfarer.api.combat.CombatEngineAPI;
import com.fs.starfarer.api.combat.CombatEntityAPI;
import com.fs.starfarer.api.combat.DamageType;
import com.fs.starfarer.api.combat.ShipAPI;
import com.fs.starfarer.api.util.IntervalUtil;
import org.lazywizard.lazylib.combat.CombatUtils;
//import org.lazywizard.lazylib.VectorUtils;
import java.awt.Color;
import java.util.*;
import org.lazywizard.console.Console;
import org.lwjgl.util.vector.Vector2f;

import com.fs.starfarer.api.Global;
import org.lwjgl.util.vector.ReadableVector2f;
import org.lazywizard.lazylib.FastTrig;
import java.util.Collection;
import com.fs.starfarer.api.combat.WeaponAPI;
import java.util.ArrayList;
import java.util.List;
import com.fs.starfarer.api.combat.EveryFrameWeaponEffectPlugin;

public class phasorEffect implements EveryFrameWeaponEffectPlugin
{

private float timer=0, randomization=0, perShotRandom = 0;
    private Vector2f muzzle= new Vector2f();
    private boolean  runOnce=false , hasFired=false, setInitialRandom = false;;
    private ArrayList<java.lang.Float> ANGLES = new ArrayList<java.lang.Float>();
private ArrayList<java.lang.Float> originalAngles = new ArrayList<java.lang.Float>();
//public static boolean runOnce=false ;
   
    public phasorEffect() {
        this.randomization = 0.0f;
}

 

    @Override
    public void advance(float amount, CombatEngineAPI engine, final WeaponAPI weapon)
    {

if (engine.isPaused()) {
            return;
        }
       
        //get the base offsets
        if(!runOnce){
// This still runs every frame at start of combat until I fire once
            runOnce=true;
            ANGLES=new ArrayList<java.lang.Float>(weapon.getSpec().getTurretAngleOffsets());

// store original angles
for(int i=0; i<ANGLES.size(); i++){
if (originalAngles.size() == 0){
originalAngles.add(ANGLES.get(i));
// Console.showMessage("setting orig angle " + i + " to " + ANGLES.get(i));
}
}

            randomization= (float)Math.random()*10;
            if(weapon.getSlot().isHardpoint()){
                muzzle=(Vector2f)weapon.getSpec().getHardpointFireOffsets().get(0);
            } else if(weapon.getSlot().isTurret()){
                muzzle=(Vector2f)weapon.getSpec().getTurretFireOffsets().get(0);
            }
        }
       
        //move the offsets while firing
        if(weapon.getChargeLevel()>0){
            timer+=amount;
if (!setInitialRandom) {
//Console.showMessage("setting new random at " + timer);
perShotRandom = (float)Math.random()*10 - 5f;
setInitialRandom = true;
//timer = 0;
}

            for(int i=0; i<ANGLES.size(); i++){
                //float offset=(float)FastTrig.cos(5*(timer+i+randomization))*2;
float offset=(float)FastTrig.cos(5*(perShotRandom+i+randomization))*10;

offset = offset * timer/2f;
java.lang.Float myAngle = (java.lang.Float)ANGLES.get(i);
                weapon.getSpec().getTurretAngleOffsets().set(i, myAngle+offset);
            }
        }     
       
        if(weapon.getChargeLevel()==1 && !hasFired){
            hasFired=true;
            //weapon glow
            Vector2f LOC = new Vector2f(weapon.getLocation());
            Vector2f.add(LOC, muzzle, LOC);
            engine.addHitParticle(
                    LOC,
                    weapon.getShip().getVelocity(),
                    40,
                    1f,
                    0.3f,
                    new Color(50,100,255,255)
            );
            engine.addHitParticle(
                    LOC,
                    weapon.getShip().getVelocity(),
                    20,
                    1f,
                    0.1f,
                    Color.WHITE
            );
        } else if(hasFired && weapon.getChargeLevel()==0){
            hasFired=false;
timer = 0f;
setInitialRandom = false;
this.randomization = (float)Math.random()*10;
// Reset turret angles for next time
for(int i=0; i<ANGLES.size(); i++){
weapon.getSpec().getTurretAngleOffsets().set(i, originalAngles.get(i));
}
        }

       
       
    }
}
Logged

Ruddygreat

  • Admiral
  • *****
  • Posts: 544
  • Seals :^)
    • View Profile
Re: Help wityh phasor effect please
« Reply #1 on: February 19, 2023, 04:15:59 AM »

you need to call WeaponAPI.ensureClonedSpec(); in the doOnce section, that should fix it.
(it'll also make it work correctly w/ multiple instances of the weapon one the field at the same time, rn it's modifying every one instead of just the one that you want)

rogerbacon

  • Commander
  • ***
  • Posts: 151
    • View Profile
Re: Help wityh phasor effect please
« Reply #2 on: February 19, 2023, 06:24:02 AM »

you need to call WeaponAPI.ensureClonedSpec(); in the doOnce section, that should fix it.
(it'll also make it work correctly w/ multiple instances of the weapon one the field at the same time, rn it's modifying every one instead of just the one that you want)

Oh, thank you, thank you, thank you. A one line fix and it's working perfectly.
Logged