<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package wge3.game.engine.ai;

import static com.badlogic.gdx.math.MathUtils.random;
import static com.badlogic.gdx.math.MathUtils.randomBoolean;
import wge3.game.engine.ai.tasks.MeleeAttackTask;
import wge3.game.engine.ai.tasks.MoveTask;
import wge3.game.engine.ai.tasks.RangedAttackTask;
import wge3.game.engine.ai.tasks.WaitTask;
import wge3.game.entity.creatures.Creature;
import wge3.game.entity.creatures.NonPlayer;
import wge3.game.entity.items.Gun;

/**
 *
 * @author chang
 */
public class RangedAI extends AI {

    public RangedAI(NonPlayer creature) {
        super(creature);
    }
    
    @Override
    public void update() {
        if (!isAttacking()) {
            checkForEnemies();
        }
        
        if (!currentTask.isFinished()) {
            currentTask.execute();
            return;
        }
        
        if (randomBoolean(2/3f)) {
            currentTask = new MoveTask(NPC, NPC.getNewMovementDestination());
        } else {
            currentTask = new WaitTask(random(3000));
        }
    }
    
    @Override
        public void checkForEnemies() {
        for (Creature dude : NPC.getEnemiesWithinFOV()) {
            // If dude is within range, attack:
            if (!NPC.getSelectedItem().isGun()) {
                currentTask = new MeleeAttackTask(NPC, dude);
                return;
            }
            if (isWithinGunRange(dude)) {
                currentTask = new RangedAttackTask(NPC, dude);
                return;
            }
        }
    }
    
        public boolean isWithinGunRange(Creature enemy) {
            Gun gun = (Gun) NPC.getSelectedItem();
            
            return super.NPC.getDistanceTo(enemy) <= gun.getRange();
        }
        
}<commit_msg>Optimized RangedAI.checkForEnemies()<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package wge3.game.engine.ai;

import static com.badlogic.gdx.math.MathUtils.random;
import static com.badlogic.gdx.math.MathUtils.randomBoolean;
import wge3.game.engine.ai.tasks.MeleeAttackTask;
import wge3.game.engine.ai.tasks.MoveTask;
import wge3.game.engine.ai.tasks.RangedAttackTask;
import wge3.game.engine.ai.tasks.WaitTask;
import wge3.game.entity.creatures.Creature;
import wge3.game.entity.creatures.NonPlayer;
import wge3.game.entity.items.Gun;

/**
 *
 * @author chang
 */
public class RangedAI extends AI {

    public RangedAI(NonPlayer creature) {
        super(creature);
    }
    
    @Override
    public void update() {
        if (!isAttacking()) {
            checkForEnemies();
        }
        
        if (!currentTask.isFinished()) {
            currentTask.execute();
            return;
        }
        
        if (randomBoolean(2/3f)) {
            currentTask = new MoveTask(NPC, NPC.getNewMovementDestination());
        } else {
            currentTask = new WaitTask(random(3000));
        }
    }
    
    @Override
    public void checkForEnemies() {
        if (NPC.getSelectedItem().isGun()) {
            Gun gun = (Gun) NPC.getSelectedItem();
            int gunRange2 = gun.getRange() * gun.getRange();
            for (Creature dude : NPC.getEnemiesWithinFOV()) {
                if (NPC.getDistance2To(dude) <= gunRange2) {
                    currentTask = new RangedAttackTask(NPC, dude);
                    return;
                }
            }
        } else {
            for (Creature dude : NPC.getEnemiesWithinFOV()) {
                currentTask = new MeleeAttackTask(NPC, dude);
                return;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10757693302301526, 'fraction_numerical': 0.008016550297388157, 'mean_word_length': 2.0894568690095845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3388712', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1157, 'n_words': 238}