Tore und Zugbrücken öffnen und schließen

» Siedler Map Source Forum » Siedler DEdK Script Forum » Tore und Zugbrücken öffnen und schließen

Seiten: 1

Play4FuN
#1
23.03.2020 12:55
Beiträge: 704

Tore und Zugbrücken öffnen und schließen

Ich habe kürzlich für meine Dario vs Kerberos III Karte etwas gebastelt, um Steintore und Zugbrücken sicher öffnen bzw. schließen zu können (d.h. beim Schließen wird geprüft, ob sich Einheiten im Blocking befinden und ggf. auf die richtige Seite gesetzt). Bonus: es kann ein netter Sound dazu abgespielt werden.

Aufrufe, jeweils mit Sound:

-- öffnen
OpenGate( "gate1", true )
OpenDrawBridge( "drawbridge1", true )
-- schließen
CloseGate( "gate2", true )
CloseDrawBridge( "drawbridge2", true )


Statt einem Namen kann auch die ID der Entity verwendet werden. Alle 4 Funktionen geben die ID der neu erstellten Einheit zurück (bzw. 0, falls nichts geändert wurde, wenn eine ungültige Entity angegeben wurde)

Vielleicht benötigt der eine oder andere so eine Funktionalität mal für seine Karte - könnt ihr gerne tun... Lasst es mich wissen, falls damit Probleme auftreten oder jemand Fragen hat

Hier der Code für Steintore (egal ob Wall oder DarkWall):

function OpenGate( entity, playSound )
	local entityId = GetID( entity )
	local entityType = Logic.GetEntityType( entityId )
	local replaceType
	
	if entityType == Entities.XD_WallStraightGate_Closed then
		replaceType = Entities.XD_WallStraightGate
	elseif entityType == Entities.XD_DarkWallStraightGate_Closed then
		replaceType = Entities.XD_DarkWallStraightGate
	end
	if not replaceType then
		return 0
	end
	
	if playSound then
		Sound.PlayGUISound( Sounds.Buildings_SO_StonemineCraneUp )
	end
	
	return ReplaceEntity( entityId, replaceType )
	
end

function CloseGate( entity, playSound )
	local entityId = GetID( entity )
	local entityType = Logic.GetEntityType( entityId )
	local replaceType
	
	if entityType == Entities.XD_WallStraightGate then
		replaceType = Entities.XD_WallStraightGate_Closed
	elseif entityType == Entities.XD_DarkWallStraightGate then
		replaceType = Entities.XD_DarkWallStraightGate_Closed
	end
	
	if not replaceType then
		return 0
	end
	
	local orientation = Logic.GetEntityOrientation( entityId )
	local pos = GetPosition( entityId )
	-- access category 2 = settler
	local entities = { Logic.GetEntitiesInArea( 0, pos.X, pos.Y, 300, 64, 2 ) }
	
	if entities[1] > 0 then
		local dx, dy = 0, 0
		
		-- move entities in front of or behind the gate
		if orientation == 0 or orientation == 180 then
			dx = 200
		elseif orientation == 90 or orientation == 270 then
			dy = 200
		end
		
		for i = 2, entities[1]+1 do
			local savePos = { X = pos.X, Y = pos.Y }
			local entPos = GetPosition( entities[i] )
			if dx ~= 0 then
				if entPos.X < pos.X then
					savePos.X = pos.X - dx
				else
					savePos.X = pos.X + dx
				end
			elseif dy ~= 0 then
				if entPos.Y < pos.Y then
					savePos.Y = pos.Y - dy
				else
					savePos.Y = pos.Y + dy
				end
			end
			SetPosition( entities[i], savePos )
		end
	end
	
	if playSound then
		Sound.PlayGUISound( Sounds.Buildings_SO_StonemineCraneSide )
	end
	
	return ReplaceEntity( entityId, replaceType )
end



Und hier der Code für die Zugbrücken:

function OpenDrawBridge( entity, playSound )
	local entityId = GetID( entity )
	local entityType = Logic.GetEntityType( entityId )
	local replaceType
	
	if entityType == Entities.XD_DrawBridgeOpen1 then
		replaceType = Entities.PB_DrawBridgeClosed1
	elseif entityType == Entities.XD_DrawBridgeOpen2 then
		replaceType = Entities.PB_DrawBridgeClosed2
	end
	if not replaceType then
		return 0
	end
	
	if playSound then
		Sound.PlayGUISound( Sounds.Buildings_SO_StonemineCraneUp )
	end
	
	return ReplaceEntity( entityId, replaceType )
	
end

function CloseDrawBridge( entity, playSound )
	local entityId = GetID( entity )
	local entityType = Logic.GetEntityType( entityId )
	local replaceType
	local dx, dy = 0, 0
	
	if entityType == Entities.PB_DrawBridgeClosed1 then
		replaceType = Entities.XD_DrawBridgeOpen1
		dx = 1100
	elseif entityType == Entities.PB_DrawBridgeClosed2 then
		replaceType = Entities.XD_DrawBridgeOpen2
		dy = 1100
	end
	
	if not replaceType then
		return 0
	end
	
	local orientation = Logic.GetEntityOrientation( entityId )
	local pos = GetPosition( entityId )
	-- access category 2 = settler
	local entities = { Logic.GetEntitiesInArea( 0, pos.X, pos.Y, 1000, 64, 2 ) }
	
	if entities[1] > 0 then
		
		-- move entities towards the edges of the bridge
		
		for i = 2, entities[1]+1 do
			local savePos = { X = pos.X, Y = pos.Y }
			local entPos = GetPosition( entities[i] )
			-- note: do not move entities that are not actually on the drawbridge (check using the width)
			
			if dx ~= 0 then
				if math.abs( entPos.Y - pos.Y ) < 400 then
					if entPos.X < pos.X then
						savePos.X = pos.X - dx
					else
						savePos.X = pos.X + dx
					end
					SetPosition( entities[i], savePos )
				end
			elseif dy ~= 0 then
				if math.abs( entPos.X - pos.X ) < 400 then
					if entPos.Y < pos.Y then
						savePos.Y = pos.Y - dy
					else
						savePos.Y = pos.Y + dy
					end
					SetPosition( entities[i], savePos )
				end
			end
			
		end
	end
	
	if playSound then
		Sound.PlayGUISound( Sounds.Buildings_SO_StonemineCraneSide )
	end
	
	return ReplaceEntity( entityId, replaceType )
end



____________________
LG Play4FuN

Siedler DEdK Mapping + Scripting Tutorials

Seiten: 1

SiteEngine v1.5.0 by nevermind, ©2005-2007
Design by SpiderFive (www.siedler-games.de) - English translation by juja

Impressum