Blob Shadows

Blob Shadows

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Blob Shadows

Post by Argh »

I've been looking at the blob shadow Widget in CA, had a few questions:

1. How would we get the sun's angle and use it to project the shadow correctly, based on angle?

2. I see that aircraft still keep drawing their blobs even though regular shadows are on. What's up with that? Is there any way to selectively turn shadows on and off, per Unit, via LUA? Or are we going to need to draw shadows entirely via LUA first?

3. I tried to set it up so that I could experiment with using it for ground units. I changed the code that looks at the unitdef for canFly, and tried something else, which isn't working. Here's the original chunk:

Code: Select all

function widget:Update(dt)
  gameFrame = GetGameFrame()

  if ((gameFrame%90)<1) then
    aircraftList = {}
    local teams = GetTeamList()
    for _,teamID in ipairs(teams) do
      teamUnits = GetTeamUnits(teamID)
      teamUnits.n = nil 
      for _,unitID in ipairs(teamUnits) do
        unitDefID = GetUnitDefID(unitID)
        unitDef   = UnitDefs[unitDefID or -1]
        if (unitDef)and(unitDef.canFly) then
          insert(aircraftList, unitID)
        end
      end
    end
  end
Here's what I tried to use, ported from the Morph Gadget:

Code: Select all

function widget:Update(dt)
  gameFrame = GetGameFrame()

  if ((gameFrame%90)<1) then
    aircraftList = {}
    local teams = GetTeamList()
    for _,teamID in ipairs(teams) do
      teamUnits = GetTeamUnits(teamID)
      teamUnits.n = nil 
      for _,unitID in ipairs(teamUnits) do
        unitDefID = GetUnitDefID(unitID)
        unitDef   = UnitDefs[unitDefID or -1]
        local cats = UnitDefs[UnitDefID].modCategories
 	 if (cats) then
  	 if     (cats["BLOBSHADOW"]) then insert(aircraftList, unitID)
					end
				end
			end
		end
	end

end
Which generated the following error:

Error in Update(): [string "LuaUI/Widgets/unit_blobshadow.lua"]:160: attempt to index field '?' (a nil value)

Line 160 is: local cats = UnitDefs[UnitDefID].modCategories

It just seems strange... didn't I already define that field? All I wanted was to use the Category line in the FBI to store the string "BLOBSHADOW"... is there something else I needed to do, to parse this out?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Blob Shadows

Post by AF »

Lets say I have an array of 5 items, I could do:

Code: Select all

array[3] = 2
right?

Now lets say I specify an array index with a variable instead of putting it in directly

Code: Select all

i = 2+1
array[i] = 2
all good!

But now lets say we give i a value of nil

Code: Select all

i = nil
array[i] = 2
There is no nil position in the array, after all how can you access the null'th index? Its somewhat of a paradox to ask for the value at the index of 'not a value'. Imagine picking the nothing'th contestant in a line up, not the 0th not the last or first, the nothing'th.

Now your not going to actually do that though are you? It'd generate an exception/error and it wouldn't work.

So lets say instead of assigning nil, we just don't assign it at all.

local i
array = 2

basically exactly the same. Because i hasn't been initialized, it takes the default value of nil


Now why does this apply? Because lua is case sensitive, and you've put UnitDefID not unitDefID, which means lua is creating a new variable of value nil in that statement.

It would also make it clearer to you if instead of

local cats = UnitDefs[UnitDefID].modCategories

you instead did

Code: Select all

unitDef   = UnitDefs[unitDefID or -1]
if (unitDef ~= -1) then
    local cats = unitDef.modCategories
    -- do cats stuff
end
Aside from removing an unnecessary table look up the code is broken down into further smaller steps. Remember, if you shove a chunk of code into a long statement then that's what you get. But if you break it up into multiple lines and smaller statements then a clever compiler has an easier time optimizing the code, and its much easier to debug, since you don't have to worry about which action in the statement caused the error because there's only ever one action anyway.

That and I'm sure there's a better method than storing it in the category line. As a custom tag perhaps would be better? Or perhaps you can change the entire file for fbi to lua format? That way you could add a hasBlobShadow function to every unit and call that. Although if you introduced a unit type that didn't have the function then it would probably crash when it came to it unless you fiddled it.
Sheekel
Posts: 1391
Joined: 19 Apr 2005, 19:23

Re: Blob Shadows

Post by Sheekel »

I messed around with this a bit and got it to work with ground units. what i would really like to see is the size of the shadow changing based on the side of the unit. I attempted to combine code from blobshadow and teamplatter (platters change based on unit size) but failed at that also.

Nice to see someone working on this.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Blob Shadows

Post by Argh »

Thanks, AF! That almost made complete sense. I swear I'll get this LUA stuff, someday.

Making it bigger / smaller, based on some conditions (er, now that I know why it was returning nil), is actually really easy.

The hard part is getting the sun's vector and then using it to do an offset- I'm kind've lost there. It should be pretty easy to get the vector, but I'm not sure how to turn that into an offset on the 2D heightmap- I don't care about perspective or whatever. And I have no idea about the shadows the unit already casts.
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Blob Shadows

Post by lurker »

Raytrace from the center of the unit?
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Blob Shadows

Post by Argh »

I've been reading. It requires something called a "projection matrix" to generate the angle.

Anybody know where I can see source for this? I know that the C++ code contains some examples but, naturally I don't think they're going to work in LUA, and I don't understand how they work, either.
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Blob Shadows

Post by lurker »

Why would you need to generate the angle in the first place? Shouldn't having it as a vector be the preferred format?
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Blob Shadows

Post by Argh »

Well, um... I dunno!

I would think that, since it's a blob shadow, we simply want the quad we're using to show the texture with to be projected at the correct angle from the sun, and then rotated to match the normal of the triangle it's being "projected" onto, so that it doesn't clip too often (raising it a wee bit about the triangle would also be a good thing). Is there some better way to do this? I have no idea, lol...
Post Reply

Return to “Lua Scripts”