Advertisement
skypop

CC LaserConfig

Aug 21st, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.05 KB | None | 0 0
  1. -- Laser sentry based mob farm
  2. -- by SukaiPoppuGo
  3. --
  4. -- Config API
  5. --
  6.  
  7.  
  8. --------------------------------------------------
  9. -- Default settings file
  10. local SETTINGS_FILE = "mob_spawnerPosition"
  11. file = {}
  12. function file.get() return SETTINGS_FILE end
  13. function file.set(s) SETTINGS_FILE = s end
  14.  
  15.  
  16. --------------------------------------------------
  17. -- Display
  18. local screenW, screenH = term.getSize()
  19. local screenHalfW, screenHalfH = math.floor(screenW/2), math.floor(screenH/2)
  20. local screenHR, screenHalfHR = string.rep("-", screenW), string.rep("-", screenHalfW)
  21.  
  22. --------------------------------------------------
  23. -- utils
  24. function string.ucfirst(sText)
  25.     return string.upper(string.sub(sText,1,1))..string.sub(sText,2)
  26. end
  27.  
  28. --------------------------------------------------
  29. -- function init
  30. -- Load settings file, or attempts to create it
  31. --
  32. -- return table mob_spawner Relative coordinates X, Y, Z
  33. function init()
  34.     -- init modules
  35.     local modules = peripheral.find("manipulator")
  36.     if not modules then error("Cannot find manipulator", 0) end
  37.  
  38.     --------------------------------------------------
  39.     -- Config mob_spawner relative position
  40.     if not settings.load(SETTINGS_FILE) then
  41.         term.setCursorPos(1, 1)
  42.         term.clear()
  43.         print(screenHR)
  44.         print("Laser sentry - config wizard")
  45.         print(screenHR)
  46.         print("Mob spawner relative coordinates")
  47.         print(screenHalfHR)
  48.  
  49.         -- auto config
  50.         local selectedBlock = false
  51.         if modules.hasModule("plethora:scanner") then
  52.             print("Auto config")
  53.             local map = modules.scan()
  54.             print("scan result:", #map)
  55.             term.clearLine()
  56.             for i,block in ipairs(map) do
  57.                 if block.name == "minecraft:mob_spawner" then
  58.                     print(screenHalfHR)
  59.                     print("#"..i, block.name)
  60.                     print("X:", block.x, ", Y:", block.y, ", Z:", block.z)
  61.                     print("Dir:",
  62.                         ( block.z > 0 ) and "south"      or ( block.z < 0 and "north"        or ""            ),
  63.                         ( block.x > 0 ) and "east"       or ( block.x < 0 and "west"         or ""            ),
  64.                         ( block.y > 0 ) and "above (up)" or ( block.y < 0 and "below (down)" or "same height" )
  65.                     )
  66.                     term.write("Select this target Y/N ? ")
  67.                     repeat
  68.                         local e,p = os.pullEvent("key")
  69.                         if p == keys.n then
  70.                             print("> No.")
  71.                             print(screenHalfHR)
  72.                         elseif p == keys.y then
  73.                             print("> Yes.")
  74.                             selectedBlock = block
  75.                         end
  76.                     until p == keys.n or p == keys.y
  77.                     if selectedBlock then
  78.                         break
  79.                     end
  80.                 else
  81.                     local cursor = {term.getCursorPos()}
  82.                     term.setTextColor(colors.gray)
  83.                     term.clearLine()
  84.                     term.write(math.floor(i*100/#map).."% "..(block.name or "nil"))
  85.                     term.setTextColor(colors.white)
  86.                     term.setCursorPos(unpack(cursor))
  87.                     if i%math.floor(#map/50)==0 then sleep() end
  88.                 end
  89.             end
  90.             if not selectedBlock then
  91.                 printError("Mob spawner block not found.")
  92.                 print("May be out of range for block scanner.")
  93.             end
  94.             print(screenHalfHR)
  95.         end
  96.        
  97.         -- manual config
  98.         if not modules.hasModule("plethora:scanner") or not selectedBlock then
  99.             print("Manual config")
  100.             print("Input mob_spawner relative position\n(from the laser position)")
  101.             selectedBlock = {}
  102.             for i,axe in ipairs( {"x", "y", "z"} ) do
  103.                 local input
  104.                 repeat
  105.                     term.setTextColor(colors.lightGray)
  106.                     term.write("Relative "..string.upper(axe).." >")
  107.                     term.setTextColor(colors.white)
  108.                     input = tonumber( read() )
  109.                 until input
  110.                 term.setTextColor(colors.white)
  111.                 selectedBlock[ axe ] = input
  112.             end
  113.             print(screenHalfHR)
  114.         end
  115.        
  116.         -- save config
  117.         if selectedBlock then
  118.             settings.set("mob_spawner.x", selectedBlock.x)
  119.             settings.set("mob_spawner.y", selectedBlock.y)
  120.             settings.set("mob_spawner.z", selectedBlock.z)
  121.             settings.save(SETTINGS_FILE)
  122.             assert(fs.exists(SETTINGS_FILE), string.format("Fails to save settings file \"%s\"", SETTINGS_FILE))
  123.             print("Settings saved:", SETTINGS_FILE)
  124.         end
  125.     end
  126.  
  127.     local mob_spawner = {
  128.         x = settings.get("mob_spawner.x", false),
  129.         y = settings.get("mob_spawner.y", false),
  130.         z = settings.get("mob_spawner.z", false),
  131.     }
  132.     assert(fs.exists(SETTINGS_FILE) and mob_spawner.x and mob_spawner.y and mob_spawner.z, string.format([[Warning!
  133.     Your settings file "%s" may be corrupted. You should delete it and restart]], SETTINGS_FILE))
  134.     assert(mob_spawner.x and mob_spawner.y and mob_spawner.z, string.format([[Warning!
  135.     Setup Laser sentry require mob_spawner relative position.
  136.     Install block scanner module, or create a settings file "%s" with each relative coordinates:
  137.     {
  138.       mob_spawner.x = number,
  139.       mob_spawner.y = number,
  140.       mob_spawner.z = number,
  141.     }]], SETTINGS_FILE))
  142.    
  143.     print(string.rep("-", screenW))
  144.     print("Laser sentry - config wizard terminated")
  145.     print(string.rep("-", screenW))
  146.    
  147.     return mob_spawner
  148. end
  149.  
  150. --------------------------------------------------
  151. -- Init Entity whitelist
  152. function getWhiteList()
  153.  
  154.     term.setCursorPos(1,1)
  155.     term.clear()
  156.     print(screenHR)
  157.     print("Laser sentry - White list wizard")
  158.     print(screenHR)
  159.    
  160.     os.loadAPI("api/EnumMob.lua")
  161.     assert(EnumMob, "Require API EnumMob.lua\npastebin get mAraxgcc api/EnumMob.lua")
  162.    
  163.     local mobList = EnumMob
  164.     local function completeMobName(sText)
  165.         if sText == "" then
  166.             return ""
  167.         end
  168.         sText = string.ucfirst(sText)
  169.         return textutils.complete(sText, mobList)
  170.     end
  171.    
  172.     local prevWhiteList = settings.get("mob_spawner.whiteList", {})
  173.     local selectedMobs = prevWhiteList
  174.    
  175.     -- Whitelist display
  176.     local displayList = window.create(
  177.         term.current(),
  178.         screenW - screenHalfW +1, 4, --pos
  179.         screenHalfW, screenH -3, --size
  180.         true)
  181.    
  182.     -- Whitelist display update
  183.     function displayList.update()
  184.         displayList.setCursorPos(1,1)
  185.         displayList.setBackgroundColor(colors.gray)
  186.         displayList.clear()
  187.         displayList.blit(
  188.             string.rep("\131", screenHalfW),
  189.             string.rep("f", screenHalfW),
  190.             string.rep("0", screenHalfW)
  191.         )
  192.         displayList.setCursorPos(1,2)
  193.         displayList.blit(
  194.             string.sub(" White list"..string.rep(" ", screenHalfW), 1, screenHalfW),
  195.             string.rep("f", screenHalfW),
  196.             string.rep("0", screenHalfW)
  197.         )
  198.        
  199.         local y=4
  200.         for name,keep in pairs(selectedMobs) do
  201.             if keep == true then
  202.                 displayList.setCursorPos(2, y)
  203.                 displayList.write(name)
  204.                 y = y+1
  205.                 if y > screenH -5 then break end
  206.             end
  207.         end
  208.         displayList.redraw()
  209.     end
  210.    
  211.     -- Whitelist edit
  212.     term.setCursorPos(1,4)
  213.     print("Define a white list :")
  214.     print("Write which mob's types\nshould be shot by laser")
  215.     print(screenHalfHR)
  216.     local cursor = {term.getCursorPos()}
  217.     local confirmSave = false
  218.     repeat -- back to edit or save/not save and exit
  219.         repeat --adding several mobs in a list
  220.             term.setCursorPos(1,8)
  221.             term.clearLine()
  222.             displayList.update()
  223.            
  224.             --Input mob types
  225.             term.setCursorPos(1,8)
  226.             term.write("> ")
  227.             local input = string.ucfirst( read(nil, nil, completeMobName) )
  228.            
  229.             --Already in whitelist -> ask to remove
  230.             if selectedMobs[ input ] == true then
  231.                 print("Already listed")
  232.                 print("Remove this mob ? Y/N")
  233.                 local confirm
  234.                 repeat
  235.                     local e,p = os.pullEvent("key")
  236.                     os.pullEvent("key_up") --clear char event
  237.                     if p == keys.y then
  238.                         selectedMobs[ input ] = false
  239.                     end
  240.                 until p == keys.n or p == keys.y
  241.                 term.setCursorPos(1,9)
  242.                 term.clearLine()
  243.                 term.setCursorPos(1,10)
  244.                 term.clearLine()
  245.                
  246.             --Add in whitelist
  247.             elseif EnumMob[ input ] then
  248.                 selectedMobs[ input ] = true
  249.            
  250.             --Unreconized mobs
  251.             elseif input ~= "" and not EnumMob[ input ] then
  252.                 print("isn't registered as mob")
  253.                 print("in database. Confirm: Y/N")
  254.                 local confirm = true
  255.                 repeat
  256.                     local e,p = os.pullEvent("key")
  257.                     os.pullEvent("key_up") --clear char event
  258.                     confirm = p == keys.y
  259.                 until p == keys.n or p == keys.y
  260.                 if confirm then
  261.                     selectedMobs[ input ] = true
  262.                 end
  263.                 term.setCursorPos(1,9)
  264.                 term.clearLine()
  265.                 term.setCursorPos(1,10)
  266.                 term.clearLine()
  267.             end
  268.        
  269.         --Enter empty line to exit
  270.         until #selectedMobs > 0 or input == ""
  271.        
  272.         --Saving confirmation
  273.         term.setCursorPos(1,8)
  274.         term.clearLine()
  275.         displayList.update()
  276.         term.setCursorPos(1,8)
  277.         print("Save white list ? Y/N")
  278.         print("Any key to resume")
  279.         local e,p = os.pullEvent("key")
  280.         os.pullEvent("key_up") --clear char event
  281.         confirmSave = p == keys.y
  282.     until p == keys.n or p == keys.y
  283.    
  284.     --clear display
  285.     displayList.setVisible(false)
  286.     displayList.redraw()
  287.     for y=4, screenH do
  288.         term.setCursorPos(1,y)
  289.         term.clearLine()
  290.     end
  291.     term.setCursorPos(1,4)
  292.    
  293.     --Do not save
  294.     if confirmSave then
  295.         --Saving
  296.         settings.set("mob_spawner.whiteList", selectedMobs)
  297.         settings.save(SETTINGS_FILE)
  298.         assert(fs.exists(SETTINGS_FILE), string.format([[Warning!
  299.         Your settings file "%s" may be corrupted. You should delete it and restart]], SETTINGS_FILE))
  300.     else
  301.         print("Abort white list edition")
  302.         print("Press any key")
  303.         os.pullEvent("key")
  304.         os.pullEvent("key_up") --clear char event
  305.         selectedMobs = prevWhiteList
  306.     end
  307.    
  308.     --Display list (final)
  309.     term.write("whiteList = ")
  310.     textutils.pagedPrint(textutils.serialize(selectedMobs))
  311.  
  312.     return selectedMobs
  313. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement