-- Jetpack minimod by misterPhyrePhox (v1.0) -- Thanks to andyvincent for his snippet too determine whether a player is on the ground! -- This lua script hooks into events; you ccan run it in any map by -- just typing "lua_openscript jetpack.lua"" or whatever. --[[ Implements a tribes-style jetpack thatt can be activated by +walk. The jetpack must be activated in the air; it won't work if you are on the ground, so take a little jump, and press +walk, and fly. You should probably bind an easily reachable key to +walk (I use V). In the upper left corner of your screen, you will see a fuel bar that depletes as you use your jetpack. If you run out of fuel, you will not be able to jetpack anymore... although it recharges over time. You can control the jetpack depletion rate and various other variables by editing them below or by using console commands. See a list of jetpack-related console commands by typing "jet_help" into the console after executing the script. ]]-- --[[ Currently, the script only lets peoplee with a userid of 1 use the console commands, to prevent other players from screwing it up... I'm not sure if anyone can execute scripted console commands, or only admins, so I decided to play it safe. So if you run a dedicated server, you may be out of luck. If you want, you can ask me, or you can edit the script yourself, below... ]]-- _OpenScript("includes/vector3.lua"); _OpenScript("includes/eventhook.lua"); _OpenScript("includes/luathink.lua"); --[[ BEGIN USER CONFIGURABLE VARIABLES ]]--- JETPACK_POWER = 10; JETPACK_MOVESPEED = 15; JETPACK_DEPLETION = 2; JETPACK_RECHARGE = 2; -- Since this hooks into other mods and gammetypes, we run the risk that our -- GModRect slots may conflict with the sloots of the parent gametype... so -- if you run into any problems, just changge SLOT_BASE. SLOT_BASE = 1337; SLOT_BLACKRECT = SLOT_BASE + 0; SLOT_FUELRECT = SLOT_BASE + 1; KEY_LEFT = 512; -- IN_MOVELEFT KEY_RIGHT = 1024; -- IN_MOVERIGHT KEY_FORWARD = 8; -- IN_FORWARD KEY_BACKWARD = 16; -- IN_BACK KEY_JETPACK = 262144; -- IN_WALK --[[ END USER CONFIGURABLE VARIABLES ]]-- registry = {}; bindings = {}; function cmd_jetpackPower(userid, args) if(userid ~= 1) then return; end if(args == "") then _Msg("Sets the power of the jetpack; how fast it propels you\n"); _Msg("\tCurrent value: " .. JETPACK_POWER .. "\n"); _Msg("\tDefault value: 10\n"); else JETPACK_POWER = args; end end function cmd_jetpackMoveSpeed(userid, args) if(userid ~= 1) then return; end if(args == "") then _Msg("How fast people move while in the air\n"); _Msg("\tCurrent value: " .. JETPACK_MOVESPEED .. "\n"); _Msg("\tDefault value: 15\n"); else JETPACK_MOVESPEED = args; end end function cmd_jetpackDepletion(userid, args) if(userid ~= 1) then return; end if(args == "") then _Msg("The number of seconds it takes for a full bar of fuel to deplete\n"); _Msg("\tCurrent value: " .. JETPACK_DEPLETION .. "\n"); _Msg("\tDefault value: 2\n"); else JETPACK_DEPLETION = args; end end function cmd_jetpackRecharge(userid, args) if(userid ~= 1) then return; end if(args == "") then _Msg("The number of seconds it takes for a full bar of fuel to recharge\n"); _Msg("\tCurrent value: " .. JETPACK_RECHARGE .. "\n"); _Msg("\tDefault value: 2\n"); else JETPACK_RECHARGE = args; end end function cmd_jetpackHelp(userid, args) if(userid ~= 1) then return; end _Msg("Jetpack minimod by misterPhyrePhox (v1.0)\n"); _Msg("Available commands: jet_recharge, jet_deplete, jet_power, jet_movespeed, jet_help\n"); _Msg("Type a command with no parameters for information about that command\n"); end CONCOMMAND("jet_recharge", cmd_jetpackRecharge); CONCOMMAND("jet_deplete", cmd_jetpackDepletion); CONCOMMAND("jet_movespeed", cmd_jetpackMoveSpeed); CONCOMMAND("jet_power", cmd_jetpackPower); CONCOMMAND("jet_help", cmd_jetpackHelp); function initPlayer(name, userid, steamid) registry[userid] = {}; registry[userid].fuel = 100; registry[userid].keys = {}; registry[userid].jetting = false; registry[userid].inair = false; registry[userid].lastpos = _EntGetPos(userid); registry[userid].keyTime = _CurTime(); registry[userid].keyFuel = 100; _GModRect_Start("gmod/white"); _GModRect_SetPos(0.025, 0.055, 0.21, 0.065); _GModRect_SetColor(0, 0, 0, 255); _GModRect_SetTime(2048, 0, 0); _GModRect_Send(userid, SLOT_BLACKRECT); _GModRect_Start("gmod/white"); _GModRect_SetPos(0.03, 0.06, 0.2, 0.05); _GModRect_SetColor(0, 200, 0, 128); _GModRect_SetTime(2048, 0, 0) _GModRect_Send(userid, SLOT_FUELRECT); end function spawnPlayer(userid) initPlayer(nil, userid, nil); end function destroyPlayer(name, userid, address, steamid, reason) registry[userid] = nil; end HookEvent("eventPlayerActive", initPlayer); HookEvent("eventPlayerDisconnect", destroyPlayer); HookEvent("eventPlayerSpawn", spawnPlayer); function updateFuel(userid) if(registry[userid].jetting == true) then registry[userid].fuel = registry[userid].keyFuel - ((_CurTime() - registry[userid].keyTime) / JETPACK_DEPLETION) * 100; else registry[userid].fuel = registry[userid].keyFuel + ((_CurTime() - registry[userid].keyTime) / JETPACK_RECHARGE) * 100; end if(registry[userid].fuel < 0) then registry[userid].fuel = 0; end if(registry[userid].fuel > 100) then registry[userid].fuel = 100; end end function beginJet(userid) registry[userid].keyTime = _CurTime(); registry[userid].keyFuel = registry[userid].fuel; _GModRect_Start("gmod/white"); _GModRect_SetPos(0.03, 0.06, 0.2 * (registry[userid].fuel / 100), 0.05); _GModRect_SetColor(0, 255, 0, 255); _GModRect_SetTime(2048, 0, 0) _GModRect_Send(userid, SLOT_FUELRECT); _GModRect_Start("gmod/white"); _GModRect_SetPos(0.03, 0.06, 0, 0.05); _GModRect_SetColor(0, 255, 0, 255); _GModRect_SetTime(2048, 0, 0) _GModRect_SendAnimate(userid, SLOT_FUELRECT, JETPACK_DEPLETION * (registry[userid].fuel / 100), 0.5); end function endJet(userid) registry[userid].keyTime = _CurTime(); registry[userid].keyFuel = registry[userid].fuel; _GModRect_Start("gmod/white"); _GModRect_SetPos(0.03, 0.06, 0.2 * (registry[userid].fuel / 100), 0.05); _GModRect_SetColor(0, 200, 0, 128); _GModRect_SetTime(2048, 0, 0) _GModRect_Send(userid, SLOT_FUELRECT); _GModRect_Start("gmod/white"); _GModRect_SetPos(0.03, 0.06, 0.2, 0.05); _GModRect_SetColor(0, 200, 0, 128); _GModRect_SetTime(2048, 0, 0) _GModRect_SendAnimate(userid, SLOT_FUELRECT, JETPACK_RECHARGE * ((100 - registry[userid].fuel) / 100), 0.5); end function keyPressed(userid, key) if(key == KEY_JETPACK) then updateFuel(userid); registry[userid].jetting = true; beginJet(userid); end for k, v in bindings do if(k == key) then registry[userid].keys[key] = true; end end end function keyReleased(userid, key) if(key == KEY_JETPACK) then updateFuel(userid); registry[userid].jetting = false; endJet(userid); end for k, v in bindings do if(k == key) then registry[userid].keys[key] = false; end end end HookEvent("eventKeyPressed", keyPressed); HookEvent("eventKeyReleased", keyReleased); nextTrailLine = _CurTime() + 0.1; function jetpackThink() for ka, va in registry do -- Thanks to andyvincent for this snippet! local pos = _EntGetPos(ka); local ang = _EntGetUpVector(ka); ang.z = -ang.z; _TraceLine(pos, ang, 16, ka); if (_TraceHitWorld() == false) then registry[ka].inair = true; end if(registry[ka].inair == true) then for kb, vb in registry[ka].keys do if(registry[ka].keys[kb] == true) then bindings[kb].func(ka); end end end updateFuel(ka); if(registry[ka].fuel == 0 or registry[ka].inair == false) then keyReleased(userid, KEY_JETPACK); end end if(nextTrailLine <= _CurTime()) then for i = 1, _MaxPlayers() do if(registry[i].jetting == true) then -- Fading line thingy stolen from gm_laserdance _EffectInit(); _EffectSetEnt(i); _EffectSetOrigin(vecAdd(_EntGetPos(i), vector3(0, 0, 30))); _EffectSetStart(vecAdd(registry[i].lastpos, vector3(0, 0, 30))); _EffectSetScale(20); _EffectSetMagnitude(10); _EffectDispatch("FadingLineTeamSolid"); end registry[i].lastpos = _EntGetPos(i); end nextTrailLine = _CurTime() + 0.1; end end AddThinkFunction(jetpackThink); function binding_jetpack(userid) if(registry[userid].fuel == 0) then return; end local vel = _EntGetVelocity(userid); _EntSetVelocity(userid, vecAdd(vel, vector3(0, 0, JETPACK_POWER))); end function binding_moveForward(userid) local vel = _EntGetVelocity(userid); _EntSetVelocity(userid, vecAdd(vel, vecMul(_EntGetForwardVector(userid), vector3(JETPACK_MOVESPEED, JETPACK_MOVESPEED, JETPACK_MOVESPEED)))); end function binding_moveBackward(userid) local vel = _EntGetVelocity(userid); _EntSetVelocity(userid, vecAdd(vel, vecMul(_EntGetForwardVector(userid), vector3(-JETPACK_MOVESPEED, -JETPACK_MOVESPEED, -JETPACK_MOVESPEED)))); end function binding_moveLeft(userid) local vel = _EntGetVelocity(userid); _EntSetVelocity(userid, vecAdd(vel, vecMul(_EntGetRightVector(userid), vector3(-JETPACK_MOVESPEED, -JETPACK_MOVESPEED, -JETPACK_MOVESPEED)))); end function binding_moveRight(userid) local vel = _EntGetVelocity(userid); _EntSetVelocity(userid, vecAdd(vel, vecMul(_EntGetRightVector(userid), vector3(JETPACK_MOVESPEED, JETPACK_MOVESPEED, JETPACK_MOVESPEED)))); end function jetpackInit() for i = 1, _MaxPlayers() do initPlayer(0, i, 0); end bindings[KEY_JETPACK] = {func = binding_jetpack}; bindings[KEY_FORWARD] = {func = binding_moveForward}; bindings[KEY_BACKWARD] = {func = binding_moveBackward}; bindings[KEY_LEFT] = {func = binding_moveLeft}; bindings[KEY_RIGHT] = {func = binding_moveRight}; cmd_jetpackHelp(1, nil); end jetpackInit();