//Property of Stan423321, all wrongs reserved. As long as you credit him, you are good to use it.

#library "PSZCZOLA ZABOJCA JEST GLODNA I ZLA"
#include "zcommon.acs"

function int sqrtx(int fix) {
   int slider = 0.0;
	int range = 128.0;
	while (range>0.0) {
		slider+=range;
		if (FixedMul(slider, slider)<fix) {
			slider-=range;
		}
		range>>=1;
	}
	return slider;
}


//100.0 - Random radius (bigger - better quality, but risk of blowing up)
//Circler - What orbits around actor
//Rocket - What object spawns during death
//anything beginning with $ will not spawn
//IAmGonnaDie - Death trigger
//70.0, 90.0 - Random for radius
//0.7 - if it's 1.0, then we get circle, otherwise it's ellipsis (random)
//0.03 - orbiting speed
//1st 0.25 - Object spawn speed when it stops orbiting
//2nd 0.25 - Angle control after orbiting ends

int ScriptConfig[1][10] = {
	{220.0, "ViolatorOrbiter", "$ViolatorOrbiter", "ViolatorDetonate",
		700.0, 810.1, 0.999, 0.01, 0.01, 0.25}
};

script 713 (int liczba) {
	int randomium = ScriptConfig[liczba][0];

	int vx = Random(-randomium, randomium);
	int vy = Random(-randomium, randomium);
	int vz = Random(-randomium, randomium);
	int wx = Random(-randomium, randomium);
	int wy = Random(-randomium, randomium);
	int wz = Random(-randomium, randomium);

	int ratio = FixedDiv(
		FixedMul(wx, vx)+FixedMul(wy, vy)+FixedMul(wz, vz),
		FixedMul(vx, vx)+FixedMul(vy, vy)+FixedMul(vz, vz));
	wx -= FixedMul(vx, ratio);
	wy -= FixedMul(vy, ratio);
	wz -= FixedMul(vz, ratio);
// v _|_ w

	str sep = ", ";
	//Log(s:"L1 ", f:vx, s:sep, f:vy, s:sep, f:vz, s:sep,
		//f:wx, s:sep, f:wy, s:sep, f:wz);
	
	int len =
		sqrtx(FixedMul(vx, vx)+FixedMul(vy, vy)+FixedMul(vz, vz));
	vx = FixedDiv(vx, len);
	vy = FixedDiv(vy, len);
	vz = FixedDiv(vz, len);
	len = sqrtx(FixedMul(wx, wx)+FixedMul(wy, wy)+FixedMul(wz, wz));
	wx = FixedDiv(wx, len);
	wy = FixedDiv(wy, len);
	wz = FixedDiv(wz, len);

	int radius = random(
		ScriptConfig[liczba][4], ScriptConfig[liczba][5]);
	vx = FixedMul(vx, radius);
	vy = FixedMul(vy, radius);
	vz = FixedMul(vz, radius);
	radius = FixedMul(random(ScriptConfig[liczba][6], 1.0), radius);
	wx = FixedMul(wx, radius);
	wy = FixedMul(wy, radius);
	wz = FixedMul(wz, radius);

	//Log(s:"L2 ", f:vx, s:sep, f:vy, s:sep, f:vz, s:sep,
		//f:wx, s:sep, f:wy, s:sep, f:wz);

	int TID = 0;
	for (int ii = 20000; ii < 30000; ii++)
	{
		if (ThingCount(T_NONE, ii) < 1) {TID = ii; ii = 99999;}
	}
	int fx = GetActorX(0)+vx;
	int fy = GetActorY(0)+vy;
	int fz = GetActorZ(0)+vz;
	Spawn(ScriptConfig[liczba][1], fx, fy, fz, TID);
	SetActorVelocity(TID, 0.0, 0.0, 0.0, 0, 0);
	int angle = 0;
	delay(1);

	while (CheckInventory(ScriptConfig[liczba][3])==0) {
		if (ThingCount(T_NONE, TID) < 1) {
			Restart;
		}
		angle+=ScriptConfig[liczba][7]; angle%=1.0;
		fx = GetActorX(0)
			+ FixedMul(cos(angle), vx) + FixedMul(sin(angle), wx);
		fy = GetActorY(0)
			+ FixedMul(cos(angle), vy) + FixedMul(sin(angle), wy);
		fz = GetActorZ(0)
			+ FixedMul(cos(angle), vz) + FixedMul(sin(angle), wz);
		if (!SetActorPosition(TID, fx, fy, fz, 0)) {
			if (SetActorState(TID, "DieDieDie2")==0
				&& SetActorState(TID, "DieDieDie")==0) {
				SetActorState(TID, "Death");
			}
			for (int i = 0; i<8; i++) {
				delay(1);
				if (CheckInventory(ScriptConfig[liczba][3])>0) {
					Terminate;
				}
			}
			Restart;
		}
		delay(1);
	}

	if (SetActorState(TID, "DieDieDie")==0
		&& SetActorState(TID, "DieDieDie2")==0) {
		SetActorState(TID, "Death");
	}
	if (GetChar(ScriptConfig[liczba][2], 0)!='$') {
		while (ThingCount(T_NONE, TID) < 1) {
			delay(1);
		}
		Spawn(ScriptConfig[liczba][2], fx, fy, fz, TID);
		angle+=ScriptConfig[liczba][9]; angle%=1.0;
		fx = FixedMul(cos(angle), vx) + FixedMul(sin(angle), wx);
		fy = FixedMul(cos(angle), vy) + FixedMul(sin(angle), wy);
		fz = FixedMul(cos(angle), vz) + FixedMul(sin(angle), wz);
		SetActorVelocity(TID, FixedMul(fx, ScriptConfig[liczba][8]),
			FixedMul(fy, ScriptConfig[liczba][8]),
			FixedMul(fz, ScriptConfig[liczba][8]), 0, 0);
	}
}
