File: wwzones.cpp

package info (click to toggle)
bzflag 2.4.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,488 kB
  • sloc: cpp: 150,376; ansic: 3,463; sh: 2,535; makefile: 2,194; perl: 486; python: 260; objc: 246; php: 206
file content (281 lines) | stat: -rw-r--r-- 9,426 bytes parent folder | download
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
/* bzflag
 * Copyright (c) 1993-2025 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "bzfsAPI.h"
#include <map>
#include <cmath>

class WWZEventHandler : public bz_Plugin, bz_CustomMapObjectHandler
{
public:
    virtual const char* Name()
    {
        return "World Weapon Zones";
    }
    virtual void Init(const char* config);
    virtual void Cleanup();
    virtual void Event(bz_EventData *eventData);

    virtual bool MapObject (bz_ApiString object, bz_CustomMapObjectInfo *data);
};

BZ_PLUGIN(WWZEventHandler)

void WWZEventHandler::Init (const char* /*commandLineParameter*/)
{
    // Register our custom world object
    bz_registerCustomMapObject("WWZONE", this);

    // Register our events
    Register(bz_eTickEvent);

    // Set an initial minimum wait time of 0.5 seconds
    MaxWaitTime = 0.5f;
}

void WWZEventHandler::Cleanup (void)
{
    // Remove our events
    Flush();

    // Remove our custom world object
    bz_removeCustomMapObject("WWZONE");
}

class WWZPlyrInfo
{
public:
    WWZPlyrInfo()
    {
        wwzplyrID = -1;
        wwzPlyrInTime = 0;
    }

    int wwzplyrID;
    double wwzPlyrInTime;
};

WWZPlyrInfo wwzNewPlyr;

class WWZone : public bz_CustomZoneObject
{
public:
    WWZone() : bz_CustomZoneObject()
    {
        zoneWeapon = "";
        zoneWeaponLifetime = 0;
        zoneWeaponPosition[0] = 0;
        zoneWeaponPosition[1] = 0;
        zoneWeaponPosition[2] = 0;
        zoneWeaponTilt = 0;
        zoneWeaponDirection = 0;
        zoneWeaponShotID = 0;
        zoneWeaponRepeat = false;
        zoneWeaponFired = false;
        zoneWeaponLastFired = 0;
        zoneWeaponMinFireTime = 0.5;
        zoneWeaponTimeDelay = 0;
        zoneWeaponInfoMessage = false;
        zoneWeaponSentMessage = false;
        wwzPlyrList.clear();
    }

    std::vector <WWZPlyrInfo> wwzPlyrList;

    bz_ApiString zoneWeapon;
    float zoneWeaponLifetime, zoneWeaponPosition[3], zoneWeaponTilt, zoneWeaponDirection;
    double zoneWeaponMinFireTime, zoneWeaponTimeDelay, zoneWeaponLastFired;
    bool zoneWeaponRepeat, zoneWeaponInfoMessage, zoneWeaponFired, zoneWeaponSentMessage;
    int zoneWeaponShotID;

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

std::vector <WWZone> zoneList;

bool WWZEventHandler::MapObject (bz_ApiString object, bz_CustomMapObjectInfo *data)
{
    if (object != "WWZONE" || !data)
        return false;

    WWZone newZone;
    newZone.handleDefaultOptions(data);

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

        bz_APIStringList *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 == "ZONEWEAPON" && nubs->size() > 9)
            {
                newZone.zoneWeapon = nubs->get(1);
                newZone.zoneWeaponLifetime = (float)atof(nubs->get(2).c_str());
                newZone.zoneWeaponPosition[0] = (float)atof(nubs->get(3).c_str());
                newZone.zoneWeaponPosition[1] = (float)atof(nubs->get(4).c_str());
                newZone.zoneWeaponPosition[2] = (float)atof(nubs->get(5).c_str());
                newZone.zoneWeaponTilt = (float)(atof(nubs->get(6).c_str()) * M_PI/180.0);  // convert degrees to radians
                newZone.zoneWeaponDirection = (float)(atof(nubs->get(7).c_str()) * M_PI/180.0); // convert degrees to radians
                newZone.zoneWeaponShotID = (int)atoi(nubs->get(8).c_str());
            }
            else if (key == "REPEAT" && nubs->size() == 1)
                newZone.zoneWeaponRepeat = true;

            else if (key == "REPEAT" && nubs->size() > 1)
            {
                newZone.zoneWeaponRepeat = true;
                newZone.zoneWeaponMinFireTime = (double)atof(nubs->get(1).c_str());
                if (newZone.zoneWeaponMinFireTime < MaxWaitTime && newZone.zoneWeaponMinFireTime >= 0.1)
                    MaxWaitTime = (float)newZone.zoneWeaponMinFireTime - 0.05f;   // tick faster than ww calls
            }
            else if (key == "TIMEDELAY" && nubs->size() > 1)
            {
                newZone.zoneWeaponTimeDelay = (double)atof(nubs->get(1).c_str());
                if (newZone.zoneWeaponTimeDelay < 0)
                    newZone.zoneWeaponTimeDelay = 0;
            }
            else if (key == "PLAYERMESSAGE" && nubs->size() > 1)
                newZone.playermessage = nubs->get(1).c_str();

