#include "quakedef.h"
#include "r_local.h"

#include "progs.h"
#include "world.h"

#define BOT_VERSION "0.70"

cvar_t aimbot_enable = { "aimbot_enable", "0", true };
cvar_t aimbot_weapons = { "aimbot_weapons", "255", true };
cvar_t aimbot_panic = { "aimbot_panic", "0", true };

cvar_t aimbot_love = { "aimbot_love", "", true };
cvar_t aimbot_hate = { "aimbot_hate", "", true };

cvar_t aimbot_maxangle = { "aimbot_maxangle", "30", true };
cvar_t aimbot_maxangle_near = { "aimbot_maxangle_near", "70", true };
cvar_t aimbot_near_threshold = { "aimbot_near_threshold", "200", true };

cvar_t aimbot_zshift = { "aimbot_zshift", "4", true };
cvar_t aimbot_zshift_rocket = { "aimbot_zshift_rocket", "-22", true };
cvar_t aimbot_zshift_rocket_fly = { "aimbot_zshift_rocket_fly", "4", true };
cvar_t aimbot_zshift_min = { "aimbot_zshift_min", "-28", true };
cvar_t aimbot_zshift_max = { "aimbot_zshift_max", "36", true };

cvar_t aimbot_shot_slowdown = { "aimbot_shot_slowdown", "1", true };

cvar_t aimbot_predict = { "aimbot_predict", "1000", true };
cvar_t aimbot_predict_pixels = { "aimbot_predict_pixels", "18", true };
cvar_t aimbot_predict_traceshift = { "aimbot_predict_traceshift", "17", true };

cvar_t dodgebot_enable = { "dodgebot_enable", "0", true };
cvar_t dodgebot_time = { "dodgebot_time", "2000", true };
cvar_t dodgebot_speed = { "dodgebot_speed", "1", true };
cvar_t dodgebot_jump = { "dodgebot_jump", "1", true };
cvar_t dodgebot_frame = { "dodgebot_frame", "50", true };
cvar_t dodgebot_steps = { "dodgebot_steps", "10", true };
cvar_t dodgebot_extrasteps = { "dodgebot_extrasteps", "10", true };
cvar_t dodgebot_xsize = { "dodgebot_xsize", "16", true };
cvar_t dodgebot_zdownsize = { "dodgebot_zdownsize", "24", true };
cvar_t dodgebot_zupsize = { "dodgebot_zupsize", "32", true };
cvar_t dodgebot_near_threshold = { "dodgebot_near_threshold", "16", true };
cvar_t dodgebot_time_threshold = { "dodgebot_time_threshold", "500", true };

cvar_t speedbot = { "speedbot", "1", true };

cvar_t wallhack = { "wallhack", "0", true };
cvar_t fake_snapshot = { "fake_snapshot", "0", true };

cvar_t radar = { "radar", "0", true };
cvar_t radar_rotate = { "radar_rotate", "0", true };
cvar_t radar_scale = { "radar_scale", "1000", true };
cvar_t radar_size = { "radar_size", "100", true };

#define SV_Error Sys_Error
static float ClipMoveToWorld(vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end);

#define ROUND(f) ((int)((double)(f) + 0.5))

void toggle_f()
{
	char *var;
	if (Cmd_Argc() != 2) {
		Con_Printf ("usage: toggle <variable>\n");
		return;
	}
	var = Cmd_Argv(1);
	Cvar_SetValue(var, !Cvar_VariableValue(var));
}

void lamebot_print()
{
	Con_Printf("\nLAMEBOT by lamer%c%c%c\n\nversion: "BOT_VERSION"\n\n", (char)0xd0, (char)0xd2, (char)0xcf);
}

void lamebot_init()
{

	lamebot_print();

	Cvar_RegisterVariable(&aimbot_predict_traceshift);
	Cvar_RegisterVariable(&aimbot_predict_pixels);
	Cvar_RegisterVariable(&aimbot_predict);

	Cvar_RegisterVariable(&aimbot_shot_slowdown);

	Cvar_RegisterVariable(&aimbot_zshift_max);
	Cvar_RegisterVariable(&aimbot_zshift_min);
	Cvar_RegisterVariable(&aimbot_zshift_rocket_fly);
	Cvar_RegisterVariable(&aimbot_zshift_rocket);
	Cvar_RegisterVariable(&aimbot_zshift);

	Cvar_RegisterVariable(&aimbot_near_threshold);
	Cvar_RegisterVariable(&aimbot_maxangle_near);
	Cvar_RegisterVariable(&aimbot_maxangle);

	Cvar_RegisterVariable(&aimbot_hate);
	Cvar_RegisterVariable(&aimbot_love);

	Cvar_RegisterVariable(&aimbot_panic);
	Cvar_RegisterVariable(&aimbot_weapons);
	Cvar_RegisterVariable(&aimbot_enable);

	Cvar_RegisterVariable(&dodgebot_time_threshold);
	Cvar_RegisterVariable(&dodgebot_near_threshold);
	Cvar_RegisterVariable(&dodgebot_zupsize);
	Cvar_RegisterVariable(&dodgebot_zdownsize);
	Cvar_RegisterVariable(&dodgebot_xsize);
	Cvar_RegisterVariable(&dodgebot_extrasteps);
	Cvar_RegisterVariable(&dodgebot_steps);
	Cvar_RegisterVariable(&dodgebot_frame);
	Cvar_RegisterVariable(&dodgebot_jump);
	Cvar_RegisterVariable(&dodgebot_speed);
	Cvar_RegisterVariable(&dodgebot_time);
	Cvar_RegisterVariable(&dodgebot_enable);

	Cvar_RegisterVariable(&speedbot);

	Cvar_RegisterVariable(&fake_snapshot);
	Cvar_RegisterVariable(&wallhack);

	Cvar_RegisterVariable(&radar_size);
	Cvar_RegisterVariable(&radar_scale);
	Cvar_RegisterVariable(&radar_rotate);
	Cvar_RegisterVariable(&radar);

	Cmd_AddCommand("toggle", toggle_f);
	Cmd_AddCommand("lamebot", lamebot_print);
}

int lamebot_players;
int lamebot_entities;

#define N_SLOTS 4

