File: fairCTF.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 (375 lines) | stat: -rw-r--r-- 10,703 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* 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"

class fairCTF : public bz_Plugin, public bz_CustomSlashCommandHandler
{
public:
    virtual const char* Name ()
    {
        return "Fair CTF";
    }

    virtual void Init ( const char* config );
    virtual void Cleanup ( void );
    virtual void Event ( bz_EventData *eventData );

    virtual bool SlashCommand (int playerID, bz_ApiString command, bz_ApiString message, bz_APIStringList *params);

    virtual void DropTeamFlag(int playerID);
    virtual void SetDropTime();
    virtual void UpdateState(bz_eTeamType teamLeaving);
    virtual bool isEven(bz_eTeamType teamLeaving);

    bool allowCTF;
    bool autoMode;

    float max_ratio;
    int max_gap_by_1;
    int max_gap;
    int drop_delay;


    double droptime;
};

BZ_PLUGIN(fairCTF)

void fairCTF::Init ( const char* config )
{
    // Initialize defaults
    allowCTF = true;
    autoMode = true;
    max_ratio = .25;
    max_gap_by_1 = 2;
    max_gap = 3;
    drop_delay = 5;

    // Parse out args

    std::string rawparams = config;

    std::string params[4];
    params[0] = "";
    params[1] = "";
    params[2] = "";
    params[3] = "";

    unsigned int n = 0;

    for (unsigned int i = 0; i < rawparams.length(); i++)
    {
        if (rawparams.at(i) == ':')
        {
            n++;

            if (n > 3)
                break;
        }
        else
            params[n] += rawparams.at(i);
    }

    if (params[0].length() > 0)
    {
        float tempratio = (float)atof(params[0].c_str());
        if (tempratio > 0.0)
            max_ratio = tempratio;
    }

    if (params[1].length() > 0)
    {
        int tempmax1gap = atoi(params[1].c_str());
        if (tempmax1gap > 0)
            max_gap_by_1 = tempmax1gap;
    }

    if (params[2].length() > 0)
    {
        int tempmaxgap = atoi(params[2].c_str());
        if (tempmaxgap > 0)
            max_gap = tempmaxgap;
    }

    if (params[3].length() > 0)
    {
        int tempdelay = atoi(params[3].c_str());
        if (tempdelay > 0)
            drop_delay = tempdelay;
    }

    Register(bz_eAllowFlagGrab);
    Register(bz_ePlayerJoinEvent);
    Register(bz_ePlayerPartEvent);
    Register(bz_eTickEvent);

    bz_registerCustomSlashCommand ("ctf", this);

    bz_debugMessage(4,"fairCTF plugin loaded");

    UpdateState(eNoTeam);
}

void fairCTF::Cleanup()
{

    Flush();
    bz_removeCustomSlashCommand ("ctf");

    bz_debugMessage(4,"fairCTF plugin unloaded");
}

void fairCTF::Event(bz_EventData *eventData)
{
    if (eventData->eventType == bz_eAllowFlagGrab)
    {
        bz_AllowFlagGrabData_V1* grabData = (bz_AllowFlagGrabData_V1*)eventData;

        if (!allowCTF)
        {
            // Don't allow a team flag grab
            std::string flagtype = bz_getFlagName(grabData->flagID).c_str();
            if (flagtype == "R*" || flagtype == "G*" || flagtype == "B*" || flagtype == "P*")
            {
                grabData->allow = false;
                bz_sendTextMessage (BZ_SERVER, grabData->playerID, "CTF play is currently disabled.");
            }
        }
    }
    else if (eventData->eventType == bz_ePlayerJoinEvent)
        UpdateState(eNoTeam);
    else if (eventData->eventType == bz_ePlayerPartEvent)
    {
        bz_PlayerJoinPartEventData_V1* partData = (bz_PlayerJoinPartEventData_V1*)eventData;
        // Need to compensate for that leaving player.
        UpdateState(partData->record->team);
    }
    else if (eventData->eventType == bz_eTickEvent)
    {

        if (droptime != 0.0 && bz_getCurrentTime() >= droptime)
        {
            // Time to drop any team flags.
            bz_APIIntList* pl = bz_getPlayerIndexList();

            for (unsigned int x = 0; x < pl->size(); x++)
                DropTeamFlag(pl->get(x));

            droptime = 0.0;
        }
    }
    else
    {
        // Huh?
        return;
    }
}

