Skip to content

Commit

Permalink
Merge pull request #5 from Osariusz/main
Browse files Browse the repository at this point in the history
Intro to empire generation and new Stellaris version update
  • Loading branch information
Osariusz authored Feb 7, 2024
2 parents 5bb30d4 + 3f96e97 commit a2bbe0d
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 19 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
out/
gamestate
__pycache__/
Release/
Release/
pyinstaller/
build/
dist/
main.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Disabled
11 changes: 11 additions & 0 deletions Stellarior/Source/Country.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

class Country:

def __init__(self, primary_species, dictionary):
self.primary_species = primary_species
self.dictionary = dictionary

self.name = dictionary["name"]
self.adjective = dictionary["adjective"]

self.token = self.name.replace(" ","_").lower()
96 changes: 95 additions & 1 deletion Stellarior/Source/Mod.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
from Planet import Planet
from System import System
from Country import Country
from Species import Species
import os
import shutil
import logging
Expand All @@ -25,9 +27,11 @@ class = "{star_class}"
"""

def __init__(self, save_name : str , systems : list[System]):
def __init__(self, save_name : str , systems : list[System], countries : list[Country], species : list[Species]):
self.mod_name = "Converted - " + save_name
self.systems = systems
self.countries = countries
self.species = species

self.mod_folder_name = save_name.replace(" ","_").lower()
self.mod_folder = os.path.join("output",self.mod_folder_name)
Expand Down Expand Up @@ -79,10 +83,97 @@ def get_system_initializer(self, system):
result += planet_string
return result

def get_prescripted_empire(self, empire : Country):
result = f"{empire.token} = {{\n"
result += f"name = \"{empire.name}\"\n"

result += """adjective = "PRESCRIPTED_adjective_tzynn"
spawn_enabled = yes # yes / no / always
ship_prefix = "PRESCRIPTED_ship_prefix_tzynn"
"""
#temporary
result += """ species = {
class = "REP"
portrait = "rep9"
name = "PRESCRIPTED_species_name_tzynn"
plural = "PRESCRIPTED_species_plural_tzynn"
adjective = "PRESCRIPTED_species_adjective_tzynn"
name_list = "REP1"
trait = "trait_strong"
trait = "trait_resilient"
trait = "trait_rapid_breeders"
trait = "trait_decadent"
trait = "trait_quarrelsome"
}
"""

result += """room = "personality_slaving_despots_room"
authority = "auth_imperial"
civics = { "civic_police_state" "civic_slaver_guilds" }
government = gov_star_empire
origin = "origin_default"
ethic = "ethic_authoritarian"
ethic = "ethic_fanatic_militarist"
planet_name = "PRESCRIPTED_planet_name_tzynn"
planet_class = "pc_desert"
system_name = "PRESCRIPTED_system_name_tzynn"
graphical_culture = "reptilian_01"
city_graphical_culture = "reptilian_01"
empire_flag = {
icon= {
category = "pointy"
file = "flag_pointy_2.dds"
}
background= {
category = "backgrounds"
file = "v.dds"
}
colors={
"red"
"black"
"null"
"null"
}
}
ruler = {
name = "PRESCRIPTED_ruler_name_tzynn"
gender = male
portrait = "rep9"
texture = 1
clothes = 4
leader_class = ruler
}
}
"""

return result

def create_path_if_doesnt_exist(self, path):
if(not os.path.exists(path)):
os.makedirs(path)

def write_prescripted_countries(self):
prescripted_countries_folder_path = os.path.join(self.mod_folder,"prescripted_countries")
prescripted_countries_path = os.path.join(prescripted_countries_folder_path,"98_prescripted_countries.txt")

self.create_path_if_doesnt_exist(prescripted_countries_folder_path)

with open(prescripted_countries_path,"w",encoding="utf-8") as file:
file.write("")
with open(prescripted_countries_path, "a", encoding="utf-8") as file:
for empire in self.countries:
file.write(self.get_prescripted_empire(empire))


def write_initializers(self):
initializers_folder_path = os.path.join(self.mod_folder,"common\solar_system_initializers")
initializers_path = os.path.join(initializers_folder_path,"init_initializers.txt")
Expand Down Expand Up @@ -154,6 +245,9 @@ def create_mod(self):
logging.info("Writing system initializers")
self.write_initializers()
logging.progress("75%")
logging.info("Writing prescripted empires")
self.write_prescripted_countries()
logging.progress("82%")
logging.info("Writing maps")
self.write_map()
logging.progress("99%")
129 changes: 113 additions & 16 deletions Stellarior/Source/Save.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import re
from Planet import Planet
from System import System
from Species import Species
from Country import Country
import logging

class Save:
re_planet = R"[0-9]+={\n\t\t\tname={"
re_system = R"[0-9]+={\n\t\tcoordinate={"
planet_finish_text = "auto_slots_taken"
system_finish_text = "sector"
re_planet = R"[0-9]+=\n\t*{\n\t\t\tname=\n\t*{"
re_system = R"[0-9]+=\n\t*{\n\t\tcoordinate=\n\t*{"
re_country = R"[0-9]+=\n\t*{\n\t\tflag=\n\t*{"
re_species = R"[0-9]+=\n\t*{\n\t\t(?:.*\n\t\t){0,1}name_list="

def __init__(self, text):
self.text = text
self.generated_species = None

def get_save_attribute(self, attribute):
result = re.search(f"{attribute}=(.+)\n",self.text).groups()[0]
result = result.replace("\"","")
return result
return result

def get_save_name(self):
return self.get_save_attribute("name")
Expand All @@ -36,17 +39,16 @@ def get_dictionary_from_entries(self, entries):
dictionary[dictionary_entry[0]] = dictionary_entry[1].replace("\"","")
return dictionary



def get_planet_dictionaries(self):
planet_finish_text = "auto_slots_taken"
result = []
start_planets = self.text.find("\nplanets={\n")
end_planets = self.text.find("\ncountry={\n")
start_planets = self.text.find("\nplanets=\n{\n")
end_planets = self.text.find("\ncountry=\n{\n")
shorter_text = self.text[:end_planets]
for planet_start in self.get_starts(self.re_planet,start_planets,end_planets):
start = shorter_text.find(planet_start)
shorter_text = shorter_text[start:]
end = shorter_text.find(self.planet_finish_text)
end = shorter_text.find(planet_finish_text)
planet_text = shorter_text[:end]

dictionary_entries = re.findall("\n\t\t\t(.+)=(.+)",planet_text)
Expand All @@ -67,7 +69,7 @@ def get_planets(self):
planet = Planet(planet_dictionary["id"],planet_dictionary["name"],planet_dictionary["planet_class"],planet_dictionary)
result.append(planet)
logging.progress("25%")
logging.info("Got planets")
logging.info(f"Got {len(result)} planets")
return result

def set_planet_moons(self, planet_dictionary : list[Planet]):
Expand All @@ -81,14 +83,15 @@ def set_planet_moons(self, planet_dictionary : list[Planet]):


def get_system_dictionaries(self):
system_starting_point = self.text.find("galactic_object={")
system_ending_point = self.text.find("\nambient_object={")
system_finish_text = "sector"
system_starting_point = self.text.find("galactic_object=\n{")
system_ending_point = self.text.find("\nambient_object=\n{")
result = []
shorter_text = self.text[:system_ending_point]
for system_start in self.get_starts(self.re_system,system_starting_point,system_ending_point):
start = shorter_text.find(system_start)
shorter_text = shorter_text[start:]
end = shorter_text.find(self.system_finish_text)
end = shorter_text.find(system_finish_text)
system_text = shorter_text[:end]

dictionary_entries = re.findall("\n\t\t(.+)=(.+)",system_text)
Expand Down Expand Up @@ -125,9 +128,103 @@ def get_systems(self):
for system_dictionary in self.get_system_dictionaries():
system = System(system_dictionary["id"],system_dictionary["name"],system_dictionary["star_class"],system_dictionary)
for planet_id in system_dictionary["planet"]:
system.planets.append(planet_dictionary[planet_id])
if(planet_id in planet_dictionary):
system.planets.append(planet_dictionary[planet_id])
else:
logging.error(f"No planet with id: {planet_id} found! Ignoring")
result.append(system)

logging.progress("50%")
logging.info("Got systems")
logging.info(f"Got {len(result)} systems")
return result

def get_species_dictionaries(self):
species_finish_text = "gender"
species_starting_point = self.text.find("species_db={")
species_ending_point = self.text.find("spy_networks={")
result = []
shorter_text = self.text[:species_ending_point]
for species_start in self.get_starts(self.re_species,species_starting_point,species_ending_point):
start = shorter_text.find(species_start)
shorter_text = shorter_text[start:]
end = shorter_text.find(species_finish_text)
species_text = shorter_text[:end]

dictionary_entries = re.findall("\n\t\t(.+)=(.+)",species_text)
id = species_text[:species_text.find("=")]
name = re.search("key=\"(.+)\"",species_text[species_text.find("name="):]).groups()[0]
plural = re.search("key=\"(.+)\"",species_text[species_text.find("plural="):]).groups()[0]
adjective = re.search("key=\"(.+)\"",species_text[species_text.find("adjective="):]).groups()[0]
if(adjective == "%ADJECTIVE%"):
adjective = re.search("key=\"(.+)\"",species_text[species_text.find("value={\n\t\t\t\t\t"):]).groups()[0]
dictionary = self.get_dictionary_from_entries(dictionary_entries)
dictionary["id"] = id
dictionary["name"] = name
dictionary["plural"] = plural
dictionary["adjective"] = adjective

result.append(dictionary)
return result

def get_species(self):
logging.info("Getting species")
if(self.generated_species != None):
return self.generated_species
else:
result = []

for species_dictionary in self.get_species_dictionaries():
species = Species(species_dictionary)
result.append(species)

logging.info("Got species")
self.generated_species = result
return result

def get_country_dictionaries(self):
country_finish_text = "sector"
country_starting_point = self.text.find("country={")
country_ending_point = self.text.find("\first_contact={")
result = []
shorter_text = self.text[:country_ending_point]
for country_start in self.get_starts(self.re_country,country_starting_point,country_ending_point):
start = shorter_text.find(country_start)
shorter_text = shorter_text[start:]
end = shorter_text.find(country_finish_text)
country_text = shorter_text[:end]

dictionary_entries = re.findall("\n\t\t(.+)=(.+)",country_text)
id = country_text[:country_text.find("=")]
name = re.findall("key=\"(.+)\"",country_text)[0]
adjective_command_list = ["%ADJ%","%ADJECTIVE%"]
if(name in adjective_command_list):
name_segment = country_text[country_text.find("name="):country_text.find("adjective=")]
name_values = re.findall("value={\n\t+key=\"(.+)\"",name_segment)
name = " ".join(name_values)

#also add country adjective if the names works

dictionary = self.get_dictionary_from_entries(dictionary_entries)
dictionary["id"] = id
dictionary["name"] = name

result.append(dictionary)
return result

def get_countries(self):
logging.info("Getting countries")
result = []

species = self.get_species()
species_id_dictionary = {}
for one_species in species:
species_id_dictionary[int(one_species.dictionary["id"])] = one_species

for country_dictionary in self.get_country_dictionaries():
founder_species_id = int(country_dictionary["founder_species_ref"])
country = Country(species_id_dictionary[founder_species_id],country_dictionary)

result.append(country)

logging.info("Got countries")
return result
6 changes: 6 additions & 0 deletions Stellarior/Source/Species.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


class Species:

def __init__(self, dictionary):
self.dictionary = dictionary
2 changes: 1 addition & 1 deletion Stellarior/Source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def logToRoot(message, *args, **kwargs):
logging.info("Found save location")
logging.progress("5%")
logging.info("Preparing save information")
mod = Mod(save_name,save.get_systems())
mod = Mod(save_name,save.get_systems(),save.get_countries(),save.get_species())
mod.create_mod()
except Exception as e:
logging.exception("Exception occurred")
10 changes: 10 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe" -m -p:Configuration=Release Fronter\Fronter.sln
copy Fronter\Release Release

:: Requires a virtual environment with pyinstaller package in the pyinstaller folder
pyinstaller\Scripts\pyinstaller.exe Stellarior\Source\main.py --onefile
copy dist\main.exe Release\Stellarior
xcopy Stellarior\Data_Files\blankMod\ Release\Stellarior\blankMod\ /E /Y

python build.py
pause

0 comments on commit a2bbe0d

Please sign in to comment.