Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Pages: 1 ... 10 11 [12] 13 14 ... 710

Author Topic: Misc modding questions that are too minor to warrant their own thread  (Read 1726086 times)

Wyvern

  • Admiral
  • *****
  • Posts: 3803
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #165 on: January 22, 2013, 04:22:45 PM »

I'd strongly suggest fixing it in both places - dealing with x and y separately is a great way to get a system that has biases either towards or against cardinal directions.

For another example, suppose you're taking a ship moving at 100,200 (actual speed ~223), and you want to reduce its speed to 100.  Simply setting velocity to 100,100 will not only not reduce the speed to 100 (it'll actually be moving at ~141), it also changes the direction in which the ship is moving.  The actual correct behavior would result in a velocity vector of roughly 45,89 - which is actually at speed ~100, and, just as important, going in the same direction as the initial vector.
(Note: values in above example rounded to nearest whole number for brevity.)

This is a slightly extreme example to make the point - but even if you're making small adjustments, you'll still get behavior that's slightly off.  For your forced deceleration clamping, for example, you might actually see something like:
100,200, clamped to speed 200: your result: 100,200; accurate result: 89.4,178.9
100,200, clamped to speed 190: your result: 100,190; accurate result: 85.0,169.9
etc.
Logged
Wyvern is 100% correct about the math.

EnderNerdcore

  • Commander
  • ***
  • Posts: 172
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #166 on: January 22, 2013, 04:25:59 PM »

That's a fair point, though those conditions are most likely to happen in the (very) small frame of time during the initial system activation while the acceleration is very high if the player is rapidly changing the directions they are trying to move in.

I suppose fixing it in one place will generate re-usable code to fix it in the other.  :)


EDIT: it's either that or I write a library to convert the whole thing to polar coordinates and back again. Harder to write at the beginning but so much easier to deal with for future systems.
« Last Edit: January 22, 2013, 04:27:39 PM by EnderNerdcore »
Logged

Wyvern

  • Admiral
  • *****
  • Posts: 3803
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #167 on: January 22, 2013, 04:28:38 PM »

Yup!  And LazyWizard's utility library probably has functions already built to do this sort of thing for you; I just have no idea what they are because I haven't looked at it yet.

Edit: I'd avoid polar coordinates, myself.  I find them to be a bit of a nuisance - though it depends on what you're doing.  For just adjusting speed, yeah, polar saves you a call to square root; if you ever need to add two vectors, though, it's a pain and a half.
« Last Edit: January 22, 2013, 04:31:44 PM by Wyvern »
Logged
Wyvern is 100% correct about the math.

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24125
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #168 on: January 22, 2013, 04:37:50 PM »

Oversight on my part. Rusty java skills + smoking weed while doing vector calculations made me completely overlook the fact that we could end up with too much magnitude of velocity.

Let's not go there on this forum.


As far as velocity being polar vs cartesian, it's fairly simple to convert between the two with trig functions :)
Logged

EnderNerdcore

  • Commander
  • ***
  • Posts: 172
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #169 on: January 22, 2013, 06:24:19 PM »

Well that makes things... rather simpler. Quick and dirty version below. Does NOT take into account player skills.


Code
                float currentSpeed = (float) math.sqrt((ourShip.getVelocity().getX() * ourShip.getVelocity().getX()) + (ourShip.getVelocity().getY() * ourShip.getVelocity().getY()));
                float desiredSpeed = stats.getMaxSpeed().getBaseValue();
                if (fluxIsAtZero) { desiredSpeed += 50f; }
                float multiplier =  desiredSpeed / currentSpeed;
                
                if (currentSpeed > desiredSpeed) {                    
                    ourShip.getVelocity().x *= multiplier;
                    ourShip.getVelocity().y *= multiplier;
                }
                  

EDIT4: that seems to do the trick. Now I just need to fetch player skills for the zero flux speed
« Last Edit: January 22, 2013, 06:43:11 PM by EnderNerdcore »
Logged

Wyvern

  • Admiral
  • *****
  • Posts: 3803
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #170 on: January 22, 2013, 06:36:29 PM »

Ah... not quite.  You'll want to add the +50 to desiredSpeed, not velocity.x and velocity.y.
Logged
Wyvern is 100% correct about the math.

EnderNerdcore

  • Commander
  • ***
  • Posts: 172
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #171 on: January 22, 2013, 06:40:27 PM »

Ah... not quite.  You'll want to add the +50 to desiredSpeed, not velocity.x and velocity.y.
Yep, just realized that in the past 30 seconds lol
Logged

EnderNerdcore

  • Commander
  • ***
  • Posts: 172
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #172 on: January 22, 2013, 07:01:27 PM »

Simpler than I thought, just had to use:

