Jump to content
SubSpace Forum Network

Recommended Posts

Posted

since the fusion site went down, and i cant edit my other post, im putting the source code here.

 

its a functional bot, but needs adjustments. any help i can be contacted via a ?message proz

 

good luck

 

package twcore.bots.multibot.baseduel;

import twcore.bots.MultiModule;
import twcore.core.BotAction;
import twcore.core.EventRequester;
import twcore.core.events.FrequencyShipChange;
import twcore.core.events.FrequencyChange;
import twcore.core.events.Message;
import twcore.core.events.PlayerEntered;
import twcore.core.events.PlayerLeft;
import twcore.core.events.PlayerDeath;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import twcore.core.game.Player;
import twcore.core.util.ModuleEventRequester;
import twcore.core.events.PlayerPosition;
import java.util.Random;
import java.util.TimerTask;
//import java.util.Formatter;
//import twcore.core.BotSettings;
//import java.io.File;
//import twcore.core.events.LoggedOn;
//import java.util.Map;
//import java.util.HashMap;
//import twcore.core.*;
//import java.util.Iterator;
//import twcore.core.game.Arena;
//import twcore.core.game.Ship;
//import twcore.core.util.Tools;
//import twcore.core.command.TWCoreException;

//baseduel extension by Proz from SSCI FUSION, SSCU Redstar, and Extreme Games zones.
public class baseduel extends MultiModule{
//playingFreqs contains the freqs challenged or in duel.
public static HashSet <Short> playingFreqs = new HashSet<Short>();
//playingIds holds the name of the players challenged or currently dueling.
public static HashSet <Short> playingIds = new HashSet<Short>();
//defines which bases are playing
public static Set <Short> playingBases = Collections.synchronizedSet(new HashSet<Short>());
//has the freqs currently challenged
public static HashSet <Short> chsFreqs = new HashSet<Short>();
//baseduel base object array
BaseDuelBase base[] = new BaseDuelBase[13];
//challenge freqs control object
chFreqs challenge[] = new chFreqs[13];
//bot frequency holder 
public Short botFreq;
//TODO: should be changed to centerbase and generate a random position between the centerbase safe limits. 
public final int[] center = {512,416};//{500,510};//{514,398};
public final int[] graveyard={512,512};//{510,690};//
public static HashSet <Short> idsAllowedToJoin = new HashSet<Short>();
//see int []center
public final int[] centerBase={};
//center safe square, format is {x1,y1,x2,y2}
public static final int[] centerSafe={503,505,520,520};//{503,682,520,697}; //{502,496,524,522};
/*all of the coordinates for the playing bases, if a new base needs to be added it should be done here.
TODO:basecoord could be parameterized so it could be read from a file.*/
public static final int[][] basecoord={
	/*{724,201,699,201},
		{842,438,827,453},
		{686,730,686,686},
		{476,874,552,874},
		{286,675,344,736},
		{150,416,218,487},
		{322,219,290,172},
		{532,97,486,97}	
	 */
	{482,127,543,127},
	{650,154,709,212},
	{769,277,850,262},
	{841,403,841,488},
	{818,656,863,656},
	{725,800,752,800},
	{492,855,531,855},
	{303,820,271,774},
	{191,647,144,610},
	{94,446,195,446},
	{140,297,245,243},
	{342,157,357,191},
	{570,736,454,736}};
public String botName; 

//total of bases available 
final int totalbases=(basecoord.length);

public void requestEvents(ModuleEventRequester events)	{
	events.request(this, EventRequester.PLAYER_DEATH);
	events.request(this, EventRequester.FREQUENCY_CHANGE); 
	events.request(this, EventRequester.PLAYER_LEFT);
	events.request(this, EventRequester.FREQUENCY_SHIP_CHANGE);
	events.request(this, EventRequester.ARENA_JOINED);
	events.request(this, EventRequester.PLAYER_ENTERED);
	events.request(this, EventRequester.PLAYER_POSITION); 
	events.request(this, EventRequester.PLAYER_LEFT);
	//events.request(this, EventRequester.MESSAGE);  
	// req.request(EventRequester.PLAYER_LEFT);
	// req.request(EventRequester.PLAYER_DEATH);
	// req.request(EventRequester.PRIZE);
	// req.request(EventRequester.SCORE_UPDATE);
	// req.request(EventRequester.WEAPON_FIRED);
	// req.request(EventRequester.FREQUENCY_CHANGE);
	// req.request(EventRequester.FREQUENCY_SHIP_CHANGE);
}
public void init() {
	//m_botAction.setPlayerPositionUpdating( 100 );
	m_botAction.sendArenaMessage("Base duel loaded with "+String.valueOf(totalbases)+" bases");
	botName = m_botAction.getBotName();
	
	/*TODO: read bot settings from a file.
	 * 
		Random generator = new Random();
		int totalBases = m_botAction.getBotSettings().getInt("totalbases");
		m_botAction.getBotSettings().getInt("base"+Integer.toString(baseNum));		
		String coreRoot = m_botAction.getGeneralSettings().getString(
		"Core Location");
		BotSettings cfg = new BotSettings(coreRoot+"/twcore/bots/twbot/"+"baseduel.cfg");
		int totalBases = cfg.getInt("totalbases");
	 **/
	/* }*/
}

/**
 * Handles players position information.
 */
public void handleEvent(PlayerPosition event){
	// posX and posY stores the player's position.
	int posX=(event.getXLocation())/16; 
	int posY=(event.getYLocation())/16; 
	Short nameid=event.getPlayerID();
	

	if(nameid!=null){
		Short freq= m_botAction.getPlayer(nameid).getFrequency();
		//this checks if player is in the graveyard. 
		if ((posX>=centerSafe[0])&&(posX<=centerSafe[2])&&
				(posY>=centerSafe[1])&&(posY<=centerSafe[3])){
			if (baseduel.playingIds.contains(nameid)==false){
				m_botAction.warpTo(nameid, center[0], center[1]);
			}
			if (baseduel.playingIds.contains(nameid)==true){
				checkOtherPositions(posX,posY,freq,nameid);
			}			
		}else{
			checkOtherPositions(posX,posY,freq,nameid);
		}
	}
}

/**
 * Handles messages.
 */
public void handleEvent( Message event )
{
	if(event.getMessageType() == Message.PUBLIC_MESSAGE)
	{	
		String name = m_botAction.getPlayerName( event.getPlayerID() );
		String message = event.getMessage();
		if(name!=null){
		handleCommand(name,message);
		}
	}
	if(event.getMessageType()==Message.PRIVATE_MESSAGE){
		String name = m_botAction.getPlayerName( event.getPlayerID() );
		String message = event.getMessage();
		if(name!=null){
			handlePrivateCommand(name,message);
		}
		
	}
}

/**
 * Triggered when a player enters the arena.
 */
public void handleEvent(PlayerEntered event) 
{
	Short nameid = event.getPlayerID();
	if (baseduel.playingIds.contains(nameid)==false){
		String name = m_botAction.getPlayerName(event.getPlayerID());
		if(name!=null){
			m_botAction.sendPrivateMessage(nameid,"Welcome to baseduel arena, for help, type  !duelhelp");
		}
		//m_botAction.sendPrivateMessage(nameid,"REMEMBER THIS IS STILL ON DEVELOPMENT FOR MORE INFO GO TO");
		//m_botAction.sendPrivateMessage(nameid,"http://ss-fusion.com/phpBB3/viewtopic.php?f=3&t=1689  -Proz");
	}
}

/**
 * This event is triggered when the player leaves the arena, in case the player left a duel,
 * his id is removed from the playing list.
 */
public void handleEvent(PlayerLeft event){
	Short nameid = event.getPlayerID();
	if (baseduel.playingIds.contains(nameid)){
		if(nameid!=null){
			baseduel.playingIds.remove(nameid);
			checkEmptyFreqs(nameid);
			removeIdFromDuel(nameid);
		}
	}
}
/**
 * This is triggered on a death, it might be the wrong way to handle it, i think im missing
 * handleling nulls when getting the player information.
 */
public void handleEvent(PlayerDeath event){
	Short DeathId=event.getKilleeID();
	Short killerId=event.getKillerID();
	int i=0;
	for(i=0;i<totalbases;i++){
		if(base[i]!=null){
			if (base[i].hasId(DeathId)){

				if(DeathId!=null&&killerId!=null){
					base[i].addKill(killerId, DeathId);
				}
				break;
			}
		}
	}
}
/**
 * Triggered when the player changes ship and might change frequency.
 * in the event that the player might spec, it removes its name from the list, and 
 * checks for empty frequencies.
 */

public void handleEvent(FrequencyShipChange event) {
	Short nameid = event.getPlayerID();
	Short freq;
	int ship=m_botAction.getPlayer(Integer.valueOf(nameid)).getShipType();
	freq = m_botAction.getPlayer(Integer.valueOf(nameid)).getFrequency();
	if (baseduel.playingIds.contains(nameid)==false){
		String name = m_botAction.getPlayerName(event.getPlayerID());
		m_botAction.warpTo(name, center[0], center[1]);
	}else if(baseduel.playingIds.contains(nameid)==true&&ship==0){
		playingIds.remove(nameid);
		
		if(nameid!=null){
			checkEmptyFreqs(nameid);
			removeIdFromDuel(nameid);
		}
		
		
	}
	int j=0;
	for (j=0;j<challenge.length;j++){
		if ((challenge[j]!=null)&&(ship==0)){
			if(nameid!=null){
				if (challenge[j].hasId(nameid)){					  
					challenge[j].cancelDuel(nameid);
					break;
				}
			}
		}
	}
}

/**
 * If a player changes frequencies during a duel, it removes its name from the list, and checks for
 * empty frequencies.
 */
public void handleEvent(FrequencyChange event){
	Short nameid = event.getPlayerID();
	Short freq;
	freq = m_botAction.getPlayer(Integer.valueOf(nameid)).getFrequency();
	if(playingFreqs.contains(freq)&&(idsAllowedToJoin.contains(nameid)==false)){
		m_botAction.spec(nameid);
		m_botAction.spec(nameid);
		m_botAction.setFreq(nameid, botFreq); 		
		m_botAction.sendPrivateMessage(nameid, "Frequency is engaged in a duel and its currently locked");
	}else{
		if(playingIds.contains(nameid)){
			playingIds.remove(nameid);
			m_botAction.warpTo(nameid, center[0], center[1]);
		}
	}
	//check if someone leaves a playing freq so playing bases and freqs are scrapped
	if(nameid!=null){
		checkEmptyFreqs(nameid);
		removeIdFromDuel(nameid);
	}
	int j=0;
	for (j=0;j<challenge.length;j++){
		if (challenge[j]!=null){
			if (challenge[j].hasId(nameid)){					  
				challenge[j].cancelDuel(nameid);
				break;
			}
			if(challenge[j].hasFreq(freq)){
				challenge[j].cancelDuel3();
				break;
			}
		}
	}
}

/**
 * when a player leaves a frequency, this verifies if the player's previous frequency is left empty.
 * if so, it will award the opposing freq a win.
 * @param nameid
 */
private void checkEmptyFreqs(Short nameid){
	int x=0;
	for (x=0;x<totalbases;x++){
		if (base[x]!=null){
			base[x].emptyFreqCheck(nameid);
		}
	}
}
private void removeIdFromDuel(Short nameid){
	int x=0;
	for (x=0;x<totalbases;x++){
		if (base[x]!=null){
			if(base[x].hasId(nameid)==true){
				base[x].deleteid(nameid);
				
			}
		}
	}
}

/**
 * This is for smods and up, it makes the bot say something. (filters ! and * commands for security
 * reasons).
 * 
 * @param name the player who initiated the message.
 * @param message the message that the bot will say.
 */

public void handlePrivateCommand( String name, String message){
	if( (message.startsWith( "!say " )&& m_botAction.getOperatorList().isModerator(name))){
		if((message.startsWith( "!say !" ))||(message.startsWith( "!say *" ))){
			//m_botAction.warnPlayer(name, "No ! or * commands ");
			m_botAction.sendPrivateMessage(name, "command ignored");
		}else{
			message=message.substring(5, message.length());
			m_botAction.sendPublicMessage(message);
			//m_botAction.sendSmartPrivateMessage("Proz", name+" " + message);
		}
	}
	if( (message.startsWith( "!j " )) || (message.startsWith( "!J " )) ){
		Short nameId = (short)m_botAction.getPlayerID(name);
		Short joinFreq=null;
		try{
			joinFreq = Short.valueOf(message.substring(3));
			//m_botAction.sendPublicMessage(String.valueOf(joinFreq));
			joinDuel(nameId, joinFreq);
		}catch (NumberFormatException e){
			m_botAction.sendSmartPrivateMessage(name, "The command requires the number of a frequency to join ex: :"+
			m_botAction.getBotName() +":!j 0");
		}
		finally{
		}
	}
}

public void joinDuel(Short id, Short freq){
	int x=0;
	for (x=0;x<totalbases;x++){
		if (base[x]!=null){
			if(base[x].hasFreq(freq)==true){
				base[x].addPlayer(id, freq);
			}
		}
	}
}


/**
 * 
 * @param name the player who initiated the message
 * @param message
 */
public void handleCommand( String name, String message)
{
	Short nameId = (short)m_botAction.getPlayerID(name);
	//this returns the scores for the current duels.
	if(message.toLowerCase().contentEquals("!s") || message.toLowerCase().contentEquals("!score")){
		int x=0;
		boolean atLeastOneDuel=false;
		for (x=0;x<totalbases;x++){
			if (base[x]!=null){
				base[x].baseDuelInfo(nameId);
				atLeastOneDuel=true;
			}
		}
		if(atLeastOneDuel==false){
			m_botAction.sendPrivateMessage(nameId, "No Current Duels");
		}
	}
	//!r rejects a duel
	if(message.toLowerCase().contentEquals("!r")){
		Short freq2=m_botAction.getPlayer(name).getFrequency();
		int j=0;
		Short freq1;
		boolean freqFound=false;
		for (j=0;j<challenge.length;j++){
			if (challenge[j]!=null){
				if (challenge[j].hasFreq(freq2)){
					freq1=challenge[j].otherFreq(freq2);
					challenge[j].cancelDuel2();
					freqFound=true;
					m_botAction.sendOpposingTeamMessageByFrequency(freq1, "Duel rejected");
					m_botAction.sendOpposingTeamMessageByFrequency(freq2, "Duel rejected");
					break;
				}
			}
		}
		if(freqFound==false){
			m_botAction.sendSmartPrivateMessage(name, "No pending Duels.");
		}
	}
	//accepts a duel and sends the frequency information to initbase method.
	if(message.toLowerCase().contentEquals("!a")){
		Short freq2=m_botAction.getPlayer(name).getFrequency();
		int j=0;
		Short freq1;
		boolean freqFound=false;
		for (j=0;j<challenge.length;j++){
			if (challenge[j]!=null){
				if (challenge[j].hasFreq2(freq2)){
					freq1=challenge[j].acceptDuel(freq2);
					initBase(freq1,freq2);
					freqFound=true;
					m_botAction.sendOpposingTeamMessageByFrequency(freq1, "Duel Accepted");
					m_botAction.sendOpposingTeamMessageByFrequency(freq2, "Duel Accepted");
					break;
				}
			}
		}
		if(freqFound==false){
			m_botAction.sendSmartPrivateMessage(name, "No pending Duels.");
		}
	}
	//Smods+ only, !clears, resets all of the duels data, this was for testing purposes, but may be used if someone gets stuck
	if( (message.startsWith( "!clear" )&& m_botAction.getOperatorList().isSmod(name))){
		baseduel.playingIds.clear();
		baseduel.playingFreqs.clear();
		baseduel.playingBases.clear();
		int x;
		for (x=0;x<totalbases;x++){
			base[x]=null;
		}
		m_botAction.sendPrivateMessage(name, "all clear." );
		m_botAction.sendPrivateMessage(name,  name );
	}
	//Sends messages with command and other information.
	if( message.startsWith( "!duelhelp" )){
		String[] spam = {"!ch <name> -  challenge someone to a duel",
				"!a - Used after your team is challenged to accept duel",
				"!r - Used after your team is challenged to reject duel",
				"!s - Shows current Duel scores",
				":"+botName +  ":!j <freq> - Joins an existing duel, where <freq> is the frequency to join",
				"(Baseduel Bot extension by Proz)"};
		m_botAction.smartPrivateMessageSpam(name, spam);
		
		//Here is where challenges get handdled	
	} else if( message.startsWith( "!ch")&&(message.length()>=5))
	{
		String player2 = m_botAction.getFuzzyPlayerName(message.substring(4)); //get the challenged player name
		Short freq1=m_botAction.getPlayer(name).getFrequency();//get the first player's freq
		/*
		 * TODO: i should get the bot's frequency in the beginning,
		 */
		botFreq = m_botAction.getPlayer(m_botAction.getBotName()).getFrequency();
		Short nameId2 = (short)m_botAction.getPlayerID(player2);// gets the challenged players id
		if((player2!=null)){ //check that the player is present in the arena
			Short freq2=m_botAction.getPlayer(player2).getFrequency();
			if (
					(name!=player2)&&
					(freq1.equals(botFreq)==false)&&
					(m_botAction.getPlayer(name).getShipType() != 0)&&
					(baseduel.playingFreqs.contains(freq1)==false )&&
					(baseduel.playingIds.contains(nameId)==false)&&
					(player2!=null)&&
					(m_botAction.getPlayer(player2).getShipType() != 0)&&
					(baseduel.playingIds.contains(nameId2)==false)&&
					(freq2.equals(botFreq)==false)&&
					(player2.length()>0)&&
					(freq1.equals(freq2)==false)&&
					(baseduel.playingFreqs.contains(freq2)==false)&&
					(baseduel.chsFreqs.contains(freq1)==false)&&
					(baseduel.chsFreqs.contains(freq2)==false)
			)	   		
			{
				challenge(freq1,freq2);
			}else if ((m_botAction.getPlayer(name).getFrequency()==botFreq)||
					(m_botAction.getPlayer(name).getShipType() == 0)){
				m_botAction.sendSmartPrivateMessage(name, "Only non-speccing players may challenge.");
			}else if (baseduel.playingFreqs.contains(m_botAction.getPlayer(name).getFrequency())==true ){
				m_botAction.sendSmartPrivateMessage(name, "You are already engaged in a duel.");
			}else if((baseduel.playingIds.contains((short)nameId2)==true) ||(playingIds.contains((short)nameId)==true)){
				m_botAction.sendSmartPrivateMessage(name, "Only players not engaged in a duel may (be) challenged.");
			}else if (name==player2){
				m_botAction.sendSmartPrivateMessage(name, "you cannot challenge yourself.");
			}else if(freq2.equals(botFreq)){
				m_botAction.sendSmartPrivateMessage(name, "Cannot challenge speccing players.");
			}else if (baseduel.chsFreqs.contains(freq1)){
				m_botAction.sendSmartPrivateMessage(name, "You already challenged someone else.");
			}else if (baseduel.chsFreqs.contains(freq2)){
				m_botAction.sendSmartPrivateMessage(name, "Freq Already challenged.");
			}else if(freq1.equals(freq2)){
				m_botAction.sendSmartPrivateMessage(name, "Cannot challenge players on your frequency.");
			}else {
				m_botAction.sendArenaMessage(botFreq.toString()+ " " + freq2.toString());
			}
		} else {
			if (message.length()>=18){message=message.substring(0, 17)+"...";}
			m_botAction.sendSmartPrivateMessage(name, "Player "+(message.substring(4))+" not found.");
		}
	}
}

private void challenge (Short freq1, Short freq2){
	String allF1PlayerNames="";
	String allF2PlayerNames="";
	String delimiter="";
	chsFreqs.add(freq1);
	chsFreqs.add(freq2);

	Iterator<Player> i = BotAction.getBotAction().getPlayingPlayerIterator();
	while( i.hasNext() ){
		Player p = i.next();
		if( freq1.equals(p.getFrequency())){
			if (allF1PlayerNames.length()!=0){
				delimiter=", ";
			}else{
				delimiter="";
			}

			allF1PlayerNames=allF1PlayerNames+delimiter+ p.getPlayerName();
		}
		if (freq2.equals(p.getFrequency())){

			if (allF2PlayerNames.length()!=0){
				delimiter=", ";
			}else{
				delimiter="";
			}

			allF2PlayerNames=allF2PlayerNames+delimiter+ p.getPlayerName();

		}
	}
	m_botAction.sendOpposingTeamMessageByFrequency(freq1, "Your team: "+allF1PlayerNames+" have challenged "+allF2PlayerNames+" they have 20 secs to accept");
	m_botAction.sendOpposingTeamMessageByFrequency(freq2, "Your team: "+allF2PlayerNames+" has been challenged by "+allF1PlayerNames+" type !a to accept or !r to reject, duel will time out in 20 seconds  ");
	int j=0;
	for (j=0;j<challenge.length;j++){
		if (challenge[j]==null){
			challenge[j]= new chFreqs();
			challenge[j].addfreqs(freq1, freq2, j);
			break;
		}
	}
}

public void initBase(Short freq1, Short freq2 ){
	Random randomgen = new Random();
	Short x = (short)randomgen.nextInt(totalbases);
	while (baseduel.playingBases.contains(x)==true){
		if (x!=0){
			x=0;
		}
		x++;
	}
	if(baseduel.playingBases.size()==totalbases){
		m_botAction.sendOpposingTeamMessageByFrequency(freq1, "no" +
				" bases available" +
		"for duel - please try again when a base is available.");
		m_botAction.sendOpposingTeamMessageByFrequency(freq2, "no" +
				" bases available" +
		"for duel");
	}else if(baseduel.playingBases.contains(x)==false){
		base[x]= new BaseDuelBase();
		baseduel.playingBases.add(x);
		base[x].matchStart(x,freq1,freq2);
	}
}

public void initBase(Short freq1, Short freq2, Short BaseNum ){
	if (baseduel.playingBases.contains(BaseNum)==true){
		m_botAction.sendOpposingTeamMessageByFrequency(freq1, "Base is already in use ");
	}else{
		if(baseduel.playingBases.size()==totalbases){
			m_botAction.sendOpposingTeamMessageByFrequency(freq1, "no" +
					" bases available" +
			"for duel - please try again when a base is available.");
			m_botAction.sendOpposingTeamMessageByFrequency(freq2, "no" +
					" bases available" +
			"for duel");
		}else if(baseduel.playingBases.contains(BaseNum)==false){
			base[BaseNum]= new BaseDuelBase();
			baseduel.playingBases.add(BaseNum);
			base[BaseNum].matchStart(BaseNum,freq1,freq2);
		}
	}
}


public void checkOtherPositions(int posX,int posY, Short freq, Short nameid){
	int num=playerIsBaseSafe(nameid,posX,posY);
	if(num>=0){
		if (base[num]!=null){
			base[num].baseSafe((short)freq, posX, posY);
		}
	}	   

	if(checkFreqIsSafe((short)freq)==true){
		num=CheckForFreq(freq);
		if((num!=-1)){
			if (base[num].playingStatus()==true){
				base[num].centerSafe(freq);
			}
			return;  
		}   		   
	}
}

public boolean checkFreqIsSafe(Short freq){
	Short freqTotal=(short)(m_botAction.getFrequencySize(freq));
	Short playersInSafe=0;
	Iterator<Player> i = m_botAction.getPlayingPlayerIterator(); 
	while( i.hasNext() ){
		Player p = i.next();
		if(freq.equals(p.getFrequency())){
			if ((p.getXTileLocation()>=centerSafe[0])&&(p.getXTileLocation()<=centerSafe[2])&&
					(p.getYTileLocation()>=centerSafe[1])&&(p.getYTileLocation()<=centerSafe[3])){
				playersInSafe++;
				if (playersInSafe.equals(freqTotal)){
					playersInSafe=0;
					return true;

				}
			}

		}
	}
	playersInSafe=0;
	return false;
}

/**
 * 
 * @param playerId is the id of the player
 * @param x is the xtile position of the player
 * @param y is the ytile position of the player
 * @return
 */
public int playerIsBaseSafe(int playerId, int x, int y){
	int i=0;
	for (i=0;i<totalbases;i++){
		if (((x==basecoord[i][0])&&(y==basecoord[i][1]))||
				((x==basecoord[i][2])&&(y==basecoord[i][3])))
		{
			return i;
		}   
	}
	return -1;
}
/**
 * Returns the number of the base that contains the frequency.
 * If it returns -1 then no base has frequency
 * @param freq
 * @return 
 */
public Short CheckForFreq(Short freq){
	int x=0;
	for (x=0;x<totalbases;x++){
		if (base[x]!=null){
			if(base[x].hasFreq((short)freq)==true){	
				if (base[x].playingStatus()==true){
					return (short)x;   
				} else{
					return -1;
				}
			}
		}

	}
	return -1;
}

/**
 * Removes the playing id's of the players in the duel.
 * removes both frequencies.
 * sets the baseduel base object to null.
 * and warps freqs to center.
 * @param num ( number of the base to kill.)
 * @param f1 
 * @param f2
 */
public void killBase(int num, Short f1, Short f2){

	base[num].deleteIds();
	playingBases.remove((short)num);
	playingFreqs.remove(f1);
	playingFreqs.remove(f2);
	base[num]=null;
	m_botAction.warpFreqToLocation(Integer.valueOf(f1),center[0], center[1]);
	m_botAction.warpFreqToLocation(Integer.valueOf(f2),center[0], center[1]);

}
/**
 * 
 * @param num Challenge number
 * @param f1 Frequency that initiated the challange
 * @param f2 Frequency that was challenged
 */
public void killchallenge(int num, Short f1, Short f2){
	challenge[num]=null;
	chsFreqs.remove(f1);
	chsFreqs.remove(f2);
}
public String[] getHelpMessages()
{
	String[] help = {
			"!duelhelp - this message.",
			"!help - same message as duel help",
			"!score - lists all current duels and their score",
			"!ch <name> - challenge someone to a baseduel for 5 rounds ",
			"BaseDuel extension version 0.9.1 - by Proz"
	};
	return help;
}
public void cancel()
{

}
public boolean isUnloadable(){
	return true;
}

public String[] getModHelpMessage() {
	String[] help = {

			//"!manual - Toggles a few automatic hosting features on or off (default is on)",
	};
	return help;
}
/**
 * Baseduelbase holds the information relative to a duel occurring in a base.
 * @author Proz
 * 
 */	
public class BaseDuelBase {
	//Num holds the base number
	private int Num;
	private int Round=1;
	private boolean playing=false;

