/*	kd:
	
	For stuff like DA SORD, we'd like to cleave from one side to another in a
	sweep-like attack. gzd is epic lulz-r-us with its bullet puff spawning,
	like everything just breaks when you look up, and furthermore, you shouldn't
	be able to hit the same actor 10-million times with the same swing because
	you had the inguinuity to come up with "I'll just A_Punch in all directions"
	before anybody else.
	
	So yea try this.
	
	*/

// kd: Here the actual sword cleaving is done. The stuff below is just input.

struct Gc_SwordCleave play {
	Actor			target;
	int				target_tics;
	int				tics;
	
	class <Actor>	impact_puff;
	class <Actor>	squishy_sounder;
	class <Actor>	bloodless_sounder;
	
	bool			played_squishy_sound;
	bool			played_bloodless_sound;
	
	void Begin (
	class <Actor>	puff				= "BulletPuff",
	class <Actor>	squishy_hit_class	= "BulletPuff",
	class <Actor>	bloodless_hit_class	= "BulletPuff") {
	// String			clank_sound			= "") {
		// av:
		impact_puff			= puff;
		squishy_sounder		= squishy_hit_class;
		bloodless_sounder	= bloodless_hit_class;
		// sound_clank			= clank_sound;
		
		// Timing:
		played_squishy_sound	= false;
		played_bloodless_sound	= false;
		tics					= 0;
		target_tics				= 0;
		target					= NULL;
	}
	
	void TargetDone () {
		target		= NULL;
		target_tics	= 0;
	}
	
	// kd: This'll cycle through the actors in the target list found by the
	// scanner (which already did all the necessary distance checks). They'll
	// be cleaved based on the tic they were spotted.
	
	// The puff spawned on hit will enter a state BleedingTarget or
	// BloodlessTarget. master is the player, target is the... target.
	
	void CleaveOnBrother (Gc_CleaveScan scan) {
		if(scan.HasTarget() && CanContinue()) {
			[target, target_tics] = scan.PopTargetAndHitTics();
		}
		
		if(target/* && scan.SwingTics() - target_tics <= tics*/) {
			// kd: This guy got hit.
			Actor puff = Actor.Spawn(
				impact_puff,
				target.vec3offset(0, 0, 0.5 * target.height));
			
			if(puff) {
				// kd: Contrived, but I won't change it cuz this is exactly
				// what pillow requested.
				InitPuff(puff, target, scan.User());
			}
			
			// kd: We might have another thing coming on the same tic.
			TargetDone();
			CleaveOnBrother(scan);
			return;
		}
		
		tics++;
	}
	
	private bool CanContinue () const {
		return target == NULL;
	}
	
	private void InitPuff (Actor puff, Actor target, Actor user) const {
		if(target.bnoblood) {
			// puff.SetStateLabel("BloodlessTarget");
			
			if(!played_bloodless_sound) {
				Actor mo = Actor.Spawn(bloodless_sounder, user.pos);
				
				if(mo) {
					played_bloodless_sound = true;
					mo.target = user;
					mo.tracer = target;
				}
			}
		}
		
		else {
			// puff.SetStateLabel("BleedingTarget");
			
			if(!played_squishy_sound) {
				Actor mo = Actor.Spawn(squishy_sounder, user.pos);
				
				if(mo) {
					played_squishy_sound = true;
					mo.target = user;
					mo.tracer = target;
				}
			}
		}
		
		puff.target	= user;
		puff.tracer	= target;
		puff.master	= NULL;
	}
}

struct Gc_CleaveScan play {
	
	// kd: What was hit and at which tic.
	private Array <Actor>			ignore;
	private Array <Actor>			targets;
	private Array <int>				tics;
	private Gc_FovClosestMonster	scanner;
	private PlayerPawn				user_po;
	private double					distance_sq;
	private int						tics_to_swing;
	
	void AttachTo (PlayerInfo player) {
		scanner.AttachTo(player);
		user_po = player.mo;
	}
	
	Actor User () const {
		return user_po;
	}
	
	private void ClearLists () {
		targets.clear();
		tics.clear();
		ignore.clear();
	}
	
	// kd: This finds targets and puts them into an array. If the array is
	// empty, we'll see further (in that case, we'll spawn a big-ish projectile
	// to clank with whatever may be in front of us, which might be a wall,
	// floor, ceiling or another solid that isn't evil).
	
	void LeftToRight (
	double	angle_covered	= 70,
	double	pitch_covered	= 5,
	int		swing_tics		= 15,
	double	distance		= 200) {
		distance_sq = distance * distance;
		tics_to_Swing = swing_tics;
		
		ClearLists();
		
		scanner.Reset();
		scanner.Begin(
			hor_fov:	0.5 * angle_covered,
			ver_fov:	0.5 * pitch_covered,
			ray_cols:	20,
			ray_rows:	3,
			scan_time:	swing_tics);
		
		// RunCleave();
	}
	
	void RightToLeft (
	double	angle_covered	= 70,
	double	pitch_covered	= 5,
	int		swing_tics		= 15,
	double	distance		= 200) {
		distance_sq = distance * distance;
		tics_to_Swing = swing_tics;
		
		ClearLists();
		
		scanner.Reset();
		scanner.Begin(
			hor_fov:	0.5 * angle_covered,
			ver_fov:	0.5 * pitch_covered,
			ray_cols:	20,
			ray_rows:	3,
			scan_time:	swing_tics);
		scanner.Reverse();
		
		// RunCleave();
	}
	
	void NextBatch () {
		scanner.StartStep();
		
		while(scanner.IsIterating()) {
			Actor spotted = scanner.Trace();
			scanner.Iterate();
				
			// kd: Save unique target.
			if(spotted && ignore.find(spotted) == ignore.size()) {
				
				// kd: I swear gzd had a function for this but they're all
				// over the place so nobody can find them.
				let diff = user_po.vec3to(spotted);
				
				if(diff dot diff < distance_sq) {
					targets.push(spotted);
					ignore.push(spotted);
					tics.push(scanner.CurrentTics());
				}
			}
		}
	}
	
	bool HasTarget () const {
		return 0 < targets.size();
	}
	
	Actor, int PopTargetAndHitTics () const {
		Actor	mo		= targets	[0];
		int		time	= tics		[0];
		targets.		delete(0);
		tics.			delete(0);
		return mo, time;
	}
	
	/* private void RunCleave () {
		targets.clear();
		tics.clear();
		
		while(!scanner.IsDone()) {
			scanner.StartStep();
			
			while(scanner.IsIterating()) {
				Actor spotted = scanner.Trace();
				scanner.Iterate();
				
				// kd: Save unique target.
				if(spotted && targets.find(spotted) == targets.size()) {
					
					// kd: I swear gzd had a function for this but they're all
					// over the place so nobody can find them.
					let diff = user_po.vec3to(spotted);
					
					if(diff dot diff < distance_sq) {
						targets.push(spotted);
						tics.push(scanner.CurrentTics());
					} // imagine being a letter and
				} // getting
			} // knocked
		} // down
	} // this
	// staircase */
	
	void Print () const {
		for(int i = targets.size(); 0 <= --i;) {
			console.printf("spotted a %s, %i", targets [i].GetClassName(), tics [i]);
		}
	}
	
	int SwingTics () const {
		return tics_to_swing;
	}
	
	bool IsDone () const {
		return scanner.IsDone();
	}
}