Version = "0.4.2"
Author  = "^wMicha^0!"
Modname = "^0INFECTE^1D"
Homepage = "forums.warchest.com"
--[[ 					Thanks to Perlo_0ung?!

-------------------------------------------------------------------------------------
---------------------------------CONFIG START----------------------------------------
-------------------------------------------------------------------------------------

true means on
false means off

changeable values:
--]]

rules_cmd                = "!rules"			--Rules shrubbot command
RULES_FILENAME           = "rules.txt"		--Rules filename

infected_max_hp			 = 150				--Maximum healthpoints for [infected] players
infected_hp_chargetime 	 = 1.2				--Time in seconds of extra healthpoints for [infected] (except medic)
human_max_hp 		 	 = 120				--Value of the [humans] players healthpoints (execpt medic)
human_medic_max_hp 		 = 120				--Lowers the [humans] medics healthpoints to balance out the classes

heartbeatuse             = false			--Heartbeat sound if [human] has 40 or less hitpoints (must be false on infected mod)

min_level                = 9				--Minimum level needed to reset/change a map during a running match
allowtospec 			 = 9				--Minimum level needed to spectate [humans]

newscommand				 = false			--Allow players to use the !news command

mapblock                 = true				--Cycles the "mapchangevoteblock" on/off after map was voted
											--"callvote map"
											--"callvote poll random map"
											--Prevents players from calling a vote to change the map after a vote was recently called to change the map. This allows other users to fully connect to the server without the map being changed.
	
playambiencesound 		 = true				--Play global sounds from the timer_table (used for ambience)
	
userAxisRespawnTime 	 = "1"				--Command: g_userAxisRespawnTime (recommend: maximal 5)
userAlliedRespawnTime 	 = "20"				--Command: g_userAlliedRespawnTime

ref_typo                 = "7ref"			--Blocks the referee typo

fightsound 				 = "sound/rtcw/axis1_coming.wav"
preparesound			 = "sound/osp/prepare.wav"
refereesound 			 = "sound/misc/referee.wav"
heartbeatsound 			 = "sound/player/heartbeat.wav"


-------//---------------------maps which are avaible in the "callvote poll random map"----------------------------
randommaptable = {
    { mapname = "oasis" },
    { mapname = "radar" },
    { mapname = "railgun" },
    { mapname = "goldrush" },
    { mapname = "battery" },
    { mapname = "raw_te" },
    { mapname = "adlernest" },
    { mapname = "drush" },
    { mapname = "frostbite" },
    { mapname = "haunted_mansion" },
    { mapname = "necrology_b2" },
    { mapname = "reactor_final" },
    { mapname = "t_spookyb2" },
    { mapname = "school" },
    { mapname = "goldrush-ga" },
    { mapname = "stonehenge_koth" },
    { mapname = "supply" },
    { mapname = "te_escape2" },
    { mapname = "tounine_b2" },
    { mapname = "warbell" },
    { mapname = "pirates" },
    { mapname = "et_ice" },
    { mapname = "et_beach" },
    { mapname = "caen2" },
    { mapname = "falkenstein_sw2" },
    { mapname = "et_village" },
    { mapname = "et_mor2_night_final" },
    { mapname = "resurrection" },
    { mapname = "mp_communique" },
    { mapname = "coldfortzombie" },
    { mapname = "bucket_v2" },
    { mapname = "erdenberg_b3" },
    { mapname = "purefrag" },
    { mapname = "braundorf_b4" },
}

-------//---------------------remove doors on the following maps----------------------------
map_table = {
    { mapname = "eisfaust" },
    { mapname = "haemar_b1" },
    { mapname = "frosty" },
    { mapname = "frost_final" },
    { mapname = "stargate_1945" },
    { mapname = "canyon3" },
    { mapname = "wtc_heist" },
    { mapname = "1944_beach" },
    { mapname = "heli_m1" },
    { mapname = "missile_b4" },
    { mapname = "rhineland_bridge_4" },
    { mapname = "island2" },
    { mapname = "lp1_1" },
    { mapname = "school" },
    { mapname = "goldrush-ga" },
    { mapname = "tm_halloween1" },
	{ mapname = "coldfort" },
	{ mapname = "uje_the_beast" },
	{ mapname = "trooptrain" },
}
--

-------------------------------------------------------------------------------------
-------------------------------CONFIG END--------------------------------------------
-------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------
----------DO NOT CHANGE THE FOLLOWING IF YOU DO NOT KNOW WHAT YOU ARE DOING----------
-------------------------------------------------------------------------------------

-------//---------------------DO NOT CHANGE THESE VARS----------------------------
et.MAX_WEAPONS = 50
et.CS_VOTE_STRING = 7
et.CS_INTERMISSION = 12
et.CS_MULTI_INFO = 13
et.CS_MULTI_MAPWINNER = 14
et.CS_PLAYERS = 689
samplerate = 1
timer = 0
timervalue = 0.25
fightsoundstart = 0
preparesoundblock = 0
votetimernumber = 100		--time which "callvote map"/"callvote poll random map" is disabled after a reset or mapchange
news_cmd = "!news"
TEAM_INFECTED = 1
TEAM_HUMANS = 2
TEAM_SPECTATOR = 3
htimer = {}
punishteama = {}
punishteamaspam = {}
putteaminfected = {}
blockteamb = {}
killamount = {}
guid_table = {}
playertable = {}
putteaminfectedcount = {}
--math.randomseed( os.time() )
math.randomseed( et.trap_Milliseconds() + samplerate )
--

-------//---------------------Infected Damage----------------------------
--[[
	MOD_UNKNOWN,								// 0
	MOD_MACHINEGUN,								// 1
	MOD_BROWNING,								// 2
	MOD_MG42,									// 3
	MOD_GRENADE,								// 4
	MOD_ROCKET,									// 5

	MOD_KNIFE,									// 6
	MOD_LUGER,									// 7
	MOD_COLT,									// 8
	MOD_MP40,									// 9
	MOD_THOMPSON,								// 10
	MOD_STEN,									// 11
	MOD_GARAND,									// 12
	MOD_SNOOPERSCOPE,							// 13
	MOD_SILENCER,								// 14
	MOD_FG42,									// 15
	MOD_FG42SCOPE,								// 16
	MOD_PANZERFAUST,							// 17
	MOD_GRENADE_LAUNCHER,						// 18
	MOD_FLAMETHROWER,							// 19
	MOD_GRENADE_PINEAPPLE,						// 20
	MOD_CROSS,									// 21

	MOD_MAPMORTAR,								// 22
	MOD_MAPMORTAR_SPLASH,						// 23

	MOD_KICKED,									// 24
	MOD_GRABBER,								// 25

	MOD_DYNAMITE,								// 26
	MOD_AIRSTRIKE,								// 27
	MOD_SYRINGE,								// 28
	MOD_AMMO,									// 29
	MOD_ARTY,									// 30

	MOD_WATER,									// 31
	MOD_SLIME,									// 32
	MOD_LAVA,									// 33
	MOD_CRUSH,									// 34
	MOD_TELEFRAG,								// 35
	MOD_FALLING,								// 36
	MOD_SUICIDE,								// 37
	MOD_TARGET_LASER,							// 38
	MOD_TRIGGER_HURT,							// 39
	MOD_EXPLOSIVE,								// 40

	MOD_CARBINE,								// 41
	MOD_KAR98,									// 42
	MOD_GPG40,									// 43
	MOD_M7,										// 44
	MOD_LANDMINE,								// 45
	MOD_SATCHEL,								// 46
	MOD_TRIPMINE,								// 47
	MOD_SMOKEBOMB,								// 48
	MOD_MOBILE_MG42,							// 49
	MOD_SILENCED_COLT,							// 50
	MOD_GARAND_SCOPE,							// 51

	MOD_CRUSH_CONSTRUCTION,						// 52
	MOD_CRUSH_CONSTRUCTIONDEATH,				// 53
	MOD_CRUSH_CONSTRUCTIONDEATH_NOATTACKER,		// 54

	MOD_K43,									// 55
	MOD_K43_SCOPE,								// 56

	MOD_MORTAR,									// 57

	MOD_AKIMBO_COLT,							// 58
	MOD_AKIMBO_LUGER,							// 59
	MOD_AKIMBO_SILENCEDCOLT,					// 60
	MOD_AKIMBO_SILENCEDLUGER,					// 61

	MOD_SMOKEGRENADE,							// 62

	MOD_SWAP_PLACES,							// 63

	MOD_SWITCHTEAM,								// 64

	MOD_GOOMBA,									// 65
	MOD_POISON,									// 66
	MOD_FEAR,									// 67
	MOD_THROWN_KNIFE,							// 68

	MOD_REFLECTED_FF,							// 69
--]]

