All pastes #157197 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-09-01 11:30 UTC
rendered paste body
// Grishracetemplate
// This is a template for making race modes in samp
//
// Alter the code marked with ***** to suit your mode
// all the code that needs altering is near the top of this script
// so you don't need to hunt around for what to change
//
// [DRuG]Grish
// 27/6/2006

#include <a_samp>

// *****
// Set the filename for storing best times here.
// Make sure you have got the \scriptfiles folder
// in your samp server folder.
// Make it unique to your mode.
#define RECORDFILE "grishracetemplatefile.txt"

// *****
// Set the numper of entries in the checkpoints array here.
// The number of race checkpoints will show up as 1 less than this number.
#define MAXCP 37

// *****
// Put your starting grid locations here.
// Format is {x,y,z,angle}
// Same amount as you have starting vehicles.
// Be sure to change the [8] to the number of grid locations you have.
new Float:racestart[8][4] = {
{2031.9198,847.7925,7.1002,90.0},
{2031.9198,857.7925,7.1002,90.0},
{2041.9198,847.7925,7.1002,90.0},
{2041.9198,857.7925,7.1002,90.0},
{2051.9198,847.7925,7.1002,90.0},
{2051.9198,857.7925,7.1002,90.0},
{2061.9198,847.7925,7.1002,90.0},
{2061.9198,857.7925,7.1002,90.0}
};

// *****
// Put your checkpoint locations here.
// Same amount as you defied MAXCP as.
// Format is {x,y,z}
// Make the final checkpoint {0.0,0.0,0.0} after the finish checkpoint.
new Float:checkpoints[MAXCP][3] = {
{1769.4868,851.7225,10.0452},
{1314.9775,882.4576,6.4374},
{1225.8315,1286.3774,6.3629},
{1224.6378,2215.3162,6.3589},
{1616.2266,2455.3342,6.4663},
{2239.5613,2600.9739,6.3867},
{2686.1184,2399.5120,6.3594},
{2709.6755,1876.6189,6.3592},
{2636.7515,917.0633,6.3591},
{2407.0552,851.8081,6.3631},
{2030.4202,834.5757,6.3632},
{1449.7327,834.2547,6.4409},
{1208.7852,1172.1589,6.4374},
{1225.8546,1292.6505,6.3630},
{1209.3243,1474.1068,6.3609},
{1226.8951,1712.4558,6.3592},
{1209.7170,1889.8855,6.3594},
{1225.5254,2010.1133,6.3595},
{1210.3157,2135.2490,6.3589},
{1369.8258,2466.9639,6.3589},
{1621.3256,2458.7861,6.4651},
{1972.4384,2550.2344,6.4216},
{2231.7883,2599.7429,6.3866},
{2539.1138,2592.1216,4.4841},
{2690.5945,2395.5029,6.3590},
{2726.4358,2250.4768,6.3688},
{2707.7854,2120.5283,6.3667},
{2724.2791,2042.9327,6.3629},
{2709.8926,1872.7764,6.3590},
{2726.1953,1596.2194,6.3629},
{2709.2205,1312.4891,6.3592},
{2725.3789,1220.3663,6.3645},
{2631.1663,928.4933,6.3631},
{2431.0984,855.8843,6.3590},
{2340.5420,974.2465,10.2954},
{2388.1653,992.1713,10.4452},
{0.0,0.0,0.0}
};


new playercp[MAXCP];
new msg[250];
new playerdriving[100]; 
new playerinrace[100]; 
new playercheck[100]; 
new playerposition[100]; 
new timestart[100]; 
new timeend[100]; 
new Float:timetotal[100]; 
new minutes[100]; 
new seconds[100]; 
new mseconds[100]; 
new ready = 0; 
new rcount = 0; 
new raceon = 0; 
new trig = 0; 
new trigg = 0;
new triggg = 0;
new racers = 0; 
new finished = 0; 
new File:fileh; 

main()
{
	// *****
	// Change this to the name of your mode.
	print("\n----------------------------------");
	print("  Grishracetemplate");
	print("----------------------------------\n");
}



