View difference between Paste ID: rfkQAC2f and eXdnekNF
SHOW: | | - or go back to the newest paste.
1
package twcore.bots.testbot;
2
3
import twcore.core.BotAction;
4
import twcore.core.BotSettings;
5
import twcore.core.EventRequester;
6
import twcore.core.SubspaceBot;
7
import twcore.core.events.ArenaJoined;
8
import twcore.core.events.LoggedOn;
9
import twcore.core.events.Message;
10
11
/**
12
 * This is a barebones template bot for use when creating a new bot.  With slight
13
 * modification, it can suit just about any purpose.  Note that if you are
14
 * thinking of creating a typical eventbot, you should strongly consider creating
15
 * a MultiBot module instead, which is more simple; also, you might consider the
16
 * UltraBot template, which includes a command interpreter for easy command setup.
17
 *
18
 * @author  Stefan / Mythrandir
19
 */
20
public class testbot extends SubspaceBot {
21
22
    private BotSettings m_botSettings;          // Stores settings for your bot as found in the .cfg file.
23
                                                // In this case, it would be settings from basicbot.cfg
24
25
    /**
26
     * Creates a new instance of your bot.
27
     */
28
    public testbot(BotAction botAction) {
29
        super(botAction);
30
        requestEvents();
31
32
        // m_botSettings contains the data specified in file <botname>.cfg
33
        m_botSettings = m_botAction.getBotSettings();
34
    }
35
36
37
    /**
38
     * This method requests event information from any events your bot wishes
39
     * to "know" about; if left commented your bot will simply ignore them.
40
     */
41
    public void requestEvents() {
42
        EventRequester req = m_botAction.getEventRequester();
43
        req.request(EventRequester.MESSAGE);
44
        // req.request(EventRequester.ARENA_LIST);
45
        req.request(EventRequester.ARENA_JOINED);
46
        // req.request(EventRequester.PLAYER_ENTERED);
47
        // req.request(EventRequester.PLAYER_POSITION);
48
        // req.request(EventRequester.PLAYER_LEFT);
49
        // req.request(EventRequester.PLAYER_DEATH);
50
        // req.request(EventRequester.PRIZE);
51
        // req.request(EventRequester.SCORE_UPDATE);
52
        // req.request(EventRequester.WEAPON_FIRED);
53
        // req.request(EventRequester.FREQUENCY_CHANGE);
54
        // req.request(EventRequester.FREQUENCY_SHIP_CHANGE);
55
        req.request(EventRequester.LOGGED_ON);
56
        // req.request(EventRequester.FILE_ARRIVED);
57
        // req.request(EventRequester.FLAG_VICTORY);
58
        // req.request(EventRequester.FLAG_REWARD);
59
        // req.request(EventRequester.SCORE_RESET);
60
        // req.request(EventRequester.WATCH_DAMAGE);
61
        // req.request(EventRequester.SOCCER_GOAL);
62
        // req.request(EventRequester.BALL_POSITION);
63
        // req.request(EventRequester.FLAG_POSITION);
64
        // req.request(EventRequester.FLAG_DROPPED);
65
        // req.request(EventRequester.FLAG_CLAIMED);
66
    }
67
68
69
    /**
70
     * You must write an event handler for each requested event/packet.
71
     * This is an example of how you can handle a message event.
72
     */
73
    public void handleEvent(Message event) {
74
        // Retreive name. If the message is remote, then event.getMessager() returns null, and event.getPlayerID returns a value.
75
        // If the message is from the same arena, event.getMessager() returns a string, and event.getPlayerID will return 0.
76
        String name = event.getMessager() != null ? event.getMessager() : m_botAction.getPlayerName(event.getPlayerID());
77
        if (name == null) name = "-anonymous-";
78
79
        /*
80
        m_botAction.sendPublicMessage(  "I received a Message event type ("+ event.getMessageType()+") from " + name +
81
                                        " containing the following text: " + event.getMessage());
82
		*/
83
        
84
        // Default implemented command: !die
85
        if (event.getMessageType() == Message.PRIVATE_MESSAGE && event.getMessage().equalsIgnoreCase("!die")) {
86
            //m_botAction.sendPublicMessage(name + " commanded me to die. Disconnecting...");
87
            try { Thread.sleep(50); } catch (Exception e) {};
88
            m_botAction.die();
89
        }
90
        // Default implemented command: !die
91
        if (event.getMessageType() == Message.PRIVATE_MESSAGE && event.getMessage().equalsIgnoreCase("!YourCommand")) {
92
        	m_botAction.sendPublicMessage("Response to your command");
93
        }
94
95
    }
96
97
98
    /**
99
     * The LoggedOn event is fired when the bot has logged on to the system, but
100
     * has not yet entered an arena.  A normal SS client would automatically join
101
     * a pub; since we do things manually we must tell the bot to join an arena
102
     * after it has successfully logged in.  Usually the arena is stored in the
103
     * settings file of the bot, which shares the name of the source and class 
104
     * files, but has a cfg extension.  
105
     */
106
    public void handleEvent(LoggedOn event) {
107
        m_botAction.joinArena(m_botSettings.getString("arena"));
108
    }
109
110
111
    /**
112
     * Set ReliableKills 1 (*relkills 1) to make sure your bot receives every packet.
113
     * If you don't set reliable kills to 1 (which ensures that every death packet
114
     * of players 1 bounty and higher [all] will be sent to you), your bot may not
115
     * get a PlayerDeath event for every player that dies.
116
     */
117
    public void handleEvent(ArenaJoined event) {
118
        m_botAction.setReliableKills(1);
119
    }
120
121
}