-------//---------------------Country table----------------------------
-------//------------------made by phisherman--------------------------
countries = {
[0]=    "Unknown",
[1]=    "Asia/Pacific Region", 
[2]=    "Europe", 
[3]=    "Andorra", 
[4]=    "United Arab Emirates",
[5]=    "Afghanistan", 
[6]=    "Antigua and Barbuda", 
[7]=    "Anguilla", 
[8]=    "Albania", 
[9]=    "Armenia",
[10]=    "Netherlands Antilles", 
[11]=    "Angola", 
[12]=    "Antarctica", 
[13]=    "Argentina", 
[14]=    "American Samoa",
[15]=    "Austria", 
[16]=    "Australia", 
[17]=    "Aruba", 
[18]=    "Azerbaijan", 
[19]=    "Bosnia and Herzegovina",
[20]=    "Barbados", 
[21]=    "Bangladesh", 
[22]=    "Belgium", 
[23]=    "Burkina Faso", 
[24]=    "Bulgaria", 
[25]=    "Bahrain",
[26]=    "Burundi", 
[27]=    "Benin", 
[28]=    "Bermuda", 
[29]=    "Brunei Darussalam", 
[30]=    "Bolivia", 
[31]=    "Brazil",
[32]=    "Bahamas", 
[33]=    "Bhutan", 
[34]=    "Bouvet Island", 
[35]=    "Botswana", 
[36]=    "Belarus", 
[37]=    "Belize",
[38]=    "Canada", 
[39]=    "Cocos (Keeling) Islands", 
[40]=    "Congo-Kinshasa",
[41]=    "Central African Republic", 
[42]=    "Congo-Brazzaville", 
[43]=    "Switzerland", 
[44]=    "Cote D'Ivoire", 
[45]=    "Cook Islands",
[46]=    "Chile", 
[47]=    "Cameroon", 
[48]=    "China", 
[49]=    "Colombia", 
[50]=    "Costa Rica", 
[51]=    "Cuba", 
[52]=    "Cape Verde",
[53]=    "Christmas Island", 
[54]=    "Cyprus", 
[55]=    "Czech Republic", 
[56]=    "Germany", 
[57]=    "Djibouti",
[58]=    "Denmark", 
[59]=    "Dominica", 
[60]=    "Dominican Republic", 
[61]=    "Algeria", 
[62]=    "Ecuador", 
[63]=    "Estonia",
[64]=    "Egypt", 
[65]=    "Western Sahara", 
[66]=    "Eritrea", 
[67]=    "Spain", 
[68]=    "Ethiopia", 
[69]=    "Finland", 
[70]=    "Fiji",
[71]=    "Falkland Islands (Malvinas)", 
[72]=    "Micronesia", 
[73]=    "Faroe Islands",
[74]=    "France", 
[75]=    "France, Metropolitan", 
[76]=    "Gabon", 
[77]=    "United Kingdom",
[78]=    "Grenada", 
[79]=    "Georgia", 
[80]=    "French Guiana", 
[81]=    "Ghana", 
[82]=    "Gibraltar", 
[83]=    "Greenland",
[84]=    "Gambia", 
[85]=    "Guinea", 
[86]=    "Guadeloupe", 
[87]=    "Equatorial Guinea", 
[88]=    "Greece", 
[89]=    "South Georgia and the South Sandwich Islands",
[90]=    "Guatemala", 
[91]=    "Guam", 
[92]=    "Guinea-Bissau",
[93]=    "Guyana", 
[94]=    "Hong Kong", 
[95]=    "Heard Island and McDonald Islands", 
[96]=    "Honduras",
[97]=    "Croatia", 
[98]=    "Haiti", 
[99]=    "Hungary", 
[100]=    "Indonesia", 
[101]=    "Ireland", 
[102]=    "Israel", 
[103]=    "India",
[104]=    "British Indian Ocean Territory", 
[105]=    "Iraq", 
[106]=    "Iran",
[107]=    "Iceland", 
[108]=    "Italy", 
[109]=    "Jamaica", 
[110]=    "Jordan", 
[111]=    "Japan", 
[112]=    "Kenya", 
[113]=    "Kyrgyzstan",
[114]=    "Cambodia", 
[115]=    "Kiribati", 
[116]=    "Comoros", 
[117]=    "Saint Kitts and Nevis", 
[118]=    "North Korea",
[119]=    "South Korea", 
[120]=    "Kuwait", 
[121]=    "Cayman Islands",
[122]=    "Kazakhstan", 
[123]=    "Laos", 
[124]=    "Lebanon", 
[125]=    "Saint Lucia",
[126]=    "Liechtenstein", 
[127]=    "Sri Lanka", 
[128]=    "Liberia", 
[129]=    "Lesotho", 
[130]=    "Lithuania", 
[131]=    "Luxembourg",
[132]=    "Latvia", 
[133]=    "Libya", 
[134]=    "Morocco", 
[135]=    "Monaco", 
[136]=    "Moldova",
[137]=    "Madagascar", 
[138]=    "Marshall Islands", 
[139]=    "Macedonia",
[140]=    "Mali", 
[141]=    "Burma", 
[142]=    "Mongolia", 
[143]=    "Macau", 
[144]=    "Northern Mariana Islands",
[145]=    "Martinique", 
[146]=    "Mauritania", 
[147]=    "Montserrat", 
[148]=    "Malta", 
[149]=    "Mauritius", 
[150]=    "Maldives",
[151]=    "Malawi", 
[152]=    "Mexico", 
[153]=    "Malaysia", 
[154]=    "Mozambique", 
[155]=    "Namibia", 
[156]=    "New Caledonia",
[157]=    "Niger", 
[158]=    "Norfolk Island", 
[159]=    "Nigeria", 
[160]=    "Nicaragua", 
[161]=    "Netherlands", 
[162]=    "Norway",
[163]=    "Nepal", 
[164]=    "Nauru", 
[165]=    "Niue", 
[166]=    "New Zealand", 
[167]=    "Oman", 
[168]=    "Panama", 
[169]=    "Peru", 
[170]=    "French Polynesia",
[171]=    "Papua New Guinea", 
[172]=    "Philippines", 
[173]=    "Pakistan", 
[174]=    "Poland", 
[175]=    "Saint Pierre and Miquelon",
[176]=    "Pitcairn Islands", 
[177]=    "Puerto Rico", 
[178]=    "Palestinian Territory",
[179]=    "Portugal", 
[180]=    "Palau", 
[181]=    "Paraguay", 
[182]=    "Qatar", 
[183]=    "Reunion", 
[184]=    "Romania",
[185]=    "Russian Federation", 
[186]=    "Rwanda", 
[187]=    "Saudi Arabia", 
[188]=    "Solomon Islands",
[189]=    "Seychelles", 
[190]=    "Sudan", 
[191]=    "Sweden", 
[192]=    "Singapore", 
[193]=    "Saint Helena", 
[194]=    "Slovenia",
[195]=    "Svalbard and Jan Mayen", 
[196]=    "Slovakia", 
[197]=    "Sierra Leone", 
[198]=    "San Marino", 
[199]=    "Senegal",
[200]=    "Somalia", 
[201]=    "Suriname", 
[202]=    "Sao Tome and Principe", 
[203]=    "El Salvador", 
[204]=    "Syria",
[205]=    "Swaziland", 
[206]=    "Turks and Caicos Islands", 
[207]=    "Chad", 
[208]=    "French Southern Territories",
[209]=    "Togo", 
[210]=    "Thailand", 
[211]=    "Tajikistan", 
[212]=    "Tokelau", 
[213]=    "Turkmenistan",
[214]=    "Tunisia", 
[215]=    "Tonga", 
[216]=    "Timor-Leste", 
[217]=    "Turkey", 
[218]=    "Trinidad and Tobago", 
[219]=    "Tuvalu",
[220]=    "Taiwan", 
[221]=    "Tanzania", 
[222]=    "Ukraine",
[223]=    "Uganda", 
[224]=    "United States Minor Outlying Islands", 
[225]=    "United States", 
[226]=    "Uruguay",
[227]=    "Uzbekistan", 
[228]=    "Holy See (Vatican City State)", 
[229]=    "Saint Vincent and the Grenadines",
[230]=    "Venezuela", 
[231]=    "Virgin Islands, British", 
[232]=    "Virgin Islands, U.S.",
[233]=    "Vietnam", 
[234]=    "Vanuatu", 
[235]=    "Wallis and Futuna", 
[236]=    "Samoa", 
[237]=    "Yemen", 
[238]=    "Mayotte",
[239]=    "Serbia", 
[240]=    "South Africa", 
[241]=    "Zambia", 
[242]=    "Montenegro", 
[243]=    "Zimbabwe",
[244]=    "Anonymous Proxy",
[245]=    "Satellite Provider",
[246]=    "Other",
[247]=    "Aland Islands",
[248]=    "Guernsey",
[249]=    "Isle of Man",
[250]=    "Jersey",
[251]=    "Saint Barthelemy",
[252]=    "Saint Martin",
[255]=    "Localhost",
}

