#library "DragCorpsesScript"

#include "zcommon.acs"

bool dragging = false;
bool already_dragging = false;
// This is used to make certain things happen 

int counter = 0;

// script "DragCorpsesScript" ENTER
// {
// 	while(TRUE)
// 	{
// 		if (dragging == false)
// 		{ // fast skip if inactive
// 			Delay(1);
// 			continue;
// 		}
// 		else{
// 			SetFont("BDCRA1");
// 			HudMessage(s:"A"; HUDMSG_PLAIN, 0, 0, 0.1, 0.8, 0.0);
// 			counter++;
// 		}
// 	}
// }


script "toggleDrag" (void)
{
	int angle = GetActorAngle(0);
	int pitch = GetActorPitch(0);
	

	int actorMask = MF_CORPSE;
	int wallMask  = ML_BLOCKEVERYTHING | ML_BLOCKHITSCAN;
	int max_distance = 100.0;

	if (dragging == true)
	{
		dragging = false;
		already_dragging = false;
		PlaySound(0, "bodydump");

		int radius = 75;
		int sn = sin(angle + 0.25);
		int cs = cos(angle + 0.25);

		int x = GetActorX(0) + sn * radius;
		int y = GetActorY(0) - cs * radius;
		int z = GetActorZ(0);
		SetActorProperty(0,APROP_SPEED,1.0);
		Spawn("OfficeCorpse", x, y, z, 0);
		HudMessage(s:"You stash the woman's body on the ground"; HUDMSG_FADEOUT, 999, 0, 0.1, 0.8, 0.5);
	}
	else
	{
	

 		int old_tid = PickActor(0, angle, pitch, max_distance, 0, actorMask, wallMask, PICKAF_RETURNTID);
		int new_tid = UniqueTID();


		if(PickActor(0, angle, pitch, max_distance, new_tid, actorMask, wallMask, PICKAF_FORCETID)){
				dragging = true;
				Thing_Remove(new_tid);
		}

		SetActorProperty(0,APROP_SPEED,0.25);
// 		int angle = GetActorAngle(0);
// 		int pitch = GetActorPitch(0);
// 		int actorMask = MF_CORPSE | MF_SPECIAL;
// 		int wallMask  = ML_BLOCKEVERYTHING | ML_BLOCKHITSCAN;
// 		int max_distance = 100.0;

		//NOTE:	PickActor requires careful handling. See the zdoom wiki about that!
		//		Actor's old TID needs to be saved, assigned a new TID, then restored back to old TID to keep things operational.
		old_tid = PickActor(0, angle, pitch, max_distance, 0, actorMask, wallMask, PICKAF_RETURNTID);
		str carry_image = rollForImage();
		SetFont(carry_image);
		HudMessage(s:"A"; HUDMSG_PLAIN, 999, 0, 0.34, 0.8, 0.0);
		rollForSound(20);
	}
}


function void rollForSound(int chance)
{	
	str grunts[5] = {"grunt1", "grunt2", "grunt3", "grunt5"};
    int roller = Random (1, chance);
// 	if  (roller == 7)
// 	{
// 		PlaySound(0, "fartew");
// 		print(s:"A dry fart escapes the woman's body. She's gassy! She should have laid off the snacks");
// 	}
	if (roller > 10){ 
		int gruntroller = Random (0, 3);
		PlaySound(0, grunts[gruntroller]);
	} else {	
	
		PlaySound(0, "unf");
	}
}

function str rollForImage(void)
{	
	str bodies[5] = {"BDCRA1", "BDCRA2", "BDCRA3", "BDCRA4"};
	int roller = Random (0, 3);
	return bodies[roller];
}