	private boolean matchOver=false;
	private boolean matchStarted=false;
	private Short freq1;
	private Short freq2;

	private int freq1size;
	private int freq1InitSize;
	private int freq2size;
	private int freq2InitSize;

	private String allF1PlayerNames="";
	private String allF2PlayerNames="";

	private HashSet <Short> freq1Players = new HashSet<Short>();
	private HashSet <Short> freq2Players = new HashSet<Short>();

	private int freq1wins=0;
	private int freq2wins=0;

	private int freq1Losses;
	private int freq2Losses;

	private int safe[]={0,0,0,0};

	private int f1x;
	private int f1y;

	private int f2x;
	private int f2y;

	private boolean freq1InSafe;
	private boolean freq2InSafe;
	playerData pdata[]= new playerData[30];


	//TimerTask updateState;

	public class playerData{
		private int id;
		private int kills;
		private int deaths;
		private int spree;
		private String name;


		public void initPlayerData(int newid){
			id=newid;
			kills=0;
			deaths=0;
			spree=0;
			name = BotAction.getBotAction().getPlayerName(id);
		}
		public int getId(){
			return id;
		}

		public int getKills(){
			return kills;
		}

		public void addKill(){
			kills++;
			spree++;
			checkSpree();
		}
		public int getDeaths(){
			return deaths;

		}
		public void addDeath(){
			deaths++;
			spree=0;
		}
		public void checkSpree(){
			if (spree%10==0&&spree!=0){
				BotAction.getBotAction().sendArenaMessage( name + " is on a " + String.valueOf(spree)+" spree");
			}
		}
	}