-------//---------------------World sounds----------------------------
timer_table = {
    { timer= { 1000, 9400}, 			sound="sound/zombies/Sound0.wav", },        
    { timer= { 2200, },				sound="sound/zombies/Sound1.wav", },  
    { timer= { 2800, },				sound="sound/zombies/Sound2.wav", },  
    { timer= { 3600, }, 			sound="sound/zombies/Sound3.wav", },  
    { timer= { 4800, }, 			sound="sound/zombies/Sound4.wav", },  
    { timer= { 5600, }, 			sound="sound/zombies/Sound5.wav", }, 
    { timer= { 6400, }, 			sound="sound/rtcw/ambzomb1.wav", },     
    { timer= { 6840, 11040 }, 		sound="sound/rtcw/axis2_done_that.wav", },
    { timer= { 7035, }, 			sound="sound/rtcw/axis3_nothing_human.wav", }, 
    { timer= { 8000, }, 			sound="sound/rtcw/firstsight2.wav", }, 
    { timer= { 8800, }, 			sound="sound/rtcw/zombieeating.wav", },
    { timer= { 10000, }, 			sound="sound/rtcw/hein_death1.wav", }, 
    { timer= { 11140, }, 			sound="sound/rtcw/b_dont_know.wav", }, 
    { timer= { 13000, }, 			sound="sound/rtcw/war_giant2.wav", },
    { timer= { 13800, }, 			sound="sound/rtcw/cn1_slaughterhouse.wav", },
}

-------//---------------------Infected weapons----------------------------
--note: Some of these have no comments because they're not weapons.
weapons = {
    true,    --WP_KNIFE - THROWNKNIFE, // 1
    false,    --WP_LUGER,                // 2
    false,    --WP_MP40,                // 3
    false,    --WP_GRENADE_LAUNCHER,        // 4
    false,    --WP_PANZERFAUST,            // 5
    false,    --WP_FLAMETHROWER,        // 6
    false,    --WP_COLT,                // 7    // equivalent american weapon to german luger
    false,    --WP_THOMPSON,            // 8    // equivalent american weapon to german mp40
    false,    --WP_GRENADE_PINEAPPLE,    /    // 9
    false,    --WP_STEN,                // 10    // silenced sten sub-machinegun
    false,    --WP_MEDIC_SYRINGE,        // 11    // JPW NERVE -- broken out from CLASS_SPECIAL per Id request
    false,    --WP_AMMO,                // 12    // JPW NERVE likewise
    true,    --WP_ARTY,                // 13
    false,    --WP_SILENCER,            // 14    // used to be sp5
    false,    --WP_DYNAMITE,            // 15
    true,    --WP_SMOKETRAIL,        // 16
    false,    --WP_MAPMORTAR,        // 17
    false,    --VERYBIGEXPLOSION,        // 18
    false,    --WP_MEDKIT,            // 19
    false,    --WP_BINOCULARS,            // 20
    true,    --WP_PLIERS,                // 21
    false,    --WP_SMOKE_MARKER,        // 22
    false,    --WP_KAR98,                // 23    // WolfXP weapons
    false,    --WP_CARBINE,            // 24
    false,    --WP_GARAND,            // 25
    false,    --WP_LANDMINE,            // 26
    false,    --WP_SATCHEL,            // 27
    false,    --WP_SATCHEL_DET,            // 28
    false,    --WP_TRIPMINE            // 29
    true,    --WP_SMOKE_BOMB,            // 30
    false,    --WP_MOBILE_MG42,            // 31
    false,    --WP_K43,                // 32
    false,    --WP_FG42,                // 33
    false,    --WP_DUMMY_MG42,        // 34
    false,    --WP_MORTAR,            // 35
    false,    --WP_LOCKPICK,        // 36
    false,    --WP_AKIMBO_COLT,            // 37
    false,    --WP_AKIMBO_LUGER,        // 38
    false,    --WP_GPG40,            // 39
    false,    --WP_M7,                // 40
    false,    --WP_SILENCED_COLT,        // 41
    false,    --WP_GARAND_SCOPE,        // 42
    false,    --WP_K43_SCOPE,            // 43
    false,    --WP_FG42SCOPE,            // 44
    false,    --WP_MORTAR_SET,            // 45
    false,    --WP_MEDIC_ADRENALINE,        // 46
    false,    --WP_AKIMBO_SILENCEDCOLT,    // 47
    false,    --WP_AKIMBO_SILENCEDLUGER,    // 48
    false,    --WP_MOBILE_MG42_SET,    // 49
}
     
-------//---------------------Humans weapons----------------------------	 
weaponsa = {
    false,    --WP_KNIFE - THROWNKNIFE, // 1
    true,    --WP_LUGER,                // 2
    true,    --WP_MP40,                // 3
    true,    --WP_GRENADE_LAUNCHER,        // 4
    true,    --WP_PANZERFAUST,            // 5
    true,    --WP_FLAMETHROWER,        // 6
    true,    --WP_COLT,                // 7    // equivalent american weapon to german luger
    true,    --WP_THOMPSON,            // 8    // equivalent american weapon to german mp40
    false,    --WP_GRENADE_PINEAPPLE,    /    // 9
    true,    --WP_STEN,                // 10    // silenced sten sub-machinegun
    false,    --WP_MEDIC_SYRINGE,        // 11    // JPW NERVE -- broken out from CLASS_SPECIAL per Id request
    false,    --WP_AMMO,                // 12    // JPW NERVE likewise
    true,    --WP_ARTY,                // 13
    true,    --WP_SILENCER,            // 14    // used to be sp5
    false,    --WP_DYNAMITE,            // 15
    true,    --WP_SMOKETRAIL,        // 16
    true,    --WP_MAPMORTAR,        // 17
    false,    --VERYBIGEXPLOSION,        // 18
    true,    --WP_MEDKIT,            // 19
    true,    --WP_BINOCULARS,            // 20
    true,    --WP_PLIERS,                // 21
    true,    --WP_SMOKE_MARKER,        // 22
    true,    --WP_KAR98,                // 23    // WolfXP weapons
    true,    --WP_CARBINE,            // 24
    true,    --WP_GARAND,            // 25
    true,    --WP_LANDMINE,            // 26
    false,    --WP_SATCHEL,            // 27
    false,    --WP_SATCHEL_DET,            // 28
    true,    --WP_TRIPMINE            // 29
    true,    --WP_SMOKE_BOMB,            // 30
    true,    --WP_MOBILE_MG42,            // 31
    true,    --WP_K43,                // 32
    true,    --WP_FG42,                // 33
    true,    --WP_DUMMY_MG42,        // 34
    true,    --WP_MORTAR,            // 35
    true,    --WP_LOCKPICK,        // 36
    true,    --WP_AKIMBO_COLT,            // 37
    true,    --WP_AKIMBO_LUGER,        // 38
    true,    --WP_GPG40,            // 39
    true,    --WP_M7,                // 40
    true,    --WP_SILENCED_COLT,        // 41
    true,    --WP_GARAND_SCOPE,        // 42
    true,    --WP_K43_SCOPE,            // 43
    true,    --WP_FG42SCOPE,            // 44
    false,    --WP_MORTAR_SET,            // 45
    false,    --WP_MEDIC_ADRENALINE,        // 46
    true,    --WP_AKIMBO_SILENCEDCOLT,    // 47
    true,    --WP_AKIMBO_SILENCEDLUGER,    // 48
    true,    --WP_MOBILE_MG42_SET,    // 49
}

-------//---------------------Start of functions----------------------------
function et_InitGame(levelTime,randomSeed,restart)
    gamestate = tonumber(et.trap_Cvar_Get( "gamestate" ))
    maxclients = tonumber( et.trap_Cvar_Get( "sv_maxClients" ) )
    mapname = et.trap_Cvar_Get( "mapname" )
    et.G_Print("^z["..Modname.."^z] Version:"..Version.." Loaded\n")
    et.RegisterModname(Modname .. " ^wby Micha! " .. Version)
    et.trap_SendServerCommand(-1, "chat \""..Modname.." ^0[^7LUA^0] ^0powered by "..Author.." \"" )
    et.trap_SendConsoleCommand( et.EXEC_APPEND, "ref speclock\n" )
    if et.trap_Cvar_Get( "mod_url" ) ~= "forums.warchest.com" then
        et.trap_Cvar_Set( "mod_url", ""..Homepage.."" )
    end
    if et.trap_Cvar_Get( "gamename" ) ~= "infected" then
        et.trap_Cvar_Set( "gamename", "infected" )
    end
	et.trap_Cvar_Set( "g_userAxisRespawnTime", userAxisRespawnTime )
	et.trap_Cvar_Set( "g_userAlliedRespawnTime", userAlliedRespawnTime )
    removeentity = 100
    spamblock = 0
    playercount = 0
    counthumans = 0
    countinfected = 0
    allhumansdeath = 0
	votetimer = 1
	
    if mapname == "adlernest_hns" or mapname == "warbell" or mapname == "school" or mapname == "haunted_mansion" or mapname == "nightmare" then
        et.trap_Cvar_Set( "g_usertimelimit", "10" )
    elseif mapname == "purefrag" then
        et.trap_Cvar_Set( "g_usertimelimit", "5" )
    else
        et.trap_Cvar_Set( "g_usertimelimit", "8" )
    end
    
    if gamestate == 0 then    
        timer = 1
        fightsoundstart = 1
    end

    for cno = 0, (maxclients - 1) do
        if gamestate == 2 and checkteam(cno) == TEAM_INFECTED then
            et.trap_SendConsoleCommand( et.EXEC_APPEND, "put "..cno.." b\n" )
        end
        htimer[cno] = 0
        putteaminfected[cno] = 0
        blockteamb[cno] = false
        punishteama[cno] = 0
        punishteamaspam[cno] = 0
        killamount[cno] = 0
        putteaminfectedcount[cno] = 0
    end
end

-- called every server frame
function et_RunFrame( levelTime ) 
    gamestate = tonumber(et.trap_Cvar_Get( "gamestate" ))
	if math.mod(levelTime, samplerate) ~= 0 then return end

	-------//--------------------prepare sound----------------------------
    if gamestate == 1 and preparesoundblock == 0 then
        et.G_globalSound ( preparesound )
        preparesoundblock = 1
    end
	-------//--------------------infected counter----------------------------
    if countinfected > 0 then
        infectedblock = 1
    else
        infectedblock = 0
    end
	-------//--------------------remove map objects----------------------------
    for i = 64, 1021 do
        if et.gentity_get(i, "classname") == "misc_mg42" or et.gentity_get(i, "classname") == "team_CTF_redflag" or et.gentity_get(i, "classname") == "team_CTF_blueflag" then
            et.trap_UnlinkEntity( i )
        end
    end
    if removeentity > 0 then
        removeentity = removeentity - 1
        for i = 64, 1021 do
            if mapname == "adlernest_hns" then
                et.trap_UnlinkEntity( 302 ) 
                et.trap_UnlinkEntity( 303 ) 
                et.trap_UnlinkEntity( 304 )
                et.trap_UnlinkEntity( 305 )
                et.trap_UnlinkEntity( 306 )
                et.trap_UnlinkEntity( 307 )
                if et.gentity_get(i, "model") == "*80" or et.gentity_get(i, "model") == "*81" or et.gentity_get(i, "model") == "*82" or et.gentity_get(i, "model") == "*83" or et.gentity_get(i, "model") == "*84" or et.gentity_get(i, "model") == "*85" then
                    et.trap_UnlinkEntity( i )
                end
            end
            if et.gentity_get(i, "classname") == "trigger_ammo" or et.gentity_get(i, "classname") == "trigger_heal" then  
                et.trap_UnlinkEntity( i ) 
            end
            if et.gentity_get(i, "classname") == "misc_mg42" then
                et.trap_UnlinkEntity( i )
            end
            table.foreach(map_table, function(k,v)
                if mapname == v.mapname then
                    if et.gentity_get(i, "classname") == "func_door_rotating" then
                        et.trap_UnlinkEntity( i )
                    end
                end
            end)
            if mapname == "adlernest" then
                if et.gentity_get(i, "origin") == "733 -2971 -120" then
                    et.trap_UnlinkEntity( i )
                end
            elseif mapname == "reactor_final" then
                if et.gentity_get(i, "classname") == "func_door" then
                    et.trap_UnlinkEntity( i )
                end
            elseif mapname == "2hide_cal_r1" then
                if et.gentity_get(i, "origin") == "-3797 -768 352" or et.gentity_get(i, "model") == "*82" then
                    et.trap_UnlinkEntity( i )
                end
            elseif mapname == "stargate_1945" or mapname == "fueldump" or mapname == "nomercy" then
                if et.gentity_get(i, "classname") == "trigger_hurt" then
                    et.trap_UnlinkEntity( i )
                end
            elseif mapname == "uje_vulcano_escape" then
                if et.gentity_get(i, "classname") == "target_push" or et.gentity_get(i, "origin") == "-4146.49 -1895.74 679.77"  or et.gentity_get(i, "origin") == "-3890.49 -279.74 543.77"  or et.gentity_get(i, "origin") == "-2424.11 -2265.48 311.77"  or et.gentity_get(i, "origin") == "-1424.26 -2130.49 63.77" or et.gentity_get(i, "classname") == "info_notnull" then
                    et.trap_UnlinkEntity( i )
                end
            elseif mapname == "bucket_v2" then
                if et.gentity_get(i, "classname") == "target_kill" or et.gentity_get(i, "classname") == "trigger_hurt" or et.gentity_get(i, "target") == "axis_killer" or et.gentity_get(i, "target") == "allies_killer" then
                    et.trap_UnlinkEntity( i )
                end
			elseif mapname == "odenthal_b2" then
				if et.gentity_get(i, "targetname") == "sideentrance" then --remove sideentrance odenthal_b2 @ e8
					et.trap_UnlinkEntity( i ) 
				end
			end
        end
    end
	
    for cno = 0, (maxclients - 1) do
		-------//--------------------allow to spectate----------------------------
        if checkteam(cno) == TEAM_SPECTATOR and et.G_shrubbot_level(cno) < allowtospec then
            et.gentity_set(cno,"sess.spec_invite",1)
        end
        if checkteam(cno) == TEAM_SPECTATOR and et.G_shrubbot_level(cno) >= allowtospec then
            et.gentity_set(cno,"sess.spec_invite",3)
        end
		-------//--------------------anti switch after infected----------------------------
        if gamestate == 0 and checkteam(cno) == TEAM_INFECTED then
            blockteamb[cno] = true
        end
		-------//--------------------ammo----------------------------
        for k=1, (et.MAX_WEAPONS - 1), 1 do
            if not weapons[k] then
                if checkteam(cno) == TEAM_INFECTED then
                    et.gentity_set(cno, "ps.ammoclip", k, 0)
                    et.gentity_set(cno, "ps.ammo", k, 0)
                    if et.trap_Cvar_Get( "g_infectedWeaps" ) == "" or et.trap_Cvar_Get( "g_infectedWeaps" ) == nil then --in case server don't use infected mod
                        et.gentity_set(cno, "ps.ammo", 13, 0) --arty
                        et.gentity_set(cno, "ps.ammoclip", 13, 0) --arty
                    end
                    --et.gentity_set(j, "ps.powerups", 12, et.trap_Milliseconds() + samplerate + 50) --stamina
                end
			end
			-------//--------------------empty humans ammoclip----------------------------
            if not weaponsa[k] then
                if checkteam(cno) == TEAM_HUMANS then
                    et.gentity_set(cno, "ps.ammoclip", k, 0)
                end
            end
        end
		-------//--------------------heartbeat----------------------------
		local hp = tonumber(et.gentity_get(cno,"health"))
		if htimer[cno] >= 1 then
			htimer[cno] = htimer[cno] + 1
		end
		if hp <= 40 and htimer[cno] == 0 and heartbeatuse then
			htimer[cno] = 1
		end
		if hp >= 41 and htimer[cno] >= 1 then
			htimer[cno] = 0
		end
		if htimer[cno] == 40 and hp > 0 then
			et.G_ClientSound( cno, et.G_SoundIndex(heartbeatsound) )
			htimer[cno] = 1
		end
	end
	-------//--------------------end match----------------------------
    if gamestate == 0 and allhumansdeath == 1 and spamblock == 0 then
        et.trap_SendConsoleCommand( et.EXEC_NOW, "timelimit 0.01" )
        spamblock = 1
    end
    if gamestate == 3 and global_first_intermission == nil then
    global_first_intermission = 1
        
        if allhumansdeath == 0 then -- humans win
            local cs = et.trap_GetConfigstring(et.CS_MULTI_MAPWINNER)
            cs = et.Info_SetValueForKey( cs, "winner", "1" )
            et.trap_SetConfigstring(et.CS_MULTI_MAPWINNER, cs)
            et.trap_SendServerCommand(-1, string.format('%s \"%s"\n',"chat","^1Infected Mod: ^4HUMANS ^3WIN!" ))
        elseif allhumansdeath == 1 then -- infected win
            local cs = et.trap_GetConfigstring(et.CS_MULTI_MAPWINNER)
            cs = et.Info_SetValueForKey( cs, "winner", "0" )
            et.trap_SetConfigstring(et.CS_MULTI_MAPWINNER, cs)
            et.trap_SendServerCommand(-1, string.format('%s \"%s"\n',"chat","^1Infected Mod: ^1INFECTED ^3WIN!" ))
        end
    end
    if math.mod(levelTime, 200) ~= 0 then return end
    ---------------------------count players---------------------------
        counthumans = 0
        countinfected = 0
    for i = 0, maxclients - 1 do
        if checkteam(i) == TEAM_INFECTED then
            countinfected = countinfected + 1
        elseif checkteam(i) == TEAM_HUMANS then    
            counthumans = counthumans + 1
        end
            
        if putteaminfected[i] == 1 then
            putteaminfectedcount[i] = putteaminfectedcount[i] + 1
            if putteaminfectedcount[i] >= 3 then
                et.trap_SendServerCommand(i, "cpm \"^0You have been infected^1!!! \"" )
                et.trap_SendServerCommand(i, "bp \"^0You have been infected^1!!! \"" )
                et.trap_SendConsoleCommand(et.EXEC_APPEND, "put "..i.." r\n" )
                putteaminfectedcount[i] = 0
            end
        end
        if checkteam(i) == TEAM_INFECTED then
            putteaminfected[i] = 0
        end

    end

	for cno = 0, (maxclients - 1) do
		-------//--------------------timer----------------------------
		if timer > 0 then
			timer = timer + timervalue
		end
		if timer == 11000 then
			et.trap_SendServerCommand(cno, "cpm \"^0Enjoy the ^1SURVIVAL^7 "..playerName(cno).."^1!!! \"" )
        end
		if timer > 15000 then
			timer = timervalue
		end
		-------//--------------------ambience sound----------------------------
		for _, line in ipairs(timer_table) do
			for _, time in ipairs(line.timer) do
				if timer == time and gamestate ~= 3 and playambiencesound then
					local sound = line.sound
					et.G_globalSound( sound )
				end
			end
		end
		-------//--------------------fightsound----------------------------
        if fightsoundstart >= 1 then
            fightsoundstart = fightsoundstart + 1
        end
        if fightsoundstart == 200 then
            et.G_globalSound ( fightsound )
            fightsoundstart = 0
        end
	end
	-------//--------------------votetimer----------------------------
	if votetimer > 0 then
		votetimer = votetimer + 1
	end
	if votetimer == votetimernumber then
		votetimer = 0 
	end
    -------//--------------------infected charge----------------------------
    if math.mod(levelTime, (infected_hp_chargetime*1000)) ~= 0 then return end --every x:1000 second
    for cno = 0, (maxclients - 1) do
		local hp = (tonumber(et.gentity_get(cno,"health")))
		local ghp = (tonumber(et.gentity_get(cno,"health")) + 3 )
        if checkteam(cno) == TEAM_INFECTED then
            if hp < 1 then 
                return
            elseif checkclass(cno) ~= 1 and hp < infected_max_hp then
                et.gentity_set(cno, "health", ghp)
            end
        end
	end
