Bug that I can't figure out

Bug that I can't figure out

Discuss game development here, from a distinct game project to an accessible third-party mutator, down to the interaction and design of individual units if you like.

Moderator: Moderators

Post Reply
User avatar
Relinquished
Posts: 126
Joined: 22 Jun 2006, 22:08

Bug that I can't figure out

Post by Relinquished »

Hey,

I'm working on a mod (mutator more like) for BA and I'm putting CA's deployment thing into it. I've got it all working nicely except for this one random bug I just can't figure out :|.

First thing, this is a link to a rar file containing the mod, and images showcasing the bug - feel free to extract the mod to try and figure it out.

Basically what happens is that it lets you deploy units, but it doesn't seem to like letting you place them in certain locations, and instead it just throws an error (you can see the error in the picture) and issues an order for the commander to build it. I can't really explain it farther than that, best if you just download the mod and see what I mean - here are a few pics though, maybe it's enough that you can help :).

In-Game bug & example cause:
http://files.springrts.de/7/ErrorMessage.jpg
As you can see from ^. The deployment lets you place buildings in some locations, but not others :shock:.

This is the code that causes the error (actual file in the downloadable rar above):
http://files.springrts.de/7/SourceOfError.jpg

Thanks a lot to anyone that can help :).

Also on a side note, if anyone wants to figure out how to get the "Deployment" info and bars centered on screen that'd be awesome - it's the least of my worries though.

Again, I didn't write the deployment widget, pretty sure it was trepan originally - I'm just borrowing it for a mod cause it's awesome :lol:

[EDIT] Forgot to mention - you need BA712.sd7 to run the mod, since it's more of a mutator.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Bug that I can't figure out

Post by Argh »

It's really hard to understand your source post.

Basically though, you have an error when it gets to that function, where it's sending two nil values.

Therefore the problem is further back. But it's impossible to tell from your jpg. Post the whole AllowCommand() code (not the entire Lua file) and let's take a look at that.
User avatar
Relinquished
Posts: 126
Joined: 22 Jun 2006, 22:08

Re: Bug that I can't figure out

Post by Relinquished »

Sorry about the unclear post - I tried to include everything needed :oops:. Here's all the relevant code (as far as I can tell) including the functions that are called:

This function is where the error originates (GetCancelBuildID near the top calls the function where the error occurs):

Code: Select all

function AllowCommand(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOptions)
  local team = teams[unitTeam]
  if (team == nil) then
    return false
  end

  if ((cmdID < 0) and (unitID	== team.comm)) then
    local cancelID = GetCancelBuildID(unitTeam, -cmdID, cmdParams)
    if (cancelID == nil) then
      AddBuild(team, -cmdID, cmdParams)
    else
      if (cancelID ~= team.comm) then
        RemoveBuild(team, cancelID)
      end
    end
  elseif (cmdID == CMD_DEPLOY_DELETE) then
    for _,team in pairs(teams) do
      if (unitID == team.comm) then
        return false
      end
    end
    RemoveBuild(team, unitID)
  elseif ((#cmdParams == 1) and (cmdOptions.right) and (cmdOptions.alt)) then
    for _,team in pairs(teams) do
      if (unitID == team.comm) then
        RemoveBuild(team, cmdParams[1])
      end
    end
  elseif (cmdID == CMD_DEPLOY_READY) then
    ReadyCommand(team, cmdOptions)
  else
    return true
  end
  
  return false
end
Here's GetCancelBuildID:

Code: Select all

local function GetCancelBuildID(teamID, buildID, cmdParams)
  if (#cmdParams < 3) then
    return nil
  end
  local bud = UnitDefs[buildID]
  if (not bud) then
    return nil
  end
  local bx, by, bz = cmdParams[1], cmdParams[2], cmdParams[3]
  local bfacing = (#cmdParams >= 4) and cmdParams[4] or 0

  local btwist = ((bfacing % 2) == 1)
  local bsx = btwist and bud.ysize or bud.xsize
  local bsz = btwist and bud.xsize or bud.ysize
  
  local function max(a, b) return (a > b) and a or b end

  local ss = Game.squareSize
  local units = Spring.GetTeamUnits(teamID)
  for _,uid in ipairs(units) do
    local udid = Spring.GetUnitDefID(uid)
    local ud = UnitDefs[udid]
    local facing = Spring.GetUnitBuildFacing(uid)
    local twist = ((facing % 2) == 1)
    local sx = twist and ud.ysize or ud.xsize
    local sz = twist and ud.xsize or ud.ysize
    local x, y, z = Spring.GetUnitBasePosition(uid)
    if (((math.abs(bx - x) * 2) <= (ss * max(sx, bsx))) and
        ((math.abs(bz - z) * 2) <= (ss * max(sz, bsz)))) then
      return uid
    end
  end

  return nil
end
The line:

Code: Select all

local function max(a, b) return (a > b) and a or b end
is the comparison that's causing the problem and it's when the values sx and bsx, or sz and bsz from here are both nil:

Code: Select all

if (((math.abs(bx - x) * 2) <= (ss * max(sx, bsx))) and
        ((math.abs(bz - z) * 2) <= (ss * max(sz, bsz)))) then
      return uid
    end
So basically the problem is when these equations become nil:

Code: Select all

local btwist = ((bfacing % 2) == 1)
  local bsx = btwist and bud.ysize or bud.xsize
  local bsz = btwist and bud.xsize or bud.ysize
                    ...
    local twist = ((facing % 2) == 1)
    local sx = twist and ud.ysize or ud.xsize
    local sz = twist and ud.xsize or ud.ysize
I'm only in first year programming in uni, have never really worked with coding for spring, or the lua language at all - so I'm not 100% positive on what's going on, but it seems to me that it has to do with the size and orientation of the building or placement of the cursor :|.

Anyway I hope that's a bit more helpful - lemme know if there's anything else I can post :).
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Bug that I can't figure out

Post by Argh »

So the ysize and xsize are returning nil.

Therefore the unitDef that this is being sent is either bad, or that's not written right.

Oh. It's not ysize, it's zsize. That is probably what's wrong.

ud.ysize would return nil, and break any attempt at a math step.
User avatar
Relinquished
Posts: 126
Joined: 22 Jun 2006, 22:08

Re: Bug that I can't figure out

Post by Relinquished »

That fixed that problem, thank you! :-).

I'm running into a few other weird occurrences now, but I think I should be able to figure them out - I'll post again if I can't :P.
Post Reply

Return to “Game Development”