static int lamebot_autoaim[N_SLOTS];
static int lamebot_noyaim[N_SLOTS];
static float lamebot_angle_1[N_SLOTS];
static float lamebot_angle_2[N_SLOTS];

static int slot_weapon[N_SLOTS];
static float maxangle[N_SLOTS];
static entity_t *target[N_SLOTS];
static vec3_t target_org[N_SLOTS];

static int scan_list(char *name, char *list)
{
	int i;
	char *s;
	rep:
	if (*list == ',') {
		list++;
		goto rep;
	}
	if (!*list) return 0;
	s = name;
	while (*s != *list && *list != '?') {
		if (!*s) {
			while (*list && *list != ',') list++;
			goto rep;
		}
		spp:s++;
	}
	for (i = 1; list[i] && list[i] != ','; i++) if (list[i] != '?' && list[i] != s[i]) goto spp;
	return 1;
}

static void fix_string(char *str)
{
	while (*str) {
		if ((unsigned char)*str > 128) *str -= 128;
		if (*str >= 0x12 && *str <= 0x1B) *str -= 0x12 + '0';
		if (*str >= 'A' && *str <= 'Z') *str += 32;
		str++;
	}
}

static int player_state[MAX_CLIENTS];
static int pls_cnt = 0;

static void update_hate_love()
{
	int i;
	for (i = 0; i < MAX_CLIENTS; i++) {
		char name[MAX_SCOREBOARDNAME + 1];
		player_info_t *pl = &cl.players[i];
		player_state[i] = 0;
		if (!*pl->name) continue;
		memcpy(name, pl->name, MAX_SCOREBOARDNAME);
		name[MAX_SCOREBOARDNAME] = 0;
		fix_string(name);
		fix_string(aimbot_love.string);
		fix_string(aimbot_hate.string);
		if (scan_list(name, aimbot_love.string)) player_state[i] = 1;
		else if (scan_list(name, aimbot_hate.string)) player_state[i] = -1;
		else if (!*aimbot_love.string && *aimbot_hate.string) player_state[i] = 1;
		//Con_Printf("Player %s: %d\n", name, player_state[i]);
	}
}

static int lamebot_disclose = 0;

#define FORGET_CHEATS	300	/* 5 minutes */

static void lamebot_disclose_cheats()
{
	static int framecnt = 0;
	static int aimbot_cheat = 0, speedbot_cheat = 0, wallhack_cheat = 0, radar_cheat = 0, dodgebot_cheat = 0;
	static int aimbot_cheating = 0, speedbot_cheating = 0, wallhack_cheating = 0, radar_cheating = 0, dodgebot_cheating = 0;
	static double aimbot_time = -1, speedbot_time = -1, wallhack_time = -1, radar_time = -1, dodgebot_time = -1;

	if (aimbot_enable.value) aimbot_cheat = 1;
	if (speedbot.value != 1) speedbot_cheat = 1;
	if (wallhack.value) wallhack_cheat = 1;
	if (radar.value) radar_cheat = 1;
	if (dodgebot_enable.value) dodgebot_cheat = 1;

	if (++framecnt >= 64) {
		double t = Sys_DoubleTime();
		framecnt = 0;
		if (aimbot_cheat) aimbot_time = t, aimbot_cheating = 1;
		else if (aimbot_cheating && t - aimbot_time > FORGET_CHEATS)
			aimbot_cheating = 0;
		if (speedbot_cheat) speedbot_time = t, speedbot_cheating = 1;
		else if (speedbot_cheating && t - speedbot_time > FORGET_CHEATS)
			speedbot_cheating = 0;
		if (wallhack_cheat) wallhack_time = t, wallhack_cheating = 1;
		else if (wallhack_cheating && t - wallhack_time > FORGET_CHEATS)
			wallhack_cheating = 0;
		if (radar_cheat) radar_time = t, radar_cheating = 1;
		else if (radar_cheating && t - radar_time > FORGET_CHEATS)
			radar_cheating = 0;
		if (dodgebot_cheat) dodgebot_time = t, dodgebot_cheating = 1;
		else if (dodgebot_cheating && t - dodgebot_time > FORGET_CHEATS)
			dodgebot_cheating = 0;
		aimbot_cheat = speedbot_cheat = wallhack_cheat = radar_cheat = dodgebot_cheat = 0;
	}

	if (lamebot_disclose && (aimbot_cheating || speedbot_cheating || wallhack_cheating || radar_cheating || dodgebot_cheating)) {
		int i;
		int w;
		char *msgs[] = {"aimbot", "speedbot", "wallhack", "radar", "dodgebot"};
		char str[1024];
		lamebot_disclose = 0;
		strcpy(str, "say \"I am big LAMER!!! I am using lameBOT (version "BOT_VERSION") with ");
		for (w = 0, i = 0; i < 5; i++) {
			if (
				i == 0 ? aimbot_cheating :
				i == 1 ? speedbot_cheating :
				i == 2 ? wallhack_cheating :
				i == 3 ? radar_cheating :
				i == 4 ? dodgebot_cheating :
				0) {
				if (w++) strcat(str, ", ");
				strcat(str, msgs[i]);
			}
		}
		strcat(str, " feature");
		if (w > 1) strcat(str, "s");
		strcat(str, " enabled!\"");
		Cmd_ExecuteString(str);
	}
}

static struct {
	char *mod_name;
	int speed;
	int z;
} weapons[] = {
	"", -1, 0,
	"progs/v_axe.mdl", -1, 0,
	"progs/v_shot.mdl", 0, 15,
	"progs/v_shot2.mdl", 0, 15,
	"progs/v_nail.mdl", 1000, 16,		/* fixme */
	"progs/v_nail2.mdl", 1000, 16,		/* fixme */
	"progs/v_rock.mdl", 600, 0,
	"progs/v_rock2.mdl", 1000, 16,
	"progs/v_light.mdl", 0, 16,
	NULL, 0, 0,
};

static int current_weapon = 0;