	public void baseDuelInfo(Short playerID){
		getCurrentNames();
		
		if(allF1PlayerNames.length()>19){
			allF1PlayerNames=allF1PlayerNames.substring(0, 20)+"...";
		}
		if(allF2PlayerNames.length()>19){
			allF2PlayerNames=allF2PlayerNames.substring(0, 20)+"...";
		}
		
		
		if(playerID!=null){
			m_botAction.sendPrivateMessage(playerID,allF1PlayerNames+" ("+ String.valueOf(freq1wins)+" points) vs " + allF2PlayerNames+ " ("+ String.valueOf(freq2wins)+" points)"+ ", base "+ String.valueOf(Num+1) );
		}
		//m_botAction.sendPrivateMessage(playerID,"-");
	}

	public boolean playingStatus(){
		return playing;
	}
	/**
	 * will return if match has started.
	 * @return true if match is started
	 */
	public boolean matchStarted(){
		return matchStarted;
	}
	
	public void matchStart(int baseNum, Short f1, Short f2){
		Num=baseNum;
		freq1=f1;
		freq2=f2;
		freq1size=m_botAction.getFrequencySize(freq1);
		freq2size=m_botAction.getFrequencySize(freq2);
		freq1InitSize=freq1size;
		freq2InitSize=freq2size;
		
		//pdata=new playerData[freq1size+freq2size];
		int i=0;
		for(i=0;i<=3;i++){
			safe[i]=baseduel.basecoord[Num][i];
		}
		getIds(freq1,freq2);
		warpSetup();
		matchStarted=true;
		//roundStart();
	}

