File: rabbitTimer.cpp

package info (click to toggle)
bzflag 2.0.13.20080902-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 27,564 kB
  • ctags: 34,716
  • sloc: cpp: 139,842; ansic: 14,510; sh: 10,715; makefile: 2,454; perl: 477; php: 428; python: 345; objc: 243; xml: 24
file content (159 lines) | stat: -rw-r--r-- 4,012 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// rabbitTimer.cpp : Defines the entry point for the DLL application.
//

#include "bzfsAPI.h"
#include <cstdlib>

BZ_GET_PLUGIN_VERSION

class rabbitTimer : public bz_EventHandler
{
public:
	virtual void process(bz_EventData *eventData);

	float rabbitKillTimeLimit; //the max time the rabbit has to kill someone
	bool rollOver; //whether or not to roll over leftover seconds

	float rabbitDeathTime;

	int currentRabbit;
};

rabbitTimer rabbittimer;

void rabbitTimer::process(bz_EventData *eventData)
{
	if (eventData->eventType == bz_eTickEvent)
	{
		bz_TickEventData* tickdata = (bz_TickEventData*)eventData;

		if (currentRabbit != -1 && tickdata->time >= rabbitDeathTime)
		{
			//kill the wabbit!
			bz_killPlayer(currentRabbit, false, BZ_SERVER);

			//stopgap. the kill event should do this, really...
			currentRabbit = -1;
			rabbitDeathTime = tickdata->time + rabbitKillTimeLimit;

			bz_sendTextMessage (BZ_SERVER, BZ_ALLUSERS, "Time's up! Selecting new rabbit.");
		}
		else if (currentRabbit == -1 && bz_getTeamCount(eHunterTeam) >= 3) //make sure we've got enough people before reactivating the timer
		{
			//find the new rabbit
			bzAPIIntList pl;
			bz_getPlayerIndexList(&pl);

			for (unsigned int i = 0; i < pl.size() && currentRabbit == -1; i++)
			{
				bz_PlayerRecord* pr = bz_getPlayerByIndex(pl.get(i));

				if (pr != NULL)
				{
					if (pr->team == eRabbitTeam)
					{
						currentRabbit = pr->playerID;
						int limit = (int)rabbitKillTimeLimit;
						bz_sendTextMessage(BZ_SERVER, currentRabbit, bz_format("You have %d seconds to make a kill!", limit));
					}
					bz_freePlayerRecord(pr);
				}
			}
		}
	}
	else if (eventData->eventType == bz_ePlayerDieEvent)
	{

		bz_PlayerDieEventData* killdata = (bz_PlayerDieEventData*)eventData;

		if (killdata->team == eRabbitTeam)
		{
			currentRabbit = -1; //we will sort this out on the next tick

			rabbitDeathTime = killdata->time + rabbitKillTimeLimit;
		}
		else if (killdata->killerTeam == eRabbitTeam && currentRabbit != -1)
		{
			if (rollOver)
			{
				rabbitDeathTime += rabbitKillTimeLimit;

				int limit = (int)rabbitKillTimeLimit;
				int timeremaining = (int)(rabbitDeathTime - killdata->time);

				bz_sendTextMessage(BZ_SERVER, currentRabbit, bz_format("+%d seconds: %d seconds remaining.", limit, timeremaining));
			}
			else
			{
				rabbitDeathTime = killdata->time + rabbitKillTimeLimit;

				int limit = (int)rabbitKillTimeLimit;
				bz_sendTextMessage(BZ_SERVER, currentRabbit, bz_format("%d seconds remaining.", limit));
			}
		}
	}
	else if (eventData->eventType == bz_ePlayerDieEvent)
	{
		bz_PlayerJoinPartEventData* partdata = (bz_PlayerJoinPartEventData*)eventData;

		if (partdata->team == eRabbitTeam) //we need to select a new rabbit if the rabbit leaves.
		{
			currentRabbit = -1; //we will sort this out on the next tick

			rabbitDeathTime = partdata->time + rabbitKillTimeLimit;
		}
	}
}


BZF_PLUGIN_CALL int bz_Load(const char* commandLine)
{
	rabbittimer.rabbitKillTimeLimit = 30.0;
	rabbittimer.rollOver = false;
	rabbittimer.currentRabbit = -1;
	rabbittimer.rabbitDeathTime = 3600.0; //something large

	std::string param = commandLine;

	if (param.size() > 0 && param.at(0) == '+')
	{
		rabbittimer.rollOver = true;
		param  = param.erase(0,1);
	}

	int newtime = atoi(param.c_str());

	if (newtime > 0)
	{
		rabbittimer.rabbitKillTimeLimit = (float)newtime;
	}

	bz_registerEvent(bz_ePlayerDieEvent,&rabbittimer);
	bz_registerEvent(bz_ePlayerPartEvent,&rabbittimer);
	bz_registerEvent(bz_eTickEvent,&rabbittimer);

	bz_debugMessage(4, "rabbitTimer plugin loaded");
	return 0;
}

BZF_PLUGIN_CALL int bz_Unload()
{

	bz_removeEvent(bz_ePlayerDieEvent,&rabbittimer);
	bz_removeEvent(bz_ePlayerPartEvent,&rabbittimer);
	bz_removeEvent(bz_eTickEvent,&rabbittimer);

	bz_debugMessage(4, "rabbitTimer plugin unloaded");
	return 0;
}



// Local Variables: ***
// mode:C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8