Help: Rotation code wont work visually!

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
I have a modified version of Gedemons rotation code which I need to loop through all units on the map and make sure they are facing in a south-east direction always.

The code seems to work correctly and sets the facing to 2 (south-east) but visually the counters are still rotated incorrectly on the map. What could I be doing wrong??

Code:
function SetUnitDirection()
-- This function makes sure that all units are facing South-East as which is the correct
-- visual orientation for the scenario counter units.

	for playerID = 0, GameDefines.MAX_CIV_PLAYERS - 1 do
	local player = Players[playerID]
		for unit in player:Units() do
			SetDirection(unit, 2) -- 2 is South-East
			Dprint("Unit direction:"..unit:GetFacingDirection())
		end
	end
end

function SetDirection (unit, requiredDirection)
	local direction_types = {
			DirectionTypes.DIRECTION_NORTHEAST,
			DirectionTypes.DIRECTION_EAST,
			DirectionTypes.DIRECTION_SOUTHEAST,
			DirectionTypes.DIRECTION_SOUTHWEST,
			DirectionTypes.DIRECTION_WEST,
			DirectionTypes.DIRECTION_NORTHWEST
	}
	for loop, direction in ipairs(direction_types) do
		if unit:GetFacingDirection() == requiredDirection then
			return
		else
			unit:RotateFacingDirectionClockwise(1);
		end
	end
end
 
I guess Gedemon's code was suitable for his needs but not yours (and it looks like it has some history and went through very different evolutions). Here is a more generic version that should work (not tested, and I don't know whether there are some tricks to be aware of with units directions).

Code:
function SetDirection (unit, requiredDirection)
   while unit:GetFacingDirection() ~= requiredDirection do
      unit:RotateFacingDirectionClockwise(1);
   end
end

If it crashes your game, it means the unit could not be properly oriented, I don't know if such a case is possible. We would then have to add a safety check (a counter to stop the code once 6 rotations occurred).
 
That's a very nice way to say it :D
All developers have pieces of code like that buried in the darkest layers of their codebase. ^^
And it's especially frequent with civ5 modding: you start with a certain idea of the algorithm based on the undocumented API listings from the 2k wiki, then it turns out the API does not behave as you intuitively expected it to, then you start inserting debugging garbage to figure out how it does, and finally you find yourself with a dirty piece of code that works. Of course at that point you don't want to bother to clean it up, because, hell, you would have to test it (even if it's simple you could have made a typo - death to the dynamic languaes), which means restarting the game one more time, while you already lost far too much time on what should have been a ridiculous problem. Now come back thousand revisions later and it looks like the junk grew by itself like some infection and you would like to swear you never wrote that.
 
All developers have pieces of code like that buried in the darkest layers of their codebase. ^^
And it's especially frequent with civ5 modding: you start with a certain idea of the algorithm based on the undocumented API listings from the 2k wiki, then it turns out the API does not behave as you intuitively expected it to, then you start inserting debugging garbage to figure out how it does, and finally you find yourself with a dirty piece of code that works. Of course at that point you don't want to bother to clean it up, because, hell, you would have to test it (even if it's simple you could have made a typo - death to the dynamic languaes), which means restarting the game one more time, while you already lost far too much time on what should have been a ridiculous problem. Now come back thousand revisions later and it looks like the junk grew by itself like some infection and you would like to swear you never wrote that.

Amen to that brother......your preaching to a true believer.:gripe:

Embarresed to admit how many times I have come up with a good chunk of code, got it to work (even though its messy as hell) and then revisited it 2 months later and rewrote it in a much easier way! And Yes, all those restarts to test small code chunks eats away at a lot of your mod time. I guesstimate atleast 1 month wasted watching the loadup process on Civ5 or Worldbuilder! :sleep:
 
Back
Top Bottom