	private void warpSetup(){
		f1x=safe[0];
		f1y=safe[1];
		f2x=safe[2];
		f2y=safe[3];
		BotAction.getBotAction().warpFreqToLocation(Integer.valueOf(freq1), f1x, f1y);
		BotAction.getBotAction().warpFreqToLocation(Integer.valueOf(freq2), f2x, f2y);
		playing=true;
	}
	final public void roundStart(){
		//delay=false;
		playing=false;
		shipReset();
		Round++;
		if(Round%2==1){
			f1x=safe[0];
			f1y=safe[1];
			f2x=safe[2];
			f2y=safe[3];
			//	TimerTask nextRound = new TimerTask() {
			//	   public void run() {
			m_botAction.warpFreqToLocation(Integer.valueOf(freq1), f1x, f1y);
			m_botAction.warpFreqToLocation(Integer.valueOf(freq2), f2x, f2y);
			//	   }
			//};BotAction.getBotAction().scheduleTask(nextRound, 4000);
			//BotAction.getBotAction().warpFreqToLocation(Integer.valueOf(freq1), f1x, f1y);
			//BotAction.getBotAction().warpFreqToLocation(Integer.valueOf(freq2), f2x, f2y);
		}else{
			f2x=safe[0];
			f2y=safe[1];
			f1x=safe[2];
			f1y=safe[3];
			//TimerTask nextRound = new TimerTask() {
			//public void run() {
			m_botAction.warpFreqToLocation(Integer.valueOf(freq1), f1x, f1y);
			m_botAction.warpFreqToLocation(Integer.valueOf(freq2), f2x, f2y);
			//	}
			//};BotAction.getBotAction().scheduleTask(nextRound, 4000);
		}	
		centerCheck();
		freq2InSafe=false;
		freq1InSafe=false;
		if(freq1wins==5||freq2wins==5){
			matchEnd();
			return;
		}
		setPlayingTrue();
		//playing=true;
	}

