/*================================================================================
	[ZP] Extra: Extra Jump
	
	Buyable extra item that grants +1 mid-air jump per purchase for the
	current round (humans only). Stackable up to any number.
================================================================================*/

#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <zombie_plague_special>

#define PLUGIN_NAME  "[ZP] Extra: Extra Jump"
#define PLUGIN_VERSION "1.0"
#define PLUGIN_AUTHOR "VIP work"

new g_extra_jumps[33]      // bonus jumps purchased this round (per-player)
new jump_count[33]         // current jump count for this air-time
new bool:do_jump[33]       // flag to trigger the extra jump velocity
new g_itemid               // registered extra-item id

public plugin_init() {
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
	g_itemid = zp_register_extra_item("Extra Jump (+1)", 10, ZP_TEAM_HUMAN)

	register_event("HLTV", "event_round_start", "a", "1=0", "2=0")
	register_forward(FM_PlayerPreThink, "fw_PreThink")
	register_forward(FM_PlayerPostThink, "fw_PostThink")
}

// Reset all counts at round start
public event_round_start() {
	arrayset(g_extra_jumps, 0, sizeof(g_extra_jumps))
	arrayset(jump_count, 0, sizeof(jump_count))
}

// Also reset on round end (in case HLTV event is missed)
public zp_round_ended(winteam) {
	arrayset(g_extra_jumps, 0, sizeof(g_extra_jumps))
	arrayset(jump_count, 0, sizeof(jump_count))
}

public client_disconnected(id) {
	g_extra_jumps[id] = 0
	jump_count[id] = 0
	do_jump[id] = false
}

public zp_extra_item_selected(id, itemid) {
	if(itemid != g_itemid)
		return PLUGIN_CONTINUE

	if(zp_get_user_zombie(id))
		return PLUGIN_HANDLED  // humans only

	g_extra_jumps[id]++
	client_print_color(id, print_team_default, "^4[ZP]^1 Extra jump purchased! Total bonus jumps: ^3%d^1.", g_extra_jumps[id])
	return PLUGIN_HANDLED
}

// Detect jump-press while in mid-air
public fw_PreThink(id) {
	if(!is_user_alive(id))
		return FMRES_IGNORED

	if(g_extra_jumps[id] <= 0)
		return FMRES_IGNORED

	if(zp_get_user_zombie(id))
		return FMRES_IGNORED

	new nbut = get_user_button(id)
	new obut = get_user_oldbutton(id)
	new flags = get_entity_flags(id)

	// Player pressed jump while in air (not on ground)
	if((nbut & IN_JUMP) && !(flags & FL_ONGROUND) && !(obut & IN_JUMP)) {
		if(jump_count[id] < g_extra_jumps[id]) {
			do_jump[id] = true
			jump_count[id]++
		}
	}

	// Reset jump count when landing
	if((nbut & IN_JUMP) && (flags & FL_ONGROUND)) {
		jump_count[id] = 0
	}

	return FMRES_IGNORED
}

// Apply upward velocity for the extra jump
public fw_PostThink(id) {
	if(!do_jump[id])
		return FMRES_IGNORED

	static Float:velocity[3]
	entity_get_vector(id, EV_VEC_velocity, velocity)
	velocity[2] = random_float(265.0, 285.0)
	entity_set_vector(id, EV_VEC_velocity, velocity)

	do_jump[id] = false
	return FMRES_IGNORED
}
