Server Help

Bot Questions - Voting tutorial...

tansey - Thu Feb 24, 2005 10:47 am
Post subject: Voting tutorial...
I've had several people ask me in game for how to make a bot take a vote. Since I can't spare the hours ( days ) it would take to teach people about object oriented design and then show them how my bot does it, I wrote a tutorial using only the 3 files most people use in MERV. Lemme know what you guys think:

http://www.sectionops.net/er/voting.html

--tansey
Bak - Thu Feb 24, 2005 11:42 am
Post subject:
Another way to do it would be to keep track of the votes using the player tags mervbot provides:

Code: Show/Hide

#define VOTING_TAG      6326
enum _VotePossibilities
{
   VOTE_Obstain,  // leave this one at the start

   // add whatever you want here
   VOTE_Dodgeball, // 1
   VOTE_Football, // 2
   VOTE_Hockey, // 3

   VOTE_Count // leave this one at the end
};

struct VoteResults
{
    int results[VOTE_Count];
};

void botInfo::vote(int value, Player *p)
{
     if (value > 0 && value < VOTE_Count);
         set_tag(p,VOTING_TAG,value);
}

void botInfo::resetVotes()
{
    for (_listnode <Player> *parse = playerlist->head; parse; parse = parse->next)
    {
        set_tag(parse->item,VOTING_TAG,VOTE_Obstain);
    }
}

VoteResults botInfo::getResults()
{
     VoteResults vr;

     for (int x = 0; x < VOTE_Count;++x)
        vr.results[x] = 0;

     for (_listnode <Player> *parse = playerlist->head; parse; parse = parse->next)
     {
         if (parse->item != me)
         {
              int vote = get_tag(parse->item,VOTING_TAG);

              vr.results[vote]++;
         }
     }

    return vr;
}



tansey - Thu Feb 24, 2005 11:55 am
Post subject:
No, I'm not. countVotes() calls votes.clear(), which blows away all the nodes in the list.
D1st0rt - Thu Feb 24, 2005 3:09 pm
Post subject:
I would think all you really have to do is modify the elim plugin, because it has voting built in
CypherJF - Fri Feb 25, 2005 12:04 am
Post subject:
this is wrong.. way wrong. icon_lol.gif


I wasn't happy w/ something someone brought to my attention so ill post my suggested edits:

spawn.h
Code: Show/Hide
VoteResults getResults();


replace with

Code: Show/Hide
VoteResults* botInfo::getResults();


spawn.cpp
Code: Show/Hide
VoteResults* botInfo::getResults()
{
     VoteResults vr;

     for (int x = 0; x < VOTE_Count;++x)
        vr.results[x] = 0;

     for (_listnode <Player> *parse = playerlist->head; parse; parse = parse->next)
     {
         if (parse->item != me)
         {
              int vote = get_tag(parse->item,VOTING_TAG);

              vr.results[vote]++;
         }
     }

    return &vr;
}

I'm still debating whether or not to mention anything about having the linked list in a for-statement. Not that it's "wrong". Just... meh.

Example Use
Code: Show/Hide
VoteResults *vr = getResults();
int res1 = vr->results[0];

Bak - Fri Feb 25, 2005 9:35 am
Post subject:
see the problem with that Cypher is it doesn't work. VoteResults is a declared in the scope of the function getResults. After the function the memory it used may be used for other things, so if you use a pointer to it, there's no guarentee it hasn't changed (in fact, it probably has). You could declare it on the heap and force the user to free() or delete the memory after, but I'm just as happy returning the array.
Mr Ekted - Fri Feb 25, 2005 9:39 am
Post subject:
Code: Show/Hide
{
VoteResults vr;

return &vr;
}


Yes. Very bad.
CypherJF - Fri Feb 25, 2005 9:59 am
Post subject:
oh duh me rotfl... doh doh doh haaha... i even just answered this one the one exam i just took explainin how the scope would affect such an object. tongue.gif
Cyan~Fire - Fri Feb 25, 2005 5:11 pm
Post subject:
@Bak: That won't work if he wants to save a player's vote after he leaves.
Bak - Fri Feb 25, 2005 5:29 pm
Post subject:
@~: You're right, but if you're voting on what game the bot will host next, do you really want to use a player's vote if he or she is not going to play?

Here's a version that doesn't return the array, use would be like
Quote:

int results[VOTE_Count];
int winner = getResults(results);


Code: Show/Hide

#define VOTING_TAG      6326
enum _VotePossibilities
{
   VOTE_Obstain,  // leave this one at the start

   // add whatever you want here
   VOTE_Dodgeball, // 1
   VOTE_Football, // 2
   VOTE_Hockey, // 3

   VOTE_Count // leave this one at the end
};

void botInfo::vote(int value, Player *p)
{
     if (value > 0 && value < VOTE_Count);
         set_tag(p,VOTING_TAG,value);
}

void botInfo::resetVotes()
{
    for (_listnode <Player> *parse = playerlist->head; parse; parse = parse->next)
    {
        set_tag(parse->item,VOTING_TAG,VOTE_Obstain);
    }
}

// get the winner, and the results where results is an int array at least as big as VOTE_Count
int botInfo::getResults(int* results)
{
    int winner = 0;
    int winnerCount = 0;

     for (int x = 0; x < VOTE_Count;++x)
        (*results)[x] = 0;

     for (_listnode <Player> *parse = playerlist->head; parse; parse = parse->next)
     {
         if (parse->item != me)
         {
              int vote = get_tag(parse->item,VOTING_TAG);
              int count = (*results)[vote] + 1;
              if (count > winnerCount)
              {
                   count = winnerCount;
                   winner = vote;
              }

              (*results)[vote] = count;
         }
     }

    return winner;
}

Cyan~Fire - Sat Feb 26, 2005 12:58 am
Post subject:
@Tansey: You might want to post that on the Wiki's example code page.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group