Welcome to the Bitcave! Nerdiness, Jokes, Mods, Ideas, and coding!

Somehow the “AI Smackdown” did not want to let me rest… an interesting topic once you have started to dig into further details. In my quest to understand more about AI, I have wrote a small script in Lua. It reads a playing field (6 x 6 squares) and determines where the actual position is, where the target is and where an obstacle is placed. After that, it assesses if the target is in the line of sight of the position.

It is a very basic script so far and has some shortcomings. Let me share the code (and I hope this is not considered as hijacking of this thread :wink:). Unfortunately discourse is not perfect for displaying code including comments (at least the way I write them). So I have uploaded the code to my webspace, including comments.

function DETERMINE_COORDINATES()
  for i=1,6 do
    for j=1,6 do
      if playground_matrix[i][j] == 1 then
        position_x = i 
        position_y = j
      elseif playground_matrix[i][j] == 2 then
        target_x = i
        target_y = j
      elseif playground_matrix[i][j] == 3 then
        obstacle_x = i
        obstacle_y = j
      end
    end
  end
end

function LOS_ONE()

los_x = position_x
los_y = position_y
end_condition = 0

  while end_condition == 0 do
  
    if los_x == target_x then   
      if los_y > target_y then
          los_y = los_y - 1
      elseif los_y < target_y then
          los_y = los_y + 1
      end
    elseif los_y == target_y then
      if los_x > target_x then
          los_x = los_x - 1
      elseif los_x < target_x then
          los_x = los_x + 1
      end 
    elseif los_x > target_x and los_y > target_y then
        los_x = los_x - 1
        los_y = los_y - 1
    elseif los_x < target_x and los_y < target_y then
        los_x = los_x + 1
        los_y = los_y + 1
    elseif los_x > target_x and los_y < target_y then
        los_x = los_x - 1
        los_y = los_y + 1
    elseif los_x < target_x and los_y > target_y then
        los_x = los_x + 1
        los_y = los_y - 1
    end

    if los_x == obstacle_x and los_y == obstacle_y then
      los_result = 0
      end_condition = 1
    elseif los_x == target_x and los_y == target_y then 
      los_result = 1
      end_condition = 1
    end
  end

  return los_result

end

playground_matrix={}
for i=1,6 do              
    playground_matrix[i]={}
  for j=1,6 do
    playground_matrix[i][j]=0
  end
end

playground_matrix[2][2]=1
playground_matrix[6][5]=2
playground_matrix[4][4]=3

position_x = 4                 
position_y = 4                 
target_x = 4
target_y = 4
obstacle_x = 4
obstacle_y = 4

los_result = 2

DETERMINE_COORDINATES()

print(position_x .. " " .. position_y)
print(target_x .. " " .. target_y)
print(obstacle_x .. " " .. obstacle_y)

LOS_ONE()                            

if los_result == 1 then
  print("Target can be seen.")
elseif los_result == 0 then
  print("Target is hidden.")
else
  print("Something weird has happened.")
end

Back to the mentioned shortcomings…

  • Very basic logic to determine the LOS.
  • Only one obstacle can be placed at the moment.
  • No check if positions make sense (e.g. target is not placed outside the playing field or not on the same field as the obstacle).
  • … and for sure many, many others.

I do have some ideas to overcome one or the other point…

  • Second and / or third LOS-logic could be included. Final result (if target can be seen) would depend on the fact that e.g. all are true.
  • Existing logic could be adjusted to extend the radius which is assessed by the LOS-logic at the moment.
  • Obstacles could be saved in an extra array to allow assessment against more than one obstacle.

I would love to hear if you “visitors of the bitcave” have any suggestions or comments…

2 Likes