public OnGameModeInit()
{
	// *****
	// Change this to the name of your mode.
	SetGameModeText("grishracetemplate");

	// *****
	// Put the player spawn location here.
	// Make sure it's near the vehicle spawns.
	AddPlayerClass(0,2035.8396,1085.9219,10.8203,180.1034, 0, 0, 0, 0, 0, 0);

	// *****
	// Put the race vehicles here.
	// Same amount as the grid locations.
	AddStaticVehicle(541,2040.0690,1078.7656,10.8493,176.9732,0,16);
	AddStaticVehicle(541,2040.3000,1068.1735,10.8502,178.3624,0,16);
	AddStaticVehicle(541,2039.5710,1058.5510,10.8478,178.7766,0,16);
	AddStaticVehicle(541,2039.9451,1048.6780,10.8483,177.4517,0,16);
	AddStaticVehicle(541,2039.7129,1038.7970,10.8417,179.8434,0,16);
	AddStaticVehicle(541,2039.9021,1028.7639,10.8488,180.7464,0,16);
	AddStaticVehicle(541,2039.6238,1018.7139,10.8475,176.8509,0,16);
	AddStaticVehicle(541,2040.3035,1007.9570,10.8538,178.2659,0,16);

    for (new i=0; i<MAXCP; i++)
    {
    playercp[i] = 999;
    }

	SetTimer("BeginRace",1000,1);
	SetTimer("IsRaceEmpty",1100,1);
	print("GameModeInit()");
	return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
	printf("OnPlayerRequestClass(%d, %d)", playerid, classid);
	SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
	SetPlayerFacingAngle(playerid,270.0);
	SetPlayerCameraPos(playerid, 1961.3783, 1343.1572, 15.3746);
	SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
	return 1;
}

public OnGameModeExit()
{
	print("GameModeExit()");
	return 1;
}

public OnPlayerConnect(playerid)
{
	SendClientMessage(playerid,0x00FF00FF,"type /joinrace once you have a car");
	SendClientMessage(playerid,0x00FF00FF,"type /winner to see who's winning");
	printf("OnPlayerConnect(%d)", playerid);
	return 1;
}

public OnPlayerDisconnect(playerid)
{
    playerinrace[playerid] = 0;
    playerposition[playerid] = 999;
	printf("OnPlayerDisconnect(%d)", playerid);
	return 1;
}

public OnPlayerSpawn(playerid)
{
	SetPlayerInterior(playerid,0);
	ResetPlayerMoney(playerid);
	GivePlayerMoney(playerid,100000);
	playerdriving[playerid] = 0;
	playerinrace[playerid] = 0;
	playercheck[playerid] = 0;
	playerposition[playerid] = 999;
	printf("OnPlayerSpawn(%d)", playerid);
	return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
    playerinrace[playerid] = 0;
    playerposition[playerid] = 999;
	printf("OnPlayerDeath(%d, %d, %d)", playerid, killerid, reason);
	return 1;
}

public OnVehicleSpawn(vehicleid)
{
	printf("OnVehicleSpawn(%d)", vehicleid);
	return 1;
}

public OnVehicleDeath(vehicleid, killerid)
{
	printf("OnVehicleDeath(%d, %d)", vehicleid, killerid);
	return 1;
}

