<commit_before>package Components {
	import flash.geom.Point;
	import Modules.Module;
	/**
	 * ...
	 * @author Nicholas "PleasingFungus" Feinberg
	 */
	public class Wire implements Carrier {
		
		public var path:Vector.<Point>;
		public var exists:Boolean;
		public var FIXED:Boolean;
		public var source:Port;
		public var connections:Vector.<Carrier>;
		
		private var lastPathEnd:Point; //microoptimization in attemptPathTo
		private var oldConnections:Vector.<Carrier>;
		protected var constrained:Boolean = true;
		
		public function Wire(Start:Point) {
			path = new Vector.<Point>;
			path.push(Start);
			
			connections = new Vector.<Carrier>;
			exists = true;
		}
		
		public function attemptPathTo(target:Point):Boolean {
			if (lastPathEnd && target.equals(lastPathEnd)) return false; //ehh
			
			var start:Point = path[0]; //amusing variant: start = path[path.length - 1]?
			path = new Vector.<Point>;
			path.push(start);
			if (constrained && U.state.objTypeAtPoint(start) == Module)
				return false;
			
			var pathEnd:Point = path[path.length - 1];
			var nextPoint:Point;
			for (var delta:Point = target.subtract(pathEnd); delta.x || delta.y; delta = target.subtract(pathEnd)) {
				nextPoint = new Point(pathEnd.x + (delta.x > 0 ? 1 : -1), pathEnd.y);
				if ((!delta.x || !mayMoveThrough(nextPoint)) && delta.y)
					nextPoint = new Point(pathEnd.x, pathEnd.y + (delta.y > 0 ? 1 : -1));
				
				if (constrained && (U.state.lineContents(pathEnd, nextPoint) || U.state.objTypeAtPoint(nextPoint) == Module))
					break;
				
				path.push(pathEnd = nextPoint);
			}
			
			while (constrained && path.length > 1 && !validPosition())
				path.pop();
			
			lastPathEnd = path[path.length - 1];
			return true;
		}
		
		protected function mayMoveThrough(p:Point):Boolean {
			return true;
		}
		
		protected function validPosition():Boolean {
			var source:Port = null;
			for each (var carriers:Vector.<Carrier> in [U.state.carriersAtPoint(path[0]), U.state.carriersAtPoint(path[path.length - 1])])
				if (carriers)
					for each (var carrier:Carrier in carriers) {
						var carrierSource:Port = carrier.getSource();
						if (carrierSource) {
							if (!source)
								source = carrierSource;
							else if (source != carrierSource)
								return false;
						}
					}
			return true;
		}
		
		
		
		
		public static function place(wire:Wire):Boolean {
			return wire.register();
		}
		
		protected function register():Boolean {
			if (path.length < 2) {<commit_msg>Changed wire pathing<commit_after>package Components {
	import flash.geom.Point;
	import Modules.Module;
	/**
	 * ...
	 * @author Nicholas "PleasingFungus" Feinberg
	 */
	public class Wire implements Carrier {
		
		public var path:Vector.<Point>;
		public var exists:Boolean;
		public var FIXED:Boolean;
		public var source:Port;
		public var connections:Vector.<Carrier>;
		
		private var lastPathEnd:Point; //microoptimization in attemptPathTo
		private var oldConnections:Vector.<Carrier>;
		protected var constrained:Boolean = true;
		
		public function Wire(Start:Point) {
			path = new Vector.<Point>;
			path.push(Start);
			
			connections = new Vector.<Carrier>;
			exists = true;
		}
		
		public function attemptPathTo(target:Point):Boolean {
			if (lastPathEnd && target.equals(lastPathEnd)) return false; //ehh
			
			var start:Point = path[0]; //amusing variant: start = path[path.length - 1]?
			path = new Vector.<Point>;
			path.push(start);
			if (constrained && U.state.objTypeAtPoint(start) == Module)
				return false;
			
			var pathEnd:Point = path[path.length - 1];
			var nextPoint:Point;
			for (var delta:Point = target.subtract(pathEnd); delta.x || delta.y; delta = target.subtract(pathEnd)) {
				nextPoint = new Point(pathEnd.x + (delta.x > 0 ? 1 : -1), pathEnd.y);
				if ((!delta.x || !mayMoveThrough(nextPoint)) && delta.y)
					nextPoint = new Point(pathEnd.x, pathEnd.y + (delta.y > 0 ? 1 : -1));
				
				if (constrained && (U.state.lineContents(pathEnd, nextPoint) || U.state.objTypeAtPoint(nextPoint) == Module))
					break;
				
				path.push(pathEnd = nextPoint);
				
				if (!mayMoveThrough(nextPoint))
					break;
			}
			
			while (constrained && path.length > 1 && !validPosition())
				path.pop();
			
			lastPathEnd = path[path.length - 1];
			return true;
		}
		
		protected function mayMoveThrough(p:Point):Boolean {
			var objType:Class = U.state.objTypeAtPoint(p);
			if (objType == null)
				return true;
			if (objType == Module)
				return false;
			
			var carriers:Vector.<Carrier> = U.state.carriersAtPoint(p);
			for each (var carrier:Carrier in carriers)
				if (carrier is Port)
					return false;
			return true;
		}
		
		protected function validPosition():Boolean {
			var source:Port = null;
			for each (var carriers:Vector.<Carrier> in [U.state.carriersAtPoint(path[0]), U.state.carriersAtPoint(path[path.length - 1])])
				if (carriers)
					for each (var carrier:Carrier in carriers) {
						var carrierSource:Port = carrier.getSource();
						if (carrierSource) {
							if (!source)
								source = carrierSource;
							else if (source != carrierSource)
								return false;
						}
					}
			return true;
		}
		
		
		
		
		public static function place(wire:Wire):Boolean {
			return wire.register();
		}
		
		protected function register():Boolean {
			if (path.length < 2) {
{'fraction_non_alphanumeric': 0.1294007490636704, 'fraction_numerical': 0.0052434456928838954, 'mean_word_length': 3.169398907103825, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3357512', 'n_tokens_mistral': 2131, 'n_tokens_neox': 1643, 'n_words': 417}