Skip to content

Commit

Permalink
Merge branch 'hyperlaw'
Browse files Browse the repository at this point in the history
  • Loading branch information
impaktor committed Aug 17, 2016
2 parents a68083e + 657b9fe commit 8206d0a
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 15 deletions.
1 change: 1 addition & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ August 2016
* F3 and F4 Toggles between worldview and their thing (#3765)
* Navbutton added to mission screens (#3736)
* Add a TCP Lua console (#3768)
* Low altitude hyperjumps are illegal (#3753)

* Fixes
* Fix crash in by Search & Rescue missions (#3770)
Expand Down
Binary file added data/icons/hyperspace_disabled_f8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/icons/hyperspace_forbidden_abort_f8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/icons/hyperspace_forbidden_f8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions data/lang/core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,18 @@
"description" : "",
"message" : "Hyperspace jump aborted."
},
"HYPERSPACE_JUMP_DISABLED" : {
"description" : "Mouse over text for hyperdrive button on ship control panel",
"message" : "Hyperdrive disabled"
},
"HYPERSPACE_JUMP_ENGAGE" : {
"description" : "Mouse over text for hyperdrive button on ship control panel",
"message" : "Engage Hyperdrive"
},
"HYPERSPACE_JUMP_FORBIDDEN" : {
"description" : "Mouse over text for hyperdrive button on ship control panel",
"message" : "Hyperdrive forbidden zone"
},
"HYPERSPACE_TARGET" : {
"description" : "",
"message" : "Hyperspace target"
Expand Down
4 changes: 4 additions & 0 deletions data/lang/ui-core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@
"description" : "",
"message" : "Hyperspace range"
},
"ILLEGAL_JUMP" : {
"description" : "A finable crime",
"message" : "Reckless hyperjump registered"
},
"IM_TIRED_OF_WORKING_FOR_NOTHING_DONT_YOU_KNOW_WHAT_A_CONTRACT_IS" : {
"description" : "",
"message" : "I'm tired of working for nothing. Don't you know what a contract is?"
Expand Down
3 changes: 2 additions & 1 deletion data/libs/Equipment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ HyperdriveType.HyperjumpTo = function (self, ship, destination)
return "INSUFFICIENT_FUEL"
end
ship:setprop('nextJumpFuelUse', fuel_use)
return ship:InitiateHyperjumpTo(destination, self.capabilities.hyperclass, duration), fuel_use, duration
local warmup_time = 5 + self.capabilities.hyperclass*1.5
return ship:InitiateHyperjumpTo(destination, warmup_time, duration), fuel_use, duration
end

HyperdriveType.OnEnterHyperspace = function (self, ship)
Expand Down
1 change: 1 addition & 0 deletions data/libs/Legal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ local Legal = {}
Legal.CrimeType = {}

Legal.CrimeType["DUMPING"] = {basefine = 1e4, name = l.DUMPING}
Legal.CrimeType["ILLEGAL_JUMP"] = {basefine = 5e2, name = l.ILLEGAL_JUMP}
Legal.CrimeType["MURDER"] = {basefine = 1.5e6, name = l.MURDER}
Legal.CrimeType["PIRACY"] = {basefine = 1e5, name = l.PIRACY}
Legal.CrimeType["TRADING_ILLEGAL_GOODS"] = {basefine = 5e3, name = l.TRADING_ILLEGAL_GOODS}
Expand Down
105 changes: 104 additions & 1 deletion data/libs/Ship.lua
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,117 @@ Ship.RemoveEquip = function (self, item, count, slot)
return ret
end

Ship.HyperjumpTo = function (self, path)

--
-- Method: IsHyperjumpAllowed
--
-- Check if hyperjump is allowed, return bool and minimum allowed
-- altitude / distance.
--
-- > is_allowed, distance = ship:IsHyperjumpAllowed()
--
--
-- Return:
--
-- is_allowed - Boolean. True if allowed at ships current position,
-- flase otherwise
--
-- distance - The minimum allowed altitude from planetary body, or
-- distance from orbital space station, for a legal hyper juump.
--
-- Availability:
--
-- 2016 August
--
-- Status:
--
-- experimental
--
Ship.IsHyperjumpAllowed = function(self)

-- in meters
local MIN_PLANET_HYPERJUMP_ALTITUDE = 10000
local MIN_SPACESTATION_HYPERJUMP_DISTANCE = 1000

-- get closest non-dynamic (not ships, cargo, missiles) body of ship
local body = self.frameBody

-- If no body close by, anything goes
if not body then
return true, 0
end

-- if alt is nil, then outside frame of ref -> OK to jump
local check = function(alt, dist) return not alt or alt >= dist, dist end

-- if body exists and not a planet -> allowed
if body and body.type == 'STARPORT_ORBITAL' then
return check(body:DistanceTo(self), MIN_SPACESTATION_HYPERJUMP_DISTANCE)
end

-- if on a planet:
-- always allowed if not body.hasAtmosphere?

-- if body is a planet or a star:
if body and body.type ~= 'GRAVPOINT' then
local lat, long, altitude = self:GetGroundPosition()
return check(altitude, MIN_PLANET_HYPERJUMP_ALTITUDE)
end
end

--
-- Method: HyperjumpTo
--
-- Hyperjump ship to system. Makes sure the ship has a hyper drive,
-- that target is withn range, and ship has enough fuel, before
-- initiating the hyperjump countdown. In addition, through the
-- optional argument, the ship can fly to a safe distance, compliant
-- with local authorities' regulation, before initiating the jump.
--
-- > status = ship:HyperjumpTo(path)
-- > status = ship:HyperjumpTo(path, is_legal)
--
-- Parameters:
--
-- path - a <SystemPath> for the destination system
--
-- isLegal - an optional Boolean argument, defaults to false. If
-- true AI will fly the ship ship to legal distance
-- before jumping
--
-- Return:
--
-- status - a <Constants.ShipJumpStatus> string that tells if the ship can
-- hyperspace and if not, describes the reason
--
-- Availability:
--
-- 2015
--
-- Status:
--
-- experimental
--
Ship.HyperjumpTo = function (self, path, is_legal)
local engine = self:GetEquip("engine", 1)
if not engine then
return "NO_DRIVE"
end

-- default to false, if nil:
is_legal = not (is_legal == nil) or is_legal

-- only jump from safe distance
local is_allowed, distance = self:IsHyperjumpAllowed()
if is_legal and self.frameBody and not is_allowed then
print("---> Engage AI for safe distance of hyperjump")
self:AIEnterLowOrbit(self.frameBody)
end

return engine:HyperjumpTo(self, path)
end


Ship.CanHyperjumpTo = function(self, path)
return self:GetHyperspaceDetails(path) == 'OK'
end
Expand Down
5 changes: 5 additions & 0 deletions data/modules/CrimeTracking/CrimeTracking.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ local onLeaveSystem = function(ship)
if not ship:IsPlayer() then return end
-- if we leave the system, the space station object will be invalid
policeDispatched = nil

if not ship:IsHyperjumpAllowed() then
Legal:notifyOfCrime(ship,"ILLEGAL_JUMP")
end
end


Expand All @@ -121,4 +125,5 @@ Event.Register("onGameStart", onGameStart)
Event.Register("onGameEnd", onGameEnd)
Event.Register("onLeaveSystem", onLeaveSystem)


Serializer:Register("CrimeTracking", serialize, unserialize)
2 changes: 2 additions & 0 deletions src/LangStrings.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ DECLARE_STRING(HULL_INTEGRITY)
DECLARE_STRING(SHIELD_INTEGRITY)
DECLARE_STRING(FUEL)
DECLARE_STRING(HYPERSPACE_JUMP_ABORT)
DECLARE_STRING(HYPERSPACE_JUMP_DISABLED)
DECLARE_STRING(HYPERSPACE_JUMP_ENGAGE)
DECLARE_STRING(HYPERSPACE_JUMP_FORBIDDEN)
DECLARE_STRING(HYPERSPACE_JUMP_ABORTED)
DECLARE_STRING(LANDED)
DECLARE_STRING(UNDOCKING)
Expand Down
5 changes: 4 additions & 1 deletion src/LuaShip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,10 @@ static int l_ship_use_ecm(lua_State *l)
/*
* Method: InitiateHyperjumpTo
*
* Ready the ship to jump to the given system.
* Ready the ship to jump to the given system. This does not perform
* any check regarding hyperdrive class, range, fuel. Nor does it
* respect minimum legal distance for hyperjump. For those features use
* <Ship.HyperjumpTo> instead.
*
* > status = ship:InitiateHyperjumpTo(path, warmup, duration, checks)
*
Expand Down
8 changes: 6 additions & 2 deletions src/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,11 @@ void Ship::StaticUpdate(const float timeStep)
m_hyperspace.countdown = 0;
m_hyperspace.now = true;
SetFlightState(JUMPING);

// We have to fire it here, because the event isn't actually fired until
// after the whole physics update, which means the flight state on next
// step would be HYPERSPACE, thus breaking quite a few things.
LuaEvent::Queue("onLeaveSystem", this);
}
}
}
Expand Down Expand Up @@ -1364,6 +1369,7 @@ bool Ship::SpawnCargo(CargoBody * c_body) const
void Ship::EnterHyperspace() {
assert(GetFlightState() != Ship::HYPERSPACE);

// Is it still a good idea, with the onLeaveSystem moved elsewhere?
Ship::HyperjumpStatus status = CheckHyperjumpCapability();
if (status != HYPERJUMP_OK && status != HYPERJUMP_INITIATED) {
if (m_flightState == JUMPING)
Expand All @@ -1374,8 +1380,6 @@ void Ship::EnterHyperspace() {
// Clear ships cached list of nearby bodies so we don't try to access them.
m_nearbyBodies.clear();

LuaEvent::Queue("onLeaveSystem", this);

SetFlightState(Ship::HYPERSPACE);

// virtual call, do class-specific things
Expand Down
68 changes: 58 additions & 10 deletions src/WorldView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ void WorldView::InitObject()

m_hyperspaceButton = new Gui::MultiStateImageButton();
m_hyperspaceButton->SetShortcut(SDLK_F7, KMOD_NONE);
m_hyperspaceButton->AddState(0, "icons/hyperspace_engage_f8.png", Lang::HYPERSPACE_JUMP_ENGAGE);
m_hyperspaceButton->AddState(1, "icons/hyperspace_abort_f8.png", Lang::HYPERSPACE_JUMP_ABORT);
m_hyperspaceButton->AddState(0, "icons/hyperspace_disabled_f8.png", Lang::HYPERSPACE_JUMP_DISABLED);
m_hyperspaceButton->AddState(1, "icons/hyperspace_forbidden_f8.png", Lang::HYPERSPACE_JUMP_FORBIDDEN);
m_hyperspaceButton->AddState(2, "icons/hyperspace_forbidden_abort_f8.png", Lang::HYPERSPACE_JUMP_ABORT);
m_hyperspaceButton->AddState(3, "icons/hyperspace_engage_f8.png", Lang::HYPERSPACE_JUMP_ENGAGE);
m_hyperspaceButton->AddState(4, "icons/hyperspace_abort_f8.png", Lang::HYPERSPACE_JUMP_ABORT);
m_hyperspaceButton->onClick.connect(sigc::mem_fun(this, &WorldView::OnClickHyperspace));
m_hyperspaceButton->SetRenderDimensions(30.0f, 22.0f);
m_rightButtonBar->Add(m_hyperspaceButton, 66, 2);
Expand Down Expand Up @@ -463,15 +466,21 @@ void WorldView::OnClickBlastoff()

void WorldView::OnClickHyperspace(Gui::MultiStateImageButton *b)
{
// Not the best way, but show the button when docked, but flip it back when pressed
if(Pi::player->GetFlightState() == Ship::DOCKED || Pi::player->GetFlightState() == Ship::LANDED)
ResetHyperspaceButton();
if(Pi::player->GetFlightState() == Ship::DOCKED || Pi::player->GetFlightState() == Ship::LANDED){
// Maybe not the best, but flip state back (from disabled to disabled, I assume?)
m_hyperspaceButton->StatePrev();
}

if (Pi::player->IsHyperspaceActive()) {
// Hyperspace countdown in effect.. abort!
Pi::player->AbortHyperjump();
m_game->log->Add(Lang::HYPERSPACE_JUMP_ABORTED);
} else {

// State backs once from original state
m_hyperspaceButton->StatePrev(); // reset to original state...
m_hyperspaceButton->StatePrev(); // ... -1 from original state
}
else{
// Initiate hyperspace drive
SystemPath path = m_game->GetSectorView()->GetHyperspaceTarget();
LuaObject<Player>::CallMethod(Pi::player, "HyperjumpTo", &path);
Expand All @@ -487,13 +496,13 @@ void WorldView::OnRequestTimeAccelInc()
void WorldView::OnRequestTimeAccelDec()
{
// requests a decrease in time acceleration
Pi::game->RequestTimeAccelDec();
Pi::game->RequestTimeAccelDec();
}

void WorldView::ResetHyperspaceButton()
{
if(m_hyperspaceButton->GetState() == 1)
m_hyperspaceButton->StatePrev();
// After a jump:
m_hyperspaceButton->SetActiveState(0);
}

void WorldView::Draw3D()
Expand Down Expand Up @@ -562,10 +571,49 @@ static Color get_color_for_warning_meter_bar(float v) {
}

void WorldView::RefreshHyperspaceButton() {

// 0 = "disabled" - if target selected but landed
// 1 = "forbidden" - if flying below allowed jump altitude
// 2 = "forbidden_abort" - if countdown below allowed jump altitude
// 3 = "engage" - above allowed jump distance
// 4 = "engage_abort" - abort current countdown, above allowed altitude
//
// (Note: when pressing a button in state 1..4, state is auto-incremented by one).

SystemPath target = m_game->GetSectorView()->GetHyperspaceTarget();
if (LuaObject<Ship>::CallMethod<bool>(Pi::player, "CanHyperjumpTo", &target))
if (LuaObject<Ship>::CallMethod<bool>(Pi::player, "CanHyperjumpTo", &target)){
// std::cout << "ONE" << std::endl;
if(Pi::player->GetFlightState() == Ship::FLYING || Pi::player->GetFlightState() == Ship::JUMPING)
{
// std::cout << "TWO" << std::endl;
// leave the "disabled" state if not landed:
if(m_hyperspaceButton->GetState() == 0)
m_hyperspaceButton->StateNext();

if(!LuaObject<Ship>::CallMethod<bool>(Pi::player, "IsHyperjumpAllowed")){
// If crossing boundary from above
if(3 <= m_hyperspaceButton->GetState()){
m_hyperspaceButton->StatePrev();
m_hyperspaceButton->StatePrev();
}
}
else{
// If crossing the boundary from below
if(2 >= m_hyperspaceButton->GetState()){
m_hyperspaceButton->StateNext();
m_hyperspaceButton->StateNext();
}
}
}
else{
//grayed out disabled button, if target set while LANDED/DOCKED/(UN)DOCKING
m_hyperspaceButton->SetActiveState(0);
}

m_hyperspaceButton->Show();
}
else
//If no target selected, then no button at all:
m_hyperspaceButton->Hide();
}

Expand Down

0 comments on commit 8206d0a

Please sign in to comment.