// A "follow the laser sight" exercise. Requires, in addition to the seeking missile,
// two puffs: one for the missile to track and one that constantly sets the former puff's
// position - it'd be a useless laser sight if it didn't move!
// By Zhs2.

#library "FollowTheYellowBrickRoadDorothy"
#include "zcommon.acs"

#libdefine TRACKTID_RANGESTART 15000
#libdefine PLAYERIDRANGE 32 // Up to this many players can have missiles with the same identifier, ideally the number supported by the engine

script "ApertureScienceProjectileTracerAssignmentScript" (void) // Missile script
{
	int identifier = GetUserVariable(0, "user_identifier"); // in case of multiple targeters
	int tempTID = UniqueTID(3264); // Create a new TID... (We search from a certain bottom up because we do not want other very temporary IDs to be randomly selected)
	Thing_ChangeTID(0, tempTID); // Set an ID for the projectile...
	SetActivatorToTarget(0); // Change over this script's activator to the player...
	int thisPlayer = PlayerNumber(); // Get his numeric identifier...
	SetActivator(tempTID); // Return to the missile...
	SetPointer(AAPTR_TRACER, TRACKTID_RANGESTART+thisPlayer+(identifier*PLAYERIDRANGE)); // And then set the tracer to our resultant target! Guide away!
	// This can call once or repeatedly. Shouldn't hurt anything either way as they will only be tracking the same target over. I hope.
}

script "ApertureSciencePositionPuffSetter" (int x, int y, int z) // Called in sequence by a lot of positioner puffs
{
	int identifier = GetUserVariable(0, "user_identifier"); // in case of multiple targeters
	SetActivatorToTarget(0); // Get the player's number
	int thisPlayer = PlayerNumber(); // Aaah
	SetActorPosition(TRACKTID_RANGESTART+thisPlayer+(identifier*PLAYERIDRANGE), x, y, z, 0); // Then set the targeter position
}

script "ApertureScienceTrackerTIDAssigner" (void) // Target script, sets TID
{
	int identifier = GetUserVariable(0, "user_identifier"); // Multiple targeters, captain?
	int thisPlayer; // We need the player's number
	int tempTID = UniqueTID(7395); // Set a temp TID
	Thing_ChangeTID(0, tempTID); // Change this thing's TID
	SetActivatorToTarget(0); // Return to player
	thisPlayer = PlayerNumber(); // Cornflakes
	Thing_ChangeTID(tempTID, TRACKTID_RANGESTART+thisPlayer+(identifier*PLAYERIDRANGE)); // Oh yes that's good
}

script "ApertureScienceTrackerDestructor" (int identifier) // Target removal script
{
	int thisPlayer = PlayerNumber(); // This script starts at the player and should be called in the weapon.
	SetUserVariable(TRACKTID_RANGESTART+thisPlayer+(identifier*PLAYERIDRANGE), "user_dead", 1); // Tell the target to fuck off!
}