bool fairCTF::SlashCommand (int playerID, bz_ApiString /*command*/, bz_ApiString message, bz_APIStringList * /*params*/)
{
    std::string cs = "UNKNOWN";
    bz_BasePlayerRecord* pr = bz_getPlayerByIndex(playerID);
    if (pr != NULL)
    {
        cs = pr->callsign.c_str();
        bz_freePlayerRecord (pr);
    }

    if (!bz_hasPerm(playerID, "FAIRCTF"))
        bz_sendTextMessage(BZ_SERVER, playerID, (cs + ", you do not have permission to use the /ctf command.").c_str());
    else
    {
        if (message == "on")
        {
            if (!autoMode && allowCTF)
                bz_sendTextMessage(BZ_SERVER, playerID, "CTF is already set to \"on\".");
            else
            {
                autoMode = false;
                bz_sendTextMessage (BZ_SERVER, eAdministrators, ("CTF setting has been changed to \"on\" by " + cs + ".").c_str());
                if (!allowCTF)
                {
                    bz_sendTextMessage (BZ_SERVER, BZ_ALLUSERS, ("CTF has been enabled by " + cs + ".").c_str());
                    allowCTF = true;
                    droptime = 0.0;
                }
            }
        }
        else if (message == "off")
        {
            if (!autoMode && !allowCTF)
                bz_sendTextMessage(BZ_SERVER, playerID, "CTF is already set to \"off\".");
            else
            {
                autoMode = false;
                bz_sendTextMessage (BZ_SERVER, eAdministrators, ("CTF setting has been changed to \"off\" by " + cs + ".").c_str());
                if (allowCTF)
                {
                    bz_sendTextMessage (BZ_SERVER, BZ_ALLUSERS, ("CTF has been disabled by " + cs + ".").c_str());
                    allowCTF = false;
                    SetDropTime();
                }
            }
        }
        else if (message == "auto")
        {
            if (autoMode)
                bz_sendTextMessage(BZ_SERVER, playerID, "CTF is already set to \"auto\".");
            else
            {
                autoMode = true;
                bz_sendTextMessage (BZ_SERVER, eAdministrators, ("CTF setting has been changed to \"auto\" by " + cs + ".").c_str());
                UpdateState(eNoTeam);
            }
        }
        else
            bz_sendTextMessage (BZ_SERVER, playerID, "Usage: /ctf on|off|auto");
    }
    return true;
}

void fairCTF::DropTeamFlag(int playerID)
{
    bz_BasePlayerRecord* droppr = bz_getPlayerByIndex (playerID);

    if (droppr != NULL)
    {
        // Are they carrying a team flag?
        if (droppr->currentFlag == "Red team flag" ||
                droppr->currentFlag == "Green team flag" ||
                droppr->currentFlag == "Blue team flag" ||
                droppr->currentFlag == "Purple team flag")
        {
            bz_removePlayerFlag(playerID);
            bz_sendTextMessage (BZ_SERVER, playerID, "CTF play is currently disabled.");
        }

        bz_freePlayerRecord(droppr);
    }
}

void fairCTF::UpdateState(bz_eTeamType teamLeaving)
{
    if (autoMode)
    {
        bool fair = isEven(teamLeaving);

        if (fair && !allowCTF)
        {
            allowCTF = true;
            droptime = 0.0;
            bz_sendTextMessage (BZ_SERVER, BZ_ALLUSERS, "Team sizes are sufficiently even. CTF play is now enabled.");
        }
        else if (!fair && allowCTF)
        {
            allowCTF = false;
            bz_sendTextMessage(BZ_SERVER, BZ_ALLUSERS, "Team sizes are uneven. CTF play is now disabled.");

            SetDropTime();
        }
    }
}

void fairCTF::SetDropTime()
{
    bz_APIIntList *playerList = bz_newIntList();
    bz_getPlayerIndexList(playerList);
    bool TeamFlagIsCarried = false;

    // is any tank carrying a team flag?
    for (unsigned int i = 0; i < playerList->size(); i++)
    {
        const char *FlagHeld = bz_getPlayerFlag((*playerList)[i]);

        if (FlagHeld != NULL && (strcmp(FlagHeld, "R*") == 0 || strcmp(FlagHeld, "G*") == 0 || strcmp(FlagHeld, "B*") == 0
                                 || strcmp(FlagHeld, "P*") == 0))
        {
            TeamFlagIsCarried = true;
            break;
        }
    }

    bz_deleteIntList(playerList);

    // announce drop delay only if some tank is carrying a team flag
    if (TeamFlagIsCarried)
    {
        if (drop_delay >= 0)
        {
            droptime = bz_getCurrentTime() + (double)drop_delay;
            if (drop_delay > 1)
                bz_sendTextMessage(BZ_SERVER, BZ_ALLUSERS, bz_format("Currently-held team flags will be dropped in %d seconds.",
                                   drop_delay));
            else
                bz_sendTextMessage(BZ_SERVER, BZ_ALLUSERS, "Currently-held team flags will be dropped in 1 second.");

        }
        else
            bz_sendTextMessage(BZ_SERVER, BZ_ALLUSERS, "Currently-held team flags will not be dropped.");
    }
}

bool fairCTF::isEven(bz_eTeamType teamLeaving)
{

    int teamsizes[4];

    teamsizes[0] = bz_getTeamCount (eRedTeam);
    teamsizes[1] = bz_getTeamCount (eGreenTeam);
    teamsizes[2] = bz_getTeamCount (eBlueTeam);
    teamsizes[3] = bz_getTeamCount (ePurpleTeam);

    int leavingTeamIndex = (int)teamLeaving;
    if (leavingTeamIndex >= 1 && leavingTeamIndex <= 4)
    {
        // Decrement the team count for the player that's leaving the game.
        teamsizes[leavingTeamIndex - 1]--;
    }


    //check fairness

    int smallestTeam = 10000; //impossibly high
    int largestTeam = 0;

    for (int x = 0; x < 4; x++)
    {
        if (teamsizes[x] > largestTeam)
            largestTeam = teamsizes[x];
        if (teamsizes[x] != 0 && teamsizes[x] < smallestTeam)
            smallestTeam = teamsizes[x];
    }

    //check differences and ratios

    if (smallestTeam == 10000 || largestTeam == smallestTeam) //equal, or server has no team tanks
        return true;
    if (smallestTeam <= max_gap_by_1) // user-defined cap on a difference of 1
        return false;
    if (largestTeam - smallestTeam == 1) //after UD limit
        return true;
    if ((static_cast<float> (largestTeam - smallestTeam)) / smallestTeam > max_ratio) //greater than specified gap
        return false;
    if (largestTeam - smallestTeam >= max_gap)
        return false;

    return true;
}

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