
; Place structures



; Vars

	_i = _this select 0
	_idc = _this select 1
	_item = _this select 2
	_selector = _this select 3
	
	PLACING = true

	_property = _item call findProperty;
	_class = _property select 3
	
	_white = (_class + "_white") camCreate [0,0,0]
	_red = (_class + "_red") camCreate [0,0,0]
	
	_dir = (getDir camtarget)
	{_x setDir _dir} forEach [_white,_red]
	
	_borderX = BORDERRIGHT - 0.245
	_borderY = BORDERUP    + 0.036
	
	
	
	
	
	
	
	


; Set pos on cursor
	
	#Place
	_obj = _white
	_x = CUX
	_y = CUY
	_z = CUZ
	~0.01

; If cursor on map	
	? SIDEBAR && MSX >= _borderX   ||  MSY < _borderY : _x=-1; {_x setPos [-1,-1,0]} forEach [_white,_red]; goto "Place"
	

; If on rock area
		#CheckTrigger
		buidingtemp setPos getpos camtarget
		? _x > 2 : buildingtemp setPos [_x, _y, _z]
		_triggers = (list rock1) + (list rock2) + (list rock3)
		if (!(buildingtemp in _triggers)) then {_obj=_red; _white setPos [1,1,1]; goto "SetPos"} else {_red setPos [1,1,1]}
		
		
; If not on other building
		? count BUILT == 0 : goto "SetPos"
		_i = 0
		_range = 18
		#CheckBuilding
		_building = BUILT select _i
		_x1 = (getpos _building select 0) - _range
		_x2 = (getpos _building select 0) + _range
		_y1 = (getpos _building select 1) - _range
		_y2 = (getpos _building select 1) + _range
		if (CUX > _x1 && CUX < _x2  &&  CUY > _y1 && CUY < _y2) then {_obj=_red; _white setPos [1,1,1]; goto "SetPos"} else {_red setPos [1,1,1]}
		? _i < count BUILT - 1 : _i=_i+1; goto "CheckBuilding"

		
; Distance from base
		_i = 0
		_distances = []
		#CheckDistance
		_built = (BUILT select _i)
		_distances = _distances + [(_built distance buildingtemp)]
		? _i < count BUILT - 1 : _i=_i+1; goto "CheckDistance"
		_cnt = "_x<31" count _distances
		if (_cnt == 0) then {_obj=_red; _white setPos [1,1,1]} else {_red setPos [1,1,1]}
		


	#SetPos
	_obj setPos [_x, _y, _z]
	
		_fixX = (getpos _obj select 0) - _x
		_fixY = (getpos _obj select 1) - _y
	
	_obj setPos [(_x - _fixX), (_y - _fixY), _z]
	
	

	? LBUTTON  &&  _x > 0  : goto "Placed"
	goto "Place"
	
	
	
	
	
	
	
	
	
	
; Finish

	#Placed
	@!LBUTTON
	? _obj == _red : playSound "cannot_build"; goto "Place"
	
	{deleteVehicle _x} forEach [_white, _red]
	_trueNew = _class createVehicle [0,0,0]
	
		_trueNew setPos [_x, _y, _z]
	
			_fixX = (getpos _trueNew select 0) - _x
			_fixY = (getpos _trueNew select 1) - _y
	
		_trueNew setPos [(_x - _fixX), (_y - _fixY), _z]
	
	_trueNew setDir _dir
	
	_flag = "FlagCarrier" camCreate [0,0,0]
	_a = 5
	_b = -7.5
	_flagX = _x + _b * sin(_dir) + _a * cos(_dir)
	_flagY = _y + _b * cos(_dir) - _a * sin(_dir)
	_flag setPos [_flagX, _flagY, 0]
	_flag setFlagTexture ("img\flag_" + MYSIDE + ".jpg")
	
	playSound "built"
	BUILT = BUILT + [_trueNew]
	ctrlShow [_idc, false]
	STATUS set [_selector,0]
	_item exec "NewConstruction.sqs"
	base animate ["rotate_crane",1]
	CRANEANIM = true
	~0.2
	PLACING = false
	
	~0.5
	base animate ["rotate_crane",0]
	~0.7
	CRANEANIM = false