Purpose of normalize(Position )?


#1

Hi can anybody explain the purpose of the normalized function? What needs to be normalized? I tried a couple of sample inputs and it doesn’t seem to do anything.

I’m sure I’m missing something.

I’m


#2

It converts arbitrary interger x,y to 0<=x<map.width, 0<=y<map.height. Try it with e.g. x=-1. y=-1


#3

I see. I never tried it with x=-1 & y=-1. Why would you negative values on the first place?


#4

I think the function directional_offset can return positions that need to be normalized. I was having crashes on the edge of the map, so now I am calling normalize on all position references in my code


#5

I (and lots of other people) have changed directional_offset to be a function that needs or is part of a game map, and normalises the positions. That way you don’t have to worry about unnormalised positions floating around in your main logic.


#6

@teccles Do you know how to rewrite the code in Python? How can I change the code of a function of the Position class to make it better? Or, how can I add that function into the Map class?


#7

Well, that code is all in the hlt library, which is just part of your bot. So you can edit it as you like. I moved over to Java, but IIRC what I did in Python was to add “map” to the parameters of directional_offset, so I could normalise the position that it produces.


#8

So if you are at the edge of the map, this function will be useful in calculating the actual position of the adjacent cells…


#9

@teccles, I don’t know how to edit the hlt library (I am using the Code Editor if that matters). Would doing something like

hlt.Position.directional_offset = myfunction(self, direction, game_map)

Be the correct way to write this in Python 3 ? Or can you only edit the hlt library if you are running locally…


#10

I’m afraid I don’t know much about the online editor! But if you can’t edit the hlt library source, you could always define your own directional offset function outside it; something like

def safe_directional_offset(game_map, position, direction):
  unnormalised = position.directional_offset(direction)
  return game_map.normalize(unnormalised)

(Though I’ve not tested that actually runs or works :slight_smile: )


#11

I don’t think there’s a way to edit the hlt files using the online editor. That’s the reason I switched to coding locally.