void lamebot_next_frame()
{
	int i;
	lamebot_disclose_cheats();
	slot_weapon[0] = slot_weapon[1] = slot_weapon[2] = slot_weapon[3] = -1;
	for (i = 0; i < N_SLOTS; i++) maxangle[i] = 99999, target[i] = NULL;
	current_weapon = 0;
	//Con_Printf("pos: %f %f %f\n", cl.simorg[0], cl.simorg[1], cl.simorg[2]);
	if (!(pls_cnt++ & 31)) update_hate_love();
	if (!cl.viewent.model->name) return;
	for (i = 1; weapons[i].mod_name; i++) if (!stricmp(weapons[i].mod_name, cl.viewent.model->name)) {
		current_weapon = i;
		return;
	}
	Con_Printf("LAMEBOT WARNING: unknown model %s\n", cl.viewent.model->name);
}

static int check_trace_for_walls(vec3_t org, vec3_t targetorg)
{
	float clip;
	vec3_t target_c;
	VectorSubtract(org, targetorg, target_c);
	clip = ClipMoveToWorld(org, vec3_origin, vec3_origin, target_c);
	//Con_Printf("clip: %f\n", clip);
	if (clip < 0.99999) return 1;
	return 0;
}


static int check_shot_for_walls(vec3_t org, vec3_t targetorg)
{
	return check_trace_for_walls(org, targetorg);
/*
#define CHK_B	7
	if (check_trace_for_walls(org, targetorg)) return 1;
	targetorg[2] += CHK_B;
	if (check_trace_for_walls(org, targetorg)) {
		targetorg[2] -= CHK_B;
		return 1;
	}
	targetorg[2] -= CHK_B * 2;
	if (check_trace_for_walls(org, targetorg)) {
		targetorg[2] += CHK_B;
		return 1;
	}
	targetorg[2] += CHK_B;
	return 0;
*/
}

static void lamebot_try_weapon(int plnum, int weapon, int slot);

/* input:
	entity_t *currententity
*/

void lamebot_register_player(int plnum)
{
	lamebot_try_weapon(plnum, 0, current_weapon);
	if (current_weapon != 6) lamebot_try_weapon(plnum, 1, 6);
	if (current_weapon != 7) lamebot_try_weapon(plnum, 2, 7);
	if (current_weapon != 8) lamebot_try_weapon(plnum, 3, 8);
}

static void lamebot_try_weapon(int plnum, int slot, int weapon)
{
	float zshift;
	float length;
	float angle;
	vec3_t tgt;
	vec3_t targetorg;
	vec3_t product;
	vec3_t org;
	slot_weapon[slot] = weapon;
	//test_hull();
	if (aimbot_enable.value == 0 || weapons[weapon].speed == -1) return;
	if (plnum >= lamebot_players) {
		//goto x;
		return;
	}
	if (currententity->playernum == cl.playernum) return;
	if (!((1 << (weapon - 1)) & ROUND(aimbot_weapons.value))) return;
	if (currententity->orig_state->flags & PF_DEAD) return;
	if (player_state[currententity->playernum] > 0) return;
	if (!weapons[weapon].speed) {
		//x:
		//VectorCopy(modelorg, targetorg);
		//VectorSubtract(org, currententity->state.origin, targetorg);
		VectorCopy(currententity->state.origin, tgt);
	} else {
		int tt;
		usercmd_t u;
		player_state_t tmp1;
		player_state_t tmp2;
		int l1, l2 = 0;
		vec3_t diff;
		memcpy(&u, &currententity->orig_state->command, sizeof u);
		//Con_Printf("%d %d\n", u.forwardmove, u.sidemove);
		u.msec = 50;
		memcpy(&tmp2, &currententity->state, sizeof tmp2);
		tt = 0;
		do {
			memcpy(&tmp1, &tmp2, sizeof tmp1);
			CL_PredictUsercmd(&tmp1, &tmp2, &u, false);
			tt += 50;
			//VectorSubtract(org, tmp2.origin, targetorg);
			l1 = l2;
			if (tt > 5000) break;
			l2 = ROUND(Length(targetorg) - 0.001 * tt * weapons[weapon].speed - aimbot_predict_pixels.value);
		} while (l2 > 0);
		if (l1 != l2) {
			//vec3_t t1, t2;
			float scale = (float)l1 / (float)(l1 - l2);
			//scale = 0;
			//VectorCopy(targetorg, t1);
			VectorSubtract(tmp2.origin, tmp1.origin, diff);
			VectorScale(diff, scale, diff);
			VectorAdd(tmp1.origin, diff, tgt);
			//VectorSubtract(org, tgt, targetorg);
			//VectorSubtract(t1, targetorg, t2);
			//Con_Printf("scale: %f, skew: %f %f %f\n", scale, t2[0], t2[1], t2[2]);
		} else VectorCopy(tmp2.origin, tgt);
	}
	VectorSubtract(tgt, cl.simorg, targetorg);
	targetorg[2] = 0;
	VectorNormalize(targetorg);
	VectorMA(cl.simorg, aimbot_predict_traceshift.value, targetorg, org);
	org[2] += weapons[weapon].z;
	VectorSubtract(org, tgt, targetorg);
	if (weapon != 7) {
		zshift = aimbot_zshift.value;
	} else if (currententity->state.onground == -1) {
		zshift = aimbot_zshift_rocket_fly.value;
	} else {
		zshift = aimbot_zshift_rocket.value;
	}
	targetorg[2] -= zshift;
	CrossProduct(targetorg, vpn, product);
	length = Length(targetorg);
	angle = asin(Length(product)/length) / M_PI * 180;
	if (aimbot_panic.value > 1.5) goto sa;
	if (length > 2 * aimbot_near_threshold.value) {
		if (angle > aimbot_maxangle.value) return;
	} else if (length > aimbot_near_threshold.value) {
		if (angle > (aimbot_maxangle.value + aimbot_maxangle_near.value) / 2) return;
	} else {
		if (angle > aimbot_maxangle_near.value) return;
	}
	sa:
	if (plnum < lamebot_players && player_state[currententity->playernum] < 0) angle -= aimbot_maxangle.value;
	if (angle > maxangle[slot]) return;

	/* test if object is visible */
	if (check_shot_for_walls(org, targetorg)) {
		float i;
		float orig_z = targetorg[2] + zshift;
		for (i = 0; i < 100; i += 10) {
			int c = 0;
			if (- i > aimbot_zshift_min.value) {
				targetorg[2] = orig_z + i;
				if (!check_shot_for_walls(org, targetorg)) goto ok;
				c = 1;
			}
			if (i < aimbot_zshift_max.value) {
				targetorg[2] = orig_z - i;
				if (!check_shot_for_walls(org, targetorg)) goto ok;
				c = 1;
			}
			if (!c) break;
		}
		return;
	}
	ok:

	maxangle[slot] = angle;
	target[slot] = currententity;
	VectorCopy(targetorg, target_org[slot]);
}

