Skip to content

Commit

Permalink
incremental changes
Browse files Browse the repository at this point in the history
incremental changes
  • Loading branch information
joonicks committed Oct 3, 2021
1 parent 02f33b6 commit 5f04813
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 60 deletions.
4 changes: 4 additions & 0 deletions data/lang/lore/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
"SOL_MARS_OLYMPUS" : {
"description" : "",
"message" : "Dont forget to enjoy the longest ski slope within a hundred lightyears!"
},
"SOL_MERCURY_CALORIS" : {
"description" : "",
"message" : "Located in Caloris Planitia, one of the largest impact craters in the system. Mercury being so close to the sun has lead to most things around here being named accordingly, 'Calor' even means 'heat' in latin and here there is a surplus of it. The almost unlimited solar power has been put to good use in the recycling business."
}
}
15 changes: 9 additions & 6 deletions data/libs/SpaceStation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local l = Lang.GetResource("ui-core")

function SpaceStation:Constructor()
-- Use a variation of the space station seed itself to ensure consistency
local rand = Rand.New(self.seed .. '-techLevel')
local rand = Rand.New(self.seed .. 'techLevel')
local techLevel = rand:Integer(1, 6) + rand:Integer(0,6)
local isHomeworld = 0
if Game.system.faction ~= nil and Game.system.faction.hasHomeworld and self.path:IsSameSystem(Game.system.faction.homeworld) then
Expand All @@ -37,13 +37,16 @@ function SpaceStation:Constructor()
self:setprop("techLevel", techLevel)
self:setprop("isHomeworld", isHomeworld)
local props = self:GetPropertyDefaults()
if props then
for k,v in pairs(props) do
if k and v then
self:setprop(k, v)
end
if props == nil then return end
for k,v in pairs(props) do
if k and v then
self:setprop(k, v)
end
end
if self.techModifier then
local tmp = self.techLevel + self.techModifier
self:setprop("techLevel", (tmp <= 10) and tmp or 10) -- maximum adjusted level: 10
end
end

