Advertisement
Guest User

Factory Game Devlog 2 Code

a guest
Aug 2nd, 2022
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D
  2.  
  3. var ticks = 0
  4. var groups = []
  5. var crafters = []
  6. var crafter_recipies = []
  7. var crafter_rects = []
  8. const directions = {
  9.     [false, false]:Vector2(1,0),
  10.     [true, true]:Vector2(0,1),
  11.     [true, false]:Vector2(-1,0),
  12.     [false, true]:Vector2(0,-1)
  13.     }
  14.  
  15. const block_types = {
  16.     "fist":0,
  17.     "otherfist":3,
  18.     "joiner":1,
  19.     "block":4,
  20.     "spawner":2,
  21.     "arm":5,
  22.     "crafter":6,
  23.     "bin":8
  24. }
  25.  
  26. const recipies = {
  27.     [[-2, 4, -2],
  28.     [4, 4, 4],
  29.     [-2, 4, -2],]:[[-2,-1,-2],
  30.                     [-1,7,-1],
  31.                     [-2,-1,-2]],
  32.     [[4, 4, 4],
  33.     [4, -2, 4],
  34.     [4, 4, 4],]:[[-1,-1,-1],
  35.                     [-1,-2,-1],
  36.                     [-1,7,-1]],
  37.     [[4, 4, 4],
  38.     [4, 4, 4],
  39.     [4, 4, 4],]:[[-1,-1,-1],
  40.                     [-1,7,-1],
  41.                     [-1,-1,-1]]
  42. }
  43.  
  44. func flood_fill(pos, cells, list):
  45.     var index = cells.find(pos)
  46.     if index != -1:
  47.         list.append(pos)
  48.         cells.remove(index)
  49.         flood_fill(pos + Vector2(1,0), cells, list)
  50.         flood_fill(pos + Vector2(-1,0), cells, list)
  51.         flood_fill(pos + Vector2(0,1), cells, list)
  52.         flood_fill(pos + Vector2(0,-1), cells, list)
  53.  
  54. func find_crafters():
  55.     var crafter_cells = $machine.get_used_cells_by_id(block_types["crafter"])
  56.     while not crafter_cells.empty():
  57.         var crafter = []
  58.         flood_fill(crafter_cells[0], crafter_cells, crafter)
  59.         crafters.append(crafter)
  60.         crafter_recipies.append(null)
  61.        
  62. func min_vec(list):
  63.     var min_vec = Vector2(9999,9999)
  64.     for vec in list:
  65.         min_vec.x = min(min_vec.x, vec.x)
  66.         min_vec.y = min(min_vec.y, vec.y)
  67.     return min_vec
  68. func max_vec(list):
  69.     var max_vec = Vector2(-9999,-9999)
  70.     for vec in list:
  71.         max_vec.x = max(max_vec.x, vec.x)
  72.         max_vec.y = max(max_vec.y, vec.y)
  73.     return max_vec
  74.        
  75. func find_recipies():
  76.     for index in crafters.size():
  77.         var crafter = crafters[index]
  78.         var start = min_vec(crafter)
  79.         var rect = Rect2(start, max_vec(crafter)-start+Vector2(1,1))
  80.         crafter_rects.append(rect)
  81.            
  82.         for recipie in recipies:
  83.             var broken = false
  84.             if recipie.size() == rect.size.y and recipie[0].size() == rect.size.x:
  85.                 for x in rect.size.x:
  86.                     for y in rect.size.y:
  87.                         var in_crafter =  recipie[y][x] != -2
  88.                         if (Vector2(x,y)+rect.position in crafter) != in_crafter:
  89.                             broken = true
  90.                             break
  91.                     if broken:
  92.                         break
  93.                 if not broken:
  94.                     crafter_recipies[index] = recipie#they store copy of recipie insted of index
  95.                     break
  96.                
  97.            
  98.  
  99. func scan_colours():
  100.     for index in range(64):
  101.         var group = $colours.get_used_cells_by_id(index)
  102.         if !group.empty():
  103.             groups.append(group)
  104.             for pos in group:
  105.                 $blocks.set_cellv(pos, block_types["block"])
  106.                 $visible_blocks.set_cellv(pos, block_types["block"])
  107.  
  108. func do_debug():
  109.     $colours.clear()
  110.     for index in groups.size():
  111.         for pos in groups[index]:
  112.             $colours.set_cellv(pos, index%64)
  113.  
  114. func _ready():
  115.     scan_colours()
  116.     find_crafters()
  117.     find_recipies()
  118.  
  119.  
  120. func _process(_delta):
  121.     if Input.is_action_just_pressed("ui_right"):
  122.         tick()
  123.     #print(Engine.get_frames_per_second())
  124.  
  125. func tick():
  126.     ticks+=1
  127.  
  128.     for pos in $machine.get_used_cells():
  129.         var state = $machine.get_cellv(pos)
  130.         var direction = get_direction(pos)
  131.  
  132.         if ticks % 2 == 0:
  133.             if state == block_types["fist"]:
  134.                 var cell = $blocks.get_cellv(pos+direction)
  135.                 if cell != -1:
  136.                     move(pos+direction, direction)
  137.  
  138.         else:
  139.             if state == block_types["spawner"] and $blocks.get_cellv(pos) == -1:
  140.                 $blocks.set_cellv(pos, block_types["block"])
  141.             elif state == block_types["otherfist"]:
  142.                 var cell = $blocks.get_cellv(pos+direction)
  143.                 if cell != -1:
  144.                     move(pos+direction, direction)
  145.                    
  146.     for pos in $machine.get_used_cells():
  147.         var state = $machine.get_cellv(pos)
  148.         var direction = get_direction(pos)
  149.  
  150.         if state == block_types["joiner"] and $machine.is_cell_x_flipped(pos.x, pos.y):
  151.             join(pos, direction)
  152.                    
  153.     for pos in $machine.get_used_cells():
  154.         var state = $machine.get_cellv(pos)
  155.         var direction = get_direction(pos)
  156.  
  157.         if ticks % 2 == 0:
  158.             pass
  159.         else:
  160.             if state == block_types["arm"]:
  161.                 do_arm(pos, direction)
  162.  
  163.     for pos in $machine.get_used_cells():
  164.         var state = $machine.get_cellv(pos)
  165.         var direction = get_direction(pos)
  166.  
  167.         if state == block_types["joiner"] and $machine.is_cell_x_flipped(pos.x, pos.y):
  168.             join(pos, direction)
  169.         elif state == block_types["bin"]:
  170.             var index = find_nested_index(pos, groups)
  171.             if index != -1:
  172.                 for group_pos in groups[index]:
  173.                     $blocks.set_cellv(group_pos, -1)
  174.                 groups.remove(index)
  175.             else:
  176.                 $blocks.set_cellv(pos, -1)
  177.                
  178.     for crafter_index in crafters.size():
  179.         var crafter = crafters[crafter_index]
  180.         var index = find_nested_index(crafter[0], groups)
  181.         if index != -1:
  182.             var recipie = crafter_recipies[crafter_index]
  183.             if recipie!=null:
  184.                 var broken = false
  185.                 for pos in crafter:
  186.                     var recipie_pos = pos-crafter_rects[crafter_index].position
  187.                     if not recipie[recipie_pos.y][recipie_pos.x] == $blocks.get_cellv(pos):
  188.                         broken = true
  189.                         break
  190.                     if not pos in groups[index]:
  191.                         broken = true
  192.                         break
  193.                 if broken:
  194.                     continue
  195.                 groups.remove(index)
  196.                 for pos in crafter:
  197.                     var recipie_pos = pos-crafter_rects[crafter_index].position
  198.                     var item = recipies[recipie][recipie_pos.y][recipie_pos.x]
  199.                     if item != -2:
  200.                         $blocks.set_cellv(pos, item)#can do without dict if i store index of recipe:output pair
  201.            
  202.  
  203.     do_debug()
  204.  
  205. func do_arm(arm_pos, direction):
  206.     var start_index = find_nested_index(arm_pos-direction, groups)
  207.     var end_index = find_nested_index(arm_pos+direction, groups)
  208.  
  209.     var start_group = []
  210.     if start_index!=-1:
  211.         start_group = groups[start_index].duplicate(true)
  212.  
  213.     var end_group = []
  214.     if end_index!=-1:
  215.         end_group = groups[end_index].duplicate(true)
  216.  
  217.     var moves = []
  218.  
  219.     for index in start_group.size():
  220.         var pos = start_group[index]
  221.         var startpos = pos
  222.         pos-=arm_pos
  223.         pos=-pos
  224.         pos+=arm_pos
  225.         if $blocks.get_cellv(pos)!=-1:
  226.             return false
  227.         start_group[index]=pos
  228.         moves.append([startpos, pos])
  229.  
  230.     for index in end_group.size():
  231.         var pos = end_group[index]
  232.         var startpos = pos
  233.         pos-=arm_pos
  234.         pos=-pos
  235.         pos+=arm_pos
  236.         if $blocks.get_cellv(pos)!=-1:
  237.             return false
  238.         end_group[index]=pos
  239.         moves.append([startpos, pos])
  240.  
  241.     if start_index==-1 and end_index==-1:#individual blocks
  242.         if $blocks.get_cellv(arm_pos-direction)!=-1:
  243.             moves.append([arm_pos-direction, arm_pos+direction])
  244.         elif $blocks.get_cellv(arm_pos+direction)!=-1:
  245.             moves.append([arm_pos+direction, arm_pos-direction])
  246.  
  247.     for move in moves:
  248.         if start_index!=-1:
  249.             groups[start_index]=start_group
  250.         if end_index!=-1:
  251.             groups[end_index]=end_group
  252.         var cell = $blocks.get_cellv(move[0])
  253.         $blocks.set_cellv(move[0], -1)
  254.         $blocks.set_cellv(move[1], cell)
  255.  
  256.  
  257.  
  258. func join(pos, direction):
  259.     var other_pos = pos+direction
  260.     if $blocks.get_cellv(pos) != -1 and $blocks.get_cellv(other_pos) != -1:#ugly solution to adding to existing group
  261.         var found = [find_nested(pos, groups), find_nested(other_pos, groups)]
  262.         if not (found[0].empty() and found[1].empty()):
  263.             if found[0].empty():
  264.                 if not pos in found[1]:
  265.                     found[1].append(pos)
  266.             elif found[1].empty():
  267.                 if not other_pos in found[0]:
  268.                     found[0].append(other_pos)
  269.             elif found[0]!=found[1]:
  270.                 groups.erase(found[0])
  271.                 var index = find_nested_index(other_pos, groups)#index of one i didnt remove
  272.                 groups[index]+=found[0]
  273.         else:
  274.             groups.append([pos,other_pos])
  275.  
  276.  
  277. func get_direction(pos):
  278.     return directions[[$machine.is_cell_x_flipped(pos.x, pos.y), $machine.is_cell_transposed(pos.x, pos.y)]]
  279.  
  280. func move(pos, direction):
  281.     var done_groups = []
  282.     var tiles = []
  283.     if push_wrapper(pos, direction, tiles, done_groups):
  284.         var states = []
  285.         for tile in tiles:
  286.             states.append($blocks.get_cellv(tile))
  287.             $blocks.set_cellv(tile, -1)
  288.         for index in tiles.size():
  289.             $blocks.set_cellv(tiles[index]+direction, states[index])
  290.  
  291. func push_wrapper(pos, direction, tiles, done_groups):
  292.     var group = find_nested(pos, groups)
  293.     if !group.empty() and not group in done_groups:
  294.         done_groups.append(group)
  295.         for pos in group:
  296.             if not pos+direction in group:
  297.                 if not push(pos, direction, tiles, done_groups):
  298.                     return false
  299.         for pos in group:#lazy fix for all the tiles that arent on the outward face(right face to move right etc)
  300.             if pos+direction in group:
  301.                 tiles.append(pos)
  302.         for index in group.size():
  303.             group[index]+=direction
  304.         return true
  305.     else:
  306.         return push(pos, direction, tiles, done_groups)
  307.  
  308. func push(pos, direction, tiles, done_groups):
  309.     var newpos = pos+direction
  310.     var check = $blocks.get_cellv(newpos)
  311.     if check==1:#if its check is in a wall
  312.         return false
  313.     else:
  314.         if not pos in tiles:
  315.             tiles.append(pos)
  316.         if check==-1:#if its check is air
  317.             return true
  318.         else:#otherwise move the checked tile
  319.             return push_wrapper(newpos, direction, tiles, done_groups)
  320.  
  321.  
  322. func find_nested(item, list):
  323.     for sublist in list:
  324.         if item in sublist:
  325.             return sublist
  326.     return []
  327.  
  328. func find_nested_index(item, list):
  329.     for index in list.size():
  330.         if item in list[index]:
  331.             return index
  332.     return -1
  333.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement