Advertisement
BizzKeryear

Improve_Move_0.1.0/control.lua

Jul 5th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.94 KB | None | 0 0
  1. -- partly made possible because of code from "turtle speed mod" from DedlySpyder
  2. -- https://mods.factorio.com/mods/DedlySpyder/Turtle_Speed
  3. -- https://github.com/DedlySpyder/Turtle_Speed
  4. -- license: cc by 4.0 (credits in info.json)
  5. require("default_config")
  6. -- initialising script
  7. sneakspeedinput = sneakspeedinput or sneakspeed
  8. crawlspeedinput = crawlspeedinput or crawlspeed
  9. moonwalkspeedinput = moonwalkspeedinput or moonwalkspeed
  10. timeoutinput = timeoutinput or timeout
  11.  
  12. script.on_load(
  13.     function()
  14.         global.subtick = nil
  15.         global.you = nil
  16.         speed=1
  17.         modifiers=1
  18.         a_varible = nil
  19.     end
  20. )
  21.  
  22.  
  23. script.on_init(
  24.     function()
  25.         global.you = nil
  26.         global.subtick = nil
  27.         speed=1
  28.         modifiers=1
  29.         for _, player in pairs(game.players) do
  30.             init_gui(player)
  31.         end
  32.         a_varible = nil
  33.     end
  34. )
  35. --- interface
  36.  
  37.  
  38. function init_gui(player)
  39.     --debug start
  40.     --  if player.gui.top["sneakinButton"] then
  41.     --      player.gui.top["sneakinButton"].destroy()
  42.     --  end    
  43.     --debug end
  44.     if (not player.gui.top["sneakinButton"]) then
  45.         player.gui.top.add{type="button", name="sneakinButton", style="sneakin_button_main"} --,style={top_padding=0,bottom_padding=0},style="flip_button_style_left"
  46. --      player.gui.top.style.top_padding=0
  47. --      player.gui.top.style.bottom_padding=0
  48.         --player.gui.top.sneakinButton.caption = "Sneakin"
  49.         player.gui.top.sneakinButton.tooltip = "Improve Move - settings"
  50.     end
  51. end
  52.  
  53. function expand_gui(player)
  54.     local frame = player.gui.left["sneak-settings"]
  55.     if (frame) then
  56.         frame.destroy()
  57.     else
  58.         frame = player.gui.left.add{type="flow", name="sneak-settings"} -- ,style = "description_title_label_style"
  59.         frame.add{type="label", name="sneak", caption="sneak",style = "description_title_label_style"}
  60.         frame.add{type="textfield", name="sneak-speed", text=sneakspeedinput,style="sneakin_textfield"}
  61.             local stt="Set sneak speed as fraction of base speed here.\n 1=base speed;  0=stop;  allowed 1-0.05"
  62.             frame.sneak.tooltip=stt
  63.             frame["sneak-speed"].tooltip = stt
  64.            
  65.         frame.add{type="label", name="crawl", caption="crawl",style = "description_title_label_style"}
  66.         frame.add{type="textfield", name="crawl-speed", text=crawlspeedinput,style="sneakin_textfield"}
  67.             local ctt ="Set crawl speed as fraction of base speed here.\n1=base speed;  0=stop;  allowed 1-0.05\nDefault=0.1"
  68.             frame.crawl.tooltip=ctt
  69.             frame["crawl-speed"].tooltip = ctt
  70.            
  71.         frame.add{type="label", name="moonwalk", caption="moonwalk",style = "description_title_label_style"}
  72.         frame.add{type="textfield", name="moonwalk-speed", text=moonwalkspeedinput,style="sneakin_textfield"}
  73.             local mtt ="Set the speed you want to walk backwards at as fraction of base speed here.\n1=base speed;  0=stop;  allowed 1-0.05\nDefault=0.8"
  74.             frame.moonwalk.tooltip = mtt
  75.             frame["moonwalk-speed"].tooltip = mtt
  76.        
  77.         frame.add{type="label", name="to", caption="timeout",style = "description_title_label_style"}
  78.         frame.add{type="textfield", name="timeout", text=timeoutinput,style="sneakin_textfield"}
  79.             local ttt = "Set the timeout of the key press here in 1/60 seconds.\nSo e.g. 60 would be one second. Allowed values 1-120\nDefault=20"
  80.             frame.to.tooltip = ttt
  81.             frame.timeout.tooltip = ttt
  82.         --[[
  83.         frame.add{type="button", name="blueprint-string-load", style="blueprintstring_button_load"}
  84.         frame.add{type="button", name="blueprint-string-save-as", style="blueprintstring_button_saveas"}
  85.         frame.add{type="button", name="blueprint-string-save-all", style="blueprintstring_button_saveall"}
  86.         frame.add{type="button", name="blueprint-upgrade", style="blueprintstring_button_upgrade"}
  87.         --]]       
  88.     end
  89. end
  90.  
  91. script.on_event(defines.events.on_gui_click, function(event)
  92.     local player = game.players[event.element.player_index]
  93.     local name = event.element.name
  94.     if (name == "sneakinButton") then
  95.         --debugprint("clicked on button")
  96.         if player.gui.left["sneak-settings"] then
  97.             checknum(player,name)
  98.         else
  99.             sneakspeedinput = sneakspeedinput or sneakspeed
  100.             crawlspeedinput = crawlspeedinput or crawlspeed
  101.             moonwalkspeedinput = moonwalkspeedinput or moonwalkspeed
  102.             timeoutinput = timeoutinput or timeout
  103.         end
  104.         expand_gui(player)
  105.     elseif player.gui.left["sneak-settings"] then
  106.         checknum(player,name)      
  107.     end
  108.     --]]
  109. --[[   
  110.     if (name == "blueprint-string-load") then
  111.         load_blueprint(player)
  112.     elseif (name == "blueprint-string-save-all") then
  113.         save_blueprints(player)
  114.     elseif (name == "blueprint-string-save-as") then
  115.         prompt_for_filename(player)
  116.     elseif (name == "blueprint-string-filename-save") then
  117.         save_blueprint_as(player, player.gui.center["blueprint-string-filename-prompt"]["blueprint-string-filename"].text)
  118.     elseif (name == "blueprint-string-filename-cancel") then
  119.         player.gui.center["blueprint-string-filename-prompt"].destroy()
  120.     elseif (name == "blueprint-string-button") then
  121.         expand_gui(player)
  122.     elseif (name == "blueprint-upgrade") then
  123.         upgrade_blueprint(player)
  124.     end
  125. --]]
  126. end)
  127. function checknum(player,name)
  128.     local player = player
  129.     local name = name
  130.     sneakspeedinput=tonumber(player.gui.left["sneak-settings"]["sneak-speed"].text)
  131.     sneakspeedinput = sneakspeedinput or sneakspeed
  132.     if sneakspeedinput > 1 then
  133.         sneakspeedinput =1
  134.     elseif sneakspeedinput < 0.05 then
  135.         sneakspeedinput = 0.05
  136.     end
  137.     crawlspeedinput=tonumber(player.gui.left["sneak-settings"]["crawl-speed"].text)
  138.     crawlspeedinput = crawlspeedinput or crawlspeed
  139.     if crawlspeedinput > 1 then
  140.         crawlspeedinput =1
  141.     elseif crawlspeedinput < 0.05 then
  142.         crawlspeedinput = 0.05
  143.     end
  144.     moonwalkspeedinput=tonumber(player.gui.left["sneak-settings"]["moonwalk-speed"].text)
  145.     moonwalkspeedinput = moonwalkspeedinput or moonwalkspeed
  146.     if moonwalkspeedinput > 1 then
  147.         moonwalkspeedinput =1
  148.     elseif moonwalkspeedinput < 0.05 then
  149.         moonwalkspeedinput = 0.05
  150.     end
  151.     timeoutinput=math.floor(tonumber(player.gui.left["sneak-settings"]["timeout"].text))
  152.     timeoutinput = timeoutinput or timeout
  153.     if timeoutinput > 120 then
  154.         timeoutinput =120
  155.     elseif timeoutinput < 1 then
  156.         timeoutinput = 1
  157.     end
  158. end
  159.  
  160. ----- mod functions
  161. function on_moonwalker(event) --capture moonwalker hotkey
  162.     you = game.players[event.player_index]
  163.     if (mw==false or mw== nil) then -- toggle function
  164.         mw = true
  165.         mws=moonwalkspeedinput -- backward speed
  166.         float_text(you,"You are now moonwalking\n(Warning: Controls are mirrored)",{r=1,g=0.2,b=0.2})
  167.     else
  168.         mw = false
  169.     end
  170. end
  171.  
  172. --debug function to see if modifiers work as intended
  173. --[[
  174. function on_count(bla)
  175.     for _, player in pairs(game.players) do
  176.     you = player
  177.     --you.print("1: last ="..tostring(last)..", bla ="..tostring(bla))
  178.     if (last==nil) then
  179.         last=bla
  180.         start=game.tick
  181.         --you.print("2: last ="..tostring(last)..", bla ="..tostring(bla))
  182.         --you.print("starting count @ "..tostring(start))
  183.     else
  184.         if (last==bla) then
  185.         --you.print("3: last ="..tostring(last)..", bla ="..tostring(bla))
  186.         else
  187.         total=(game.tick - start)
  188.         --you.print("ending count @ "..tostring(game.tick))
  189.         you.print("took on "..tostring(last)..": "..tostring(total).." ticks in total")
  190.         start=nil
  191.         total=nil
  192.         bla=nil
  193.         last=nil
  194.         end
  195.     end
  196.     end
  197. end
  198. --]]
  199. function on_toggleDriving(event) --overrides getting in and out vehicles
  200.     you = game.players[event.player_index]
  201.     if not you.driving then
  202.         you.driving=true -- get in vehicles normally
  203.     else
  204.         if safe or emer then --test if you can get out safely
  205.             you.driving=false
  206.         else
  207.             you.print("You are too fast. Double tap for emergency exit")
  208.             emer=true
  209.             subtick=0 --set values for testing double tap
  210.         end
  211.     end
  212. end
  213.  
  214. function on_sneak(event) --capture sneak hotkey
  215.     you = game.players[event.player_index] --set "you" to player triggering event
  216.     crawl = false --set crawl to false in case was crawling before
  217.     subtick = 0 --reset timed event
  218.     if (sneak == false or sneak == nil) then -- activate sneak if not active
  219.         if not mw and not you.driving then
  220.             sneak = true
  221.             float_text(you,"Ya now sneakin!",{r=0.2,g=1,b=0.2})
  222.         end
  223.     else
  224.         sneak = false
  225.     end
  226. end
  227.  
  228. function on_crawl(event) --comments see sneak
  229.     you = game.players[event.player_index]
  230.     sneak = false
  231.     subtick = 0
  232.     if (crawl == false or crawl == nil) then
  233.         if not mw and not you.driving then
  234.             crawl = true
  235.             float_text(you,"Ya now crawlin!",{r=0.2,g=1,b=0.2})
  236.         end
  237.     else
  238.         crawl = false
  239.     end
  240. end
  241.  
  242. function on_tick(event) --do this every tick
  243.     offset = nil -- more than one message on a tick?
  244.     if subtick==nil then
  245.         -- if subtick not set do it now (crash prevention)
  246.         subtick = 0
  247.     end
  248.     if you==nil then
  249.         --if you not set do it now (crash prevention)
  250.         you={}
  251.         you.walking_state ={}
  252.         you.walking_state.walking = false
  253.     end
  254.     if you.driving then
  255.         --if driving check positions every tick, if changed between last tick mark as unsafe
  256.         if (lastx == you.position.x and lasty == you.position.y) then
  257.             safe=true
  258.         else
  259.             safe=false
  260.             lastx = you.position.x
  261.             lasty = you.position.y
  262.         end
  263.     end
  264.     if (((sneak or crawl or emer) and not you.walking_state.walking) and subtick >= timeoutinput ) then
  265.     --
  266.     --if a status of a timed event is set and not walking then reset them after 1/3 second
  267.     --Most of function only needed cause unable to check for key down.
  268.         if not emer then
  269.             if sneak then
  270.                 out= "sneakin"
  271.             else
  272.                 out= "crawlin"
  273.             end
  274.             float_text(you,"Stopped "..out.."!",{r=0.2,g=1,b=0.2})
  275.             sneak = false --reset modifiers
  276.             crawl = false
  277.             emer = false
  278.             subtick = 120 -- also stop counting
  279.             speed = 1
  280.             modifiers = 1
  281.         end
  282.     else
  283.         if ((sneak or crawl) and you.walking_state.walking) then
  284.         -- if walking keep the modifiers active.. if any
  285.             subtick = 0
  286.         end
  287.     end
  288.     if (you.walking_state.walking and (sneak or crawl or mw)) then
  289.         modifiers = 1+calcRunningModifiers(you) --calculate movement bonuses and add base speed to it
  290.         data=tostring(you.surface.get_tile(you.position.x, you.position.y).name)
  291.         --get the name of tile you are on
  292.         pattern="concrete" -- if tile name includes concrete change speed accordantly
  293.         if string.find(data, pattern) ~= nil then
  294.             ground = 0.73  --value not calculated,but got by trial and error
  295.             --on_count(pattern) --debug
  296.         else
  297.             pattern="stone" -- if tile name includes stone change speed accordantly
  298.             if string.find(data, pattern) ~= nil then
  299.                 ground= 0.8 --value not calculated,but got by trial and error
  300.                 --on_count(pattern) --debug
  301.             else
  302.                 ground = 1 --enywhere else
  303.                 --on_count("ground") --debug
  304.             end
  305.         end
  306.         if not mw then --not moonwalking?
  307.             if sneak then --but sneaking?
  308.                 speed=sneakspeedinput*ground -- set speed accordingly sneak speed and ground modificator
  309.             else
  310.                 if crawl then -- or crawling
  311.                     speed=crawlspeedinput*ground -- set speed accordingly crawl speed and ground modificator
  312.                 else --else reset
  313.                     speed = 1
  314.                     modifiers = 1
  315.                 end
  316.             end
  317.         else
  318.             speed=-mws*ground -- set moonwalking speed
  319.         end
  320.         --you=nil
  321.     end
  322.     if not (sneak or crawl or mw) then
  323.         speed = 1
  324.         modifiers = 1
  325.     end
  326.     final=(1*speed/modifiers)-1 --last step remove the modifiers if any
  327.     you.character_running_speed_modifier = final --now set the speed
  328.     if subtick <= 120 then
  329.         --stop counting ticks if more than 120
  330.         subtick = subtick + 1
  331.     end
  332. end
  333.  
  334. function calcRunningModifiers(player) --function mainly copied from 'turtle speed mod' from DedlySpyder
  335.     local armorInventory = player.get_inventory(defines.inventory.player_armor)
  336.     local movementSpeedModifier = 0
  337.     if (armorInventory ~= nil) then
  338.         local stack = armorInventory[1]
  339.         if (stack.valid_for_read and stack.has_grid) then  
  340.             local equipmentList = stack.grid.equipment     
  341.             for _, equipment in pairs(equipmentList) do
  342.                 if (equipment.movement_bonus > 0) then
  343.                     movementSpeedModifier = movementSpeedModifier + equipment.movement_bonus
  344.                 end
  345.             end
  346.         end
  347.     end
  348.     return movementSpeedModifier
  349. end
  350.  
  351. function debugprint(text)
  352.     for _, player in pairs(game.players) do
  353.         player.print(tostring(text))
  354.     end
  355. end
  356.  
  357. function float_text(ap,text,color) --get out a text easily
  358.     color = color or {r=0.5,g=1,b=1}
  359.     text = text or "hello world (you stupid didn't send a text)"
  360.     text = tostring(text)
  361.     if type(color) ~= "table" then -- color valid?
  362.         color = {r=0.5,g=1,b=1}
  363.     end
  364.     if ap ~=nil and type(ap) ~= "table" and not ap.valid then
  365.         ap=nil
  366.         text = "Warning: Message send to a not valid player " .. text
  367.     end
  368.     ap = ap or game.players[1] -- no player set? do it for first only
  369.     offset = offset or 1
  370.     --if triggerend more than once per tick ... next message is moved down to prevent overlapping --- offset has to be reset every tick (see on_tick)
  371.     if offset > 0 then
  372.         offset = offset + 1
  373.     end
  374.     posx=ap.position.x-((string.len(text)/2)/10)
  375.     posy=ap.position.y+offset
  376.     game.surfaces[1].create_entity{name="flying-text", position={posx,posy}, text=text, color=color}
  377.     return game.tick
  378. end
  379.  
  380. --event definitions
  381. script.on_event(defines.events.on_tick, on_tick)
  382. script.on_event("sneak_modifier", on_sneak)
  383. script.on_event("crawl_modifier", on_crawl)
  384. script.on_event("moonwalker", on_moonwalker)
  385. script.on_event("toggle-driving", on_toggleDriving)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement