Spent some time upgrading passenger/driver stuff I posted a while ago. I'm happy to report about a progress! I really thought that I hit the engine limits, but woah, today's morning brought a real breakthrough. I'm attaching the code. As you can see, I commented it as heavily as I could to make it clear what I was aiming for.
I *think* I got rid of bugs that killed this mod and it should see the light of 1.0 "final" version. However, this mod is all about messing with AI. A really unpredictable thing in every game. Moreover - as I stated not once and not twice - I'm not a programmer. That's another reason I decided to describe it as close as it gets.
Also feel free to test it and do a bug-hunt. If you have skills and will of help, maybe you can even find a way to upgrade this stuff? Or tweak it? Or anything? Either way, I encourage you to do this
Code:
passenger_activated = 0 -- Place this line among other variables at the beginning of sr3_city.lua...
-- ...and following lines under your desired key bind.
local myride = get_char_vehicle_name(LOCAL_PLAYER)
local driver = vehicle_get_driver(myride)
if (passenger_activated == 0) then --and not character_is_in_vehicle(LOCAL_PLAYER) then
--if (1) passenger mode is disabled and (2) you're o foot... enable passenger mode.
vehicle_set_crazy(myride, false) -- Useful when you're entering the same car for the second time and keybind is tapped inside a vehicle.
vehicle_suppress_npc_exit(myride, false) -- Same as above.
player_force_vehicle_seat(LOCAL_PLAYER, 1) -- Force the player take passenger seat.
sandboxplus_message("Passenger mode ENABLED.\nFind a vehicle, enter it and wait for your followers. Then tap this keybind again.")
passenger_activated = 1 -- Prepare your car for riding as a passenger in a proper mode. Your current one is very buggy.
-- IMPROVEMENT IDEA NO 1: maybe make the function automatically enter "smooth ride" mode after all followers are in?
-- And clear "no npc exit", "vehicle crazy" mode automatically?
elseif (passenger_activated == 1) and character_is_in_vehicle(LOCAL_PLAYER) and (driver == "#FOLLOWER#") then
--if (1) passenger mode is enabled and (2) you're in a car and (3)you're driver is your follower ... activate riding as a passenger.
vehicle_suppress_npc_exit(myride, true) -- Prevents player and his followers from forced exiting the vehicle
set_ignore_ai_flag("#FOLLOWER2#", true) -- Make followers careless about enemies attacks and drive calmly
set_ignore_ai_flag("#FOLLOWER3#", true)
set_ignore_ai_flag("#FOLLOWER1#", true)
vehicle_set_crazy(myride, true) -- We're have to be in a rush! Driver doesn't obey to traffic law.
vehicle_max_speed(myride, 70.0) -- Caps the top-speed of car. Prevents windshield cannons and going so fast so you can't aim at anything.
sandboxplus_message("SMOOTH RIDE mode.")
passenger_activated = 2 -- Next time player taps passenger mode combination he's gonna activate "drive aggresively" mode.
elseif (passenger_activated ~= 0) and character_is_in_vehicle(LOCAL_PLAYER) and (driver ~= "#FOLLOWER#") then
--if (1) passenger mode is enabled and (2) you're in a car and (3) you DON'T have a follower as a driver ... force followers to appear in player's car.
-- This piece of code is used for situation when - for whatever reason - your driver (or some of your followers) didn't find their ways
-- to player's vehicle. It might be because they decided to take another car or are just too far away for you to wait.
vehicle_suppress_npc_exit(myride, false) --Temporary lock. Maybe it's useless, maybe it's not.
set_player_can_enter_exit_vehicles(LOCAL_PLAYER, false) -- Locking players ability to get out of the car. This is just temporary.
vehicle_exit_teleport(driver) -- Two things to describe here. As I still don't get the difference between "#FOLLOWER#" and "#FOLLOWER1#"
vehicle_exit_teleport("#FOLLOWER#") -- and this function is kinda crucial for the whole passenger-thing, I decided to make it as as safe as it gets.
vehicle_exit_teleport("#FOLLOWER1#") -- Second: it's purpose is to make followers exit any car they're currently in and rearrange their appearance
vehicle_exit_teleport("#FOLLOWER2#") -- in player's car. It takes a tic to do that and I think it works very well! We're forcing evey one of
vehicle_exit_teleport("#FOLLOWER3#") -- them - even those still sitting in your car as they should - to prevent seat-blocking.
--delay(1.0) -- You may want to enable that line to see if homies really exit their cars before appearing in player's one. Useful for
-- hunting down "#FOLLOWER#" stuff!
vehicle_enter_teleport("#FOLLOWER#", myride,0) -- Here we're forcing - already on-foot - followers to enter player's car.
vehicle_enter_teleport("#FOLLOWER2#", myride,2) -- Also: #FOLLOWER# works fine so MAYBE there's no need for "#FOLLOWER1#?
vehicle_enter_teleport("#FOLLOWER3#", myride,3)
set_player_can_enter_exit_vehicles(LOCAL_PLAYER, true) -- If player could exit the car before teleporting homies, function might flopped!
vehicle_suppress_npc_exit(myride, true) -- Force the followers to stay in car.
passenger_activated = 1 -- Again we're in that weird mode where followers drive the car for you but it's a mode that isn't fun even for a bit.
-- IMPROVEMENT IDEA NO 1 for the second time!
sandboxplus_message("Followers teleported to your car. Tap this keybind again to begin riding as a passenger!")
elseif (passenger_activated ~= 0) and not character_is_in_vehicle(LOCAL_PLAYER) then
--if (1) passenger mode is enabled and (2) player is in foot then... quit the passenger mode and become a driver again!
set_ignore_ai_flag("#FOLLOWER2#", false) -- If player want to leave the passenger mode during "drive calmly" phase,
set_ignore_ai_flag("#FOLLOWER3#", false) -- we have to give them back their original behavior.
set_ignore_ai_flag("#FOLLOWER1#", false)
set_ignore_ai_flag("#FOLLOWER#", false) -- Once again: difference between "#FOLLOWER1#" and "#FOLLOWER#" is a big mystery for me. Sometimes it works, sometime don't.
player_force_vehicle_seat(LOCAL_PLAYER, 0) -- Make the player to take the driver's seat.
vehicle_exit_teleport(driver) -- Another longer description. As you might remember, we made the car "unexitable" (via vehicle_suppress_npc_exit()).
vehicle_exit_teleport("#FOLLOWER#") -- If those lines over here didn't exists, player who quits passenger mode wouldn't have his followers back
vehicle_exit_teleport("#FOLLOWER1#") -- because it's impossible to clear "unexitable" flag while on-foot: we can only adress a function to a named
vehicle_exit_teleport("#FOLLOWER2#") -- vehicle - it means player's current - and there's nothing like "#PLAYERS_LAST_CAR#. So here we're
vehicle_exit_teleport("#FOLLOWER3#") -- forcing followers to leave via teleporting them. Unexitable flag will be activated when player enters the car
-- again as a passenger.
passenger_activated = 0
sandboxplus_message("Passenger mode DISABLED.\nIf something is wrong with your followers AI - press this keybind again. If that doesn't help, dismiss and recruit followers again.")
elseif (passenger_activated == 2) and character_is_in_vehicle(LOCAL_PLAYER)then
--if (1) passenger mode is enabled in a way that allows drive-by ride mode and (2) player is in a vehicle... then make the drive-by mode on
passenger_activated = 1
set_ignore_ai_flag("#FOLLOWER2#", false) -- Make the followers use their "gang deffensive" AI again. Now they will return fire, pull over
set_ignore_ai_flag("#FOLLOWER3#", false) -- attacking enemies but - since "vehicle_surpress_npc_exit()" is still on - they won't leave
set_ignore_ai_flag("#FOLLOWER1#", false) -- the player's car. Drive-by, baby.
sandboxplus_message("DRIVE-BY mode.")
else
sandboxplus_message("You're not in a car, you don't have a driver yet, or something else went wrong.") -- Player shouldn't see this function too often.
end
TUTORIAL
Now a bit of how-to with explanation of what bugs I've fixed and what still needs to be done.
You operate a whole thing with desired key-bind. For this tutorial, let it be 7 + PG DWN.
1) Tap it once (it doesn't matter wether you're on foot or in a car) to activate passenger mode.
2) Now enter the vehicle. Your homies should enter it with you (if not: read further, I've got that covered!) and start to drive.
3) However, this driving mode isn't very useful. Basically your followers obey traffic-law (they pull over at red lgihts and stop signs) and will engage to a gun fight if attacked. Doesn't look too bad on a paper, right? Well, engaging to a gun fight means that followers will chase the attacker and perform a scripted (read: forced) quitting the vehicle when they're near. Player's character exits the vehicle too and when he enters the car back, game crashes. Sad panda.
4) That's why I had to set up a function that prevents characters from exiting the car. That had other consequences as well, though: since we can't affect the car that player isn't currently in and quitting the passenger mode should be performed when player's on foot (I tried to limit myself to as little keybinds as possible and make this mod as user-friendly as it gets) it meant that homies stayed in the car. Player had to dismiss and recruit them again to make them any helpfull again. As you probably imgine, this kind of bug made this mod kinda useless. I got rid of that by using "vehicle_exit_teleport()". It sacrificed getting out and in the car animations (you can actually restore getting out of the car animations by removing "_teleport" part) but I think that was the right thing to do. When you're being shot at waiting for anim to end isn't fun, right?
5) The opposie function "vehicle_enter_teleport()" fixed another big bug from previous version. Sometimes followers don't feel like entering player's car. It's very likely to happen in a gunfight. This time, though, if you end up in a car without a driver (or driver and any other follower) you can press 7 + PG DWN to make them instantly teleport to your car. Now you can use this mod to escape the gunfight with a style!
6) When you're driving around Steelport , you can make your homies drive-by attackers. Press 7 + PG DWN and shoot an enemy. Follower will engage the fight but won't perform any forced exits.
7) Tap 7 + PG DWN again to restore smooth ride mode and vice versa.
8) If you want to quit passenger mode, exit the car and press the keybind again. It will teleport-exit your homies (again, you can restore anims if you wish) so you have them back!
STILL TO DO
Some bugs, shared opinions and things that I'd love to hear your opinions about.
1) If you enter a vehicle as a passenger and wait for followers, they will begin to drive in a way described in point 3 of tutorial above. Player is kinda-forced to press 7 + PG DWN again. Maybe add a while-do loop that starts to operate when passenger_activated == 1, checks every second wether follower-driver is in and then activates "smooth ride" mode? (IMPROVEMENT IDAEA NO 1)
2) Quit the passenger mode and get to the same car. Your followers will enter it again. Then quit the car. Your followers will stay, because
vehicle_set_crazy(myride, false)
vehicle_suppress_npc_exit(myride, false)
from first "if then" didn't applied to the current car. Not a big deal (you can make tap keybind again and they will act as normal) but still.
3) I thought of adding "unjackable" and "unflippable" flags to driven's vehicle but decided to left it to player decision, since that can be activated through other Sandbox+ options.
4) This thing works for cars, motorbikes and (drums) for pony cart. I'm mentioning this last thing not because of the guilty pleasure - pony cart was the vehicle that caused bug described in tutorial's point 5 most excesivelly (just press mod's keybind to teleport follower to "driver's" position). I'm not sure if it is possible to make your followers drive tanks, boats and pilot airplanes... Daven's reports weren't too optimistic
5) I hope this little project would eventually lead us to creating a working taxi. Is it possible? There are functions that sounds promising but there's a long road leading to that.
EDIT:
6) Sometimes you have to give your follower a moment to make them actually "drive" a car. If your homie is in a driver seat and car don't move, then wait few seconds. I wonder if we can fix that or it's some hard-coded AI bug (maybe it's because vehicle_crazy should be called off and on between change of driving styles?)