File: rabidRabbit.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 (350 lines) | stat: -rw-r--r-- 9,665 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// rabidRabbit.cpp : Defines the entry point for the DLL application.

#include "bzfsAPI.h"
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>

BZ_GET_PLUGIN_VERSION

class RabidRabbitHandler : public bz_CustomMapObjectHandler
{
public:
  virtual bool handle(bzApiString object, bz_CustomMapObjectInfo *data);
};

RabidRabbitHandler rabidrabbithandler;

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

RabidRabbitEventHandler	rabidrabbiteventHandler;

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

RabidRabbitDieEventHandler rabidrabbitdieeventhandler;

BZF_PLUGIN_CALL int bz_Load(const char* /*commandLineParameter*/)
{
  bz_debugMessage(4,"rabidRabbit plugin loaded");
  bz_registerCustomMapObject("RABIDRABBITZONE",&rabidrabbithandler);
  bz_registerCustomMapObject("RRSOUNDOFF",&rabidrabbithandler);
  bz_registerCustomMapObject("RRCYCLEONDIE",&rabidrabbithandler);
  bz_registerEvent(bz_eTickEvent,&rabidrabbiteventHandler);
  bz_registerEvent(bz_ePlayerDieEvent ,&rabidrabbitdieeventhandler);
  return 0;
}

BZF_PLUGIN_CALL int bz_Unload(void)
{
  bz_removeEvent(bz_eTickEvent,&rabidrabbiteventHandler);
  bz_removeEvent(bz_ePlayerDieEvent,&rabidrabbitdieeventhandler);
  bz_removeCustomMapObject("RABIDRABBITZONE");
  bz_removeCustomMapObject("RRSOUNDOFF");
  bz_removeCustomMapObject("RRCYCLEONDIE");
  bz_debugMessage(4,"rabidRabbit plugin unloaded");
  return 0;
}

class RRZoneInfo
{
public:
  RRZoneInfo()
  {
    currentKillZone = 0;
    rabbitNotifiedWrongZone = false;
    rabbitNotifiedWrongZoneNum = 0;
    soundEnabled = true;
    cycleOnDie = false;
  }

  unsigned int currentKillZone;
  unsigned int rabbitNotifiedWrongZoneNum;
  bool rabbitNotifiedWrongZone;
  bool soundEnabled;
  bool cycleOnDie;
};

RRZoneInfo rrzoneinfo;

class RabidRabbitZone
{
public:
  RabidRabbitZone()
  {
    zonekillhunter = false;
    box = false;
    xMax = xMin = yMax = yMin = zMax = zMin = rad = 0;
    WW = "";
    WWLifetime = 0;
    WWPosition[0] = 0;
    WWPosition[1] = 0;
    WWPosition[2] = 0;
    WWTilt = 0;
    WWDirection = 0;
    WWShotID = 0;
    WWDT = 0;
    WWRepeat = 0.5;
    WWFired = false;
    WWLastFired = 0;
    pi = 3.14159265358979323846;
  }

  bool zonekillhunter;
  bool box;
  float xMax,xMin,yMax,yMin,zMax,zMin;
  float rad;

  bzApiString WW;
  float WWLifetime, WWPosition[3], WWTilt, WWDirection, WWDT;
  double pi, WWLastFired, WWRepeat;
  bool WWFired;
  int WWShotID;

  std::string playermessage;
  std::string servermessage;

  bool pointIn(float pos[3])
  {
    if (box) {
      if (pos[0] > xMax || pos[0] < xMin)
	return false;

      if (pos[1] > yMax || pos[1] < yMin)
	return false;

      if (pos[2] > zMax || pos[2] < zMin)
	return false;
    } else {
      float vec[3];
      vec[0] = pos[0]-xMax;
      vec[1] = pos[1]-yMax;
      vec[2] = pos[2]-zMax;

      float dist = sqrt(vec[0]*vec[0]+vec[1]*vec[1]);
      if (dist > rad)
	return false;

      if (pos[2] > zMax || pos[2] < zMin)
	return false;
    }
    return true;
  }
};

std::vector <RabidRabbitZone> zoneList;

