Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Release Forum » [script]random map rotation code && !setnextmap  () 2 Votes
[script]random map rotation code && !setnextmap [message #344397] Fri, 01 August 2008 14:46 Go to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
I wanted to make the map rotation of my reborn server random, it was also needed for the APB server too. This is how I implemented it.

In gmmain.cpp for ssgm 2.0.2 I declared a global variable, like this:

char *mapname;


Then I made a function that sends a message in-game telling players what the next map will be. I wrote this first as it is called several times from different functions, and it suited me to place it at the top of my .cpp file.

//This just announces what the next map will be. The global variable "mapname" gets set by the request random map code.
void mapnameannounce(){
Console_Input(StrFormat("msg The next map will be %s",mapname).c_str());
}


Then I needed to write the function itself, I have included some comments to help people read it properly.

//This is the function that makes the next map a random one from the current list of maps on your server.
//You can call it at any time with "requestrandommap();".
void requestrandommap(){
//I get the name of the current map here
char *currmapname = The_Game()->MapName;
//I initialise and delcare the variable "numberofmaps here"
int numberofmaps = 0;
//Many thanks to Roshambo for this nice little "for" loop
//The loop is basically responsible for getting the amount of maps in the rotation
for(;*The_Game()->MapList[numberofmaps] != 0; numberofmaps++);
//I get a random number between 0 (maps use 0 based indexing) and the amount of maps in rotation (hence the need to know the amount of maps).
int RandomNum = Commands->Get_Random_Int(0,numberofmaps);
//This code here makes the server think that the current map is a different one, so it logically will play the map next in the list to the one it thinks is currently being played
The_Game()->MapNumber = RandomNum;
//Therefore the next map that will get played is the one after the one that the server thinks is playing right now (but isn't), so this is how I get the name of the next map
mapname = The_Game()->MapList[RandomNum +1];
if (RandomNum + 1 > numberofmaps - 1){
mapname = The_Game()->MapList[0];
}
//Code to make sure the next map will never be the same one as the current map, you need to be running more then one map for this to work. I will always use more then one map so I never bothered to account for this.
if ((strcmp(currmapname,mapname))== 0){
	Console_Output("Had to request a respawn, just saved you playing the same map twice...\n");
requestrandommap();
}
else {
//Just log the next map on the console
Console_Output("The next map will be: %s\n",mapname);
//Console_Output("The next map number is: %i\n",RandomNum + 1);
//Call the function that announces the next map to be played in-game.
mapnameannounce();
}
}


To make it actually work, I decided to call the function on the level_load event, so as soon as a map loads, it's deciding immdiately what map to play next. I called the function here:

void Level_Loaded() {
	requestrandommap();



There is a problem though.. Most regulators have a !nextmap command. This command will produce errors. For our own regulator at MP-Gaming one of the other devs is changing the bot to remove this command, as I have made a chathook to replace it. You may need to ask Danpaul or the relevant person to disable this command for your server if you wish to run this code.

Also, here is the chat hook for !nextmap

class mapChatCommand : public ChatCommandClass {
	void Triggered(int ID,const TokenClass &Text,int ChatType) {
mapnameannounce();	
	}
};
ChatCommandRegistrant<mapChatCommand> mapChatCommandReg("!nextmap",CHATTYPE_ALL,0,GAMEMODE_ALL);


Whilst I am making a thread... Does anyone know how to make several alias for the chat coomand? I would prefer to have several commands trigger it "!next" || "!N" || "!Next etc etc, rather then make like 8 different chat hooks to all call the same function.


Also, some people showed an interest in an in-game moderator function to set the next map of the servers rotation. So here it is (sorry for no comments, was in a hurry):

class setmapChatCommand : public ChatCommandClass {
	void Triggered(int ID,const TokenClass &Text,int ChatType) {
		if(Is_Mod(Get_Player_Name_By_ID(ID))){
if (!Text[1].empty()) {
int number = 0;

std::string mapname2 = Text[1].c_str();

int numberofmaps = 0;
for(;*The_Game()->MapList[numberofmaps] != 0; numberofmaps++);


for (int i = 0; i < numberofmaps; i++) {
if ((strcmp(The_Game()->MapList[i],mapname2.c_str()))==0) {
The_Game()->MapNumber = i-1;

mapname = The_Game()->MapList[i];
number++;
Console_Input(StrFormat("msg Moderator %s has changed the next map!",Get_Player_Name_By_ID(ID)).c_str());
mapnameannounce();
break;
}
}

if(number < 1){
Console_Input(StrFormat("ppage %d Map does not exist, are you sure you spelt it correctly?",ID).c_str());
}
}
		}
				else {
Console_Input(StrFormat("ppage %d You're not a mod, GTFO.",ID).c_str());
		}
	}
};
ChatCommandRegistrant<setmapChatCommand> setmapChatCommandReg("!setnextmap",CHATTYPE_ALL,1,GAMEMODE_ALL);


To use it, you have to have your name listed in the mods.txt file, then you would type !setnextmap C&C_Hourglass.mix or some other map name in the servers rotation.

Oh, and the ismod function is here:

bool Is_Mod(const char *Name)
{
     fstream file("mods.txt", ios::in);
     string tmp;
     while(file >> tmp)
     { 
         if(strcmp(Name, tmp.c_str()) == 0)
          {
               return 1;
         }
     }
     return 0;
}


You just need a a mods.txt file in your servers directory with player names of people you want to be a mod in it.


Many thanks to roshambo for his pretty neat for loop to count maps, awesome dude! Really helped a million Very Happy



[Updated on: Fri, 30 January 2009 14:39] by Moderator

Report message to a moderator

Re: random map rotation code && !setnextmap [message #344398 is a reply to message #344397] Fri, 01 August 2008 14:50 Go to previous messageGo to next message
Genesis2001
Messages: 1397
Registered: August 2006
Karma: 0
General (1 Star)
Nice work Reborn! Smile
Re: random map rotation code && !setnextmap [message #344405 is a reply to message #344397] Fri, 01 August 2008 15:26 Go to previous messageGo to next message
wittebolx is currently offline  wittebolx
Messages: 332
Registered: May 2007
Location: the netherlands
Karma: 0
Recruit
reborn wrote on Fri, 01 August 2008 23:46


Oh, and the ismod function is here:

bool Is_Mod(const char *Name)
{
     fstream file("mods.txt", ios::in);
     string tmp;
     while(file >> tmp)
     { 
         if(strcmp(Name, tmp.c_str()) == 0)
          {
               return 1;
         }
     }
     return 0;
}


You just need a a mods.txt file in your servers directory with player names of people you want to be a mod in it.


Many thanks to roshambo for his pretty neat for loop to count maps, awesome dude! Really helped a million Very Happy


first of all Great Work on the Script!!
second, the mods.txt will give much problems since it only detects the player name and since we have many nick spoofers that could be a problem...

im not here to judge but people must now this could be a problem.
anyway the code is very helpfull in creating a new Bot Wink


Re: random map rotation code && !setnextmap [message #344484 is a reply to message #344397] Sat, 02 August 2008 01:26 Go to previous message
jnz is currently offline  jnz
Messages: 3396
Registered: July 2006
Location: 30th century
Karma: 0
General (3 Stars)
Nice one Reborn, another good release Big Ups

You might want to change:
Console_Input(StrFormat("msg Moderator %s has changed the next map!",Get_Player_Name_By_ID(ID)).c_str());


to

Console_Input(StrFormat("msg Moderator %S has changed the next map!",Get_Wide_Player_Name_By_ID(ID)).c_str());

[Updated on: Sat, 02 August 2008 01:27]

Report message to a moderator

Previous Topic: BloodMod
Next Topic: Coop Beta 2.50
Goto Forum:
  


Current Time: Fri Nov 29 17:22:39 MST 2024

Total time taken to generate the page: 0.00899 seconds