stats.getZeroFluxSpeedBoost().getModifiedValue();

and instead of comparing the flux to zero, I check that it is less than or equal to:

stats.getZeroFluxMinimumFluxLevel().getModifiedValue()


The lesson from all of this?
If your initial code is based on an incorrect assumption (in my case, that Velocity was in polar coordinates) then it's better to wipe clean and start over than to incrementally improve your code, because you'll end up with a mess.
« Last Edit: January 22, 2013, 07:08:03 PM by EnderNerdcore »
Logged

Cycerin

  • Admiral
  • *****
  • Posts: 1665
  • beyond the infinite void
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #173 on: January 23, 2013, 02:44:29 PM »

Thanks a lot to everyone for big help on getting this to work. It runs like a charm right now. ;D
Logged

EnderNerdcore

  • Commander
  • ***
  • Posts: 172
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #174 on: January 23, 2013, 07:14:55 PM »

Hey guys, I'm getting a null pointer error in here that I'm unable to track down. I'm writing an onHit effect for a weapon shot. Basically, it deals bonus energy damage if it impacts the ship (and not its shields).

The game throws the NPE when this happens (i.e., testing against a Buffalo mk2)

The NPE from the log is here:
Code
36959 [Thread-6] ERROR com.fs.starfarer.combat.D  - java.lang.NullPointerException
java.lang.NullPointerException
at data.scripts.weapons.ShardOnHitEffect.onHit(ShardOnHitEffect.java:27)
at com.fs.starfarer.combat.entities.BallisticProjectile.super(Unknown Source)
at com.fs.starfarer.combat.entities.BallisticProjectile.notifyDealtDamage(Unknown Source)
at com.fs.starfarer.combat.class.super.O0OO.super(Unknown Source)
at com.fs.starfarer.combat.class.B.o00000(Unknown Source)
at com.fs.starfarer.combat.class.B.o00000(Unknown Source)
at com.fs.starfarer.combat.CombatEngine.advance(Unknown Source)
at com.fs.starfarer.combat.F.ÖÖÒ000(Unknown Source)
at com.fs.A.A.new(Unknown Source)
at com.fs.starfarer.combat.D.o00000(Unknown Source)
at com.fs.starfarer.StarfarerLauncher$2.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)

And here's the concerned area of code:

Code
public void onHit(DamagingProjectileAPI projectile, CombatEntityAPI target, Vector2f point, boolean shieldHit, CombatEngineAPI engine) {
if (!shieldHit && target instanceof ShipAPI) {
                        particleVelocity.x = ((float) Math.random() * 10f); //give the particle a random velocity.
                        particleVelocity.y = ((float) Math.random() * 10f); //Math.random() results in a number between 0.0 and 1.0
engine.applyDamage(target, point, damageAmount, DamageType.ENERGY, empAmount, false, dealsSoftFlux, engine);
                        engine.addHitParticle(point, particleVelocity, particleSize, particleBrightness, particleDuration, particleColor);
soundToPlay.playSound(soundName, pitch, volume, point, particleVelocity);
}
}
Logged

Nekora

  • Ensign
  • *
  • Posts: 28
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #175 on: January 23, 2013, 10:46:17 PM »

Sometesting of the code shows that
Code
            particleVelocity.x = ((float) Math.random() * 10f); //give the particle a random velocity.
            particleVelocity.y = ((float) Math.random() * 10f); //Math.random() results in a number between 0.0 and 1.0

are causeing the problems. As for the corect code? not sure ill look into it.
Oh also the damage spawning line of code, you had "engine" as the source of the damage. I changed that to projectile.getSource(), dont know if that has any relevance though as it didnt fix the problem till i got rid of the .x .y lines of code.

EDIT*
After some testing i got the code to work... as for why? i dont know. Maybe its because i set up the particalVelocity vairable useing getVelocity()... not idea. Well:

heres what i used that worked without error:
Code
package data.scripts.weapons;

import java.awt.Color;

import org.lwjgl.util.vector.Vector2f;

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.DamagingProjectileAPI;
import com.fs.starfarer.api.combat.OnHitEffectPlugin;
import com.fs.starfarer.api.combat.ShipAPI;

