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: Simulator Enhancements (03/13/24)

Author Topic: spawnProjectile bug  (Read 1805 times)

dmaiski

  • Captain
  • ****
  • Posts: 422
  • resistance is futile
    • View Profile
spawnProjectile bug
« on: December 15, 2013, 04:34:57 AM »

Spoiler
Spoiler
missiles spawned with spawnProjectile do not get their AI consistently

to be specific it seems to have problems about 90% of the time

Code
        package data.scripts.MissileAI;


import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.combat.*;
import com.fs.starfarer.api.util.IntervalUtil;
import org.lazywizard.lazylib.combat.AIUtils;
import org.lwjgl.util.vector.Vector2f;

import java.awt.*;


import static jar.UtilityKit.getNearestMissileToPoint;


public class BalundirLightningAI implements MissileAIPlugin{

    private final MissileAPI missile;
    private ShipAPI launchingShip;

    private IntervalUtil MainTimer = new IntervalUtil(2f, 0.5f);
    private IntervalUtil ShortTimer = new IntervalUtil(.5f, 0.4f);
    private IntervalUtil LongTimer = new IntervalUtil(5f, 0.5f);

    private static boolean Fire=false;
    private static boolean Fire2=false;

    public BalundirLightningAI(MissileAPI missile, ShipAPI launchingShip)
    {
        this.missile = missile;
        this.launchingShip = launchingShip;
    }

    //main missile AI
    private Vector2f MouseT=new Vector2f(0,0);
    @Override
    public void advance(float amount)
    {
        CombatEngineAPI engine = Global.getCombatEngine();
        // pause when you are not needed
        if (missile.isFading() || missile.isFizzling() || engine.isPaused())
        {
            return;
        }



        if (MouseT.lengthSquared()==0)
        {
            MouseT = new Vector2f(launchingShip.getMouseTarget());
            if (MouseT.lengthSquared()==0)
            {
                missile.setSource(AIUtils.getNearestAlly(missile));
            }
        }

        ShortTimer.advance(amount);
        MainTimer.advance(amount);
        LongTimer.advance(amount);

        if (MainTimer.intervalElapsed())
        {
            Fire = true;
        }

        if (ShortTimer.intervalElapsed()){
            Fire2 = true;
        }

        if (Fire && Fire2) {
            Fire2=false;
            engine.spawnProjectile(
                    launchingShip,
                    null,
                    "targetdot",
                    MouseT,
                    0,
                    new Vector2f(0,0));

            MissileAPI target = getNearestMissileToPoint(MouseT);

            engine.spawnEmpArc(
                    launchingShip,
                    missile.getLocation(),
                    target, target,
                    DamageType.ENERGY,
                    0f, //real
                    0,   //emp
                    10000f, // max range
                    "tachyon_lance_emp_impact",
                    5f, // thickness
                    new Color(255,10,15,255),
                    new Color(31, 214,255,255)
            );
        }

        if (LongTimer.intervalElapsed()) {
            engine.removeEntity(missile);    }
    }
}

[close]

curently trying to track down what exactly is causing the problem

it seems that very often they fail to get missile.getSource

ok after the first missile is launched(after startup) subsequent missiles become unable to getSource,
spawnProjectile also breaks(this happens on the first run through when the rest of the AI is working corectly, spawnEmpArk works fine in this case)

final test from tonight:
exiting combat(general): causes missileAI to become unable to call missile.getSource or use launchingShip from when the ai is set
spawnProjectile missiles: unable to use spawnProjectile commands on ocasions (related to above issue)

note: this has not caused any null exceptions to date, there is a chance that this happend because the ShipAPI from the previous instance has not been cleared

no the ai itself activates fine, its just parts of the ai like ShipAPI launchingship that encounter problems that souldnt really happen also i need to move this top bug reports, wrote it at 5am today :P

So just set the source as the closest friendly ShipAPI?

Also, you can set the AI directly after it's spawned, instead of hoping that it's caught when the MissileAPI is instantiated via the Plugin :)

im not actualy sure what is going on with this case, but something is broken (error seems to be related to spwanProjectile but im not sure what is cause)

postid this thread in sugestions by accident
[close]
fixed/bugfound, getSource does not work properly on spawnProjectile objects: using launchingship seems to be more much more consistent
« Last Edit: December 15, 2013, 05:02:55 AM by dmaiski »
Logged
BISO
(WIP) lots of shiny new weapons ( :-[ i have more weapons then sprites :-[ )

i got a cat pad
its like a mouse pad but better!

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 23986
    • View Profile
Re: spawnProjectile bug
« Reply #1 on: December 15, 2013, 09:04:23 AM »

Just ran some quick tests (spawning a missile, checking what the source is) and I'm not seeing any problems.

From inside EveryFrameCombatPlugin.advance():
Code: java
if ((float) Math.random() > 0.25f) {
MissileAPI missile = (MissileAPI) engine.spawnProjectile(engine.getPlayerShip(), null, "harpoon", engine.getPlayerShip().getLocation(), 0, new Vector2f());
System.out.println("Source: " + missile.getSource());
}
Never failed to properly print the source. Looking at the code, it's hard to see how the source for a missile would not get set; it's part of the constructor for the missile, the value for source just gets passed straight through from the spawnProjectile method, and the source isn't changed at any point after that.

Am I missing something here? I'm not sure I'm understanding what you mean. Things like "does not work" are awfully unclear.
Logged

dmaiski

  • Captain
  • ****
  • Posts: 422
  • resistance is futile
    • View Profile
Re: spawnProjectile bug
« Reply #2 on: December 15, 2013, 10:23:39 AM »

maybe i didnt put it corectly:


this works fine
Spoiler
Code: java
                engine.spawnProjectile(
                        launchingShip,
                        null,
                        "targetdot",
                        MouseT,
                        0,
                        new Vector2f(0,0));

                MissileAPI target = getNearestMissileToPoint(MouseT);

                engine.spawnEmpArc(
                        launchingShip,
                        missile.getLocation(),
                        target, target,
                        DamageType.ENERGY,
                        0f,
                        0,  
                        10000f,
                        "tachyon_lance_emp_impact",
                        5f,
                        new Color(255,10,15,255),
                        new Color(31, 214,255,255)
                );
[close]

this dose not work consistently (after the first missile it starts to fail for every subsequent missile)
the engine.spawnProjectile does not spawn anything (im thinking there is some kind of minor problem with the returned ShipAPI)
Spoiler
Code: java
                engine.spawnProjectile(
                        missile.getSource(),     //this is the source of error prevents spawnProjectile from working
                        null,                    //dose not cause null exception so its some form of error in the data
                        "targetdot",                //given to it by ShipAPI, ie. non-null return value with errors in it
                        MouseT,
                        0,
                        new Vector2f(0,0));

                MissileAPI target = getNearestMissileToPoint(MouseT);

                engine.spawnEmpArc(
                        missile.getSource(),    //this oddly enough dose not cause problems
                        missile.getLocation(),
                        missile.getSource(), missile.getSource(),          //if i set taget to missile.getSource() it works perfectly
                        DamageType.ENERGY,
                        0f,
                        0,  
                        10000f,
                        "tachyon_lance_emp_impact",
                        5f,
                        new Color(255,10,15,255),
                        new Color(31, 214,255,255)
                );
[close]
« Last Edit: December 15, 2013, 10:25:50 AM by dmaiski »
Logged
BISO
(WIP) lots of shiny new weapons ( :-[ i have more weapons then sprites :-[ )

i got a cat pad
its like a mouse pad but better!

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 23986
    • View Profile
Re: spawnProjectile bug
« Reply #3 on: December 15, 2013, 11:09:22 AM »

(It's "does", btw, not "dose".)


I can't quite tell what the problem is, but at this point it seems more likely to be in your mod's code, so moving the thread here. Maybe someone else will be able to spot it.
Logged