<commit_before>package bbth.game.ai;

import java.util.ArrayList;
import java.util.EnumMap;

import bbth.engine.ai.FlockRulesCalculator;
import bbth.game.BBTHGame;
import bbth.game.Team;
import bbth.game.Unit;

public class AIController {
	EnumMap<Team, ArrayList<Unit>> m_entities;
	EnumMap<Team, FlockRulesCalculator> m_flocks;
	
	DefensiveAI m_aggressive;
	
	private float m_fraction_to_update = 0.33f;
	
	int m_last_updated = 0;

	public AIController() {
		m_aggressive = new DefensiveAI();
		
		m_flocks = new EnumMap<Team, FlockRulesCalculator>(Team.class);
		
		for (Team t : Team.values()) {
			m_flocks.put(t, new FlockRulesCalculator());
		}
        
    	m_entities = new EnumMap<Team, ArrayList<Unit>>(Team.class);
    	
    	for (Team t : Team.values()) {
			m_entities.put(t, new ArrayList<Unit>());
		}
   	}
	
	public void addEntity(Unit u) {
		m_entities.get(u.getTeam()).add(u);
		m_flocks.get(u.getTeam()).addObject(u);
	}
	
	public void removeEntity(Unit u) {
		m_entities.get(u.getTeam()).add(u);
		m_flocks.get(u.getTeam()).addObject(u);
	}
	
	public ArrayList<Unit> getEnemies(Unit u) {
		return m_entities.get(u.getTeam().getOppositeTeam());
	}
	
	public void update() {
		for (Team t : Team.values()) {
			update(m_entities.get(t), m_flocks.get(t));
		}
	}
	
	private void update(ArrayList<Unit> entities, FlockRulesCalculator flock) {
		int size = entities.size();
		int num_to_update = (int) ((size * m_fraction_to_update)+1);
		
		if (size == 0) {
			return;
		}
		
		int i = m_last_updated;
		while (num_to_update > 0) {
			Unit entity = entities.get(i);
			
			// TODO: Use the correct AI for the individual unit.
			m_aggressive.update(entity, this, flock);
			
			num_to_update--;

			if (i >= size-1) {
				i = 0;
			} else {
				i++;
			}
		}
		
		if (i >= size-1) {
			m_last_updated = 0;
		} else {
			m_last_updated = i+1;
		}
	}

	public float getWidth() {
		return BBTHGame.WIDTH; 
	}
	
	public float getHeight() {
		return BBTHGame.HEIGHT;<commit_msg>fixed incremental ai updates<commit_after>package bbth.game.ai;

import java.util.ArrayList;
import java.util.EnumMap;

import android.util.Log;
import bbth.engine.ai.FlockRulesCalculator;
import bbth.game.BBTHGame;
import bbth.game.Team;
import bbth.game.Unit;

public class AIController {
	EnumMap<Team, ArrayList<Unit>> m_entities;
	EnumMap<Team, Integer> m_last_updated;
	EnumMap<Team, FlockRulesCalculator> m_flocks;
	
	DefensiveAI m_aggressive;
	
	private float m_fraction_to_update = 0.33f;
	
	public AIController() {
		m_aggressive = new DefensiveAI();
		
		m_flocks = new EnumMap<Team, FlockRulesCalculator>(Team.class);
		m_last_updated = new EnumMap<Team, Integer>(Team.class);
    	m_entities = new EnumMap<Team, ArrayList<Unit>>(Team.class);
		
		for (Team t : Team.values()) {
			m_flocks.put(t, new FlockRulesCalculator());
			m_last_updated.put(t, 0);
			m_entities.put(t, new ArrayList<Unit>());
		}
   	}
	
	public void addEntity(Unit u) {
		m_entities.get(u.getTeam()).add(u);
		m_flocks.get(u.getTeam()).addObject(u);
	}
	
	public void removeEntity(Unit u) {
		m_entities.get(u.getTeam()).add(u);
		m_flocks.get(u.getTeam()).addObject(u);
	}
	
	public ArrayList<Unit> getEnemies(Unit u) {
		return m_entities.get(u.getTeam().getOppositeTeam());
	}
	
	public void update() {
		for (Team t : Team.values()) {
			update(m_entities.get(t), m_flocks.get(t), t);
		}
	}
	
	private void update(ArrayList<Unit> entities, FlockRulesCalculator flock, Team team) {
		int size = entities.size();
		int num_to_update = (int) ((size * m_fraction_to_update)+1);
		
		if (size == 0) {
			return;
		}
		
		int last_updated = m_last_updated.get(team);
		
		if (last_updated > size-1) {
			last_updated = 0;
		}
		
		int i = last_updated;
		while (num_to_update > 0) {
			Unit entity = entities.get(i);
			
			// TODO: Use the correct AI for the individual unit.
			m_aggressive.update(entity, this, flock);
			
			num_to_update--;

			if (i >= size-1) {
				i = 0;
			} else {
				i++;
			}
		}
		
		if (i >= size-1) {
			m_last_updated.put(team, 0);
		} else {
			m_last_updated.put(team, i+1);
		}
	}

	public float getWidth() {
		return BBTHGame.WIDTH; 
	}
	
	public float getHeight() {
		return BBTHGame.HEIGHT;
{'fraction_non_alphanumeric': 0.13997148288973385, 'fraction_numerical': 0.006178707224334601, 'mean_word_length': 3.5799782372143634, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 30, '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': '5986275', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1575, 'n_words': 319}