end

function et_ClientConnect( cno, firstTime, isBot )
	playercount = playercount + 1
	
    if isBot == 1 then
        et.trap_SendConsoleCommand( et.EXEC_APPEND, "put "..cno.." b\n" )
    end

    if isBot ~= 1 and firstTime == 1 then
        et.trap_SendServerCommand(cno, "cpm \"^1INFECTED: ^0kill the Humans ^w/// ^1HUMANS: ^0survive\"" )
        local userinfo = et.trap_GetUserinfo(cno)
        local protocol = tostring(et.Info_ValueForKey(userinfo, "protocol"))
        
        if protocol == nil then
            clientversion = "*unknown*"
        elseif protocol == "" then
            clientversion = "missing protocol"
        elseif protocol == "82" then
            clientversion = "2.55"
        elseif protocol == "83" then
            clientversion = "2.56"
        elseif protocol == "84" then
            clientversion = "2.6b"
        end
        
        local message = "^7"..playerName(cno).." ^7connected from ^3<country> ^7and clientv. ^3"..clientversion.."^7."
        
        local msg = string.gsub(message, ""..playerName(cno).."", et.Info_ValueForKey( et.trap_GetUserinfo( cno ), "name" ))

        local countryCode = et.gentity_get( cno, "sess.uci" )
        local countryName = countries[countryCode]

        if countryName then
            msg = string.gsub(msg, "<country>", countryName)
        else
            msg = string.gsub(msg, "<country>", countryCode)
        end
        et.trap_SendConsoleCommand(et.EXEC_APPEND,"qsay " .. msg)
    end
    dummy = getguid(cno)
    table.foreach(guid_table, function(k)
        if dummy == guid_table[k] then
            return "Connecting 2 times with same guid is not allowed"
        end
    end) 
    table.insert (guid_table , dummy)
end

function et_ClientDisconnect(cno)
	playercount = playercount - 1
		
    table.foreach(guid_table, function(k)
        if dummy == guid_table[k] then
        table.remove(guid_table , k)
        end
    end)
end
    
function et_ClientBegin( cno )
	-------//--------------------welcome message----------------------------
    et.trap_SendServerCommand(cno, "cp \"^0Enjoy the ^1SURVIVAL^7 "..playerName(cno).."^1!!!\"")
    et.trap_SendServerCommand(cno, "cpm \""..Modname.." ^0[^7LUA^0] ^zv^w"..Version.." \n\"" )
    et.trap_SendServerCommand(cno, "cpm \"^1INFECTED: ^0kill the Humans ^w/// ^1HUMANS: ^0survive\"" )
	-------//--------------------allow to spectate----------------------------
    if checkteam(cno) == TEAM_SPECTATOR and et.G_shrubbot_level(cno) < allowtospec then
        et.gentity_set(cno,"sess.spec_invite",1)
    end
    if checkteam(cno) == TEAM_SPECTATOR and et.G_shrubbot_level(cno) >= allowtospec then
        et.gentity_set(cno,"sess.spec_invite",3)
    end
end