local equipmentStock = {}
Expand Down
2 changes: 1 addition & 1 deletion data/pigui/views/mainmenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ local max_flavours = 22
local startLocations = {
{['name']=lui.START_AT_MARS,
['desc']=lui.START_AT_MARS_DESC,
['location']=SystemPath.New(0,0,0,0,18),
['location']=SystemPath.New(0,0,0,0,22),
['logmsg']=lui.START_LOG_ENTRY_1,
['shipType']='sinonatrix',['money']=100,['hyperdrive']=true,
['equipment']={
Expand Down
12 changes: 9 additions & 3 deletions data/world/WORLD.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ world/systems/<##_Name>.lua
## determines order of precedence when loading
Name filename only. faction name is determined by contents of the file

world/stations/<system+stationID>.json
world/stations/<system>/<stationID>.json
world/stations/<faction>.json
world/stations/<system>.json
world/stations/<system+station>.json
world/stations/<system>/<station>.json

Contains patch properties for individual stations.
System subdirectories is checked first.
Faction is the first to be loaded, then system, then both (or either) system+station files.
Any attribute in earlier files that reoccur in later loaded files will be overridden by the
last loaded. For example, faction values can be overridden by specific station files.

Stations Json format:
{
Expand All @@ -35,6 +39,8 @@ world/stations/<system>/<stationID>.json

techLevel Override stations randomly generated techLevel

techModifier Alter the randomly generated techLevel by this number, up to a max of 10 or a minimum of 1

lore Use this key, translated from lang/lore/*.json, as station lore text

nolanglore Use this text as-is, as station lore text
Expand Down
8 changes: 8 additions & 0 deletions data/world/stations/00000000-Sol/Caloris.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"techLevel" : {
"value" : "10"
},
"lore" : {
"value" : "SOL_MERCURY_CYDONIA"
}
}
6 changes: 6 additions & 0 deletions data/world/stations/SolarFederation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"techModifier" : {
"comment" : "added to the techLevel of all Solar Federation stations, up to 10 max",
"value" : "1"
}
}
29 changes: 26 additions & 3 deletions data/world/systems/00_sol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ local mercury = CustomSystemBody:new('Mercury', 'PLANET_TERRESTRIAL')
:orbital_phase_at_start(fixed.deg2rad(f(286,1)))
:rings(false)

local mercury_starports = {
CustomSystemBody:new('Furiosa', 'STARPORT_SURFACE')
:latitude(math.deg2rad(89))
:longitude(5),
CustomSystemBody:new('Caloris', 'STARPORT_SURFACE')
:latitude(math.deg2rad(189))
:longitude(30),
CustomSystemBody:new('Fenix', 'STARPORT_ORBITAL')
-- can orbital station be made to stay in sun synchronous orbit?
:seed(666)
:semi_major_axis(f(1,25680))
:inclination(math.deg2rad(90))
}

local venus = CustomSystemBody:new('Venus', 'PLANET_TERRESTRIAL')
:radius(f(95,100))
:mass(f(815,1000))
Expand All @@ -51,6 +65,13 @@ local venus = CustomSystemBody:new('Venus', 'PLANET_TERRESTRIAL')
:orbital_phase_at_start(fixed.deg2rad(f(248,1)))
:rings(false)

local venus_starports = {
CustomSystemBody:new('Cloud Nine', 'STARPORT_ORBITAL')
:seed(6734)
:semi_major_axis(f(100,100000))
:rotation_period(f(1,24*60*3))
}

local earth = CustomSystemBody:new('Earth', 'PLANET_TERRESTRIAL')
:radius(f(1,1))
:mass(f(1,1))
Expand Down Expand Up @@ -92,15 +113,15 @@ local earth_starports = {
:latitude(math.deg2rad(34))
:longitude(math.deg2rad(118)),
CustomSystemBody:new('Gates Spaceport', 'STARPORT_ORBITAL')
:seed(1)
:seed(8088)
:semi_major_axis(f(100,100000))
:rotation_period(f(1,24*60*3)),
CustomSystemBody:new('Jobs Pad', 'STARPORT_ORBITAL')
:seed(13)
:seed(6502)
:semi_major_axis(f(100,100000))
:rotation_period(f(1,24*60*3)),
CustomSystemBody:new('Torvalds Platform', 'STARPORT_ORBITAL')
:seed(0)
:seed(80386)
:semi_major_axis(f(5.0,100000))
:rotation_period(f(1,24*60*3))
:orbital_phase_at_start(fixed.deg2rad(f(0,1)))
Expand Down Expand Up @@ -724,7 +745,9 @@ local charon = {

s:bodies(sol, {
mercury,
mercury_starports,
venus,
venus_starports,
earth,
earth_starports,
moon,
Expand Down
123 changes: 76 additions & 47 deletions src/lua/LuaSpaceStation.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

#include "Game.h"
#include "Json.h"
#include "LuaConstants.h"
#include "LuaObject.h"
#include "LuaUtils.h"
#include "Pi.h"
#include "Ship.h"
#include "SpaceStation.h"
#include "galaxy/SystemBody.h"
Expand Down Expand Up @@ -170,51 +172,12 @@ static int l_spacestation_get_nearby_traffic(lua_State *l)
*
* experimental
*/
static int l_spacestation_get_property_defaults(lua_State *l)
static void storeprops(const char *propFile, std::map<std::string, std::string> &keyvalues)
{
SpaceStation *station = static_cast<SpaceStation *>(LuaObject<Body>::CheckFromLua(1));
const SystemBody *body = station->GetSystemBody();
const SystemPath sp = body->GetPath();

lua_newtable(l);

/* No known (or licensed) human settlements further out than 100 sectors outwards,
a single byte (or hex FF) for each dimension is enough for a license number */
std::string stationID(100, '\0');
std::snprintf(&stationID[0], 100, "SCC%02X%02X%02X%02X%02X", sp.sectorX, sp.sectorY, sp.sectorZ, sp.systemIndex, sp.bodyIndex);

pi_lua_settable(l, "stationID", stationID.c_str());
pi_lua_settable(l, "visualID", stationID.c_str());

const char *src = station->GetLabel().c_str();
std::string sysstr(100, '\0');
std::snprintf(&sysstr[0], 100, "%02X%02X%02X%02X", sp.sectorX, sp.sectorY, sp.sectorZ, sp.systemIndex);
char stationname[100], *dst = stationname;

/* remove non [A-Za-z] chars from the station name */
for (; *src; src++) {
if (*src >= 'A' && *src <= 'Z')
*dst++ = *src;
else if (*src >= 'a' && *src <= 'z')
*dst++ = *src;
}
*dst = '\0';

/* try to load moar attributes from world station predefinitions
values from json may override default values */
Json preset;
std::string propFile(100, '\0');
std::snprintf(&propFile[0], 100, "world/stations/%s/%s-%s.json", sysstr.c_str(), stationID.c_str(), stationname);
Output("Looking for station presets for '%s' in '%s'\n", station->GetLabel().c_str(), propFile.c_str());
preset = JsonUtils::LoadJsonDataFile(propFile);
if (preset.is_null()) {
std::snprintf(&propFile[0], 100, "world/stations/%s-%s.json", stationID.c_str(), stationname);
Output("Looking for station presets for '%s' in '%s'\n", station->GetLabel().c_str(), propFile.c_str());
preset = JsonUtils::LoadJsonDataFile(propFile);
if (preset.is_null()) {
return 1;
}
}
if (preset.is_null())
return;

for (Json::iterator prop = preset.begin(); prop != preset.end(); ++prop) {
const std::string token = prop.key();
Expand All @@ -224,14 +187,80 @@ static int l_spacestation_get_property_defaults(lua_State *l)
/* if (!valid_token(token)) { continue; } */

Json val = prop.value()["value"];
if (val.is_null()) {
continue;
}
if (!val.is_string()) {
if (val.is_null() || val.is_string() == false) {
continue;
}

pi_lua_settable(l, token.c_str(), std::string(val).c_str());
// will overwrite existing keys
keyvalues[token] = val;
}
}

static inline void stripnonalpha(const char *src, char *dst, const char *end)
{
for (; *src && dst < end; src++) {
if ((*src >= 'A' && *src <= 'Z') || (*src >= 'a' && *src <= 'z'))
*dst++ = *src;
}
*dst = '\0';
}

// try to load attributes from world station predefinitions
// values from json may override default values
static int l_spacestation_get_property_defaults(lua_State *l)
{
SpaceStation *station = static_cast<SpaceStation *>(LuaObject<Body>::CheckFromLua(1));
const SystemBody *body = station->GetSystemBody();
const SystemPath sp = body->GetPath();
std::map<std::string, std::string> keyvalues;

char propFile[100];
char stationname[100];
char sysname[100];
char stationID[20]; // No known (or licensed) human settlements further out than 100 sectors outwards,
// a single byte (-128..127, or hex FF) for each dimension is enough for a license number
char sysstr[10];
std::snprintf(stationID, 20, "SCC%02X%02X%02X%02X%02X", sp.sectorX, sp.sectorY, sp.sectorZ, sp.systemIndex, sp.bodyIndex);
std::strncpy(sysstr, &stationID[3], 9);
sysstr[8] = 0;

keyvalues["stationID"] = stationID;
keyvalues["visualID"] = stationID;

const char *stationlabel = station->GetLabel().c_str();
stripnonalpha(stationlabel, stationname, &stationname[99]);

RefCountedPtr<StarSystem> s = Pi::game->GetGalaxy()->GetStarSystem(sp);
stripnonalpha(s->GetName().c_str(), sysname, &sysname[99]);

if (s->GetFaction()->IsValid()) {
char facname[100];

stripnonalpha(s->GetFaction()->name.c_str(), facname, &facname[99]);

std::snprintf(propFile, 100, "world/stations/%s.json", facname);
Output("Looking for station presets for '%s' in '%s'\n", stationlabel, propFile);
storeprops(propFile, keyvalues);
}

// settings for the whole system
std::snprintf(propFile, 100, "world/stations/%s-%s.json", sysstr, sysname);
Output("Looking for station presets for '%s' in '%s'\n", stationlabel, propFile);
storeprops(propFile, keyvalues);

// in the system specific directory, station names are unique enough
std::snprintf(propFile, 100, "world/stations/%s-%s/%s.json", sysstr, sysname, stationname);
Output("Looking for station presets for '%s' in '%s'\n", stationlabel, propFile);
storeprops(propFile, keyvalues);

std::snprintf(&propFile[0], 100, "world/stations/%s-%s.json", stationID, stationname);
Output("Looking for station presets for '%s' in '%s'\n", stationlabel, propFile);
storeprops(propFile, keyvalues);

// push whatever we collected into a lua table and return it
lua_newtable(l);
for (auto kv : keyvalues) {
pi_lua_settable(l, kv.first.c_str(), kv.second.c_str());
}
return 1;
}
Expand Down

0 comments on commit 5f04813

Please sign in to comment.