Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: simple coding question  (Read 2775 times)

1096bimu

  • Ensign
  • *
  • Posts: 8
    • View Profile
simple coding question
« on: September 26, 2013, 02:36:31 PM »

Probably, I have no idea cuz I've never done java or any other development work for that matter. I'm just trying to put together bits and pieces from what I can find and see if things work out.

Anyway, here is a ship system which reflects all incoming projectiles right back and possibly hit their source. but for some reason, it only works in simulation and doesn't work in actual combat.

Code
Caused by: org.codehaus.commons.compiler.CompileException: File data/shipsystems/scripts/cheatsystem.java, Line 55, Column 19: No applicable constructor/method found for actual parameters "java.lang.String"; candidates are: "public abstract void com.fs.starfarer.api.combat.DamagingProjectileAPI.setSource(com.fs.starfarer.api.combat.ShipAPI)"

                ShipAPI ship = CombatUtils.getOwner(stats);
      int owner = ship.getOwner();
      Vector2f velocity;
      float facing;
        DamagingProjectileAPI proj;
        for (Iterator iter = CombatUtils.getCombatEngine().getProjectiles().iterator();
                iter.hasNext();)
                //CombatUtils is just a plugin from another mod, this thing just calls for all the units in combat I think
        {
            proj = (DamagingProjectileAPI) iter.next();

            if (proj.getOwner() == ship.getOwner())
            {
                // Don't affect friendly projectiles
                continue;
            }

            if (CombatUtils.getDistance(proj, ship) > 280f)
            {
                // Ignore projectiles that are out of range of the field
                continue;
            }
         
         velocity = proj.getVelocity();
            velocity.set(velocity.x*-1,velocity.y*-1);
         
         facing = proj.getFacing();
         proj.setFacing(facing +180);
// send back the way they came

         proj.setOwner(owner);
//making me the owner will make missiles start to seek enemy targets.
         
         proj.setSource(ship);
// this setSource is what gives problems. I think there is some problem with getting the source of projectiles in real combat with pirates.

        }


Also, is it possible to make this a hullmod instead of ship system?

and how would I make a system that will prevent all AI ships from retreating? I think it would be very similar to this, I'm just asking since I always get the syntax wrong.
« Last Edit: September 26, 2013, 02:44:44 PM by 1096bimu »
Logged

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7227
  • Harpoon Affectionado
    • View Profile
Re: simple coding question
« Reply #1 on: September 26, 2013, 03:00:24 PM »

Wait a minute, this works in the simulation - as in what you get from the refit screen - but not in combat? That sounds more like a game bug than an error in your code.

The error you are getting is a bit strange - it claims you are passing a string, but 'ship' is a ShipAPI. Maybe try changing the line to:

 proj.setSource( (ShipAPI) ship);


It probably won't work (and will probably say that string can't be cast to ShipAPI), but this could just be a type checking freak out.
Logged

1096bimu

  • Ensign
  • *
  • Posts: 8
    • View Profile
Re: simple coding question
« Reply #2 on: September 26, 2013, 03:29:01 PM »

Wait a minute, this works in the simulation - as in what you get from the refit screen - but not in combat? That sounds more like a game bug than an error in your code.

The error you are getting is a bit strange - it claims you are passing a string, but 'ship' is a ShipAPI. Maybe try changing the line to:

 proj.setSource( (ShipAPI) ship);


It probably won't work (and will probably say that string can't be cast to ShipAPI), but this could just be a type checking freak out.

Yes, it works 100% in simulation in the refit screen, but if I try to fight anyone it gives me that error. And the same thing happens withe (ShipAPI) ship

Caused by: java.lang.ClassCastException: com.fs.starfarer.combat.systems.oo0O cannot be cast to com.fs.starfarer.combat.systems.Object





« Last Edit: September 26, 2013, 03:32:28 PM by 1096bimu »
Logged

LazyWizard

  • Global Moderator
  • Admiral
  • *****
  • Posts: 1365
    • View Profile
    • GitHub Profile
Re: simple coding question
« Reply #3 on: September 26, 2013, 03:40:17 PM »

You might not want to use the old, loose version of CombatUtils. It's extremely outdated and can cause problems with LazyLib (its successor) if another mod does a star import of whatever directory it's in.

Anyway, try it with these alterations and see if it works:
Code
        if (!(stats.getEntity() instanceof ShipAPI))
        {
            return;
        }

        ShipAPI ship = (ShipAPI) stats.getEntity();
        int owner = ship.getOwner();
        Vector2f velocity;
        float facing;
        DamagingProjectileAPI proj;
        for (Iterator iter = Global.getCombatEngine().getProjectiles().iterator();
                iter.hasNext();)
        {
            proj = (DamagingProjectileAPI) iter.next();

            if (proj.getOwner() == ship.getOwner())
            {
                // Don't affect friendly projectiles
                continue;
            }

            // If you ever decide to switch to LazyLib, this would be MathUtils.getDistance() instead
            if (CombatUtils.getDistance(proj, ship) > 280f)
            {
                // Ignore projectiles that are out of range of the field
                continue;
            }

            proj.getVelocity().scale(-1f);

            facing = proj.getFacing();
            proj.setFacing(facing + 180f);
            // send back the way they came

            proj.setOwner(owner);
            //making me the owner will make missiles start to seek enemy targets.

            proj.setSource(ship);
            // this setSource is what gives problems. I think there is some problem with getting the source of projectiles in real combat with pirates.
Logged

1096bimu

  • Ensign
  • *
  • Posts: 8
    • View Profile
Re: simple coding question
« Reply #4 on: September 26, 2013, 04:04:31 PM »

ty, I would love to use lazylib instead, I just couldn't find an example that uses it so...


Edit:
Well I got it working with Lazylib but I get the same error from it. works in simulation but not in real battle.
« Last Edit: September 26, 2013, 06:03:25 PM by 1096bimu »
Logged