--
-- Siloschaufel
--
-- M@D Author:  Heady
-- M@D date: 08.02.2011
--
-- > Copyright (C) Heady - www.planet-ls.de < --
--


if BaseMission.modMapShovelTrigger == nil then
	BaseMission.modMapShovelTrigger = {};
	print("  Register Variable: BaseMission.modMapShovelTrigger");
end;

Siloschaufel = {};

function Siloschaufel.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Fillable, specializations);
end;

function Siloschaufel:load(xmlFile)

	self.fallOut = false;
	self.fallOutParticle = false;
	self.isShovel = true;
	self.lastFillLevel = self.fillLevel;

end;


function Siloschaufel:delete()
end;
	
function Siloschaufel:mouseEvent(posX, posY, isDown, isUp, button)
end;

function Siloschaufel:keyEvent(unicode, sym, modifier, isDown)
end;

--function Siloschaufel:readStream(streamId, connection)
--end;

--function Siloschaufel:writeStream(streamId, connection)
--end;

function Siloschaufel:update(dt)
end;

function Siloschaufel:updateTick(dt)

	if self:getIsActive() then	
     if self.isServer then
	if self.currentFillType == Fillable.FILLTYPE_MAIZE then


				local x,y,z = localDirectionToWorld(self.manureTipReferenceNode, 0,1,0);
				local a,b,c = localDirectionToWorld(self.tipReferencePoint, 0,1,0);
				local benne = Utils.vector3Length(x-a, y-b, z-c);

				if benne > 0.21 then
					self.fallOut = true;
				else
					self.fallOut = false;
				end;
				local ben = (benne - 0.21)/10;

				self.kippWinkel = ben;
				

			
			
			
			if self.fallOut then

				local nearestDistance = 1.5;
				local px, py, pz = getWorldTranslation(self.tipReferencePoint);
				for i=1, table.getn(g_currentMission.attachables) do
					local vx, vy, vz = getWorldTranslation(g_currentMission.attachables[i].rootNode);
					local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);

					if distance < nearestDistance then
						local attachable = g_currentMission.attachables[i];
						if attachable.rootNode ~= self.rootNode then
							if attachable.fillTypes ~= nil and attachable.setFillLevel ~= nil and attachable.fillLevel ~= nil then
								if attachable:allowFillType(self.currentFillType, true) then
									if attachable.fillLevel < attachable.capacity and self.fillLevel > 0 then
          								local deltaLevel = (self.kippWinkel*self.kippWinkel)*(self.capacity/200)*dt
										local fillDelta = math.min(deltaLevel, self.fillLevel)
										self:setFillLevel(self.fillLevel - fillDelta, self.currentFillType);
										attachable:setFillLevel(attachable.fillLevel + fillDelta, self.currentFillType);
									end;
								end;
							end;
						end;
					end;
				end;
				
				if self.fillLevel == 0 then
					self.currentFillType = FruitUtil.FRUITTYPE_UNKNOWN;
				end;
				
			end;
			
		
		
		if self.lastFillLevel > self.fillLevel then
			self.fallOutParticle = true;
		else
			self.fallOutParticle = false;
		end;
		self.lastFillLevel = self.fillLevel;
			
		Utils.setEmittingState(self.dischargeParticleSystems[self.currentFillType], self.fallOutParticle);
	end;
    end;
    end;

	

end;

function Siloschaufel:draw()
end;

function Siloschaufel:onDetach()
end;