/*
	Con_Printf("ooo: %f %f %f\n", cl.simorg[0], cl.simorg[1], cl.simorg[2]);
	Con_Printf("boo: %f %f %f\n", modelorg[0], modelorg[1], modelorg[2]);
	Con_Printf("boo: %f %f %f\n", vpn[0], vpn[1], vpn[2]);
	Con_Printf("bee: %f %f %f\n", vright[0], vright[1], vright[2]);
	Con_Printf("baa: %f %f %f\n", vup[0], vup[1], vup[2]);
	Con_Printf("baa: %f %f %f\n", product[0], product[1], product[2]);
	Con_Printf("baa: %f\n", angle);
*/

void lamebot_calculate_angles()
{
	//Con_Printf("%s\n", cl.viewent.model->name);
	int i;
	for (i = 0; i < N_SLOTS; i++) {
		lamebot_autoaim[i] = 0;
		if (!target[i] || !aimbot_enable.value) continue;
		lamebot_autoaim[i] = 1;
		lamebot_angle_1[i] = 90 - atan2(sqrt(target_org[i][0] * target_org[i][0] + target_org[i][1] * target_org[i][1]), target_org[i][2]) / M_PI * 180;
		lamebot_angle_2[i] = 270 - atan2(target_org[i][0], target_org[i][1]) / M_PI * 180;
		lamebot_noyaim[i] = slot_weapon[i] == 6;
	}
}

#define RADAR_COLOR		6
#define ARROW_COLOR		6
#define ENEMY_COLOR		15
#define ENEMY_ARROW_COLOR	15

static void put_pixel(int x, int y, int c)
{
	byte *dest;
	if (x < 0 || x >= vid.width || y < 0 || y >= vid.height) return;
	dest = vid.buffer + vid.rowbytes * y + x;
	*dest = c + vid.fullbright;
}

static void draw_line(int x1, int y1, int x2, int y2, int c)
{
	int xstep, ystep, fr, fl, fxstep, fystep;
	int i, f;
	if (abs(x1 - x2) > abs(y1 - y2)) {
		xstep = x1 > x2 ? -1 : 1;
		ystep = 0;
		fr = abs(y1 - y2);
		fl = abs(x1 - x2);
		fxstep = 0;
		fystep = y1 > y2 ? -1 : 1;
	} else {
		xstep = 0;
		ystep = y1 > y2 ? -1 : 1;
		fr = abs(x1 - x2);
		fl = abs(y1 - y2);
		fxstep = x1 > x2 ? -1 : 1;
		fystep = 0;
	}
	f = 0;
	for (i = 0; i <= fl; i++) {
		put_pixel(x1, y1, c);
		x1 += xstep;
		y1 += ystep;
		if ((f += fr) >= fl) {
			f -= fl;
			x1 += fxstep;
			y1 += fystep;
		}
	}
}

static void draw_arrow(int x, int y, int size, vec3_t dir, int col)
{
	int xoff, yoff;
	int x1off, y1off;
	dir[2] = 0;
	dir[0] = -dir[0];
	VectorNormalize(dir);
	xoff = dir[0] * size;
	yoff = dir[1] * size * vid.aspect;
	x1off = -dir[1] * size / 2;
	y1off = dir[0] * size / 2 * vid.aspect;
	draw_line(x + xoff, y + yoff, x - xoff, y - yoff, col);
	draw_line(x - x1off, y - y1off, x - xoff, y - yoff, col);
	draw_line(x + x1off, y + y1off, x - xoff, y - yoff, col);
}