function et_ClientCommand(client, command)
    local cmd = string.lower(command)
    local arg0 = string.lower(et.trap_Argv(0))
    local arg1 = et.trap_Argv(1)
    local argv1 = string.lower(et.trap_Argv(1))
    local argv2 = string.lower(et.trap_Argv(2))
    local argv3 = string.lower(et.trap_Argv(3))
    local argv4 = string.lower(et.trap_Argv(4))
    local argv5 = string.lower(et.trap_Argv(5))
    local hpkill = tonumber(et.gentity_get(client,"health"))
    local gamestate = tonumber(et.trap_Cvar_Get( "gamestate" ))
    local isref = et.Info_ValueForKey( et.trap_GetConfigstring( et.CS_PLAYERS + client ), "ref" )

	-------//--------------------Says----------------------------
    if cmd == "m" or cmd == "pm" or cmd == "msg" or cmd == "say" or cmd == "say_team" or cmd == "say_buddy" or cmd == "say_teamnl" or cmd == "callvote" then
        if checkmuted(client) ~= 1 then
            if checkmuted(client) == 1 then -- client is muted
                return 1 -- abort PM
            end
        end
    end
	
	-------//--------------------Callvote----------------------------
    if cmd == "callvote" then
        if argv1 == "startmatch" and countinfected < 1 and gamestate == 2 then
            et.trap_SendServerCommand( client, "cpm \"^wSorry^z, ^wyou can't call this vote -> ^3no player ^win infected team\n\"" )
            return 1
        end
        if argv1 == "poll" then
            if argv2 == "infect" then
                if argv3 == "a" then
                    if argv4 == "random" then
                        if argv5 == "player" then
                            if countinfected > 0 and gamestate ~= 0 then
                                et.trap_SendServerCommand( client, "cpm \"^wSorry^z, ^wyou can't call this vote -> ^3a player ^wis already in infected team\n\"" )
                                return 1
                            end
                        end
                    end
                end
            end
            if argv2 == "infect" then
                if argv3 == "a" then
                    if argv4 == "random" then
                        if argv5 == "player" then
                            if gamestate == 0 then
                                et.trap_SendServerCommand( client, "cpm \"^wSurvival is already running\n\"" )
                                return 1
                            end
                        end
                    end
                end
            end
        end
        if argv1 == "poll" then
            if argv2 == "putteam" then
                if argv3 == "infected" then
                    if argv4 == "spectator" then
                        if gamestate ~= 2 then
                            et.trap_SendServerCommand( client, "cpm \"^wSurvival is already running\n\"" )
                            return 1
                        elseif countinfected == 0 then
                            et.trap_SendServerCommand( client, "cpm \"^wSorry^z, ^wyou can't call this vote -> ^3no player ^win infected team\n\"" )
                            return 1
                        end
                    end
                end
            end
        end
        if argv1 == "poll" then
            if argv2 == "random" then
                if argv3 == "map" then
                    if gamestate == 2 and votetimer > 0 and mapblock then
						rvotetimer = math.floor((votetimernumber - votetimer) /5)
                        et.trap_SendServerCommand(client, "chat \"^3MAPVOTE: ^7Disabled ^7for another ^3"..rvotetimer.." ^7seconds after mapchange!\"")
                        return 1
                    end
                end
            end
        end
        if argv1 == "map" and gamestate == 2 and votetimer > 0 and mapblock then
			rvotetimer = math.floor((votetimernumber - votetimer) /5)
            et.trap_SendServerCommand(client, "chat \"^3MAPVOTE: ^7Disabled ^7for another ^3"..rvotetimer.." ^7seconds after mapchange!\"")
            return 1
        end
        if argv1 == "map" or argv1 == "matchreset" then
            if gamestate == 0 and not getlevel(client) then
                et.trap_SendServerCommand( client, "cpm \"^wSurvival is already running\n\"" )
                return 1
            end
        end
    end
	if cmd == "test" then
		et.trap_SendServerCommand( client, "chat \"^w"..fightsoundstart.." "..timer.." "..htimer[client].."\"" )
		return 1
	end
	-------//--------------------Commands----------------------------
	if string.find(argv1, "^" .. ref_typo .. "") then
		et.trap_SendServerCommand( client, "cpm \"^wCkeck your typing\"" )
		return 1
	end
	
	if string.find(arg0, "^" .. rules_cmd .. "") or string.find(argv1, "^" .. rules_cmd .. "") then
		printrules(client)
		return 1
	end
	
	if (string.find(arg0, "^" .. news_cmd .. "") or string.find(argv1, "^" .. news_cmd .. "")) and not newscommand then
		et.trap_SendServerCommand( client, "cpm \"^wSorry^z, ^1!news ^ware ^1disabled ^won this server.\"" )
		return 1
	end
	
	if cmd == "readyteam" and gamestate == 2 and countinfected < 1 then
		et.trap_SendServerCommand( client, "cpm \"^wSorry^z, ^wyou can't start -> ^3no players ^win infected team\"" )
		return 1
	end
	
	if cmd == "lock" then
		et.trap_SendServerCommand( client, "cpm \"^wSorry^z, ^1lock ^wis ^1disabled ^won this server.\"" )
		return 1
	end
	
	if gamestate == 0 and cmd == "kill" and checkteam(client) == TEAM_HUMANS then
		et.trap_SendServerCommand( client, "cp \"^wSorry^z, ^1slash kill ^wis ^1disabled ^won human team^z.\"" )
		return 1 
	end
	
	if cmd == "kill" and checkteam(client) == TEAM_INFECTED and hpkill < 99 and hpkill > 0 then
		et.trap_SendServerCommand( client, "cp \"^wYour ^1healthpoints ^ware to ^3low ^wto ^3selfkill ^w(^3"..hpkill.."/100^w)\n\"" )
		return 1 
	end
	
	if cmd == "team" or cmd == "nextteam" then
		if argv1 == "r" then
			if infectedblock == 1 and checkteam(client) ~= TEAM_INFECTED and not blockteamb[client] then
				et.trap_SendServerCommand(client, "cp \"^7Sorry, you ^1can't join the infected\n\" " )
				return 1
			end
		elseif argv1 == "b" and blockteamb[client] then
			et.trap_SendServerCommand(client, "cp \"^7You are ^1infected ^7and need to stay in infected team\n\" " )
			return 1
		elseif argv1 ~= "b" and argv1 ~= "r" and argv1 ~= "s" then
			return 1
		elseif argv1 == "s" and gamestate == 0 and checkteam(client) == TEAM_HUMANS then
			punishteama[client] = 1
		end
	end
	
	if cmd == "thirdperson" or cmd == "tp" then
		if arg1 == "1" then
            et.trap_SendServerCommand(client, "forcecvar cg_thirdperson 1\n" )
            et.trap_SendServerCommand(client, "forcecvar cg_thirdpersonrange 100\n" )
            et.trap_SendServerCommand(client, "chat \"^1"..Modname..": ^3Thirdperson view has been enabled\"" )
            et.trap_SendServerCommand(client, "cpm \"^1Warning: ^7Don't cycle ^3Thirdperson ^7to much!\"" )
            return 1
        elseif arg1 == "2" then
            et.trap_SendServerCommand(client, "forcecvar cg_thirdperson 1\n" )
            et.trap_SendServerCommand(client, "forcecvar cg_thirdpersonrange 200\n" )
            et.trap_SendServerCommand(client, "chat \"^1"..Modname..": ^3Thirdperson view has been enabled\"" )
            et.trap_SendServerCommand(client, "cpm \"^1Warning: ^7Don't cycle ^3Thirdperson ^7to much!\"" )
            return 1
        elseif arg1 == "0" then
            et.trap_SendServerCommand(client, "forcecvar cg_thirdperson 0\n" )
            et.trap_SendServerCommand(client, "chat \"^1"..Modname..": ^3Thirdperson view has been disabled\"" )
            return 1
		end
		et.trap_SendServerCommand(client, "cpm \"^3/thirdperson ^10/1/2\" " )
        return 1
	end
	
	if cmd == "boom" and getlevel(client) then	--just for lulz
		et.G_AddEvent( client, 84, 1)
		return 1
	end
	
	if (cmd == "ready" or cmd == "readyteam" or cmd == "imready") and countinfected < 1 and gamestate == 2 then
		et.trap_SendServerCommand(client, "cpm \"^wSorry^z, ^wyou can't ready up -> ^3no player ^win infected team\n\"" )
		return 1
	end
	
	-------//--------------------Max Snipers for Humans----------------------------
	if cmd == "class" then
		if argv1 == "c" then
			if et.trap_Argv(2) == "3" then
				for j = 0, (maxclients - 1) do
					if checkteam(j) == TEAM_HUMANS and getWeapon(j) == 42 then
						sniperallies = sniperallies + 1
					end
				end
				if getWeapon(client) == 41 then
					sniperallies = sniperallies - 1
				end
				if sniperallies > 0 then
					sniperallies = 0
					setPlayerWeapon(client , 10)  -- Set Weapon to Sten
					return 1
				end
			end
		end
	end

	if cmd == "team" then
		if argv1 == "b" then
			if et.trap_Argv(2) == "4" then
				if et.trap_Argv(3) == "25" then
					for j = 0, (maxclients - 1) do
						if checkteam(j) == TEAM_HUMANS and getWeapon(j) == 25 then
							sniperallies = sniperallies + 1
						end
					end
				if getWeapon(client) == 25 then
					sniperallies = sniperallies - 1
				end
				if sniperallies > 0 then
					sniperallies = 0
					setPlayerWeapon(client , 10)  -- Set Weapon to Sten
					return 1
				end
				end --end "if et.trap_Argv(3) == "25" then"
			end
		end
    end

    -------//--------------------Referee commands for infected votes----------------------------
    if cmd == "infect" and isref ~= "0" and countinfected == 0 then
        et.G_globalSound ( refereesound )
        allplayers()
		local randomplayer = randomplayer()
        et.trap_SendConsoleCommand( et.EXEC_APPEND, "put "..randomplayer.." r\n" )
        et.G_Print("Player "..randomplayer.." got infected\n")
        removeplayers()
        return 1
    elseif cmd == "infect" and isref ~= "0" and countinfected > 0 then
        et.trap_SendServerCommand( client, "cpm \"^wSorry^z, ^wyou can't execute this vote -> ^3a player ^wis already in infected team\n\"" )
        return 1
    end
	
    if cmd == "infectedspec" and isref ~= "0" then
        et.G_globalSound ( refereesound )
        removeinfected()
        et.G_Print("Remove Infected\n")
        return 1
    end
	
    if cmd == "randommap" and isref ~= "0" then
        et.G_globalSound ( refereesound )
		local randommap = randommap()
        et.trap_SendConsoleCommand( et.EXEC_APPEND, "map " .. randommap .. "\n" )
        et.G_Print("Random Map: "..randommap.."\n")
        return 1
    end
	
    return 0
end

function et_Obituary( victim, killer, meansofdeath )
    if checkteam(victim) == TEAM_HUMANS and gamestate == 0 then
            if counthumans > 1 then
                putteaminfected[victim] = 1
                blockteamb[victim] = true
            end
    end
    