public OnPlayerText(playerid)
{
	printf("OnPlayerText(%d)", playerid);
	return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp(cmdtext, "/help", true)==0)
	{
	SendClientMessage(playerid,0x00FF00FF,"type /joinrace once you have a car");
	SendClientMessage(playerid,0x00FF00FF,"type /winner to see who's winning");
	SendClientMessage(playerid,0x00FF00FF,"admin command /resetrace for emergency race reset");
	return 1;
	}
	
	if (strcmp(cmdtext, "/joinrace", true)==0)
	{
	if (playerdriving[playerid] == 1 && raceon == 0 && playerinrace[playerid] == 0)
	    {
	    new vid = GetPlayerVehicleID(playerid);
    	SetPlayerInterior(playerid,0);
	    SetVehiclePos(vid,racestart[racers][0],racestart[racers][1],racestart[racers][2]);
	    SetVehicleZAngle(vid,racestart[racers][3]);
	    racers = racers + 1;
	    SetCameraBehindPlayer(playerid);
	    TogglePlayerControllable(playerid,0);
	    SendClientMessage(playerid,0x00FF00FF,"You are in the starting grid");
	    playerinrace[playerid] = 1;
	    playerposition[playerid] = 999;
		if (trig == 0)
		{
		trig = 1;
		GameTextForAll("Race will begin in 20 seconds",3000,3);
		SendClientMessageToAll(0x00FF00FF,"you have 20 seconds to get a car");
		SendClientMessageToAll(0x00FF00FF,"and type /joinrace");
		SetTimer("WaitBeforeRace",20000,0);
		}
	    }
	if (playerdriving[playerid] == 1 && raceon == 1)
	    {
	    GameTextForPlayer(playerid,"Race is in progress, please wait",3000,3);
	    }
	return 1;
	}

	if (strcmp(cmdtext, "/resetrace", true)==0)
	{
    if (IsPlayerAdmin(playerid) == 1)
	    {
		raceon = 0;
		racers = 0;
		}
	return 1;
	}

	if (strcmp(cmdtext, "/winner", true)==0)
	{
	new j = 999;
	if (raceon == 1)
	{
	for(new i=1; i<MAXCP; i++)
	    {
	    if (playercp[i] == 999)
	        {
	        break;
	        }
	    j=playercp[i];
	    }
    if (j != 999)
        {
	    new pname[MAX_PLAYER_NAME];
	    GetPlayerName(j,pname,MAX_PLAYER_NAME);
	    format(msg,sizeof(msg),"%s is winning with %d cps to go",pname,MAXCP-1-playercheck[j]);
	    SendClientMessageToAll(0x00FF00FF,msg);
        }
	}
	return 1;
	}

	return 0;
}

public OnPlayerInfoChange(playerid)
{
	printf("OnPlayerInfoChange(%d)");
	return 1;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
	printf("OnPlayerEnterVehicle(%d, %d, %d)", playerid, vehicleid, ispassenger);
	return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
	printf("OnPlayerExitVehicle(%d, %d)", playerid, vehicleid);
	return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
	if (newstate == PLAYER_STATE_DRIVER)
	    {
	    playerdriving[playerid] = 1;
	    }
	if (newstate == PLAYER_STATE_ONFOOT)
	    {
	    playerdriving[playerid] = 0;
	    DisablePlayerCheckpoint(playerid);
	    if (playerinrace[playerid] == 1)
	        {
	        playerinrace[playerid] = 0;
	        playerposition[playerid] = 999;
	        GameTextForPlayer(playerid,"you are out of the race",3000,3);
	        }
	    }
	printf("OnPlayerStateChange(%d, %d, %d)", playerid, newstate, oldstate);
	return 1;
}