public class ExtraHitEffect implements OnHitEffectPlugin {

public void onHit(DamagingProjectileAPI projectile, CombatEntityAPI target, Vector2f point, boolean shieldHit, CombatEngineAPI engine) {
if (!shieldHit && target instanceof ShipAPI)
{
//set vairables for the partical. Could do inside the call but will do it here
float particleSize = 15f;
float particleBrightness = 1f;
float particleDuration = 2f;
Color particleColor = new Color(85, 25, 215, 255);
Vector2f particleVelocity = projectile.getVelocity(); // Lines below were causing issues, so i replaced them for testing
                        particleVelocity.x = ((float) Math.random() * 10f); //give the particle a random velocity.
                        particleVelocity.y = ((float) Math.random() * 10f); //Math.random() results in a number between 0.0 and 1.0

//set damage vairables
float damageAmount = projectile.getDamageAmount() * .25f; //extra damage is 1/4 of full
float empAmount = projectile.getEmpAmount(); //extra emp is... well full amount of normal hit

engine.applyDamage(target, point, damageAmount, DamageType.ENERGY, empAmount, false, false, projectile.getSource());
                        engine.addHitParticle(point, particleVelocity, particleSize, particleBrightness, particleDuration, particleColor);
//soundToPlay.playSound(soundName, pitch, volume, point, particleVelocity);
}
}
}

Also, anyone able to take a look at my question earlier yet? http://fractalsoftworks.com/forum/index.php?topic=5061.msg85513#msg85513
« Last Edit: January 23, 2013, 11:03:27 PM by Nekora »
Logged

EnderNerdcore

  • Commander
  • ***
  • Posts: 172
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #176 on: January 23, 2013, 11:11:18 PM »

Solved it. Turns out I can't just call the sound player like that. Instead, I have to use:

Global.getSoundPlayer().playSound(soundName, pitch, volume, point, projectile.getVelocity());

To use that, we have to add:

import com.fs.starfarer.api.Global;
Logged

Nekora

  • Ensign
  • *
  • Posts: 28
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #177 on: January 24, 2013, 12:18:32 AM »

Nice catch thereEnder nerd code, didnt even see that one. Okay heres another, trying to make a shot bounce off sheilds. i can spawn the shots by this:
Code
		if (shieldHit && target instanceof ShipAPI)
{
float angle = ((float) Math.random() * 360f) - 180f;
engine.spawnProjectile(projectile.getSource(), projectile.getWeapon(),
projectile.getWeapon().getId(), point, angle, null);
}
Now i know that i can get the location of the projectile by useing "point" and i can get the location or the target by useing target.getLocation(), but can anyone show me how to calculate the angle from point to target.getLocation()? Im asumeing theres trig involved >_<
Logged

LazyWizard

  • Global Moderator
  • Admiral
  • *****
  • Posts: 1365
    • View Profile
    • GitHub Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #178 on: January 24, 2013, 06:09:58 AM »

Nice catch thereEnder nerd code, didnt even see that one. Okay heres another, trying to make a shot bounce off sheilds. i can spawn the shots by this:
Code
		if (shieldHit && target instanceof ShipAPI)
{
float angle = ((float) Math.random() * 360f) - 180f;
engine.spawnProjectile(projectile.getSource(), projectile.getWeapon(),
projectile.getWeapon().getId(), point, angle, null);
}
Now i know that i can get the location of the projectile by useing "point" and i can get the location or the target by useing target.getLocation(), but can anyone show me how to calculate the angle from point to target.getLocation()? Im asumeing theres trig involved >_<

Code
float angle = (float) Math.toDegrees(Vector2f.angle(point, target.getLocation());

Just a heads up, there's a bug in the current version that causes redirected ballistic projectiles to not render correctly.

Edit: never mind, you're not redirecting, you're creating an entirely new projectile. Carry on. :)
« Last Edit: January 24, 2013, 06:23:37 AM by LazyWizard »
Logged

EnderNerdcore

  • Commander
  • ***
  • Posts: 172
    • View Profile
Re: Misc modding questions that are too minor to warrant their own thread
« Reply #179 on: January 24, 2013, 06:14:36 PM »

When I use:

engine.applyDamage(target, point, damageAmount, DamageType.KINETIC, empAmount, false, dealsSoftFlux, engine);

Does it just straight-up apply "damageAmount" to the ship ignoring armor and the damage type, or does it internally deal some fraction of that damage based on whether it hits armor or hull?  (I'm assuming not hitting shields, here).

The reason I ask is that I want this next line to show the amount of damage it really did:

engine.addFloatingDamageText(point, damageAmount, Color.blue, target, projectile.getSource().getMutableStats().getEntity());

  • If the answer to the first question is "no", then is there a convenient API method to calculate the correct damage?
  • If the answer to the first question is "yes", then how do I ensure that the engine takes armor into account when calculating this damage?
  • Or is the answer that I do not understand how armor in Starsector works and it is an amount-per-grid and it doesn't reduce incoming damage? (in which case, how do we know if it's hitting hull or armor since Kinetic does half damage to armor and full damage to hull?)

EDIT: Updated the question for Kinetic since Energy always does 100% damage.
« Last Edit: January 25, 2013, 07:21:48 AM by EnderNerdcore »
Logged
Pages: 1 ... 10 11 [12] 13 14 ... 710