// nva_ = NonVoristAi_

function examine_getCorrectNumberGrammar(%num) {
	%tbl = "first second third fourth fifth sixth seventh eighth ninth";
	return ((%num - 1) >= getWordCount(%tbl) ? %num : getWord(%tbl, %num));
}


function AIPlayer::wantToExamine(%this, %obj) {
	if(!isObject(%obj) || %obj.getClassName() !$= "AIPlayer" || %obj.getState() $= "Dead" || %this.getState() $= "Dead" || %this.inMouth || isObject(%this.heldBy) || !%this.isNotVorist) {
		%this.loopMarcAI();
		return;
	}

	if(isEventPending(%cs = %this.loopMarcAISchedule)) cancel(%cs);
	if(isEventPending(%cs = %this.wantToEat)) if(!%obj.answeredAll[%this]) cancel(%cs); else return;
	
	%cso = %this.canSeeObject(%obj);
	
	%canPickup = (%cso == 1 && !%obj.answeredAll[%this] && !isObject(%obj.talkingTo));
	%canSeeObj = (%cso != 0);
	
	// If we aren't holding them, then we cannot examine them, now can we? Let's pick them up.
	if(%this.talkingTo != %obj) {
		// We're trying to question any tiny we can here.
		%closestPlayer = %this.findClosestPlayer();
		if(%this.canSeeObject(getWord(%closestPlayer, 1))) {
			if(getWord(%closestPlayer, 1) != %obj) {
				if(getWord(%closestPlayer, 0) < 8 * getWord(%this.getScale(), 2)) {
					if(%this.sizeDiff(getWord(%closestPlayer, 1)) $= "smaller") {
						%obj = getWord(%closestPlayer, 1);
					}
				}
			}
		}
		
		// We cannot see them...
		if(!%canSeeObj && %canPickup) {
			if(%this.nva_find[%obj] == 0) %this.say("Wait! All I want to do is ask you some questions!");
			%this.nva_find[%obj]++;
			%this.setAimLocation(vectorAdd(vectorScale(vectorCross(%this.getForwardVector(), %this.getUpVector()), 2), %this.getEyePoint()));
			
			if(%this.nva_find[%obj]++ >= 10) {
				%this.say("Aw, I really wanted to just talk to you.. Not today, I guess.");
				%this.nva_find[%obj] = 1;
				%this.loopMarcAI();
				return;
			}
		}
		
		if(%this.nva_find[%obj] != 0 && %canPickup) {
			%this.nva_find[%obj] = 0;
			%this.say("Hey! There you are! I promise I won't hurt you!");
		}
		
		if(!isObject(%gotoObj = %obj.heldBy)) %gotoObj = %obj;
		
		%this.setMoveY(0.6);
		%this.movementLogicCheck();
		
		%this.lookAtPlayer(%gotoObj);
		
		%dist = (%obj.answeredAll[%this] && %cso != 2 && %gotoObj == %obj ? 20 : 2.1) * getWord(%this.getScale(), 2);
		
		//                                                                                                                       V - If we've already asked all we can to this tiny, then watch them from a safe distance.
		if(vectorDist(%this.getPosition(), %gotoObj.getPosition()) < %dist) {
			%movY = 0;
			
			// Backup from them if we've cornered them
			if(%obj.movey_speed != 0) {
				if(mFloatLength(getWord(%p = %obj.getPosition(), 0), 2) == mFloatLength(getWord(%this.lastPos[%obj], 0), 2) && mFloatLength(getWord(%p = %obj.getPosition(), 1), 2) == mFloatLength(getWord(%this.lastPos[%obj], 1), 2) && mFloatLength(getWord(%p, 2), 2) >= mFloatLength(getWord(%this.lastPos[%obj], 2), 2)) {
					%this.lastPosCnt[%obj]++;
				} else { %this.lastPosCnt[%obj] = 0; %this.lastPos[%obj] = %obj.getPosition(); }
				
				if(%this.lastPosCnt[%obj] >= 6 && vectorDist(%this.getPosition(), %gotoObj.getPosition()) < %dist - 1) {
					%movY = -0.4;
				}
				
				if(%this.lastPosCnt[%obj] == 100) {
					%this.say("If you don't want to talk, just tell me! But please, stop running; I feel like some sort of monster..");
					schedule(500, 0, eval, %obj @ ".runAway = 0;");
				}
			}
			
			%this.setMoveY(%movY);
			
			if(%canPickup && %gotoObj == %obj && %obj.movey_speed == 0) {
				if(%this.talkingTo == %obj && %obj.talkingTo = %this) { %this.setActionThread("sit", 1); %this.schedule(32, wantToExamine, %obj); return; }
				
				%this.talkingTo        = %obj;
				%obj.talkingTo         = %this;
				%this.lastPosCnt[%obj] = 0;
				
				%this.lookAtPlayer(%obj);
				%obj.setAimObject(%this);
				%obj.setMoveY(0);
				
				%this.setActionThread("sit", 1);
				%this.say("Is it alright if I pick you up? I just want to talk.");
				%answer = (getRandom((%obj.conf[%this] >= 10 ? 4000 : 0), 10000) > 8000);
				
				%obj.schedule(%delay = getRandom(1000, 2500), say, (%answer ? "Y... Yes." : "N-N... N-No..."));
				%this.schedule(%delay = (%delay + getRandom(1200, 2000)), say, (%answer ? "Yay! I'll be gentle, I promise!" : "Aww.. Okay."));
				
				if(%answer) {
					%this.schedule(%delay = (%delay + getRandom(600, 1000)), holdOtherPlayer, %obj);
					%this.schedule(%delay, setHeadAngle, -0.8);
					%this.schedule(%delay, clearAim);
				}
				
				%this.schedule(%delay, wantToExamine, %obj);
				return;
			}
		}
	} else {
		if((isObject(%hb = %obj.heldBy) && %hb != %this) || vectorDist(%this.getPosition(), %obj.getPosition()) > 2.3 * getWord(%this.getScale(), 2) || %obj.talkingTo != %this) {
			%this.talkingTo = "";
			
			cancel(%this.wantToExamine);
			%this.schedule(32, wantToExamine, %obj);
			return;
		}
		
		%cf = %obj.conf[%obj]; // Our tiny's confidence in us
		
		%q[-1 + %qCnt++] = "I wonder how many times you have to deal with predators. It must be hard!";
		%a[-1 + %aCnt++] = (%cf < 5 ? "Y-Yes. It's v-very diffic... difficult." : (%cf >= 10 ? "Yeah, it's very hard for our kind to survive in this world." : "Yea-h. It's very difficult."));
		%c[-1 + %cCnt++] = 2; // Confidence consequence -- What gets added / subtracted from the bot's confidence whilst they are "thinking" about an answer.
		
		%q[-1 + %qCnt++] = "Are you scared of me?" @ (%cf < 10 ? "\tWell, please, don't be! I'm not going to hurt you." : "");
		%a[-1 + %aCnt++] = (%cf < 5 ? "(" @ %obj.firstName @ " nods their head.)" : (%cf >= 10 ? "Not anymore." : "Y... Yes.."));
		%c[-1 + %cCnt++] = 3;
		
		%q[-1 + %qCnt++] = "What's it like being a tiny?";
		%a[-1 + %aCnt++] = (%cf < 5 ? "U-uhm... Tough. I-It's tough." : (%cf >= 10 ? "It's just another day for me. We have tons to do at the village. We've all got to pitch in; no freeloaders, that's for sure." : "It's a p-pain. Because of the predators and stuff."));
		%c[-1 + %cCnt++] = 3;
		
		%cq = getRandom(0, %qCnt - 1);
		if(strIPos(%obj.answered[%this], "_" @ %cq @ "_") != -1) {
			for(%cq=0;%cq<=%qCnt;%cq++) {
				if(%cq == %qCnt) {
					%this.say("I don't know what else to ask, so... I'll just let you go now. Run along, now.");
					%obj.answeredAll[%this] = 1;
					%obj.talkingTo          = "";
					%this.talkingTo         = "";
					%this.schedule(400, holdOtherPlayer);
					%this.clearAim();
					%this.setHeadAngle(0);
					
					cancel(%this.wantToExamine);
					%this.schedule(32, wantToExamine, %obj);
					return;
				}
				
				if(strIPos(%obj.answered[%this], "_" @ %cq @ "_") == -1) break;
			}
		}
		
		%qText = %q[%cq];
		%aText = %a[%cq];
		%cValu = %c[%cq];
		%delay = (27 * getWordCount(getField(%qText, 0))) + getRandom(2000, 3000);
		
		%this.say(getField(%qText, 0), 1);
		
		%obj.answered[%this] = %obj.answered[%this] @ "_" @ %cq @ "_";
		%obj.conf[%obj]     += %cValu;
		%obj.schedule(%delay, say, %aText, 1);
		
		for(%i=1;%i<getFieldCount(%qText);%i++) {
			%this.schedule(%delay, say, getField(%qText, %i));
			
			%delay += (27 * getWordCount(getField(%qText, %i)));
		}
		
		cancel(%this.wantToExamine);
		%this.schedule(%delay + getRandom(12000, 20000), wantToExamine, %obj);
		return;
	}
	
	cancel(%this.wantToExamine);
	%this.schedule(32, wantToExamine, %obj);
}

function AIPlayer::respondToExaminor(%this, %answer) {
	%this.say(%answer, 1);
}