forked from Cryz/battle_royale_sim
added image map generator, map cell's color and emoji legend, death players emoji and color
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import random as _random
|
||||
import copy as _copy
|
||||
import io as _io
|
||||
from PIL import Image as _Image
|
||||
from PIL import ImageDraw as _ImageDraw
|
||||
from utils import logs as _logs
|
||||
from bot_libs import syms as _bot_syms
|
||||
|
||||
|
||||
class BrSimMap():
|
||||
@@ -11,13 +15,13 @@ class BrSimMap():
|
||||
self.world_width= 10 #seems a reasonable width for smartphones larger maps would go on a new line
|
||||
self.world_height= 25
|
||||
self.game_map= []
|
||||
self.field_sym= '🟩'
|
||||
self.player_male_sym= '♂️'
|
||||
self.player_female_sym= '♀️'
|
||||
self.player_nonbinary_sym= '⚧️'
|
||||
self.dead_player_sym= '💀'
|
||||
self.item_sym= '📦'
|
||||
self.mountain_sym = '⛰️'
|
||||
self.field_sym= _bot_syms.MAP_UTF8_FIELD
|
||||
self.player_male_sym= _bot_syms.MAP_UTF8_PLAYER_MALE
|
||||
self.player_female_sym= _bot_syms.MAP_UTF8_PLAYER_FEMALE
|
||||
self.player_nonbinary_sym= _bot_syms.MAP_UTF8_PLAYER_NONBINARY
|
||||
self.dead_player_sym= _bot_syms.MAP_UTF8_DEATH_PLAYER
|
||||
self.item_sym= _bot_syms.MAP_UTF8_ITEM
|
||||
self.mountain_sym = _bot_syms.MAP_UTF8_MOUNTAIN
|
||||
self.init_map_matrix()
|
||||
self.init_players_coordinates()
|
||||
self.init_items_coordinates()
|
||||
@@ -47,7 +51,8 @@ class BrSimMap():
|
||||
def populate_map(self):
|
||||
for player in self.players:
|
||||
p_coord_x, p_coord_y= player.get_player_coordinates()
|
||||
if player.player_gender_is_male(): self.game_map[p_coord_y][p_coord_x]= self.player_male_sym
|
||||
if not player.is_alive(): self.game_map[p_coord_y][p_coord_x]= self.dead_player_sym
|
||||
elif player.player_gender_is_male(): self.game_map[p_coord_y][p_coord_x]= self.player_male_sym
|
||||
elif player.player_gender_is_female(): self.game_map[p_coord_y][p_coord_x]= self.player_female_sym
|
||||
else: self.game_map[p_coord_y][p_coord_x]= self.player_nonbinary_sym
|
||||
for item in self.items:
|
||||
@@ -55,23 +60,19 @@ class BrSimMap():
|
||||
self.game_map[i_coord_y][i_coord_x]= self.item_sym
|
||||
|
||||
def _set_coordinates(self, target):
|
||||
x= _random.randint(1, self.world_width -2) # -2 because 1 cell is occupied by the mountain
|
||||
x= _random.randint(1, self.world_width -2) # from 1 to width-2 because 1 cell is occupied by the mountain
|
||||
y= _random.randint(1, self.world_height -2)
|
||||
while self.get_map_matrix()[y][x] != self.field_sym:
|
||||
print('init_players_coordinates: collision, regenerate coordinates')
|
||||
_logs.log_debug('_set_coordinates: collision, regenerate coordinates')
|
||||
x= _random.randint(1, self.world_width -2)
|
||||
y= _random.randint(1, self.world_height -2)
|
||||
target.set_player_coordinates(x, y)
|
||||
|
||||
def init_players_coordinates(self):
|
||||
# XXX init random player.coord_x and player.coord_y (of course not already used coords)
|
||||
# parse all self.players and define random coordinates (player.coord_x, and player.coord_y)
|
||||
for player in self.players:
|
||||
self._set_coordinates(target)
|
||||
self._set_coordinates(player)
|
||||
|
||||
def init_items_coordinates(self):
|
||||
# XXX init random item.coord_x and item.coord_y (of course not already used coords)
|
||||
# parse all self.items and define random coordinates (item.coord_x, and item.coord_y)
|
||||
for item in self.items:
|
||||
self._set_coordinates(item)
|
||||
|
||||
@@ -95,3 +96,53 @@ class BrSimMap():
|
||||
res+= x
|
||||
res+= '\n'
|
||||
return res
|
||||
|
||||
def get_image_map(self):
|
||||
self.populate_map()
|
||||
scale_x= 20
|
||||
scale_y= 20
|
||||
final_x= self.world_width * scale_x
|
||||
final_y= self.world_height * scale_y
|
||||
image = _Image.new('RGB', (final_x, final_y))
|
||||
draw = _ImageDraw.Draw(image)
|
||||
|
||||
for y in range(self.world_height):
|
||||
for x in range(self.world_width):
|
||||
pixel= self.game_map[y][x]
|
||||
if pixel == self.field_sym:
|
||||
pixel_color= _bot_syms.MAP_IMAGE_FIELD
|
||||
elif pixel == self.mountain_sym:
|
||||
pixel_color= _bot_syms.MAP_IMAGE_MOUNTAIN
|
||||
elif pixel == self.item_sym:
|
||||
pixel_color= _bot_syms.MAP_IMAGE_ITEM
|
||||
elif pixel == self.player_male_sym:
|
||||
pixel_color= _bot_syms.MAP_IMAGE_PLAYER_MALE
|
||||
elif pixel == self.player_female_sym:
|
||||
pixel_color= _bot_syms.MAP_IMAGE_PLAYER_FEMALE
|
||||
elif pixel == self.player_nonbinary_sym:
|
||||
pixel_color= _bot_syms.MAP_IMAGE_PLAYER_NONBINARY
|
||||
elif pixel == self.dead_player_sym:
|
||||
pixel_color= _bot_syms.MAP_IMAGE_DEATH_PLAYER
|
||||
|
||||
scaled_x_coord= x * scale_x
|
||||
scaled_y_coord= y * scale_y
|
||||
# if x == 1 distance from top-right is 20 (because everything is 20x bigger)
|
||||
# then we want to draw a rectanghe 20x20 (instead of 1x1)
|
||||
# this mean that if x == 1 (20px from top-right), x+1 == 2 (40px from top-right)
|
||||
# the same for y
|
||||
# this means that i keep the same factor proportions but 20x bigger
|
||||
scaled_x_width= (x + 1) * scale_x
|
||||
scaled_y_height= (y + 1) * scale_y
|
||||
|
||||
draw.rectangle([scaled_x_coord, scaled_y_coord, scaled_x_width, scaled_y_height],
|
||||
fill= pixel_color
|
||||
)
|
||||
|
||||
# debug
|
||||
#image.save('/tmp/battle_royale_map.png')
|
||||
#image.show()
|
||||
|
||||
bio = _io.BytesIO()
|
||||
image.save(bio, 'PNG')
|
||||
bio.seek(0)
|
||||
return bio
|
||||
|
||||
Reference in New Issue
Block a user