            else if (key == "SERVERMESSAGE" && nubs->size() > 1)
                newZone.servermessage = nubs->get(1).c_str();

            else if (key == "INFOMESSAGE")
                newZone.zoneWeaponInfoMessage = true;
        }
        bz_deleteStringList(nubs);
    }
    zoneList.push_back(newZone);
    return true;
}

inline bool wasHere(int zoneNum, int plyrNum)
{
    for (unsigned int j = 0; j < zoneList[zoneNum].wwzPlyrList.size(); j++)
    {
        if (plyrNum == zoneList[zoneNum].wwzPlyrList[j].wwzplyrID)
            return true;
    }

    wwzNewPlyr.wwzplyrID = plyrNum;
    wwzNewPlyr.wwzPlyrInTime = bz_getCurrentTime();

    zoneList[zoneNum].wwzPlyrList.push_back(wwzNewPlyr);
    zoneList[zoneNum].zoneWeaponSentMessage = false;
    zoneList[zoneNum].zoneWeaponFired = false;

    return false;
}

inline void notHere(int zoneNum, int plyrNum)
{
    for (unsigned int j = 0; j < zoneList[zoneNum].wwzPlyrList.size(); j++)
    {
        if (plyrNum == zoneList[zoneNum].wwzPlyrList[j].wwzplyrID)
        {
            zoneList[zoneNum].wwzPlyrList.erase(zoneList[zoneNum].wwzPlyrList.begin() + j);
            zoneList[zoneNum].zoneWeaponFired = false;
            zoneList[zoneNum].zoneWeaponSentMessage = false;
            return;
        }
    }
}

inline bool OKToFire(int zoneNum, int plyrNum)
{
    for (unsigned int j = 0; j < zoneList[zoneNum].wwzPlyrList.size(); j++)
    {
        if (zoneList[zoneNum].wwzPlyrList[j].wwzplyrID == plyrNum)
        {
            if ((bz_getCurrentTime() - zoneList[zoneNum].wwzPlyrList[j].wwzPlyrInTime) > zoneList[zoneNum].zoneWeaponTimeDelay
                    && !zoneList[zoneNum].zoneWeaponFired)
            {
                zoneList[zoneNum].wwzPlyrList[j].wwzPlyrInTime = bz_getCurrentTime();
                return true;
            }
        }
    }

    return false;
}

void WWZEventHandler::Event (bz_EventData *eventData)
{
    if (eventData->eventType != bz_eTickEvent)
        return;

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

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

        if (player)
        {
            for (unsigned int i = 0; i < zoneList.size(); i++)
            {
                if (zoneList[i].pointInZone(player->lastKnownState.pos) && player->spawned)
                {
                    if (wasHere(i, player->playerID) && OKToFire(i, player->playerID) && !zoneList[i].zoneWeaponFired)
                    {
                        float vector[3];
                        bz_vectorFromRotations(zoneList[i].zoneWeaponTilt, zoneList[i].zoneWeaponDirection, vector);
                        bz_fireServerShot(zoneList[i].zoneWeapon.c_str(), zoneList[i].zoneWeaponPosition, vector);
                        zoneList[i].zoneWeaponFired = true;
                        zoneList[i].zoneWeaponLastFired = bz_getCurrentTime();
                    }
                    else
                    {
                        if ((bz_getCurrentTime() - zoneList[i].zoneWeaponLastFired) > zoneList[i].zoneWeaponMinFireTime
                                && zoneList[i].zoneWeaponRepeat)
                            zoneList[i].zoneWeaponFired = false;
                    }

                    if (!zoneList[i].zoneWeaponSentMessage && zoneList[i].zoneWeaponFired)
                    {
                        if (!zoneList[i].playermessage.empty())
                            bz_sendTextMessage(BZ_SERVER, player->playerID, zoneList[i].playermessage.c_str());
                        if (!zoneList[i].servermessage.empty())
                            bz_sendTextMessage(BZ_SERVER, BZ_ALLUSERS, zoneList[i].servermessage.c_str());
                        if (zoneList[i].zoneWeaponInfoMessage)
                            bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS, "%s triggered by %s.", zoneList[i].zoneWeapon.c_str(),
                                                player->callsign.c_str());

                        zoneList[i].zoneWeaponSentMessage = true;
                    }
                }
                else
                    notHere(i, player->playerID);
            }
            bz_freePlayerRecord(player);
        }
    }

    bz_deleteIntList(playerList);
    return;
}

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