bool RabidRabbitHandler::handle(bzApiString object, bz_CustomMapObjectInfo *data)
{
  if (object == "RRSOUNDOFF")
    rrzoneinfo.soundEnabled = false;

  if (object == "RRCYCLEONDIE")
    rrzoneinfo.cycleOnDie = true;

  if (object != "RABIDRABBITZONE" || !data)
    return false;

  RabidRabbitZone newZone;

  // parse all the chunks
  for (unsigned int i = 0; i < data->data.size(); i++) {
    std::string line = data->data.get(i).c_str();

    bzAPIStringList *nubs = bz_newStringList();
    nubs->tokenize(line.c_str()," ",0,true);

    if (nubs->size() > 0) {
      std::string key = bz_toupper(nubs->get(0).c_str());

      if (key == "BBOX" && nubs->size() > 6) {
	newZone.box = true;
	newZone.xMin = (float)atof(nubs->get(1).c_str());
	newZone.xMax = (float)atof(nubs->get(2).c_str());
	newZone.yMin = (float)atof(nubs->get(3).c_str());
	newZone.yMax = (float)atof(nubs->get(4).c_str());
	newZone.zMin = (float)atof(nubs->get(5).c_str());
	newZone.zMax = (float)atof(nubs->get(6).c_str());
      } else if (key == "CYLINDER" && nubs->size() > 5) {
	newZone.box = false;
	newZone.rad = (float)atof(nubs->get(5).c_str());
	newZone.xMax =(float)atof(nubs->get(1).c_str());
	newZone.yMax =(float)atof(nubs->get(2).c_str());
	newZone.zMin =(float)atof(nubs->get(3).c_str());
	newZone.zMax =(float)atof(nubs->get(4).c_str());
      } else if (key == "RRZONEWW" && nubs->size() > 10) {
	newZone.WW = nubs->get(1);
	newZone.WWLifetime = (float)atof(nubs->get(2).c_str());
	newZone.WWPosition[0] = (float)atof(nubs->get(3).c_str());
	newZone.WWPosition[1] = (float)atof(nubs->get(4).c_str());
	newZone.WWPosition[2] = (float)atof(nubs->get(5).c_str());
	newZone.WWTilt = (float)atof(nubs->get(6).c_str());
	newZone.WWTilt = (newZone.WWTilt / 360) * (2 * (float)newZone.pi);
	newZone.WWDirection = (float)atof(nubs->get(7).c_str());
	newZone.WWDirection = (newZone.WWDirection / 360) * (2 * (float)newZone.pi);
	newZone.WWShotID = (int)atoi(nubs->get(8).c_str());
	newZone.WWDT = (float)atof(nubs->get(9).c_str());
	newZone.WWRepeat = (float)atof(nubs->get(10).c_str());
      } else if (key == "SERVERMESSAGE" && nubs->size() > 1) {
	newZone.servermessage = nubs->get(1).c_str();
      } else if (key == "ZONEKILLHUNTER") {
	if (nubs->size() > 1)
	  newZone.playermessage = nubs->get(1).c_str();
	newZone.zonekillhunter = true;
      }
    }
    bz_deleteStringList(nubs);
  }

  zoneList.push_back(newZone);
  bz_setMaxWaitTime((float)0.1);
  return true;
}

void killAllHunters(std::string messagepass)
{
  bzAPIIntList *playerList = bz_newIntList();
  bz_getPlayerIndexList(playerList);


  for (unsigned int i = 0; i < playerList->size(); i++){

    bz_PlayerRecord *player = bz_getPlayerByIndex(playerList->operator[](i));

    if (player) {
      if (player->team != eRabbitTeam) {
	bz_killPlayer(player->playerID, true, BZ_SERVER);
	bz_sendTextMessage(BZ_SERVER, player->playerID, messagepass.c_str());
	if (rrzoneinfo.soundEnabled)
	  bz_sendPlayCustomLocalSound(player->playerID,"flag_lost");
      }
      if (player->team == eRabbitTeam &&
	  rrzoneinfo.soundEnabled &&
	  bz_getTeamCount(eHunterTeam) > 0)
	bz_sendPlayCustomLocalSound(player->playerID,"flag_won");

      bz_freePlayerRecord(player);
    }
  }

  bz_deleteIntList(playerList);

  return;
}