if meansofdeath ~= 0 and meansofdeath ~= 5 and meansofdeath ~= 21 and meansofdeath ~= 24 and meansofdeath ~= 25 and meansofdeath ~= 31 and meansofdeath ~= 32 and meansofdeath ~= 33 and meansofdeath ~= 34 and meansofdeath ~= 36 and meansofdeath ~= 38 and meansofdeath ~= 39 and meansofdeath ~= 52 and meansofdeath ~= 53 and meansofdeath ~= 54 and meansofdeath ~= 39 and meansofdeath ~= 22 and meansofdeath ~= 23 and meansofdeath ~= 35 and killer ~= 1022 and killer ~= 1023 then
    
    ---------Announce Player HP--------
    local killerhp = et.gentity_get(killer, "health")
    if checkteam(victim) ~= checkteam(killer) and killer ~= 1022 and meansofdeath ~= 64 and meansofdeath ~= 37 and killerhp > 0 then 
        --et.trap_SendServerCommand(victimnum, string.format("cpm \"^7Your ^7killer ^7%s ^7has ^7still ^o%d ^7health ^7points ^7remaining.\n\"", killername, killerhp)) 
        --this sends a message to the client only
        msg = string.format("cpm  \"" .. playerName(killer) ..  "^7 had^o " .. killerhp .. " ^7HP left\n")
        et.trap_SendServerCommand(victim, msg)
    end 
    ------------
        killamount[killer] = killamount[killer] + 1 -- count +1
        if meansofdeath  and checkteam(killer) == TEAM_INFECTED and checkteam(victim) == TEAM_HUMANS and counthumans <= 1 and gamestate == 0 then
            --et.trap_SendServerCommand(-1, "chat \"Debug \"" )
            et.trap_SendConsoleCommand( et.EXEC_NOW, "timelimit 0.01" )
            allhumansdeath = 1
        end
    end
  if meansofdeath ~= 54 and killer ~= 1022 and killer ~= 1023 and meansofdeath ~= 37 and meansofdeath ~= 64 then
    if meansofdeath == 6 then --MOD_KNIFE
        if checkteam(victim) == TEAM_HUMANS then
            local soundindex = "sound/rtcw/flame_scream3.wav"
            local soundindex2 = "sound/cough.wav" 
            et.G_Sound(killer, et.G_SoundIndex(soundindex) )
            et.G_Sound(victim, et.G_SoundIndex(soundindex2) )
            et.gentity_set(killer, "ps.ammoclip", 1, 1)
        end
    end
    if checkteam(killer) == TEAM_HUMANS then
        if killamount[killer] == 5 then
            et.gentity_set(killer, "ps.ammoclip", 5, (et.gentity_get(killer, "ps.ammoclip", 5) +1)) --pf
            et.gentity_set(killer, "ps.ammo", 40, (et.gentity_get(killer, "ps.ammo", 40) +1)) --rifle grenade
        end
        et.gentity_set(killer, "ps.ammo", 3, (et.gentity_get(killer, "ps.ammo", 3) +12)) --mp40
        et.gentity_set(killer, "ps.ammoclip", 6, (et.gentity_get(killer, "ps.ammoclip", 6)+30)) --flamethrower
        et.gentity_set(killer, "ps.ammo", 7, (et.gentity_get(killer, "ps.ammo", 7) +8)) --colt
        et.gentity_set(killer, "ps.ammo", 8, (et.gentity_get(killer, "ps.ammo", 8) +12)) --thompson
        et.gentity_set(killer, "ps.ammo", 10, (et.gentity_get(killer, "ps.ammo", 10) +12)) --sten
        et.gentity_set(killer, "ps.ammo", 14, (et.gentity_get(killer, "ps.ammo", 14) +8)) --silcenced luger
        et.gentity_set(killer, "ps.ammo", 23, (et.gentity_get(killer, "ps.ammo", 23) +5)) --rifle infected
        et.gentity_set(killer, "ps.ammo", 24, (et.gentity_get(killer, "ps.ammo", 24) +5)) --sniper humans
        et.gentity_set(killer, "ps.ammo", 25, (et.gentity_get(killer, "ps.ammo", 25) +5)) --rifle humans
        et.gentity_set(killer, "ps.ammo", 31, (et.gentity_get(killer, "ps.ammo", 31) +16)) --mg42 ammo
        et.gentity_set(killer, "ps.ammo", 32, (et.gentity_get(killer, "ps.ammo", 32) +5)) --sniper infected
        et.gentity_set(killer, "ps.ammo", 33, (et.gentity_get(killer, "ps.ammo", 33) +15)) --fg42
        et.gentity_set(killer, "ps.ammoclip", 35, (et.gentity_get(killer, "ps.ammoclip", 35) +1)) --mortar
        et.gentity_set(killer, "ps.ammo", 37, (et.gentity_get(killer, "ps.ammo", 37) +8)) --akimbo colt
        et.gentity_set(killer, "ps.ammo", 41, (et.gentity_get(killer, "ps.ammo", 41) +8)) --silenced colt
        et.gentity_set(killer, "ps.ammo", 47, (et.gentity_get(killer, "ps.ammo", 47) +8)) --akimbo silenced colt
    end
    if checkteam(victim) == TEAM_INFECTED and killer ~= 1022 and killer ~= 1023 and meansofdeath ~= 34 and meansofdeath ~= 36 and meansofdeath ~= 37 and meansofdeath ~= 38 and meansofdeath ~= 64 and meansofdeath ~= 0 then
        local soundindex2 = "sound/rtcw/wz_death1.wav"
        et.G_Sound(killer, et.G_SoundIndex(heartbeatsound) )
        et.G_Sound(victim, et.G_SoundIndex(soundindex2) )
    end
    if checkteam(victim) == TEAM_HUMANS and meansofdeath == 68 then --MOD_THROWN_KNIFE
        local soundindex = "sound/rtcw/death1.wav"
        local soundindex2 = "sound/rtcw/zombieeating.wav"
        et.G_Sound(killer, et.G_SoundIndex(soundindex) )
        et.G_Sound(victim, et.G_SoundIndex(soundindex2) )
        et.gentity_set(killer, "ps.ammoclip", 1, 1)
    end
end
end

function et_ClientSpawn(cno,revived)
    if checkteam(cno) == TEAM_INFECTED then
        setPlayerWeapon(cno , 1)
        if checkclass(cno) == 1 then
            et.gentity_set(cno, "ps.stats", 4, infected_max_hp - 16)
            et.gentity_set(cno, "health", infected_max_hp)
        else
            et.gentity_set(cno, "ps.stats", 4, infected_max_hp)
            et.gentity_set(cno, "health", infected_max_hp)
        end
        if et.trap_Cvar_Get( "g_infectedWeaps" ) == "" or et.trap_Cvar_Get( "g_infectedWeaps" ) == nil then --in case server don't use infected mod
            if checkclass(cno) == 3 then
                et.gentity_set(cno,"sess.latchPlayerType",1)
                changeclassfops(cno)
            end
        end
    elseif checkteam(cno) == TEAM_HUMANS then
        if checkclass(cno) == 1 then
            et.gentity_set(cno, "ps.stats", 4, human_medic_max_hp-12)
		else
			et.gentity_set(cno, "ps.stats", 4, human_max_hp)
			et.gentity_set(cno, "health", human_max_hp)
        end
    end
    for k=1, (et.MAX_WEAPONS - 1), 1 do
        if not weaponsa[k] then
            if checkteam(cno) == TEAM_HUMANS then --starting ammo humans (Empties the ammo to reload)
                et.gentity_set(cno, "ps.ammo", 2, 6)
                et.gentity_set(cno, "ps.ammo", 3, 0) --mp40
                et.gentity_set(cno, "ps.ammo", 4, 1)
                et.gentity_set(cno, "ps.ammoclip", 5, 1)
                et.gentity_set(cno, "ps.ammo", 6, 0)
                et.gentity_set(cno, "ps.ammo", 7, 0) --colt
                et.gentity_set(cno, "ps.ammo", 8, 0) --thompson
                et.gentity_set(cno, "ps.ammo", 9, 0)
                et.gentity_set(cno, "ps.ammoclip", 9, 0)
                et.gentity_set(cno, "ps.ammo", 10, 0) --sten
                et.gentity_set(cno, "ps.ammo", 11, 0)
                et.gentity_set(cno, "ps.ammo", 14, 0)
                et.gentity_set(cno, "ps.ammo", 23, 0)
                et.gentity_set(cno, "ps.ammo", 24, 0)
                et.gentity_set(cno, "ps.ammo", 25, 0) --sniper
				et.gentity_set(cno, "ps.ammo", 29, 0) --tripmine
                et.gentity_set(cno, "ps.ammo", 31, 0) --mg42
                et.gentity_set(cno, "ps.ammo", 32, 0)
                et.gentity_set(cno, "ps.ammo", 33, 0) --fg42
                et.gentity_set(cno, "ps.ammo", 34, 0)
                et.gentity_set(cno, "ps.ammo", 35, 12)
                et.gentity_set(cno, "ps.ammo", 36, 0)
                et.gentity_set(cno, "ps.ammo", 37, 0) --akimbo colt
                et.gentity_set(cno, "ps.ammo", 38, 0)
                et.gentity_set(cno, "ps.ammo", 39, 0) --infected rifle grenade
                et.gentity_set(cno, "ps.ammo", 40, 0) --humans rifle grenade
                et.gentity_set(cno, "ps.ammo", 41, 0) --silenced colt
                et.gentity_set(cno, "ps.ammo", 42, 0) --sniper
                et.gentity_set(cno, "ps.ammo", 43, 0) --sniper
                et.gentity_set(cno, "ps.ammo", 44, 0) --fg42 scope
                et.gentity_set(cno, "ps.ammo", 47, 0) --akimbo silenced colt
                et.gentity_set(cno, "ps.ammo", 48, 0)
			else
				et.gentity_set(cno, "ps.ammoclip", 1, 1)
            end
        end
        if checkteam(cno) == TEAM_HUMANS and punishteama[cno] == 1 then
            et.gentity_set(cno, "ps.ammo", k, 0) --empty ammo
            et.gentity_set(cno, "ps.ammoclip", k, 0) --empty ammoclip
            if punishteamaspam[cno] == 0 then
                et.trap_SendServerCommand(cno, "chat \"^1Your weapon ammo got emptied for abusing join spectator -> humans\"" )
                et.trap_SendServerCommand(cno, "cp \"^1Your weapon ammo got emptied for abusing join spectator -> humans\"" )
                punishteamaspam[cno] = 1
            end
        end
    end
end

function et_Print(text) 
    local t = ParseString(text)
    if t[2] == "Passed:" and t[3] == "[poll]" and string.lower(t[4]) == "infect" and string.lower(t[5]) == "a" and string.lower(t[6]) == "random" and string.lower(t[7]) == "player" and countinfected == 0 then
        allplayers()
       --tableplayers() --debug
		local randomplayer = randomplayer()
        et.trap_SendConsoleCommand( et.EXEC_APPEND, "put "..randomplayer.." r\n" )
        et.G_Print("Someone got infected: "..randomplayer.."\n")
        removeplayers()
    end
    if t[2] == "Passed:" and t[3] == "[poll]" and string.lower(t[4]) == "put" and string.lower(t[5]) == "infected"  and string.lower(t[6]) == "spectator" then
        removeinfected()
        et.G_Print("Remove Infected\n")
    end
    if t[2] == "Passed:" and t[3] == "[poll]" and string.lower(t[4]) == "random" and string.lower(t[5]) == "map" then
        local randommap = randommap()
		et.trap_SendConsoleCommand( et.EXEC_APPEND, "map "..randommap.."\n" )
        --et.trap_SendServerCommand( -1, "chat \"^w"..randommap.."\"" ) --Debug
        et.G_Print("Random Map: "..randommap.."\n")
    end
end


--------------------------HELPER FUNCTIONS------------------------------------------------------------------

function ParseString(inputString)
    local i = 1
    local t = {}
    for w in string.gfind(inputString, "([^%s]+)%s*") do
        t[i]=w
        i=i+1
    end
    return t
 end

function getCS()
    local cs = et.trap_GetConfigstring(et.CS_VOTE_STRING)
    local t = ParseString(cs)
    return t[3]
end

function getguid(targetID)
    if (targetID == nil) or (targetID > maxclients) then
      return nil
    end

    local userinfo = et.trap_GetUserinfo( targetID )
    local guid = et.Info_ValueForKey( userinfo, "cl_guid" )
    -- upcase for exact matches
    guid = string.upper(guid)

    return guid
end

function allplayers()
    for b=0, maxclients-1, 1 do            
        if et.gentity_get(b,"pers.connected") == 2 and et.gentity_get(b,"ps.ping") > 0 and checkteam(b) ~= TEAM_SPECTATOR then
            table.insert(playertable , b) --playertable[1] = 5 
			--et.trap_SendServerCommand(-1,"chat \"Slots: "..b.." \"") --Debug
        end
    end
end
 
function randomplayer()
    local randomplayer = math.random(#playertable)
	--et.trap_SendServerCommand(-1,"chat \"User: "..randomplayer.." \"") --Debug
    return playertable[randomplayer]
end

function removeplayers()
    for k,v in pairs(playertable) do
        playertable[k] = nil
    end
end

--test function to show table
function tableplayers()
    table.foreach(playertable, function(k,v)
        et.trap_SendServerCommand(-1,"chat \""..k..","..v.."\n\"")
    end)
end

function removeinfected()
    for cno=0, maxclients-1, 1 do 
        if checkteam(cno) == TEAM_INFECTED then
            et.trap_SendConsoleCommand( et.EXEC_APPEND, "put "..playerName(cno).." s\n" )
        end
    end
end


--[[
--old function bugged since lua version 5.1
function randommap()
    random1 = math.random(0,table.getn(randommaptable) )
	if random1 == nil then 
		random1 = math.random(0,table.getn(randommaptable) ) 
	end
    return string.lower(randommaptable[random1].mapname)
end
--]]

function randommap()
	local random1 = math.random(#randommaptable)
    return string.lower(randommaptable[random1].mapname)
end

--[[
function randommap()
    local random = math.random(table.getn(randommaptable))
    local randommap = randommaptable[random].mapname
    --if currentmap == randommap then
    if mapname == randommap then
        if random == table.getn(randommaptable) then
                random = 0
        end
     randommap = randommaptable[random+1].mapname
    end
    return random
end
--]]

function playerName(id) -- return a player's name
  local name = et.Info_ValueForKey(et.trap_GetUserinfo(id), "name")
  if name == "" then
    return "*unknown*"
  end
  return name
end

function getlevel(client)
    local lvl = et.G_shrubbot_level(client)
    if lvl >= min_level then
        return true
    end
        return nil
end

function checkmuted(client)
   local cs = et.trap_GetConfigstring(et.CS_PLAYERS + client)
    return tonumber(et.Info_ValueForKey(cs, "mu"))
end

function checkteam(client)
   local cs = et.trap_GetConfigstring(et.CS_PLAYERS + client)
    return tonumber(et.Info_ValueForKey(cs, "t"))
end

--0=Soldier, 1=Medic, 2=Engineer, 3=FieldOps, 4=CovertOps
function checkclass(client)
   local cs = et.trap_GetConfigstring(et.CS_PLAYERS + client)
    return tonumber(et.Info_ValueForKey(cs, "c"))
end

function changeclassfops(client)
    local cs = et.trap_GetConfigstring(et.CS_PLAYERS + client)
    local setclass = et.Info_SetValueForKey( cs, "c", 1 )
    et.trap_SetConfigstring(689 + client, setclass)
end

function getWeapon(playerID)
    return et.gentity_get(playerID, "sess.playerWeapon")
end 

function setPlayerWeapon(playerID , weapon)
    et.gentity_set(playerID, "sess.latchPlayerWeapon", weapon)
end

function countPlayers()
	for i=0, maxclients-1, 1 do
		if checkteam(i) == INFECTED or checkteam(i) == HUMANS then
			if et.gentity_get(i,"inuse") then		
			playercountready = playercountready + 1
			end
		end
   	end
end


-- print message to either client's console or (if clientID == nil) to server console
function printmsg(message, clientID)
    if not message then
        return
    end

    -- replace "s in message with 's
    local dummy
    message, dummy = string.gsub(message, "\"", "'")

    if clientID then
        et.trap_SendServerCommand(clientID, "print \"".. message .."^7\n\"")
    else
        if lastcallerID then
            et.trap_SendServerCommand(lastcallerID, "print \"".. message .."^7\n\"")
        else
            et.G_Print(message .."^7\n")
        end
    end
end


-- reads file and returns string array of rows (indexed from 1) and number of rows
function readFile(filename)
    local fd, len
    local rows = {}

    fd, len = et.trap_FS_FOpenFile(filename, et.FS_READ)

    if len == -1 then
        return rows, 0
    else
        --read it all
        local filestr = et.trap_FS_Read(fd, len)

        local rowcount = 0
        for line in string.gfind(filestr, "[^\r\n]+") do
            rowcount = rowcount + 1
            rows[rowcount] = line
        end

            et.trap_FS_FCloseFile(fd)
            return rows, rowcount
      end
end

-- print rules to client
function printrules(clientID)
et.trap_SendServerCommand( clientID, "chat \"^1"..Modname.."^7: ^2Open the console, ^7"..playerName(clientID).." \"" )
    rows, numrows = readFile(RULES_FILENAME)

    printmsg("", clientID)

    for i = 1, numrows do
            printmsg(rows[i], clientID)
    end

    printmsg("", clientID)
end

function isBot1(playerID)
    if et.gentity_get(playerID,"pers.connected") == 2 and et.gentity_get(playerID,"ps.ping") == 0 then
    return true
    end
end

------------------------------------------END HELPER FUNCTIONS----------------------------------------------------------