	private void shipReset(){
		Iterator<Player> i = BotAction.getBotAction().getPlayingPlayerIterator();
		while( i.hasNext() ){
			Player p = i.next();
			if( freq1.equals(p.getFrequency())||freq2.equals(p.getFrequency())){
				BotAction.getBotAction().shipReset(p.getPlayerID());
			}
		}
	}
	public boolean hasId(int id){
		boolean has=false;
		if((freq1Players.contains((short)id))||(freq2Players.contains((short)id))){
			has=true;
		}
		return has;
	}

	public boolean hasFreq(Short freq){
		if ( (freq1.equals(freq)) || (freq2.equals(freq)) ){
			return true;
		}else{
			return false;
		}
	}

	public void centerSafe(Short freq){
		if (this.playingStatus()==true){
			if(freq1.equals(freq)){
				freq1InSafe=true;
			}
			if(freq2.equals(freq)){
				freq2InSafe=true;
			}
			roundStart();
			//updateState= new updateState(this); 
			//m_botAction.scheduleTask(updateState,3000);
			//BotAction.getBotAction().scheduleTask(updateState, 4000);
			//roundDelay(Num);
			//m_botAction.scheduleTask(roundDelay, delayms)
			//roundDelay();
		}
	}

	private void setPlayingTrue(){
		TimerTask nextRound = new TimerTask() {
			public void run() {
				playing=true;
			}
		};BotAction.getBotAction().scheduleTask(nextRound, 5000);
	}

	public void emptyFreqCheck(Short nameid){
		Short freq=-1;
		if (freq1Players.contains(nameid)){
			freq=freq1;
			freq1size=freq1size-1;
			if(freq1size==0){
				matchEnd(freq);
			}
		}
		if (freq2Players.contains(nameid)){
			freq=freq2;
			freq2size=freq2size-1;
			if(freq2size==0){
				matchEnd(freq);
			}
		}
	}

	private void centerCheck(){

		if (freq1InSafe==true&&freq2InSafe==true){
			roundEnd("both");
		}
		if (freq1InSafe==true&&freq2InSafe==false){
			roundEnd(freq1);
		}
		if (freq2InSafe==true&&freq1InSafe==false){
			roundEnd(freq2);
		}
	}


	public void baseSafe(Short freq, int posX, int posY){
		if (this.playingStatus()==true){
			if(freq1.equals(freq)&&posX==f2x&&posY==f2y){
				BotAction.getBotAction().warpFreqToLocation(freq2,graveyard[0],graveyard[1]);						
			}
			if(freq2.equals(freq)&&posX==f1x&&posY==f1y){
				BotAction.getBotAction().warpFreqToLocation(freq1,graveyard[0],graveyard[1]);						
			}
		}
	}
	public void addPlayer(Short id, Short freq){
		if(id!=null){
			if(freq.equals(freq1)){
				if(freq1InitSize!=freq1size){
					freq1size++;
					idsAllowedToJoin.add(id);
					m_botAction.setShip(id, 1);
					m_botAction.setFreq(id, freq1);
					playingIds.add(id);
					idsAllowedToJoin.remove(id);
					addPlayerData(id);
					//allF1PlayerNames = allF1PlayerNames + " " + m_botAction.getPlayerName(id);
					m_botAction.sendSmartPrivateMessage(m_botAction.getPlayerName(id), "You joined frequency"+String.valueOf(freq1) );
					m_botAction.warpTo(id, graveyard[0], graveyard[1]);
				}else{
					m_botAction.sendSmartPrivateMessage(m_botAction.getPlayerName(id), "No spots available.");
				}
			
			}else if(freq.equals(freq2)){
				if(freq2InitSize!=freq2size){
					freq2size++;
					idsAllowedToJoin.add(id);
					m_botAction.setShip(id, 1);
					m_botAction.setFreq(id, freq2);
					playingIds.add(id);
					idsAllowedToJoin.remove(id);
					addPlayerData(id);
					//allF2PlayerNames = allF2PlayerNames + " " + m_botAction.getPlayerName(id);
					m_botAction.sendSmartPrivateMessage(m_botAction.getPlayerName(id), "You joined frequency"+String.valueOf(freq2) );
					m_botAction.warpTo(id, graveyard[0], graveyard[1]);
				}else{
					m_botAction.sendSmartPrivateMessage(m_botAction.getPlayerName(id), "No spots available.");
				}
			}
		}
		
	}
	
	private void addPlayerData(int id){
		int x=0;
		playerData tempData;
		for(x=0;x<pdata.length;x++){
			if(pdata[x]==null){
				pdata[x] = new playerData();
				pdata[x].initPlayerData(id);
				break;
			}
		}
	}