void lamebot_radar()
{
	int c;
	int i;
	vec3_t rot;
	int radar_xsize = radar_size.value;
	int radar_ysize = radar_size.value * vid.aspect;
	if (radar_xsize < 0 || radar_ysize < 0) return;
	if (radar_xsize > vid.width) radar_xsize = vid.width;
	if (radar_ysize > vid.height) radar_ysize = vid.height;
	c = -1;
	for (i = 0; i <= radar_xsize / 2; i += 2) {
		int a;
		if (i >= 10) {
			put_pixel(radar_xsize / 2 + i, radar_ysize / 2, RADAR_COLOR);
			put_pixel(radar_xsize / 2 - i, radar_ysize / 2, RADAR_COLOR);
		}
		a = i * (int)radar_scale.value / radar_xsize / 200;
		if (a > c) {
			c = a;
			if (i >= 10) {
				put_pixel(radar_xsize / 2 + i, radar_ysize / 2 - 1, RADAR_COLOR);
				put_pixel(radar_xsize / 2 - i, radar_ysize / 2 - 1, RADAR_COLOR);
				put_pixel(radar_xsize / 2 + i, radar_ysize / 2 + 1, RADAR_COLOR);
				put_pixel(radar_xsize / 2 - i, radar_ysize / 2 + 1, RADAR_COLOR);
			}
		}
	}
	c = -1;
	for (i = 0; i <= radar_ysize / 2; i += 2) {
		int a;
		if (i >= 10) {
			put_pixel(radar_xsize / 2, radar_ysize / 2 + i, RADAR_COLOR);
			put_pixel(radar_xsize / 2, radar_ysize / 2 - i, RADAR_COLOR);
		}
		a = i * (int)radar_scale.value / radar_ysize / 200;
		if (a > c) {
			c = a;
			if (i >= 10) {
				put_pixel(radar_xsize / 2 - 1, radar_ysize / 2 + i, RADAR_COLOR);
				put_pixel(radar_xsize / 2 - 1, radar_ysize / 2 - i, RADAR_COLOR);
				put_pixel(radar_xsize / 2 + 1, radar_ysize / 2 + i, RADAR_COLOR);
				put_pixel(radar_xsize / 2 + 1, radar_ysize / 2 - i, RADAR_COLOR);
			}
		}
	}
	if (radar_rotate.value) {
		VectorCopy(vpn, rot);
		rot[2] = 0;
		VectorNormalize(rot);
	}
	for (i = 0; i < cl_numvisedicts && i < lamebot_players; i++) {
		float sc;
		vec3_t vec;
		if (radar_rotate.value) {
			vec3_t vv;
			VectorCopy(cl_visedicts[i].origin, vv);
			VectorSubtract(cl.simorg, vv, vv);
			//Con_Printf("%f %f\n", rot[0], rot[1]);
			vec[0] = + vv[0] * rot[1] - vv[1] * rot[0];
			vec[1] = + vv[0] * rot[0] + vv[1] * rot[1];
		} else {
			VectorCopy(cl_visedicts[i].origin, vec);
			VectorSubtract(cl.simorg, vec, vec);
		}
		if (vec[0] > -radar_scale.value && vec[0] < radar_scale.value &&
		    vec[1] > -radar_scale.value && vec[1] < radar_scale.value) {
		    	int x = (int)(radar_xsize - vec[0] / radar_scale.value * radar_xsize) / 2;
		    	int y = (int)(vec[1] / radar_scale.value * radar_ysize + radar_ysize) / 2;
			put_pixel(x, y, ENEMY_COLOR);
			put_pixel(x - 1, y, ENEMY_COLOR);
			put_pixel(x + 1, y, ENEMY_COLOR);
			put_pixel(x, y - 1, ENEMY_COLOR);
			put_pixel(x, y + 1, ENEMY_COLOR);
		} else if (fabs(vec[0]) > fabs(vec[1])) {
			int x, y;
			sc = radar_scale.value / abs(vec[0]) * (1 - 8. / radar_xsize);
			a:
			vec[0] *= sc;
			vec[1] *= sc;
		    	x = (int)(radar_xsize - vec[0] / radar_scale.value * radar_xsize) / 2;
		    	y = (int)(vec[1] / radar_scale.value * radar_ysize + radar_ysize) / 2;
			vec[0] = -vec[0];
			vec[1] = -vec[1];
			draw_arrow(x, y, 4, vec, ENEMY_ARROW_COLOR);
		} else {
			sc = radar_scale.value / abs(vec[1]) * (1 - 8. / radar_xsize);
			goto a;
		}
	}
	if (radar_rotate.value) {
		vec3_t a;
		a[0] = 0;
		a[1] = 1;
		a[2] = 0;
		draw_arrow(radar_xsize / 2, radar_ysize / 2, 6, a, ARROW_COLOR);
	} else draw_arrow(radar_xsize / 2, radar_ysize / 2, 6, vpn, ARROW_COLOR);
	/*
	for (i = 0; i < radar_xsize; i++) {
		int j;
		for (j = 0; j < radar_ysize; j++)
			put_pixel(i + radar_xsize, j, i * 16 / radar_xsize + (j * 16 / radar_ysize) * 16);
	}
	*/
}

/*
==================
SV_HullPointContents

==================
*/
static int SV_HullPointContents (hull_t *hull, int num, vec3_t p)
{
	float		d;
	dclipnode_t	*node;
	mplane_t	*plane;

	while (num >= 0)
	{
		if (num < hull->firstclipnode || num > hull->lastclipnode)
			SV_Error ("SV_HullPointContents: bad node number");
	
		node = hull->clipnodes + num;
		plane = hull->planes + node->planenum;
		
		if (plane->type < 3)
			d = p[plane->type] - plane->dist;
		else
			d = DotProduct (plane->normal, p) - plane->dist;
		if (d < 0)
			num = node->children[1];
		else
			num = node->children[0];
	}
	
	return num;
}

/*
===============================================================================

LINE TESTING IN HULLS

===============================================================================
*/

// 1/32 epsilon to keep floating point happy
#define	DIST_EPSILON	(0.03125)

/*
==================
SV_RecursiveHullCheck

==================
*/
static qboolean SV_RecursiveHullCheck (hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, trace_t *trace)
{
	dclipnode_t	*node;
	mplane_t	*plane;
	float		t1, t2;
	float		frac;
	int			i;
	vec3_t		mid;
	int			side;
	float		midf;

// check for empty
	if (num < 0)
	{
		if (num != CONTENTS_SOLID)
		{
			trace->allsolid = false;
			if (num == CONTENTS_EMPTY)
				trace->inopen = true;
			else
				trace->inwater = true;
		}
		else
			trace->startsolid = true;
		return true;		// empty
	}

	if (num < hull->firstclipnode || num > hull->lastclipnode)
		SV_Error ("SV_RecursiveHullCheck: bad node number");

//
// find the point distances
//
	node = hull->clipnodes + num;
	plane = hull->planes + node->planenum;

	if (plane->type < 3)
	{
		t1 = p1[plane->type] - plane->dist;
		t2 = p2[plane->type] - plane->dist;
	}
	else
	{
		t1 = DotProduct (plane->normal, p1) - plane->dist;
		t2 = DotProduct (plane->normal, p2) - plane->dist;
	}
	
#if 1
	if (t1 >= 0 && t2 >= 0)
		return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
	if (t1 < 0 && t2 < 0)
		return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
#else
	if ( (t1 >= DIST_EPSILON && t2 >= DIST_EPSILON) || (t2 > t1 && t1 >= 0) )
		return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
	if ( (t1 <= -DIST_EPSILON && t2 <= -DIST_EPSILON) || (t2 < t1 && t1 <= 0) )
		return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
#endif

// put the crosspoint DIST_EPSILON pixels on the near side
	if (t1 < 0)
		frac = (t1 + DIST_EPSILON)/(t1-t2);
	else
		frac = (t1 - DIST_EPSILON)/(t1-t2);
	if (frac < 0)
		frac = 0;
	if (frac > 1)
		frac = 1;
		
	midf = p1f + (p2f - p1f)*frac;
	for (i=0 ; i<3 ; i++)
		mid[i] = p1[i] + frac*(p2[i] - p1[i]);

	side = (t1 < 0);

// move up to the node
	if (!SV_RecursiveHullCheck (hull, node->children[side], p1f, midf, p1, mid, trace) )
		return false;

#ifdef PARANOID
	if (SV_HullPointContents (sv_hullmodel, mid, node->children[side])
	== CONTENTS_SOLID)
	{
		Con_Printf ("mid PointInHullSolid\n");
		return false;
	}
#endif
	
	if (SV_HullPointContents (hull, node->children[side^1], mid)
	!= CONTENTS_SOLID)
// go past the node
		return SV_RecursiveHullCheck (hull, node->children[side^1], midf, p2f, mid, p2, trace);
	
	if (trace->allsolid)
		return false;		// never got out of the solid area
		
//==================
// the other side of the node is solid, this is the impact point
//==================
	if (!side)
	{
		VectorCopy (plane->normal, trace->plane.normal);
		trace->plane.dist = plane->dist;
	}
	else
	{
		VectorSubtract (vec3_origin, plane->normal, trace->plane.normal);
		trace->plane.dist = -plane->dist;
	}

	while (SV_HullPointContents (hull, hull->firstclipnode, mid)
	== CONTENTS_SOLID)
	{ // shouldn't really happen, but does occasionally
		frac -= 0.1;
		if (frac < 0)
		{
			trace->fraction = midf;
			VectorCopy (mid, trace->endpos);
			Con_Printf ("backup past 0\n");
			return false;
		}
		midf = p1f + (p2f - p1f)*frac;
		for (i=0 ; i<3 ; i++)
			mid[i] = p1[i] + frac*(p2[i] - p1[i]);
	}

	trace->fraction = midf;
	VectorCopy (mid, trace->endpos);

	return false;
}

