Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- partly made possible because of code from "turtle speed mod" from DedlySpyder
- -- https://mods.factorio.com/mods/DedlySpyder/Turtle_Speed
- -- https://github.com/DedlySpyder/Turtle_Speed
- -- license: cc by 4.0 (credits in info.json)
- require("default_config")
- -- initialising script
- sneakspeedinput = sneakspeedinput or sneakspeed
- crawlspeedinput = crawlspeedinput or crawlspeed
- moonwalkspeedinput = moonwalkspeedinput or moonwalkspeed
- timeoutinput = timeoutinput or timeout
- script.on_load(
- function()
- global.subtick = nil
- global.you = nil
- speed=1
- modifiers=1
- a_varible = nil
- end
- )
- script.on_init(
- function()
- global.you = nil
- global.subtick = nil
- speed=1
- modifiers=1
- for _, player in pairs(game.players) do
- init_gui(player)
- end
- a_varible = nil
- end
- )
- --- interface
- function init_gui(player)
- --debug start
- -- if player.gui.top["sneakinButton"] then
- -- player.gui.top["sneakinButton"].destroy()
- -- end
- --debug end
- if (not player.gui.top["sneakinButton"]) then
- player.gui.top.add{type="button", name="sneakinButton", style="sneakin_button_main"} --,style={top_padding=0,bottom_padding=0},style="flip_button_style_left"
- -- player.gui.top.style.top_padding=0
- -- player.gui.top.style.bottom_padding=0
- --player.gui.top.sneakinButton.caption = "Sneakin"
- player.gui.top.sneakinButton.tooltip = "Improve Move - settings"
- end
- end
- function expand_gui(player)
- local frame = player.gui.left["sneak-settings"]
- if (frame) then
- frame.destroy()
- else
- frame = player.gui.left.add{type="flow", name="sneak-settings"} -- ,style = "description_title_label_style"
- frame.add{type="label", name="sneak", caption="sneak",style = "description_title_label_style"}
- frame.add{type="textfield", name="sneak-speed", text=sneakspeedinput,style="sneakin_textfield"}
- local stt="Set sneak speed as fraction of base speed here.\n 1=base speed; 0=stop; allowed 1-0.05"
- frame.sneak.tooltip=stt
- frame["sneak-speed"].tooltip = stt
- frame.add{type="label", name="crawl", caption="crawl",style = "description_title_label_style"}
- frame.add{type="textfield", name="crawl-speed", text=crawlspeedinput,style="sneakin_textfield"}
- local ctt ="Set crawl speed as fraction of base speed here.\n1=base speed; 0=stop; allowed 1-0.05\nDefault=0.1"
- frame.crawl.tooltip=ctt
- frame["crawl-speed"].tooltip = ctt
- frame.add{type="label", name="moonwalk", caption="moonwalk",style = "description_title_label_style"}
- frame.add{type="textfield", name="moonwalk-speed", text=moonwalkspeedinput,style="sneakin_textfield"}
- 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"
- frame.moonwalk.tooltip = mtt
- frame["moonwalk-speed"].tooltip = mtt
- frame.add{type="label", name="to", caption="timeout",style = "description_title_label_style"}
- frame.add{type="textfield", name="timeout", text=timeoutinput,style="sneakin_textfield"}
- 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"
- frame.to.tooltip = ttt
- frame.timeout.tooltip = ttt
- --[[
- frame.add{type="button", name="blueprint-string-load", style="blueprintstring_button_load"}
- frame.add{type="button", name="blueprint-string-save-as", style="blueprintstring_button_saveas"}
- frame.add{type="button", name="blueprint-string-save-all", style="blueprintstring_button_saveall"}
- frame.add{type="button", name="blueprint-upgrade", style="blueprintstring_button_upgrade"}
- --]]
- end
- end
- script.on_event(defines.events.on_gui_click, function(event)
- local player = game.players[event.element.player_index]
- local name = event.element.name
- if (name == "sneakinButton") then
- --debugprint("clicked on button")
- if player.gui.left["sneak-settings"] then
- checknum(player,name)
- else
- sneakspeedinput = sneakspeedinput or sneakspeed
- crawlspeedinput = crawlspeedinput or crawlspeed
- moonwalkspeedinput = moonwalkspeedinput or moonwalkspeed
- timeoutinput = timeoutinput or timeout
- end
- expand_gui(player)
- elseif player.gui.left["sneak-settings"] then
- checknum(player,name)
- end
- --]]
- --[[
- if (name == "blueprint-string-load") then
- load_blueprint(player)
- elseif (name == "blueprint-string-save-all") then
- save_blueprints(player)
- elseif (name == "blueprint-string-save-as") then
- prompt_for_filename(player)
- elseif (name == "blueprint-string-filename-save") then
- save_blueprint_as(player, player.gui.center["blueprint-string-filename-prompt"]["blueprint-string-filename"].text)
- elseif (name == "blueprint-string-filename-cancel") then
- player.gui.center["blueprint-string-filename-prompt"].destroy()
- elseif (name == "blueprint-string-button") then
- expand_gui(player)
- elseif (name == "blueprint-upgrade") then
- upgrade_blueprint(player)
- end
- --]]
- end)
- function checknum(player,name)
- local player = player
- local name = name
- sneakspeedinput=tonumber(player.gui.left["sneak-settings"]["sneak-speed"].text)
- sneakspeedinput = sneakspeedinput or sneakspeed
- if sneakspeedinput > 1 then
- sneakspeedinput =1
- elseif sneakspeedinput < 0.05 then
- sneakspeedinput = 0.05
- end
- crawlspeedinput=tonumber(player.gui.left["sneak-settings"]["crawl-speed"].text)
- crawlspeedinput = crawlspeedinput or crawlspeed
- if crawlspeedinput > 1 then
- crawlspeedinput =1
- elseif crawlspeedinput < 0.05 then
- crawlspeedinput = 0.05
- end
- moonwalkspeedinput=tonumber(player.gui.left["sneak-settings"]["moonwalk-speed"].text)
- moonwalkspeedinput = moonwalkspeedinput or moonwalkspeed
- if moonwalkspeedinput > 1 then
- moonwalkspeedinput =1
- elseif moonwalkspeedinput < 0.05 then
- moonwalkspeedinput = 0.05
- end
- timeoutinput=math.floor(tonumber(player.gui.left["sneak-settings"]["timeout"].text))
- timeoutinput = timeoutinput or timeout
- if timeoutinput > 120 then
- timeoutinput =120
- elseif timeoutinput < 1 then
- timeoutinput = 1
- end
- end
- ----- mod functions
- function on_moonwalker(event) --capture moonwalker hotkey
- you = game.players[event.player_index]
- if (mw==false or mw== nil) then -- toggle function
- mw = true
- mws=moonwalkspeedinput -- backward speed
- float_text(you,"You are now moonwalking\n(Warning: Controls are mirrored)",{r=1,g=0.2,b=0.2})
- else
- mw = false
- end
- end
- --debug function to see if modifiers work as intended
- --[[
- function on_count(bla)
- for _, player in pairs(game.players) do
- you = player
- --you.print("1: last ="..tostring(last)..", bla ="..tostring(bla))
- if (last==nil) then
- last=bla
- start=game.tick
- --you.print("2: last ="..tostring(last)..", bla ="..tostring(bla))
- --you.print("starting count @ "..tostring(start))
- else
- if (last==bla) then
- --you.print("3: last ="..tostring(last)..", bla ="..tostring(bla))
- else
- total=(game.tick - start)
- --you.print("ending count @ "..tostring(game.tick))
- you.print("took on "..tostring(last)..": "..tostring(total).." ticks in total")
- start=nil
- total=nil
- bla=nil
- last=nil
- end
- end
- end
- end
- --]]
- function on_toggleDriving(event) --overrides getting in and out vehicles
- you = game.players[event.player_index]
- if not you.driving then
- you.driving=true -- get in vehicles normally
- else
- if safe or emer then --test if you can get out safely
- you.driving=false
- else
- you.print("You are too fast. Double tap for emergency exit")
- emer=true
- subtick=0 --set values for testing double tap
- end
- end
- end
- function on_sneak(event) --capture sneak hotkey
- you = game.players[event.player_index] --set "you" to player triggering event
- crawl = false --set crawl to false in case was crawling before
- subtick = 0 --reset timed event
- if (sneak == false or sneak == nil) then -- activate sneak if not active
- if not mw and not you.driving then
- sneak = true
- float_text(you,"Ya now sneakin!",{r=0.2,g=1,b=0.2})
- end
- else
- sneak = false
- end
- end
- function on_crawl(event) --comments see sneak
- you = game.players[event.player_index]
- sneak = false
- subtick = 0
- if (crawl == false or crawl == nil) then
- if not mw and not you.driving then
- crawl = true
- float_text(you,"Ya now crawlin!",{r=0.2,g=1,b=0.2})
- end
- else
- crawl = false
- end
- end
- function on_tick(event) --do this every tick
- offset = nil -- more than one message on a tick?
- if subtick==nil then
- -- if subtick not set do it now (crash prevention)
- subtick = 0
- end
- if you==nil then
- --if you not set do it now (crash prevention)
- you={}
- you.walking_state ={}
- you.walking_state.walking = false
- end
- if you.driving then
- --if driving check positions every tick, if changed between last tick mark as unsafe
- if (lastx == you.position.x and lasty == you.position.y) then
- safe=true
- else
- safe=false
- lastx = you.position.x
- lasty = you.position.y
- end
- end
- if (((sneak or crawl or emer) and not you.walking_state.walking) and subtick >= timeoutinput ) then
- --
- --if a status of a timed event is set and not walking then reset them after 1/3 second
- --Most of function only needed cause unable to check for key down.
- if not emer then
- if sneak then
- out= "sneakin"
- else
- out= "crawlin"
- end
- float_text(you,"Stopped "..out.."!",{r=0.2,g=1,b=0.2})
- sneak = false --reset modifiers
- crawl = false
- emer = false
- subtick = 120 -- also stop counting
- speed = 1
- modifiers = 1
- end
- else
- if ((sneak or crawl) and you.walking_state.walking) then
- -- if walking keep the modifiers active.. if any
- subtick = 0
- end
- end
- if (you.walking_state.walking and (sneak or crawl or mw)) then
- modifiers = 1+calcRunningModifiers(you) --calculate movement bonuses and add base speed to it
- data=tostring(you.surface.get_tile(you.position.x, you.position.y).name)
- --get the name of tile you are on
- pattern="concrete" -- if tile name includes concrete change speed accordantly
- if string.find(data, pattern) ~= nil then
- ground = 0.73 --value not calculated,but got by trial and error
- --on_count(pattern) --debug
- else
- pattern="stone" -- if tile name includes stone change speed accordantly
- if string.find(data, pattern) ~= nil then
- ground= 0.8 --value not calculated,but got by trial and error
- --on_count(pattern) --debug
- else
- ground = 1 --enywhere else
- --on_count("ground") --debug
- end
- end
- if not mw then --not moonwalking?
- if sneak then --but sneaking?
- speed=sneakspeedinput*ground -- set speed accordingly sneak speed and ground modificator
- else
- if crawl then -- or crawling
- speed=crawlspeedinput*ground -- set speed accordingly crawl speed and ground modificator
- else --else reset
- speed = 1
- modifiers = 1
- end
- end
- else
- speed=-mws*ground -- set moonwalking speed
- end
- --you=nil
- end
- if not (sneak or crawl or mw) then
- speed = 1
- modifiers = 1
- end
- final=(1*speed/modifiers)-1 --last step remove the modifiers if any
- you.character_running_speed_modifier = final --now set the speed
- if subtick <= 120 then
- --stop counting ticks if more than 120
- subtick = subtick + 1
- end
- end
- function calcRunningModifiers(player) --function mainly copied from 'turtle speed mod' from DedlySpyder
- local armorInventory = player.get_inventory(defines.inventory.player_armor)
- local movementSpeedModifier = 0
- if (armorInventory ~= nil) then
- local stack = armorInventory[1]
- if (stack.valid_for_read and stack.has_grid) then
- local equipmentList = stack.grid.equipment
- for _, equipment in pairs(equipmentList) do
- if (equipment.movement_bonus > 0) then
- movementSpeedModifier = movementSpeedModifier + equipment.movement_bonus
- end
- end
- end
- end
- return movementSpeedModifier
- end
- function debugprint(text)
- for _, player in pairs(game.players) do
- player.print(tostring(text))
- end
- end
- function float_text(ap,text,color) --get out a text easily
- color = color or {r=0.5,g=1,b=1}
- text = text or "hello world (you stupid didn't send a text)"
- text = tostring(text)
- if type(color) ~= "table" then -- color valid?
- color = {r=0.5,g=1,b=1}
- end
- if ap ~=nil and type(ap) ~= "table" and not ap.valid then
- ap=nil
- text = "Warning: Message send to a not valid player " .. text
- end
- ap = ap or game.players[1] -- no player set? do it for first only
- offset = offset or 1
- --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)
- if offset > 0 then
- offset = offset + 1
- end
- posx=ap.position.x-((string.len(text)/2)/10)
- posy=ap.position.y+offset
- game.surfaces[1].create_entity{name="flying-text", position={posx,posy}, text=text, color=color}
- return game.tick
- end
- --event definitions
- script.on_event(defines.events.on_tick, on_tick)
- script.on_event("sneak_modifier", on_sneak)
- script.on_event("crawl_modifier", on_crawl)
- script.on_event("moonwalker", on_moonwalker)
- script.on_event("toggle-driving", on_toggleDriving)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement