
/*
(c) Copyright 2002, Jonas Lindgren

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Changelog:
 2002-11-23 - Version 1.0, first version completed.
 2002-11-23 - Version 1.1, fixed a minor bug with smyga_cvar.
 2002-11-24 - Version 2.0, completely rewrote the script with a different method. Now more effective and harder to abuse.
 2002-11-25 - Version 2.1, squelched minor bug under rare conditions.
*/

#include <amxmod>
#include <amxconst>

new savervalues[32][4],saved[6]
public destuck4(id) {
	new players[32], num,team[32], coordinates[3]
	get_user_team(id,team,32)
	get_players(players,num,"ae",team)

	if (is_user_alive(id)) {
		if (saved[5] == 0) {
			if (num != 1) {
				for(new i = 0; i < num; ++i) {
					savervalues[players[i]][0] = get_user_health(players[i])
					get_user_origin(players[i],coordinates,0)
					savervalues[players[i]][1] = coordinates[0]
					savervalues[players[i]][2] = coordinates[1]
					savervalues[players[i]][3] = coordinates[2]
				}
				saved[0] = get_user_health(id)
				get_user_origin(id,coordinates,0)
				saved[1] = coordinates[0]
				saved[2] = coordinates[1]
				saved[3] = coordinates[2]
				saved[4] = id

				client_print(id,print_chat,"[AMX] Teleporting, please hold 10 seconds.")
				saved[5] = 1
				set_task(9.5,"slapuser")
				set_task(10.0,"destuck5")
			}else{
				client_print(id,print_chat,"[AMX] You're the only one alive in your team, aborting!!")
			}
		}else{
			client_print(id,print_chat,"[AMX] Someone's already being unstuck, aborting!!")
		}
	}else{
		client_print(id,print_chat,"[AMX] You have to be alive to be stuck, aborting!")
	}
}

public slapuser(){
	new players[32], num,team[32], coordinates[3]
	get_user_team(saved[4],team,32)
	get_players(players,num,"ae",team)
	get_user_origin(saved[4],coordinates,0)

	if (is_user_alive(saved[4])) {
		if (get_user_health(saved[4]) >= saved[0]) {
			if (saved[1] == coordinates[0] && saved[2] == coordinates[1] && saved[3] == coordinates[2]) {
				user_slap(saved[4],0)
			}
		}
	}
}

public destuck5(){
	saved[5] = 0

	new players[32], num,team[32], coordinates[3],targetcoordinates[3],savercoordinates[3],saver[2]
	get_user_team(saved[4],team,32)
	get_players(players,num,"ae",team)
	get_user_origin(saved[4],coordinates,0)

	if (is_user_alive(saved[4])) {
		if (get_user_health(saved[4]) >= saved[0]) {
			if (saved[1] == coordinates[0] && saved[2] == coordinates[1] && saved[3] == coordinates[2]) {
				for(new i = 0; i < num; ++i) {
					targetcoordinates[0] = savervalues[players[i]][1]
					targetcoordinates[1] = savervalues[players[i]][2]
					targetcoordinates[2] = savervalues[players[i]][3]

					if (saver[1] == 0 || saver[1] > get_distance(targetcoordinates,coordinates)) {
						get_user_origin(players[i],savercoordinates,0)
						if (get_distance(savercoordinates,coordinates) > 80 && is_user_alive(players[i]) && savervalues[players[i]][0] >= get_user_health(players[i])) {
							saver[0] = players[i]
							saver[1] = get_distance(targetcoordinates,coordinates)
						}
					}
				}
				if (saver[1] > 0) {
					targetcoordinates[0] = savervalues[saver[0]][1]
					targetcoordinates[1] = savervalues[saver[0]][2]
					targetcoordinates[2] = savervalues[saver[0]][3] + 30
					set_user_origin(saved[4], targetcoordinates)
					set_user_health(saved[4], 1)
					set_user_armor(saved[4], 0)
					client_print(saved[4],print_chat,"[AMX] You were teleported to freedom! (hopefully)")
				}else{
					client_print(saved[4],print_chat,"[AMX] Couldn't find any good locations, aborting!")
				}
			}else{
				client_print(saved[4],print_chat,"[AMX] Hey, you're not stuck at all, aborting!")
			}
		}else{
		client_print(saved[4],print_chat,"[AMX] You've been attacked during the wait, aborting!")
		}
	}
}

public smyga_cvar(plugin_name[32],plugin_version[32]){	// I'd love to be able to see which servers use my plugins, so I register a CVAR to allow server-browsers to find servers with this plugin. Comment this function out in the plugin_init if you don't want me to do this.
	new cvar[192],cvar2[192]
	if (cvar_exists("smyga_plugin") == 0){
		register_cvar("smyga_plugin", ";",FCVAR_SERVER)
	}
	get_cvar_string("smyga_plugin",cvar,192)
	add(cvar2,192,plugin_name,0)
	add(cvar2,192," ",0)
	add(cvar2,192,plugin_version,0)
	if (contain(cvar,cvar2) > 0) {
	}else{
		add(cvar,192,cvar2,0)
		add(cvar,192,";",0)
		set_cvar_string("smyga_plugin",cvar)
	}
}

public plugin_init() {
	new plugin_name[32],plugin_version[32]
	plugin_name = "Destuckme"
	plugin_version = "2.1"

	register_plugin(plugin_name,plugin_version,"Smygasaur")
//	register_plugin("Destuckme",plugin_version,"Smygasaur")	// It seems that the code that compiles this code and zips it up on http://djeyl.net/ needs this line to name the plugin, when creating the download. ;)
	smyga_cvar(plugin_name,plugin_version)
	register_clcmd("say /stuck","destuck4")
	register_clcmd("say_team /stuck","destuck4")
	return PLUGIN_CONTINUE
}

