NotBobBot

Code

Dec 20th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. from random import randint
  2.  
  3. map_content = [ #each dict will be called 'tile_dict'
  4. {'type': 'Stone', 'symbol': "[]"},
  5. {'type': 'Air', 'symbol': "O"},
  6. {'type': 'Grass', 'symbol': "_"},
  7. {'type': 'Dirt', 'symbol': "*", 'generation': [(2,2),(3,2),(2,3),(4,4),(2,1),(2,2)],'rarity': 50},
  8. {'type': 'Coal', 'symbol': "5", 'generation': [(1,0),(0,1),(1,1),(1,2),(2,1),(2,2)],'rarity': 10},
  9. {'type': 'Iron', 'symbol': "Fe", 'generation': [(0,0),(1,0),(0,1),(1,1),(1,2),(2,1)],'rarity': 30}
  10. ]
  11. #generatables=['Dirt','Coal','Iron'] # in ascending order of rarity
  12.  
  13. game_map=[]
  14. map_x = 101
  15. map_y = 101
  16. for y in range(0,map_y): #makes a blank map
  17. x_layer = []
  18. for x in range(0,map_x):
  19. x_layer.append('')
  20. game_map.append(x_layer)
  21.  
  22. # for x in map:
  23. # print(x)
  24.  
  25. #convinience functions
  26. def get_index(x,y):
  27. x_index = (((map_x/2).__ceil__())-1)+x
  28. y_index = (y*-1)+((map_y/2).__ceil__())-1
  29. return (y_index,x_index)
  30.  
  31. def get_cords(x_index,y_index):
  32. x = x_index-(((map_x/2).__ceil__())-1)
  33. y = ((y_index+1)-(map_y/2).__ceil__())*-1
  34. return (x,y)
  35.  
  36. def get_from_map_content(key,key_target,dict): ## not sure about func name
  37. for tile_dict in dict:
  38. if tile_dict[key] == key_target: # stopped here
  39. return tile_dict
  40.  
  41. def get_tile_from_map(pos): # receives cords
  42. x = pos[0]
  43. y = pos[1]
  44. index = get_index(x,y)
  45. tile = game_map[index[0]][index[1]]
  46. tile_dict = get_from_map_content('symbol',tile,map_content)
  47.  
  48. return (tile,tile_dict)
  49. #=========================================================================================================================#
  50.  
  51. #Map gen functions
  52.  
  53. # fill = what type of till will be filled in that tile
  54. def fill_map(point1,point2,content='Air'): #works like minecraft fill command only receives index
  55. #in content you have to input the type
  56. max_x = max(point1[1],point2[1])
  57. min_x = min(point1[1],point2[1])
  58. max_y = max(point1[0],point2[0])
  59. min_y = min(point1[0],point2[0])
  60.  
  61. fill = get_from_map_content('type',content,map_content)
  62.  
  63. fill_symbol = fill['symbol']
  64. fill_type = fill['type']
  65.  
  66. for layer_index, layer in enumerate(game_map):
  67. for collumn_index, tile in enumerate(layer):
  68. if min_y <= layer_index <= max_y and min_x <= collumn_index <= max_x:
  69. game_map[layer_index][collumn_index] = fill_symbol
  70.  
  71. def generate(start_point,fill): #fill is tile type
  72. tile_dict = get_from_map_content('type',fill,map_content)
  73. tile_generation = tile_dict['generation']
  74. size = tile_generation[randint(0,len(tile_generation)-1)]
  75. end_point = (start_point[0]+size[0], start_point[1]+size[1])
  76.  
  77. fill_map(start_point,end_point,fill)
  78.  
  79. def generate_template():
  80. fill_map((0, 0), (map_y - 1, map_y - 1), 'Stone')
  81.  
  82. for layer_index, layer in enumerate(game_map):
  83. for collumn_index, tile in enumerate(layer):
  84. if tile == '[]':
  85. tile_index = (layer_index, collumn_index)
  86.  
  87. def get_rarity(tile_type):
  88. rarity = get_from_map_content('type', tile_type, map_content)['rarity']
  89. return rarity
  90.  
  91. def seq_wrapper(tile_type):
  92. if randint(0, get_rarity(tile_type)) == 0:
  93. generate(tile_index, tile_type)
  94. return tile_type
  95.  
  96. generatables = ['Dirt', 'Coal', 'Iron']
  97.  
  98. result = list(map(seq_wrapper, generatables))
  99.  
  100.  
  101. generate_template()
  102. surface_y = get_cords(0,1)[1]
  103. print(surface_y)
  104. fill_map(get_index(0,0),get_index(0,0),'Stone')
  105. print('==================================================================')
  106. for x in game_map:
  107. print(x)
  108.  
  109. print(get_tile_from_map((-5,-5),))
Add Comment
Please, Sign In to add comment