# Rbots 'botcam' cog - Alpha 0.30
#
# Created by Raynar

flags=0x240

symbols

message		activated
message		startup
message		trigger
message		timer

int			old_camera		local
thing		player			local
thing		rbot			local
int			botid			local
int			botname			local
int			numbots			local
int			active			local

end

# Code Section

code

startup:
	player=GetLocalPlayerThing();
	botid=-1;
	numbots=1;
	active=0;
	return;

activated:
	if (botid != -1) SendTrigger(1, 100011, botid, -1, -1, -1); // tell current bot to switch off stats
	botid=botid+1;
	active=0;
	SendTrigger(-1, 960, botid, -1, -1, -1); // ask bot for thing number
	SetTimerEx(1.0,110,-1,-1); // check if bot has responded after 2 sec
	return;

trigger:
	if (getsourceref()==970 && getparam(0)==botid) // bot returns thing number
	{
		KillTimerEx(110);
		rbot=getparam(1);
		botname=getparam(2);
		active=1;
		SendTrigger(-1, 100010, botid, -1, -1, -1); // tell bot to display stats
		SetActorFlags(player, 0x800000); // turn off HUD
		old_camera = GetCurrentCamera();
		SetCameraFocus(0, rbot);
		SetCameraFocus(1, rbot);
		SetCurrentCamera(1);
		jkstringclear();
		jkStringConcatAsciiString("Botcam - ");
		jkStringConcatUNIString(-1, 1000+botname);
		jkStringOutput();
	}
	if (getsourceref() == 1000 && getparam(0)==botid) // if botid 0 dies, change camera back to player temporarily after 3 secs
	{
		SetTimerEx(3.0,100,-1,-1); // turn of HUD
	}
	return;

timer:
	if (GetSenderId() == 100)
	{
		ClearActorFlags(player, 0x800000); // turn off HUD
		SetCameraFocus(0, player);
		SetCameraFocus(1, player);
		SetCurrentCamera(old_camera);
	}
	else if (GetSenderId() == 110) // bot has not responded - turn botcam off
	{
		if (active) return; // hack
		KillTimerEx(110);
		botid=-1;
		jkstringclear();
		jkStringConcatAsciiString("Botcam - off");
		jkStringOutput();
		ClearActorFlags(player, 0x800000); // turn off HUD
		SetCameraFocus(0, player);
		SetCameraFocus(1, player);
		SetCurrentCamera(old_camera);
	}
	return;

end