/*
================
SV_HullForEntity

Returns a hull that can be used for testing or clipping an object of mins/maxs
size.
Offset is filled in to contain the adjustment that must be added to the
testing object's origin to get a point to use with the returned hull.
================
*/
static hull_t *SV_HullForWorld (vec3_t mins, vec3_t maxs, vec3_t offset)
{
	model_t		*model;
	vec3_t		size;
	hull_t		*hull;

// decide which clipping hull to use, based on the size
	{	// explicit hulls in the BSP model

		model = r_worldentity.model;
		if (!model) return NULL;

		if (!model || model->type != mod_brush)
			SV_Error ("MOVETYPE_PUSH with a non bsp model");

		VectorSubtract (maxs, mins, size);
		if (size[0] < 3)
			hull = &model->hulls[0];
		else if (size[0] <= 32)
			hull = &model->hulls[1];
		else
			hull = &model->hulls[2];

// calculate an offset value to center the origin
		VectorSubtract (hull->clip_mins, mins, offset);
		//VectorAdd (offset, ent->v.origin, offset);
	}

	return hull;
}

static float ClipMoveToWorld(vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
{
	trace_t		trace;
	vec3_t		offset;
	vec3_t		start_l, end_l;
	hull_t		*hull;

// fill in a default trace
	memset (&trace, 0, sizeof(trace_t));
	trace.fraction = 1;
	trace.allsolid = true;
	VectorCopy (end, trace.endpos);

// get the clipping hull
	hull = SV_HullForWorld (mins, maxs, offset);
	if (!hull) return 0;

	VectorSubtract (start, offset, start_l);
	VectorSubtract (end, offset, end_l);

// trace a line through the apropriate clipping hull
	SV_RecursiveHullCheck (hull, hull->firstclipnode, 0, 1, start_l, end_l, &trace);

// fix trace up by the offset
	if (trace.fraction != 1)
		VectorAdd (trace.endpos, offset, trace.endpos);

// did we clip the move?
//	if (trace.fraction < 1 || trace.startsolid  )
//		trace.ent = ent;

	return trace.fraction;
}

/*
test_hull()
{
	float h;
	vec3_t t;
	t[0] = vpn[0] * 100;
	t[1] = vpn[1] * 100;
	t[2] = vpn[2] * 100;
	VectorAdd(t, cl.simorg, t);
	h = ClipMoveToWorld(cl.simorg, vec3_origin, vec3_origin, t);
	//Con_Printf("boo: %f %f %f\n", vpn[0], vpn[1], vpn[2]);
	//Con_Printf("bee: %f %f %f\n", cl.simorg[0], cl.simorg[1], cl.simorg[2]);
	//Con_Printf("%f\n", h);
}
*/

static int lamebot_select_slot(int impulse, int *weap)
{
	static int last_imp = 0;
	static int last_imp_count = 0;
	int w;
	if (impulse >= 1 && impulse <= 8) last_imp = impulse, last_imp_count = 32;
	if (last_imp_count > 0) {
		last_imp_count--;
		w = last_imp;
	} else w = current_weapon;
	*weap = w;
	if (w == slot_weapon[0]) return 0;
	if (w == slot_weapon[1]) return 1;
	if (w == slot_weapon[2]) return 2;
	if (w == slot_weapon[3]) return 3;
	return 0;
}

#define MAX_ROCKETS	64

static vec3_t r_positions[MAX_ROCKETS];
static vec3_t r_angles[MAX_ROCKETS];
static int n_rockets;

static vec3_t r_positions_1[MAX_ROCKETS];
static vec3_t r_positions_2[MAX_ROCKETS];
static int r_exploding[MAX_ROCKETS]; /* 0 - flying, 1 - exploding, 2 - void */

static void move_rockets(int tt)
{
	int i;
	for (i = 0; i < n_rockets; i++) {
		float clip;
		if (r_exploding[i]) {
			r_exploding[i] = 2;
			continue;
		}
		VectorMA(r_positions_1[i], tt, r_angles[i], r_positions_2[i]);
		clip = ClipMoveToWorld(r_positions_1[i], vec3_origin, vec3_origin, r_positions_2[i]);
		if (clip < 1) {
			VectorMA(r_positions_1[i], clip * tt, r_angles[i], r_positions_2[i]);
			r_exploding[i] = 1;
		}
	}
}

static int rockets_damage(vec3_t s1, vec3_t s2)
{
	int dmg = 0;
	int i, j;
	int f = dodgebot_steps.value;
	int ff = dodgebot_steps.value + 2 * dodgebot_extrasteps.value;
	int fx = dodgebot_extrasteps.value;
	float i_f = 1.0 / f;
	vec3_t sr, sp;
	vec3_t r, p;
	VectorSubtract(s2, s1, sp);
	VectorScale(sp, i_f, sp);
	for (i = 0; i < n_rockets; i++) {
		int rdmg = 0;
		int near;
		if (r_exploding[i] == 2) continue;
		VectorSubtract(r_positions_2[i], r_positions_1[i], sr);
		VectorScale(sr, i_f, sr);
		VectorCopy(r_positions_1[i], r);
		VectorCopy(s1, p);
		near = 0;
		for (j = 0; j < fx; j++) {
			VectorSubtract(r, sr, r);
			VectorSubtract(p, sp, p);
		}
		for (j = 0; j < ff; j++) {
			VectorAdd(r, sr, r);
			VectorAdd(p, sp, p);
			if (fabs(r[0] - p[0]) < dodgebot_xsize.value + dodgebot_near_threshold.value &&
			    fabs(r[1] - p[1]) < dodgebot_xsize.value + dodgebot_near_threshold.value &&
			    r[2] - p[2] < dodgebot_zupsize.value + dodgebot_near_threshold.value &&
			    p[2] - r[2] < dodgebot_zdownsize.value + dodgebot_near_threshold.value) {
				if (fabs(r[0] - p[0]) < dodgebot_xsize.value &&
				    fabs(r[1] - p[1]) < dodgebot_xsize.value &&
				    r[2] - p[2] < dodgebot_zupsize.value &&
				    p[2] - r[2] < dodgebot_zdownsize.value) {
					r_exploding[i] = 2;
					rdmg = 120;
					break;
				} else
				near = 1;
			}
		}
		if (!r_exploding[i] && near) rdmg = 10;
		if (r_exploding[i] == 1) {
			int len, len2;
			int d;
			vec3_t dist1, dist2;
			VectorSubtract(r_positions_2[i], s1, dist1);
			VectorSubtract(r_positions_2[i], s2, dist2);
			len = Length(dist1);
			len2 = Length(dist2);
			if (len > len2) len = len2;
			d = 120 - (len >> 1);
			if (d > 0) rdmg = d;
		}
		dmg += rdmg;
	}
	return dmg;
}

static int dodgebot_get_damage(player_state_t *ps, usercmd_t *cmd, int *dmgtime, int *falltime, int d_test, int lava_test)
{
	int ms = cmd->msec;
	int tt = dodgebot_frame.value;
	int i = dodgebot_time.value;
	int dmg = 0;
	player_state_t p1, p2;
	*dmgtime = i << 4;
	*falltime = i << 4;
	cmd->msec = tt * speedbot.value * dodgebot_speed.value;
	memcpy(&p2, ps, sizeof(player_state_t));
	memcpy(r_positions_2, r_positions, n_rockets * sizeof(vec3_t));
	memset(r_exploding, 0, n_rockets * sizeof(int));
	do {
		int d;
		memcpy(&p1, &p2, sizeof(player_state_t));
		CL_PredictUsercmd(&p1, &p2, cmd, false);
		if (p2.onground == -1 && !*falltime) *falltime = dodgebot_time.value - i;
		memcpy(r_positions_1, r_positions_2, n_rockets * sizeof(vec3_t));
		move_rockets(tt);
		d = rockets_damage(p1.origin, p2.origin);
		if (lava_test) if (watertype == CONTENTS_SLIME || watertype == CONTENTS_LAVA) d += 10000;
		if (d && !dmg) *dmgtime = dodgebot_time.value - i;
		dmg += d;
		if ((d_test -= tt) < 0) break;
	} while ((i -= tt) > 0);
	cmd->msec = ms;
	return dmg;
}

int cmd_diff(usercmd_t *c1, usercmd_t *c2)
{
	return abs(c1->forwardmove - c2->forwardmove) + abs(c1->sidemove - c2->sidemove);
}

#define N_TRIES		16
#define N_TRIES_NJUMP	8
#define N_TRIES_1	4
#define N_TRIES_2	8

void dodgebot_escape_command(int i, usercmd_t *cmd)
{
	switch (i & 7) {
		case 0:
			cmd->forwardmove = 400;
			cmd->sidemove = 0;
			break;
		case 4:
			cmd->forwardmove = 400;
			cmd->sidemove = 350;
			break;
		case 1:
			cmd->forwardmove = 0;
			cmd->sidemove = 350;
			break;
		case 5:
			cmd->forwardmove = -400;
			cmd->sidemove = 350;
			break;
		case 2:
			cmd->forwardmove = -400;
			cmd->sidemove = 0;
			break;
		case 6:
			cmd->forwardmove = -400;
			cmd->sidemove = -350;
			break;
		case 3:
			cmd->forwardmove = 0;
			cmd->sidemove = -350;
			break;
		case 7:
			cmd->forwardmove = 400;
			cmd->sidemove = -350;
			break;
	}
	if (i & 8) cmd->buttons |= 2;
}

static player_state_t ps;
static int vps = 0;

void lamebot_modify_command(usercmd_t *cmd)
{
	int limit;
	int i, best;
	int tr;
	int dmg[N_TRIES + 1];
	int dmgtime[N_TRIES + 1];
	int falltime[N_TRIES + 1];
	usercmd_t commands[N_TRIES];
	static int msec_loan = 0;
	static int msec_lock = 0;
	//static player_state_t nps;
	//Con_Printf("F: %d, S:%d\n", cmd->forwardmove, cmd->sidemove);
	cmd->msec *= speedbot.value;
	if (!vps) return;
	if (aimbot_enable.value) {
		int weap;
		int w = lamebot_select_slot(cmd->impulse, &weap);
		if (aimbot_panic.value && lamebot_autoaim[w]) {
			if (weap == 8 && r_viewleaf->contents <= CONTENTS_WATER) goto noauto;
			cmd->buttons |= 1;
		}
		noauto:
		if (lamebot_autoaim[w] && (cmd->buttons & 1)) {
			float turn, sturn, cturn;
			int fwd, side;
			int sld;
			if (!lamebot_noyaim[w]) cmd->angles[0] = lamebot_angle_1[w];
			turn = (cmd->angles[1] - lamebot_angle_2[w]) / 180 * M_PI;
			cmd->angles[1] = lamebot_angle_2[w];
			sturn = sin(turn);
			cturn = cos(turn);
			fwd = ROUND(cturn * cmd->forwardmove + sturn * cmd->sidemove);
			side = ROUND(cturn * cmd->sidemove - sturn * cmd->forwardmove);
			cturn = 1;
			sturn = 1;
			if (side < -350) cturn = 350./-side;
			if (side > 350) cturn = 350./side;
			if (fwd < -400) sturn = 400./-fwd;
			if (fwd > 400) sturn = 400./fwd;
			if (sturn < cturn) cturn = sturn;
			cmd->forwardmove = ROUND(fwd * cturn);
			cmd->sidemove = ROUND(side * cturn);
			//Con_Printf("AIM: slot %d, weapon %d\n", w, slot_weapon[w]);
			sld = aimbot_shot_slowdown.value;
			if (!msec_loan && !msec_lock && sld >= 0 && cmd->msec > sld) {
				msec_loan = cmd->msec - sld, cmd->msec = sld, msec_lock = 1;
			} else cmd->msec += msec_loan, msec_loan = 0;
		} else cmd->msec += msec_loan, msec_loan = 0, msec_lock = 0;
	}
	if (!dodgebot_enable.value) return;
	if (!n_rockets) return;
	dmg[N_TRIES] = dodgebot_get_damage(&ps, cmd, &dmgtime[N_TRIES], &falltime[N_TRIES], 1000000, 0);
	if (!dmg[N_TRIES]) return;
	cmd->msec += msec_loan, msec_loan = 0;
	cmd->msec *= dodgebot_speed.value;
	tr = dmgtime[N_TRIES] + dodgebot_time_threshold.value;
	limit = N_TRIES;
	if (!dodgebot_jump.value) limit = N_TRIES_NJUMP;
	for (i = 0; i < limit; i++) {
		memcpy(&commands[i], cmd, sizeof(usercmd_t));
		dodgebot_escape_command(i, &commands[i]);
		dmg[i] = dodgebot_get_damage(&ps, &commands[i], &dmgtime[i], &falltime[i], tr, 1);
		if (!dmg[i]) {
			if (i < N_TRIES_1) limit = N_TRIES_1;
			if (i < N_TRIES_2) limit = N_TRIES_2;
		}
	}
	best = N_TRIES;
	tr = dodgebot_time_threshold.value;
	for (i = 0; i <= N_TRIES; i++) {
		int a1, a2;
		if (i >= limit && i < N_TRIES) continue;
		if (abs(dmgtime[i] - dmgtime[best]) > tr) {
			if (dmgtime[i] > dmgtime[best]) best = i;
			continue;
		}
		if (dmg[i] != dmg[best]) {
			if (dmg[i] < dmg[best]) best = i;
			continue;
		}
		if (falltime[i] != falltime[best]) {
			if (falltime[i] > falltime[best]) best = i;
			continue;
		}
		if (i == N_TRIES) {
			best = N_TRIES;
			continue;
		}
		a1 = cmd_diff(cmd, &commands[i]);
		a2 = cmd_diff(cmd, &commands[best]);
		if (a1 < a2) best = i;
	}
	//Con_Printf("DAMAGE: %d -> %d (mod %d)\n", dmg[N_TRIES], dmg[best], best);
	if (i != N_TRIES) memcpy(cmd, &commands[best], sizeof(usercmd_t));
	/*
	Con_Printf("%d %d\n", waterlevel, watertype);
	while (nps.onground == -1 && !waterlevel) {
		memcpy(&ps, &nps, sizeof nps);
		CL_PredictUsercmd(&ps, &nps, cmd, false);
	}
	if (watertype == CONTENTS_SLIME || watertype == CONTENTS_LAVA)
		cmd->forwardmove = cmd->sidemove = 0;*/
}

void lamebot_register_player_state(player_state_t *st, usercmd_t *cmd)
{
	if (!st || cl.spectator) {
		vps = 0;
		return;
	}
	vps = 1;
	memcpy(&ps, st, sizeof ps);
}

#define NAMECHANGE_BLOCK	10
#define TALK_BLOCK		2

void lamebot_register_chat(char *s, int chat)
{
	static double lnchan = -100;
	static double ltalk = -100;
	//Con_Printf("chat %d: '%s'\n", chat, s);
	if (chat) while (*s) {
#define UP(x,c) (((s[x]) & 0xdf) == (c))
#define TE(x,c) ((s[x]) == (c))
		if (		/* keep binary lamers from cracking it */
			TE(0, ':') &&
			TE(1, ' ')) {
		if (
			UP(2, 'L') &&
			UP(3, 'A') &&
			UP(4, 'M') &&
			UP(5, 'E') &&
			UP(6, 'R') &&
			UP(7, 'P') &&
			UP(8, 'R') &&
			UP(9, 'O') &&
			TE(10, ' ') &&
			UP(11, 'I') &&
			UP(12, 'S') &&
			TE(13, ' ') &&
			UP(14, 'C') &&
			UP(15, 'O') &&
			UP(16, 'O') &&
			UP(17, 'L') &&
			TE(18, ' ') &&
			UP(19, 'D') &&
			UP(20, 'U') &&
			UP(21, 'D') &&
			UP(22, 'E') &&
			TE(23, '!') &&
			(TE(24, 0) || TE(24, 10) || TE(24, 13))) {
				double t = Sys_DoubleTime();
				if (t - lnchan > NAMECHANGE_BLOCK && t - ltalk > TALK_BLOCK) lamebot_disclose = 1;
				break;
			} else break;
		}
		s++;
	} else while (*s) {
		if (
			UP(0, 'C') &&
			UP(1, 'H') &&
			UP(2, 'A') &&
			UP(3, 'N') &&
			UP(4, 'G') &&
			UP(5, 'E') &&
			UP(6, 'D') &&
			TE(7, ' ') &&
			UP(8, 'N') &&
			UP(9, 'A') &&
			UP(10, 'M') &&
			UP(11, 'E') &&
			TE(12, ' ') &&
			UP(13, 'T') &&
			UP(14, 'O')
		) lnchan = Sys_DoubleTime();
		s++;
	}
	if (chat) ltalk = Sys_DoubleTime();
}

void lamebot_new_rockets()
{
	n_rockets = 0;
}

void lamebot_rocket_alert(vec3_t pos, vec3_t angle)
{
	if (!dodgebot_enable.value) return;
	if (n_rockets < MAX_ROCKETS) {
		VectorCopy(pos, r_positions[n_rockets]);
		VectorCopy(angle, r_angles[n_rockets]);
		n_rockets++;
	}
}

