Author |
Message |
tansey Novice
Joined: Nov 03 2004 Posts: 53 Offline
|
Posted: 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 |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: 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:
#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;
}
|
_________________ SubSpace Discretion: A Third Generation SubSpace Client
Last edited by Bak on Thu Feb 24, 2005 12:05 pm, edited 4 times in total |
|
Back to top |
|
 |
tansey Novice
Joined: Nov 03 2004 Posts: 53 Offline
|
Posted: 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. |
|
Back to top |
|
 |
D1st0rt Miss Directed Wannabe

Age:37 Gender: Joined: Aug 31 2003 Posts: 2247 Location: Blacksburg, VA Offline
|
Posted: 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 _________________
 |
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Fri Feb 25, 2005 12:04 am Post subject: |
 |
|
|
|
this is wrong.. way wrong.
I wasn't happy w/ something someone brought to my attention so ill post my suggested edits:
spawn.h
VoteResults getResults(); |
replace with
VoteResults* botInfo::getResults(); |
spawn.cpp
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
VoteResults *vr = getResults();
int res1 = vr->results[0]; |
_________________ Performance is often the art of cheating carefully. - James Gosling
Last edited by CypherJF on Fri Feb 25, 2005 10:00 am, edited 1 time in total |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: 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. |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: 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.  |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: 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. _________________ This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him. |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: 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);
|
#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;
}
|
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sat Feb 26, 2005 12:58 am Post subject: |
 |
|
|
|
@Tansey: You might want to post that on the Wiki's example code page. |
|
Back to top |
|
 |
|