void RabidRabbitEventHandler::process(bz_EventData *eventData)
{
  if ((eventData->eventType != bz_eTickEvent) || (zoneList.size() < 2))
    return;

  for (unsigned int i = 0; i < zoneList.size(); i++) {
    if (!zoneList[i].WWFired && rrzoneinfo.currentKillZone == i) {
      bz_fireWorldWep(zoneList[i].WW.c_str(),
		      zoneList[i].WWLifetime,
		      BZ_SERVER,zoneList[i].WWPosition,
		      zoneList[i].WWTilt,
		      zoneList[i].WWDirection,
		      zoneList[i].WWShotID,
		      zoneList[i].WWDT);
      zoneList[i].WWFired = true;
      zoneList[i].WWLastFired = bz_getCurrentTime();
    } else {
      if ((bz_getCurrentTime() - zoneList[i].WWLastFired) > zoneList[i].WWRepeat)
	zoneList[i].WWFired = false;
    }
  }

  bzAPIIntList *playerList = bz_newIntList();
  bz_getPlayerIndexList(playerList);

  for (unsigned int h = 0; h < playerList->size(); h++) {
    bz_PlayerRecord *player = bz_getPlayerByIndex(playerList->operator[](h));

    if (player) {

      for (unsigned int i = 0; i < zoneList.size(); i++) {
	if (zoneList[i].pointIn(player->pos) &&
	    player->spawned && player->team == eRabbitTeam &&
	    rrzoneinfo.currentKillZone != i && !rrzoneinfo.rabbitNotifiedWrongZone) {
	  bz_sendTextMessage(BZ_SERVER,player->playerID,
			     "You are not in the current Rabid Rabbit zone - try another.");
	  rrzoneinfo.rabbitNotifiedWrongZone = true;
	  rrzoneinfo.rabbitNotifiedWrongZoneNum = i;
	}

	if (!zoneList[i].pointIn(player->pos) &&
	    player->spawned && player->team == eRabbitTeam &&
	    rrzoneinfo.rabbitNotifiedWrongZone &&
	    rrzoneinfo.rabbitNotifiedWrongZoneNum == i)
	  rrzoneinfo.rabbitNotifiedWrongZone = false;

	if (zoneList[i].pointIn(player->pos) &&
	    player->spawned &&
	    player->team == eRabbitTeam &&
	    rrzoneinfo.currentKillZone == i &&
	    bz_getTeamCount(eHunterTeam) > 0) {
	  killAllHunters(zoneList[i].servermessage);

	  rrzoneinfo.rabbitNotifiedWrongZone = true;
	  rrzoneinfo.rabbitNotifiedWrongZoneNum = i;

	  if (i == (zoneList.size() - 1))
	    rrzoneinfo.currentKillZone = 0;
	  else
	    rrzoneinfo.currentKillZone++;

	  rrzoneinfo.rabbitNotifiedWrongZone = true;
	  rrzoneinfo.rabbitNotifiedWrongZoneNum = i;
	}

	if (zoneList[i].pointIn(player->pos) &&
	    player->spawned &&
	    player->team != eRabbitTeam &&
	    zoneList[i].zonekillhunter) {
	  bz_killPlayer(player->playerID, true, BZ_SERVER);
	  bz_sendTextMessage (BZ_SERVER, player->playerID, zoneList[i].playermessage.c_str());
	}
      }
      bz_freePlayerRecord(player);
    }
  }

  bz_deleteIntList(playerList);
  return;
}

void RabidRabbitDieEventHandler::process(bz_EventData *eventData)
{
  if (eventData->eventType != bz_ePlayerDieEvent)
    return;

  bz_PlayerDieEventData *DieData = (bz_PlayerDieEventData*)eventData;

  if (rrzoneinfo.cycleOnDie && DieData->team == eRabbitTeam) {
    unsigned int i = rrzoneinfo.currentKillZone;
    if (i == (zoneList.size() - 1))
      rrzoneinfo.currentKillZone = 0;
    else
      rrzoneinfo.currentKillZone++;
  }

  return;
}

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