Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » Change map
Re: Change map [message #437351 is a reply to message #437234] Sat, 02 October 2010 11:05 Go to previous message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma:
General (3 Stars)
This is the source to my randon rotation plugin, it shows clearly how to changhe the next map.

#include "scripts.h"
#include <stdarg.h>
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include "engine.h"
#include "gmmain.h"
#include <time.h>
#include "randomrotation.h"


RRSettingsStruct *RRSettings = 0;
char *mapname;
std::vector<int> LastMaps;


void RRSettingsStruct::Load() {
	SettingsLoader::Load();
	LoadInt(PreviousLimit,"PreviousLimit", 5, true, false, true);
}


int Get_Random_Int_Not_Crap(int n){
return rand() % n;
}


//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());
}


//This function basically updates the most recently played maps, by pushing them along One each time One is added, then "trimming" the vector using .resize()
//How many maps are kept track of depend on the setting in the ini file.
void addmap(int MapNumber){
	LastMaps.push_back(MapNumber);
	for(int i = 0; i <= RRSettings->PreviousLimit - 1; i++){
		LastMaps[i] = LastMaps[i + 1];
	}
	LastMaps.resize(RRSettings->PreviousLimit, MapNumber);
}




//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 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 = Get_Random_Int_Not_Crap(numberofmaps);
    if (RandomNum + 1 > numberofmaps - 1){
        RandomNum = 0;
    }
    //Code to make sure the next map will never be the same one as the current map
    for(int i = 0; i <= (RRSettings->PreviousLimit - 1); i++){
        if(RandomNum == LastMaps[i]){
            Console_Output("Had to re-request a new map, this One was recently played.\n");
            requestrandommap();
        }
    }
        //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];
        //Just log the next map on the console
        Console_Output("The next map will be: %s\n",mapname);
        //Call the function that announces the next map to be played in-game.
        mapnameannounce();
}



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





void Plugin_Load() {
	srand(time(NULL));
	RRSettings = new RRSettingsStruct("RandomRotation.ini");
	printf("Loaded reborns random rotation system plugin\n");
	RRSettings->Load();
	for(int i = 0; i <= RRSettings->PreviousLimit -1; i++){ 
		LastMaps.push_back(-1);
	}

}

void Plugin_Unload() {
	printf("Un-loaded reborns random rotation system plugin\n");
	delete RRSettings;
}




extern "C" {
	DLLEXPORT void SSGM_Level_Loaded_Hook() {
		int numberofmaps = 0;
		for(;*The_Game()->MapList[numberofmaps] != 0; numberofmaps++);
		if(numberofmaps > RRSettings->PreviousLimit){
			addmap(The_Game()->MapNumber); // new map has loaded and is being played, add it to the list of recently played maps
			requestrandommap(); // Choose the next map to be played (even though the current map has just stared)
		}
		else{
			printf("ERROR! You have less maps in rotation than what's set in the RandomRotation.ini file's previous limit key.\n The plugin will not function as you want it to!\n");
		}
	}
}




#include "gmsettingsclass.h"

#define PluginName "reborn's random rotation plugin"
#define PluginVersion "1.1"


struct RRSettingsStruct : public virtual SettingsLoader {
	RRSettingsStruct(const char *ININame) : SettingsLoader(ININame) {
		PreviousLimit = 5;
	}
	void Load();
	int PreviousLimit;
};


int Get_Random_Int_Not_Crap(int n);
void mapnameannounce();
void addmap(int MapNumber);
void requestrandommap();

void Plugin_Load();
void Plugin_Unload();




[Updated on: Sat, 02 October 2010 11:06]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Measuring and rewarding team work programatically?
Next Topic: vtach/detach command
Goto Forum:
  


Current Time: Mon Nov 18 14:40:46 MST 2024

Total time taken to generate the page: 0.00828 seconds