public OnPlayerEnterCheckpoint(playerid)
{
		playercheck[playerid] = playercheck[playerid] + 1;
	if (playercheck[playerid] >= 1 && playercheck[playerid] <=MAXCP-2)
	    {
	    DisablePlayerCheckpoint(playerid);
	    SetPlayerCheckpoint(playerid,checkpoints[playercheck[playerid]][0],checkpoints[playercheck[playerid]][1],checkpoints[playercheck[playerid]][2],7.0);
	    PlayerPlaySound(playerid,1053,checkpoints[playercheck[playerid]][0],checkpoints[playercheck[playerid]][1],checkpoints[playercheck[playerid]][2]);
		format(msg,sizeof(msg),"cp %d of %d",playercheck[playerid],MAXCP-1);
		GameTextForPlayer(playerid,msg,2000,6);
		if (playercp[playercheck[playerid]] == 999)
		    {
			playercp[playercheck[playerid]] = playerid;
		    }
	    }
	if (playercheck[playerid] == MAXCP-1)
	    {
	    DisablePlayerCheckpoint(playerid);
		timeend[playerid] = tickcount();
		timetotal[playerid] = timeend[playerid] - timestart[playerid];
		timeconvert(playerid);
		PlayerPlaySound(playerid,1132,checkpoints[playercheck[playerid]][0],checkpoints[playercheck[playerid]][1],checkpoints[playercheck[playerid]][2]);
		finished = finished + 1;
        playerposition[playerid] = finished;
   		new pname[MAX_PLAYER_NAME];
		GetPlayerName(playerid, pname, MAX_PLAYER_NAME);
		new pos[] = "th";
		if (playerposition[playerid] == 1 || playerposition[playerid] == 21 || playerposition[playerid] == 31 || playerposition[playerid] == 41 || playerposition[playerid] == 51 || playerposition[playerid] == 61 || playerposition[playerid] == 71 || playerposition[playerid] == 81 || playerposition[playerid] == 91)
			{
			pos = "st";
			}
		if (playerposition[playerid] == 2 || playerposition[playerid] == 22 || playerposition[playerid] == 32 || playerposition[playerid] == 42 || playerposition[playerid] == 52 || playerposition[playerid] == 62 || playerposition[playerid] == 72 || playerposition[playerid] == 82 || playerposition[playerid] == 92)
			{
			pos = "nd";
			}
		if (playerposition[playerid] == 3 || playerposition[playerid] == 23 || playerposition[playerid] == 33 || playerposition[playerid] == 43 || playerposition[playerid] == 53 || playerposition[playerid] == 63 || playerposition[playerid] == 73 || playerposition[playerid] == 83 || playerposition[playerid] == 93)
			{
			pos = "rd";
			}
		format(msg,sizeof(msg),"you finished %d%s in %dmin %d.%dsec",playerposition[playerid],pos,minutes[playerid],seconds[playerid],mseconds[playerid]);
		GameTextForPlayer(playerid,msg,3000,3);
		format(msg,sizeof(msg),"%s finished %d%s in %dmin %d.%dsec",pname,playerposition[playerid],pos,minutes[playerid],seconds[playerid],mseconds[playerid]);
		SendClientMessageToAll(0x00FF00FF,msg);
		if (fexist(RECORDFILE))
		    {
	        new dataname[50];
	        new dataminutes[10];
	        new dataseconds[10];
	        new datamseconds[10];
	   		fileh = fopen(RECORDFILE);
	   		new filedata[100];
	   		new n[] = " ";
	   		new j;
	   		new k;
			fread(fileh,filedata,100);
				for (new i=0; i<100; i++)
				    {
				    j=i;
				    if (filedata[i] == n[0])
				    {
				    break;
				    }
				    dataname[i] = filedata[i];
				    }
				k=j+1;
				for (new i=k; i<100; i++)
				    {
				    j=i;
				    if (filedata[i] == n[0])
				    {
				    break;
				    }
				    dataminutes[i-k] = filedata[i];
				    }
				k=j+1;
				for (new i=k; i<100; i++)
				    {
				    j=i;
				    if (filedata[i] == n[0])
				    {
				    break;
				    }
				    dataseconds[i-k] = filedata[i];
				    }
				k=j+1;
				for (new i=k; i<100; i++)
				    {
				    j=i;
				    if (filedata[i] == n[0])
				    {
				    break;
				    }
				    datamseconds[i-k] = filedata[i];
				    }
			fclose(fileh);
			new old = 1;
			if (minutes[playerid] < strval(dataminutes))
			    {
			    old = 0;
			    }
			else if (minutes[playerid] == strval(dataminutes) && seconds[playerid] < strval(dataseconds))
			    {
			    old = 0;
			    }
			else if (minutes[playerid] == strval(dataminutes) && seconds[playerid] == strval(dataseconds) && mseconds[playerid] < strval(datamseconds))
			    {
			    old = 0;
			    }
			if (old == 0)
			    {
	   			fileh = fopen(RECORDFILE);
				format(msg,sizeof(msg),"%s %d %d %d ",pname,minutes[playerid],seconds[playerid],mseconds[playerid]);
				fwrite(fileh,msg);
				fclose(fileh);
				format(msg,sizeof(msg),"new best time");
				SendClientMessageToAll(0x00FF00FF,msg);
			    }
			else if (old == 1)
			    {
				format(msg,sizeof(msg),"%s holds the best time of %smin and %s:%ssec ",dataname,dataminutes,dataseconds,datamseconds);
				SendClientMessageToAll(0x00FF00FF,msg);
			    }
		    }
  		else
  		    {
    		fileh = fopen(RECORDFILE);
			format(msg,sizeof(msg),"%s %d %d %d ",pname,minutes[playerid],seconds[playerid],mseconds[playerid]);
			fwrite(fileh,msg);
			fclose(fileh);
			format(msg,sizeof(msg),"new best time");
			SendClientMessageToAll(0x00FF00FF,msg);
  		    }
		playerinrace[playerid] = 0;
		if (trigg == 0)
		{
		trigg = 1;
//		GameTextForAll("Race will begin in 20 seconds",3000,3);
//		SendClientMessageToAll(0x00FF00FF,"you have 20 seconds to get a car");
//		SendClientMessageToAll(0x00FF00FF,"and type /joinrace");
		SetTimer("WaitBeforeRaceEnd",20000,0);
		}
//		raceon = 0;
//		racers = 0;
//		rcount = 0;
	    }
	printf("OnPlayerEnterCheckpoint(%d)", playerid);
	return 1;
}

