// @unloadnpc antibot
// @loadnpc npc/thor/bot.txt

-	script	antibot	-1,{
OnPCLoginEvent:
	if ( !checkvending()) {
		.@timenow = gettimetick(2);								if(.debug) dispbottom "[PCLogin Event] Time Now: "+.@timenow;
		.@lastcheck = #bot_verification_last;							if(.debug) dispbottom "[PCLogin Event] Last Checked: "+.@lastcheck;
		if (!.@lastcheck) {
			#bot_verification_last = .@timenow;						if(.debug) dispbottom "[PCLogin Event] Set Last: "+#bot_verification_last;			
			.@remainingtime = .check_minute * 60;						if(.debug) dispbottom "[PCLogin Event] Time Left: "+.@remainingtime/60+" minutes";	
		} else {
			.@nextcheck = .@lastcheck + (.check_minute * 60);				if(.debug) dispbottom "[PCLogin Event] Next Check: "+.@nextcheck;
			if( .@nextcheck - .@timenow < 0) {
				.@remainingtime = 0;							if(.debug) dispbottom "[PCLogin Event] Time Left: "+.@remainingtime+" minutes";	
			} else {
				.@remainingtime = .@nextcheck - .@timenow;				if(.debug) dispbottom "[PCLogin Event] Time Left: "+.@remainingtime/60+" minutes";	
			}
		}
		if ( #BotAnswer$ == "") {
			addtimer ( .@remainingtime * 600),.npc_name$+"::OnCheck";			if(.debug) dispbottom "[PCLogin Event] Adding Check timer "+(.@remainingtime/60)+" minutes.";
													if(.debug) dispbottom "[PCLogin Event] No Verifiication Code. Cheking in "+.@remainingtime/60+" minutes.";
		} else {
			doevent .npc_name$+"::OnCheck";							if(.debug) dispbottom "[PCLogin Event] Player has Verification Code ( A:"+#BotA+" B:"+#BotB+" C:"+#BotC+" Ans:"+#BotAnswer$+") process checking now now";
		}
	}
	end;

OnCheck:
	if ( #BotAnswer$ == "" ) {
		#BotA = "" + rand( .bot_code_range[0], .bot_code_range[1] );				
		#BotB = "" + rand( .bot_code_range[0], .bot_code_range[1] );				
		switch( rand(2) ){
		case 1:
			set #BotC,1; 
			set #BotAnswer$,(#BotA + #BotB);						if(.debug) dispbottom "[OnCheck] NO CODE: Generating code: A:"+#BotA+" B:"+#BotB+" C:"+#BotC+" Ans:"+#BotAnswer$;
			break;
		case 2: 
			if (#BotA >= #BotB) {
				set #BotC,2; 
				set #BotAnswer$,(#BotA - #BotB);					if(.debug) dispbottom "[OnCheck] NO CODE: Generating code: A:"+#BotA+" B:"+#BotB+" C:"+#BotC+" Ans:"+#BotAnswer$;
				break;
			} else {
				set #BotC,3; 
				set #BotAnswer$,(#BotB - #BotA);					if(.debug) dispbottom "[OnCheck] NO CODE: Generating code: A:"+#BotA+" B:"+#BotB+" C:"+#BotC+" Ans:"+#BotAnswer$;
				break;
			}
		default:
			if (#BotA >= #BotB) {
				set #BotC,2; 
				set #BotAnswer$,(#BotA - #BotB);					if(.debug) dispbottom "[OnCheck] NO CODE: Generating code: A:"+#BotA+" B:"+#BotB+" C:"+#BotC+" Ans:"+#BotAnswer$;
				break;
			} else {
				set #BotC,3; 
				set #BotAnswer$,(#BotB - #BotA);					if(.debug) dispbottom "[OnCheck] NO CODE: Generating code: A:"+#BotA+" B:"+#BotB+" C:"+#BotC+" Ans:"+#BotAnswer$;
				break;
			}
		}
		#bot_verification_timer = .verify_minute;						if(.debug) dispbottom "[OnCheck] NO CODE: #bot_verification_timer is set to "+#bot_verification_timer;
		addtimer 60000,.npc_name$+"::OnFail";							if(.debug) dispbottom "[OnCheck] Adding 1 minute to OnFail";
		end;
	}
	addtimer 60000,.npc_name$+"::OnFail";								if(.debug) dispbottom "[OnCheck] Adding 1 minute to OnFail";
	end;
	
OnVerify:
	.@skip = ( getmapflag(strcharinfo(PC_MAP), mf_noteleport) || getmapflag(strcharinfo(PC_MAP), mf_nopenalty) || checkvending() || checkchatting() );
	if ( !.@skip && #bot_verification_timer && #BotAnswer$ != "" ) {
		if ( .@atcmd_parameters$ !=#BotAnswer$ ) {
			#BotTry ++;
			announce "That is not the correct code. Let's try again ("+#BotTry+")",bc_self,0xFFA500;
			.@skip = ( getmapflag(strcharinfo(PC_MAP), mf_nopenalty) || checkvending() || checkchatting() );
			if (!.@skip && #BotTry == 3) {
				set #BotTry,0;
				deltimer .npc_name$+"::OnFail";						if(.debug) dispbottom "[OnVerify] Deleting OnFail timer.";
				deltimer .npc_name$+"::OnCheck";					if(.debug) dispbottom "[OnVerify] Deleting OnCheck timer.";
				getmapxy(.@map$, @x, @y, 0);
				set mlastmap$,.@map$;
				set mlastx,@x;
				set mlasty,@y;
				set mhp,Hp;
				set msp,Sp;
				warp "office",rand(277,281),rand(161,172);
				end;
			}

		} else  {
			set #BotTry,0;
			announce "Thank you for your cooperation. Have a nice day!!!",bc_self,0xFFA500;
			// Give INC AGI			
			if(getstatus(SC_INC_AGI) <=0) {
				if(Hp>15) {
					skilleffect AL_INCAGI,0;
					sc_start SC_INC_AGI,240000,10;
					heal -15,0;
				}
			}
			// Give BLESSING
			skilleffect AL_BLESSING,0;
			sc_start SC_BLESSING,240000,10;
						
			deltimer .npc_name$+"::OnFail";					if(.debug) dispbottom "[OnVerify] Deleting OnFail timer.";
			deltimer .npc_name$+"::OnCheck";				if(.debug) dispbottom "[OnVerify] Deleting OnCheck timer.";

			#BotAnswer$ = "";						if(.debug) dispbottom "[OnVerify] Set verification code to none.";
			#bot_verification_timer = 0;					if(.debug) dispbottom "[OnVerify] Set verification timer to zero.";
			#bot_verification_last = gettimetick(2);			if(.debug) dispbottom "[OnVerify] Set verification time "+#bot_verification_last;
			
			// Reset Timers
			.@timenow = gettimetick(2);					if(.debug) dispbottom "[OnVerify] Time: "+.@timenow;
			.@lastcheck = #bot_verification_last;				if(.debug) dispbottom "[OnVerify] Last: "+.@lastcheck;
			.@nextcheck = .@timenow + (.check_minute * 60);			if(.debug) dispbottom "[OnVerify] Next: "+.@nextcheck;
			.@remainingtime = .@nextcheck-.@timenow;			if(.debug) dispbottom "[OnVerify] Left: "+.@remainingtime/60+" minutes";
			addtimer ( .@remainingtime * 600 ),.npc_name$+"::OnCheck";	if(.debug) dispbottom "[OnVerify] No Verifiication Code. Cheking in "+.@remainingtime/60+" minutes.";
		}
	}
	end;
	
OnFail:
	.@skip = ( getmapflag(strcharinfo(PC_MAP), mf_noteleport) || getmapflag(strcharinfo(PC_MAP), mf_nopenalty) || checkvending() || checkchatting() );
	if (!.@skip && #bot_verification_timer <= 0) {
		deltimer .npc_name$+"::OnFail";					if(.debug) dispbottom "[OnVerify] Failed to @verify. Deleting OnFail timer.";
		deltimer .npc_name$+"::OnCheck";				if(.debug) dispbottom "[OnVerify] Failed to @verify. Deleting OnCheck timer.";
		#bot_verification_timer = 0;					if(.debug) dispbottom "[OnVerify] Failed to @verify. Set verification timer to zero.";
		getmapxy(.@map$, @x, @y, 0);
		set mlastmap$,.@map$;
		set mlastx,@x;
		set mlasty,@y;
		set mhp,Hp;
		set msp,Sp;
		warp "office",rand(277,281),rand(161,172);
		end;
	}
	if ( !.@skip ) {
		if (#BotC==1) {
			announce "("+#bot_verification_timer+") Please verify.  If [ A = "+#BotA+" and B = "+#BotB+" ]. Solve: A + B? Type @verify answer",bc_self,0xFFA500;
		}
		if( #BotC==2) { 
			announce "("+#bot_verification_timer+") Please verify.  If [ A = "+#BotA+" and B = "+#BotB+" ]. Solve: A - B? Type @verify answer",bc_self,0xFFA500;
		}
		if ( #BotC==3) {
			announce "("+#bot_verification_timer+") Please verify.  If [ A ="+#BotA+" and B ="+#BotB+" ]. Solve: B - A? Type @verify answer ",bc_self,0xFFA500;
		}
		#bot_verification_timer--;						if(.debug) dispbottom "[OnFail] IN FIELD/DUNGEON. Setting timer DOWN ("+#bot_verification_timer+")";
	}
	addtimer 60000,.npc_name$+"::OnFail";						if(.debug) dispbottom "[OnFail] Trying to check in 1 minute. ("+#bot_verification_timer+")";
	end;

OnInit:
	setarray .bot_code_range,1,20;
	.check_minute = 20;
	.verify_minute = 10;
	.debug=0;
	.npc_name$ = strnpcinfo(3);
	bindatcmd( "verify",.npc_name$ + "::OnVerify",0,99 );
	end;


}