	private void getCurrentNames(){
		allF1PlayerNames="";
		allF2PlayerNames="";
		String delimiter;
		Iterator<Player> i = BotAction.getBotAction().getPlayingPlayerIterator();
		while( i.hasNext() ){
			Player p = i.next();
			if( freq1.equals(p.getFrequency())){
				if (allF1PlayerNames.length()!=0){
					delimiter=", ";
				}else{
					delimiter="";
				}
				allF1PlayerNames=allF1PlayerNames+delimiter+ p.getPlayerName();
			}
			if (freq2.equals(p.getFrequency())){
				if (allF2PlayerNames.length()!=0){
					delimiter=", ";
				}else{
					delimiter="";
				}
				allF2PlayerNames=allF2PlayerNames+delimiter+ p.getPlayerName();
			}
		}
	}
	
	
	private void getIds(Short freq1, Short freq2){
		baseduel.playingFreqs.add(freq1);
		baseduel.playingFreqs.add(freq2);
		int j=0;
		String delimiter;
		Iterator<Player> i = BotAction.getBotAction().getPlayingPlayerIterator();
		while( i.hasNext() ){
			Player p = i.next();
			if( freq1.equals(p.getFrequency())){

				freq1Players.add(p.getPlayerID());
				baseduel.playingIds.add(p.getPlayerID());
				pdata[j] = new playerData();
				pdata[j].initPlayerData(p.getPlayerID());
				j++;
				if (allF1PlayerNames.length()!=0){
					delimiter=", ";
				}else{
					delimiter="";
				}
				allF1PlayerNames=allF1PlayerNames+delimiter+ p.getPlayerName();
			}
			if (freq2.equals(p.getFrequency())){
				freq2Players.add(p.getPlayerID());
				baseduel.playingIds.add(p.getPlayerID());
				pdata[j] = new playerData();
				pdata[j].initPlayerData(p.getPlayerID());
				j++;

				if (allF2PlayerNames.length()!=0){
					delimiter=", ";
				}else{
					delimiter="";
				}
				allF2PlayerNames=allF2PlayerNames+delimiter+ p.getPlayerName();
			}
		}
	}

	public void addKill(int killerId, int DeathId){

		int pos1=getPlayerDataPosition(killerId);
		int pos2=getPlayerDataPosition(DeathId);
		pdata[pos1].addKill();
		pdata[pos2].addDeath();
	}

	public int getPlayerDataPosition(Integer id){
		
		int pos=0;
		for(pos=0;pos<pdata.length;pos++){
			if(pdata[pos].getId()==id&&pdata[pos]!=null&&id!=null){
				return pos;
			}
		}
		return -1;
		
	}
	
	public void deleteid(Short id){
		
		try{
			if (freq1Players.contains(id)){
				freq1Players.remove(id);			
			}
			if (freq2Players.contains(id)){
				freq2Players.remove(id);
			}
		}
		finally{
			
		}
		
		

	}
	
	

	public void deleteIds(){
		baseduel.playingFreqs.remove(freq1);
		baseduel.playingFreqs.remove(freq2);
		Iterator<Player> i = BotAction.getBotAction().getPlayingPlayerIterator();
		while( i.hasNext() ){
			Player p = i.next();
			if(freq1.equals(p.getFrequency())){
				baseduel.playingIds.remove(p.getPlayerID());
			}
			if (freq2.equals(p.getFrequency())){
				baseduel.playingIds.remove(p.getPlayerID());
			}
		}
	}

	/**
	 * Tells the roundEnd() the frequency that lost, by adding a lost and adds the opposing team a win
	 * @param freqLost
	 */ 	
	public void roundEnd(String s){
		if(s=="both"){
			freq1wins++;
			freq2wins++;
			BotAction.getBotAction().sendOpposingTeamMessageByFrequency(freq1,"Both teams are tied with "+String.valueOf(freq1wins));
			BotAction.getBotAction().sendOpposingTeamMessageByFrequency(freq2,"Both teams are tied with "+String.valueOf(freq2wins));
		}
	}
	


	public void roundEnd(Short freqLost){
		if (freqLost.equals(freq1)){
			freq1Losses++;
			freq2wins++;
		}else if (freqLost.equals(freq2)){
			freq2Losses++;
			freq1wins++;
		}

		if(freq1wins>freq2wins){
			BotAction.getBotAction().sendOpposingTeamMessageByFrequency(freq1, "Your team is winning with: "+ String.valueOf(freq1wins)+" and your opponent is losing with: "+ String.valueOf(freq2wins));
			BotAction.getBotAction().sendOpposingTeamMessageByFrequency(freq2, "Your team is losing with: "+ String.valueOf(freq2wins)+" and your opponent leads with: "+ String.valueOf(freq1wins));
		}else if (freq2wins>freq1wins){
			BotAction.getBotAction().sendOpposingTeamMessageByFrequency(freq2, "Your team is winning  with: "+ String.valueOf(freq2wins)+" and your opponent is losing with: "+ String.valueOf(freq1wins));
			BotAction.getBotAction().sendOpposingTeamMessageByFrequency(freq1, "Your team is losing  with: "+ String.valueOf(freq1wins)+" and your opponent is winning with: "+ String.valueOf(freq2wins));
		}else{
			BotAction.getBotAction().sendOpposingTeamMessageByFrequency(freq1,"Both teams are tied with "+String.valueOf(freq1wins));
			BotAction.getBotAction().sendOpposingTeamMessageByFrequency(freq2,"Both teams are tied with "+String.valueOf(freq2wins));
		}
		//Round++;
		//roundStart();

	}
	private String[] printStats(){
		int pos;
		int j=0;
		Integer id;
		int k=0;
		
		Integer statsSize=freq1size+freq2size+20;
		
		Short[] freqsOrder= new Short[2];
		//Short winningFreq;
		//Short losserFreq;
		if(freq1wins>freq2wins){
			freqsOrder[0]=freq1;
			freqsOrder[1]=freq2;
		}else{
			freqsOrder[0]=freq2;
			freqsOrder[1]=freq1;
		}
		if(freq1size==0){
			freqsOrder[0]=freq2;
			freqsOrder[1]=freq1;
		}else if(freq2size==0){
			freqsOrder[0]=freq1;
			freqsOrder[1]=freq2;
		}
		
		String playerStats[]= new String[statsSize];
		for(k=0;k<2;k++){
			if (k==0){
				playerStats[j]="|------(-- Winners --)----- |";
				j++;
				playerStats[j]="| Name			   K	D |";
				j++;
				playerStats[j]="|-------------------------- |";
				j++;
			}else{
				playerStats[j]="|------(-- Losers --)------ |";
				j++;
			}
			Iterator<Player> i = BotAction.getBotAction().getPlayingPlayerIterator();
			while( i.hasNext() ){
				Player p = i.next();
				if( freqsOrder[k].equals(p.getFrequency())){
					id=Integer.valueOf(p.getPlayerID());
					
					pos=getPlayerDataPosition(id);
					
					if(id!=null){
						playerStats[j]=String.format("| %1$-16s %2$3d  %3$3d |",
								BotAction.getBotAction().getPlayerName(id),
								pdata[pos].getKills(),
								pdata[pos].getDeaths()
						);
					}
					if (id!=null){
						j++;
					}
						  
				}
			}
		}
	
		playerStats[j]="|___________________________|";
		
		int x=0;
		Integer statsSize2=statsSize;
		for (x=0;x<statsSize;x++){
			if(playerStats[x]==null){
				statsSize2--;
			}
				
		}
		x=0;
		String playerStats2[]= new String[statsSize2];
		
		for (x=0;x<statsSize2;x++){
			playerStats2[x]=playerStats[x];
		}
		
		return playerStats2;
	}