public OnPlayerLeaveCheckpoint(playerid)
{
	printf("OnPlayerLeaveCheckpoint(%d)", playerid);
	return 1;
}

public WaitBeforeRace()
{
ready = 1;
return 1;
}

public WaitBeforeRaceEnd()
{
raceon = 0;
rcount = 0;
SendClientMessageToAll(0x00FF00FF,"you may now start a new race");
SendClientMessageToAll(0x00FF00FF,"by typing /joinrace");
return 1;
}

public BeginRace()
{
	if (ready == 1)
	    {
	    for (new i=0; i<100; i++)
	        {
	        if (playerinrace[i] == 1 && rcount == 0)
		        {
		        GameTextForPlayer(i,"race will begin in",1000,3);
       	 		}
	        if (playerinrace[i] == 1 && rcount == 1)
		        {
		        GameTextForPlayer(i,"3",1000,3);
       	 		}
	        if (playerinrace[i] == 1 && rcount == 2)
		        {
		        GameTextForPlayer(i,"2",1000,3);
       	 		}
	        if (playerinrace[i] == 1 && rcount == 3)
		        {
		        GameTextForPlayer(i,"1",1000,3);
       	 		}
	        if (playerinrace[i] == 1 && rcount == 4)
		        {
		        GameTextForAll("The race has started",1000,3);
		        TogglePlayerControllable(i,1);
		        timestart[i] = tickcount();
		        SetPlayerCheckpoint(i,checkpoints[0][0],checkpoints[0][1],checkpoints[0][2],7.0);
		        ready = 0;
		        trig = 0;
		        trigg = 0;
		        triggg = 0;
		        raceon = 1;
		        racers = 0;
		        finished = 0;
		        playercheck[i] = 0;
       	 		}
	        }
	        rcount = rcount + 1;
		    for (new i=0; i<MAXCP; i++)
		        {
		        playercp[i] = 999;
		        }
	    }
	return 1;
}

public IsRaceEmpty()
{
	new test = 0;
	for (new i=0; i<100; i++)
	{
	if (playerinrace[i] == 1)
	    {
	    test = 1;
	    }
	}
	if (test == 0 && triggg == 0)
	    {
	    triggg = 1;
	    raceon = 0;
	    rcount = 0;
		SendClientMessageToAll(0x00FF00FF,"you may now start a new race");
		SendClientMessageToAll(0x00FF00FF,"by typing /joinrace");
	    }
return 1;
}

// code from Dabombber
timeconvert(playerid)
{
	timetotal[playerid] = floatdiv(timetotal[playerid], 60000);
    minutes[playerid] = floatround(timetotal[playerid], floatround_tozero);
    seconds[playerid] = floatround(floatmul(timetotal[playerid] - minutes[playerid], 60), floatround_tozero);
    mseconds[playerid] = floatround(floatmul(floatmul(timetotal[playerid] - minutes[playerid], 60) - seconds[playerid], 1000), floatround_tozero);
}