Friday, April 22, 2022

Coding in TTS (3)

 And to round things out for this time, this next bit is part of a painfully long function which (as far as I can tell at the moment) is required to be painfully long because it is the global object dropped code, so in it must go every decision related to any time any object is dropped anywhere on the board for any reason. Which is a lot of decisions as it turns out.

I don't like this piece. Not because I think I did it wrong (though it is possible there's a more efficient set of logic to accomplish the same result). I don't like it because I don't like any piece of code that is decision nested inside decision nested inside decision nested inside decision, etc, etc. Once I get a certain amount of layers my head starts to hurt and I begin to become overwhelmed with all of the things I must remember about the prior decision but not code at the moment because I must finish with this next decision and its nested counterparts first. Headaches ensue. 

Also, this is nowhere near the most deeply nested set of if/then statements I've ever written. We don't need to talk about how far this can go.

if obj.tag == 'Card' and obj.hasTag("Battalion") then

    --find battalion card type

    local cardType = Global.call("findBattalionType", {obj.guid})

    --check if the battalion card dropped is in the discard zone

    local discardIndex = {sdiscard, kdiscard}

    local lzIndex = {slaneIndex, klaneIndex}

    local footmanBlockIndex = {sfootmanIndex, kfootmanIndex}

    local breacherBlockIndex = {sbreacherIndex, kbreacherIndex}

    local berserkerBlockIndex = {sberserkerIndex, kberserkerIndex}

    local actionZoneIndex = {sactionZone, kactionZone}

    local typeCount = 0

    local typeAction = 0

    for a = 1, 2 do

      local discardObjects = getObjectFromGUID(discardIndex[a]).getObjects()

      for _, card in ipairs(discardObjects) do

        --if the battalion card is in the discard zone, check if the game is in the skirmish phase

        if card.guid == obj.guid then

          --[battalion, discard, skirmish] find the active lane

          laneZoneIndex = lzIndex[a]

          for d = 1, 5 do

            laneObjects = getObjectFromGUID(laneZoneIndex[d]).getObjects()

            for _, tile in ipairs(laneObjects) do

              if tile.hasTag("lane") then

                if tile.getStateId() == 2 then

                  --[battalion, discard, skirmish, active lane] count battalion cards in the lane and count card type in lane

                  for _, stuffs in ipairs(laneObjects) do

                    if stuffs.tag == 'Card' and stuffs.hasTag("Battalion") and stuffs.hasTag(cardType) then

                      typeCount = typeCount + 1

                    end

                  end

                  --[battalion, discard, skirmish, active lane] set counter block to new value

                  if cardType == "Footman" then

                    blockIdIndex = footmanBlockIndex[a]

                  else

                    if cardType == "Breacher" then

                      blockIdIndex = breacherBlockIndex[a]

                    else

                      if cardType == "Berserker" then

                        blockIdIndex = berserkerBlockIndex[a]

                      else

                        blockIdIndex = nil

                      end

                    end

                  end

                  if blockIdIndex != nil then

                    counterBlock = getObjectFromGUID(blockIdIndex[d])

                    counterBlock.setName(typeCount)

                  end

                  --[battalion, discard, skirmish, active lane] count action dice matching card type in action zone

                  actionZoneObjects = getObjectFromGUID(actionZoneIndex[a]).getObjects()

                  for _, actionDie in ipairs(actionZoneObjects) do

                    if actionDie.getName() == cardType then

                      typeAction = typeAction + 1

                      --secure action die in case one needs to be removed

                      anActionDie = getObjectFromGUID(actionDie.guid)

                    end

                  end

                  --[battalion, discard, skirmish, active lane] remove action dice more than card type count

                  if typeAction > typeCount then

                    anActionDie.destruct()

                  end

                end

              end

            end

          end

        end

      end

    end

  end

No comments:

Post a Comment