	private void matchEnd(Short freq){
		
		
		
		BotAction.getBotAction().arenaMessageSpam(printStats());
		if(freq==freq1){
			BotAction.getBotAction().sendArenaMessage(allF2PlayerNames+" ("+freq2wins+ " Points)" + " defeated "+ allF1PlayerNames+" ("+freq1wins+" Points)");
			BotAction.getBotAction().sendArenaMessage("no players left on "+ allF1PlayerNames +"'s frequency" );
		}
		if(freq==freq2){
			BotAction.getBotAction().sendArenaMessage(allF1PlayerNames+" ("+freq1wins+ " Points)" + " defeated "+ allF2PlayerNames+" ("+freq2wins+" Points)");
			BotAction.getBotAction().sendArenaMessage("no players left on "+ allF2PlayerNames +"'s frequency" );
		}
		baseduel.this.killBase(Num, freq1, freq2);
	}


	private void matchEnd(){
		matchOver=true;


		BotAction.getBotAction().arenaMessageSpam(printStats());
		if(allF1PlayerNames.length()>19){
			allF1PlayerNames=allF1PlayerNames.substring(0, 20)+"...";
		}
		if(allF2PlayerNames.length()>19){
			allF2PlayerNames=allF2PlayerNames.substring(0, 20)+"...";
		}

		if(freq1wins>freq2wins){
			BotAction.getBotAction().sendArenaMessage(allF1PlayerNames+" ("+freq1wins+ " Points)" + " defeated "+ allF2PlayerNames+" ("+freq2wins+" Points)");
		}

		if(freq2wins>freq1wins){
			BotAction.getBotAction().sendArenaMessage(allF2PlayerNames+" ("+freq2wins+ " Points)" + " defeated "+ allF1PlayerNames+" ("+freq1wins+" Points)");
		}


		baseduel.this.killBase(Num, freq1, freq2);

	}

	public String[] getHelpMessages()
	{
		String[] help = {
				"!duelhelp - this message.",
				"!ch <name> - challenge someone to a baseduel for 5 rounds.",
				"!s or !score - Shows current duel scores."
		};
		return help;
	}

	public void cancel()
	{
	}
}

public class chFreqs {

	public Short f1=0;
	public Short f2=0;
	private TimerTask removefrq;
	public int chNum;
	private HashSet <Short> freq1Players = new HashSet<Short>();
	private HashSet <Short> freq2Players = new HashSet<Short>();

	public void addfreqs(Short freq1,Short freq2, int num){
		// String anf1, String anf2

		f1=freq1;
		f2=freq2;
		chNum=num;

		Iterator<Player> i = BotAction.getBotAction().getPlayingPlayerIterator();
		while( i.hasNext() ){
			Player p = i.next();
			if( freq1.equals(p.getFrequency())){
				freq1Players.add(p.getPlayerID());
			}
			if (freq2.equals(p.getFrequency())){
				freq2Players.add(p.getPlayerID());
			}
		}
		removefrq = new TimerTask() {
			public void run() {
				baseduel.chsFreqs.remove(f1);
				baseduel.chsFreqs.remove(f2);
				m_botAction.sendOpposingTeamMessageByFrequency(f1,"Duel timed out");
				m_botAction.sendOpposingTeamMessageByFrequency(f2,"Duel timed out");
				baseduel.this.killchallenge(chNum, f1, f2);
			}
		};BotAction.getBotAction().scheduleTask(removefrq, 20000);						

	}

	public boolean hasId(Short id){
		boolean has=false;

		if(freq1Players.contains(id)||freq2Players.contains(id)){
			has=true;
		}
		return has;
	}


	public void deleteFreqs(Short f1){
	}

	public boolean hasFreq(Short freq){

		if ((f1.equals(freq))||(f2.equals(freq))){
			return true;
		}
		return false;
	}
	
	public boolean hasFreq2(Short freq){

		if (f2.equals(freq)){
			return true;
		}
		return false;
	}

	public Short acceptDuel(Short freq){

		if (f2.equals(freq)){

			baseduel.this.killchallenge(chNum, f1, f2);
			removefrq.cancel();
			return f1;


		}
		return -1;
	}

	public Short otherFreq(Short freq){

		if (f2.equals(freq)){
			return f1;
		}
		return -1;
	}

	public void cancelDuel(int id){

		m_botAction.sendOpposingTeamMessageByFrequency(f1, "Duel cancelled "+  
				m_botAction.getPlayerName(id)+" Changed freq" );
		m_botAction.sendOpposingTeamMessageByFrequency(f2, "Duel cancelled "+  
				m_botAction.getPlayerName(id)+" Changed freq" );
		m_botAction.sendSmartPrivateMessage(m_botAction.getPlayerName(id), "Duel cancelled you changed freq.");
		removefrq.cancel();
		baseduel.this.killchallenge(chNum, f1, f2);

	}
	public void cancelDuel2(){
		removefrq.cancel();
		baseduel.this.killchallenge(chNum, f1, f2);
	}

	public void cancelDuel3(){
		m_botAction.sendOpposingTeamMessageByFrequency(f1,"Duel cancelled freqs changed");
		m_botAction.sendOpposingTeamMessageByFrequency(f2, "Duel cancelled freqs changed"); 
		removefrq.cancel();
		baseduel.this.killchallenge(chNum, f1, f2);
	}

}

public class PlayerInfo{
	private Short frequency;
	private int id;
	private String name;
	private int ship;
	private boolean playingStatus;
	
}
}

Guest
This topic is now closed to further replies.
×
×
  • Create New...