Advertisement
Kryzeth

control.lua for dirt path mod

Apr 10th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.13 KB | None | 0 0
  1. DIRT_THRESHOLD = 10
  2.  
  3. if MODULE_LIST then
  4.     module_list_add("Dirt Path")
  5. end
  6.  
  7. --This is all subjective.
  8. DIRT= {
  9.     ["grass-1"]="grass-3",
  10.     ["grass-2"]="grass-3",
  11.     ["grass-3"]="grass-4",
  12.     ["grass-4"]="dirt-4",
  13.     ["dirt-4"]="dirt-6",
  14.     ["dirt-6"]="dirt-7",
  15.     ["dirt-7"]="dirt-5",
  16.     ["dirt-5"]="dirt-3",
  17.     ["dirt-3"]="dirt-2",
  18.     ["dirt-2"]="dirt-1",
  19.     ["dirt-1"]="red-desert-3",
  20.     ["red-desert-3"]="sand-3",
  21.  
  22.     ["red-desert-0"]="red-desert-1",
  23.     ["red-desert-1"]="red-desert-2",
  24.     ["red-desert-2"]="red-desert-3"
  25. }
  26.  
  27. global.dirt = {}
  28.  
  29. function dirtDirt(event)
  30.     --for __, p in pairs(game.connected_players) do
  31.         local p = game.players[event.player_index]
  32.    
  33.         -- Trains aren't cars!  This breaks it.  Dunno why they're handled differently.
  34.         --if p.walking_state.walking or (p.driving and p.vehicle.speed ~= 0) then
  35.         -- Special conditional check for Factorissimo
  36.         if p.walking_state.walking or (p.vehicle and p.vehicle.type == "car" and p.vehicle.speed ~= 0) then
  37.             local tile = p.surface.get_tile(p.position)
  38.             if p.surface == game.surfaces[1] then
  39.                 if not (tile.hidden_tile or string.find(tile.name, "concrete")) then
  40.                
  41.                 --game.print("Dirt value now at: ".. global.dirt[tile.position.x][tile.position.y])
  42.                 --if global.dirt[tile.position.x][tile.position.y] >= DIRT_THRESHOLD then
  43.                     --game.print("Converting patch to dirt.")
  44.                    
  45.                     -- No longer necessary for 0.16
  46.                     -- Check for waterfix, else prevent exploit
  47.                     -- local waterfix = false
  48.                     -- if game.active_mods["water-fix"] then
  49.                     --  waterfix = true
  50.                     -- end
  51.                     -- -- for module, version in pairs(game.active_mods) do
  52.                     --  -- if module == "water-fix" then
  53.                     --      -- waterfix = true
  54.                     --  -- end
  55.                     -- -- end
  56.                     -- if not waterfix then
  57.                     -- -- Check for water to prevent landfill exploit
  58.                     --  for xx = -1, 2 do
  59.                     --      for yy = -1, 2 do
  60.                     --          local waterCheck = p.surface.get_tile(tile.position.x + xx, tile.position.y + yy)
  61.                     --          if not waterCheck or not waterCheck.valid or waterCheck.collides_with("water-tile") then
  62.                     --              return
  63.                     --          end
  64.                     --      end
  65.                     --  end
  66.                     -- end
  67.  
  68.                     dirtAdd(tile.position.x, tile.position.y) --Wear the center tile out one additional step.
  69.                     local dirt = {}
  70.                     for xx = -1, 1 do
  71.                         for yy = -1, 1 do
  72.                             if not (math.abs(xx) == math.abs(yy)) or xx == 0 then
  73.                                 -- Check twice at xx == 0, yy == 0
  74.                                 if dirtAdd(tile.position.x + xx, tile.position.y + yy) then
  75.  
  76.                                     local validTile = p.surface.get_tile(tile.position.x + xx, tile.position.y + yy)
  77.                                     if not validTile.collides_with("water-tile") and not validTile.hidden_tile and not string.find(validTile.name, "sand") then
  78.                                         local newtile = DIRT[validTile.name] or "dirt-6"
  79.                                         table.insert(dirt, {name=newtile, position={tile.position.x+xx, tile.position.y+yy}})
  80.                                     end
  81.                                 end
  82.                             end
  83.                         end
  84.                     end
  85.                     if #dirt > 0 then
  86.                         p.surface.set_tiles(dirt)
  87.                     end
  88.                 end
  89.             end
  90.         end
  91.     --end
  92. end
  93.  
  94. function dirtAdd(x, y)
  95.     local key = x .. "," .. y
  96.     if global.dirt[key] then
  97.         global.dirt[key] = global.dirt[key] + 1
  98.     else   
  99.         global.dirt[key] = 1
  100.     end
  101.     if global.dirt[key] >= DIRT_THRESHOLD then
  102.         global.dirt[key] = 0
  103.         return true
  104.     end
  105. end
  106.  
  107. function cleanDirt()
  108.     if not global.dirt then
  109.         log("Dirt Path not initialized!")
  110.         return
  111.     end
  112.     for k, v in pairs(global.dirt) do
  113.         global.dirt[k] = global.dirt[k] - 1
  114.         if global.dirt[k] <= 0 then
  115.             global.dirt[k] = nil
  116.         end
  117.     end
  118. end
  119.  
  120. function dirt_handler(event)
  121.     -- if event.tick % 30 == 0 then
  122.     --  dirtDirt()
  123.     -- end
  124.     --if (event.tick) % (60 * 2) == 0 then -- debug
  125.     if (event.tick+500) % (60 * 60 * 30) == 0 then
  126.         cleanDirt()
  127.     end
  128. end
  129.  
  130. --Since the migration failed:
  131. --Migration from 1.1.1
  132. script.on_configuration_changed(function()
  133.     if not global.flattening then
  134.         global.flattening = true
  135.         global.dirt = {}
  136.     end
  137. end)
  138.  
  139. script.on_nth_tick(108000, cleanDirt) --30 minutes
  140. --script.on_nth_tick(300, function() game.write_file("dirtdump", serpent.block(global.dirt)) end)
  141. script.on_event(defines.events.on_player_changed_position, function(event) dirtDirt(event) end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement