<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snowballmadness;

import org.bukkit.*;
import org.bukkit.entity.*;
import org.bukkit.util.*;

/**
 * This logic attracts entities to the snowball; more power makes the
 * attractions stronger and longer ranged.
 *
 * @author DanJ
 */
public class MagneticSnowballLogic extends SnowballLogic {

    private Vector previousTarget;

    @Override
    public void tick(Snowball snowball, SnowballInfo info) {
        super.tick(snowball, info);

        World world = snowball.getWorld();
        Vector target = snowball.getLocation().toVector();

        // on the first tick, we approximate the previous location by
        // using the shooter's present location. Close enough!
        if (previousTarget == null) {
            Location shooterLoc = snowball.getShooter().getLocation();

            if (shooterLoc.getWorld() == world) {
                previousTarget = shooterLoc.toVector();
            } else {
                previousTarget = target;
            }
        }

        for (Entity victim : world.getEntities()) {
            if (!(victim instanceof Snowball)) {
                accelerate(victim, target, info.power);
                accelerate(victim, previousTarget, info.power);
            } else {
                accelerate(victim, target, (0.001 * info.power));
                //victim is another snowball, we make them slightly interactive
            }
        }
        previousTarget = target;
    }

    /**
     * This method applies acceleration of a victim towards a target. We a
     * vector for 'target' instead of a Location because the methods we need are
     * found on it; semantically this is a location.
     *
     * @param victim The entity to accelerate.
     * @param target The target to move the entity towards.
     * @param power The snowball power; accelerate speed depends on this.
     */
    private void accelerate(Entity victim, Vector target, double power) {
        Vector eLoc = victim.getLocation().toVector();
        double dist = target.distance(eLoc);

        if (dist > 0.0 && dist < 32.0) {
            double factor = power / dist;
            if (factor > 0.0001) {
                Vector vel = victim.getVelocity().clone();
                double restraint = 1.0 / ((Math.pow(vel.length(), 3)) + 1.0);
                factor *= restraint;

                Vector d = target.clone();
                d.subtract(eLoc);
                d.normalize();
                d.multiply(factor);
                vel.add(d);
                victim.setVelocity(vel);
            }
        }
    }
}<commit_msg>Removed the distance limit from magnetic snowballs, and compensated by slightly increasing the fall-off. It's sort of possible to resist the traction but still dangerous. Hopefully this will work. Still not advisable to fire them directly! You can sort of fly after them if you don't mind being smacked against the ground a lot :)<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snowballmadness;

import org.bukkit.*;
import org.bukkit.entity.*;
import org.bukkit.util.*;

/**
 * This logic attracts entities to the snowball; more power makes the
 * attractions stronger and longer ranged.
 *
 * @author DanJ
 */
public class MagneticSnowballLogic extends SnowballLogic {

    private Vector previousTarget;

    @Override
    public void tick(Snowball snowball, SnowballInfo info) {
        super.tick(snowball, info);

        World world = snowball.getWorld();
        Vector target = snowball.getLocation().toVector();

        // on the first tick, we approximate the previous location by
        // using the shooter's present location. Close enough!
        if (previousTarget == null) {
            Location shooterLoc = snowball.getShooter().getLocation();

            if (shooterLoc.getWorld() == world) {
                previousTarget = shooterLoc.toVector();
            } else {
                previousTarget = target;
            }
        }

        for (Entity victim : world.getEntities()) {
            if (!(victim instanceof Snowball)) {
                accelerate(victim, target, info.power);
                accelerate(victim, previousTarget, info.power);
            } else {
                accelerate(victim, target, (0.01 * info.power));
                //victim is another snowball, we make them slightly interactive
            }
        }
        previousTarget = target;
    }

    /**
     * This method applies acceleration of a victim towards a target. We a
     * vector for 'target' instead of a Location because the methods we need are
     * found on it; semantically this is a location.
     *
     * @param victim The entity to accelerate.
     * @param target The target to move the entity towards.
     * @param power The snowball power; accelerate speed depends on this.
     */
    private void accelerate(Entity victim, Vector target, double power) {
        Vector eLoc = victim.getLocation().toVector();
        double dist = target.distance(eLoc);

        if (dist > 0.0) {
            double factor = power / dist;
            if (factor > 0.0001) {
                Vector vel = victim.getVelocity().clone();
                double restraint = 1.0 / ((Math.pow(vel.length(), 4)) + 1.0);
                factor *= restraint;

                Vector d = target.clone();
                d.subtract(eLoc);
                d.normalize();
                d.multiply(factor);
                vel.add(d);
                victim.setVelocity(vel);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09425004395990856, 'fraction_numerical': 0.005978547564621066, 'mean_word_length': 2.022316684378321, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3138622', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1414, 'n_words': 508}