Skip to content

Commit

Permalink
incremental changes
Browse files Browse the repository at this point in the history
  • Loading branch information
joonicks committed Oct 2, 2021
1 parent f8d280c commit 109612e
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 57 deletions.
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
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
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"
}
}
2 changes: 1 addition & 1 deletion data/world/systems/00_sol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ local mercury_starports = {
: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))
--:rotation_period(f(1,24*60*3))
:inclination(math.deg2rad(90))
}

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 109612e

Please sign in to comment.