SUBSIM Radio Room Forums

SUBSIM Radio Room Forums (https://www.subsim.com/radioroom/index.php)
-   SH4 Mods Workshop (https://www.subsim.com/radioroom/forumdisplay.php?f=219)
-   -   SH4 CELESTIAL NAV UPDATED PYTHON SCRIPT (https://www.subsim.com/radioroom/showthread.php?t=255397)

Pilot_76 05-06-23 10:37 PM

SH4 CELESTIAL NAV UPDATED PYTHON SCRIPT
 
Greetings Skippers.

Mike 6SJ7GT released a mod where we simulate celestial navigation in SH4. The mod is called SH4 Nav Pkg. It basically instructs you into setting up your SH4 for the real thing: No Submarine’s icon with GPS on the map at all. Dead reckoning and Celestial navigation only just like the real thing at the time.

A Python script was also present in the mod. Its purpose was pretty simple: To retrieve your submarine’s position from your saved game file then load Stellarium (a sky simulator) where the exact same position would be the default one once loaded. The user must only input the date and time in GMT/UTC. Also, the script would remove and replace the coordinates in the location bar with the name of the submarine, preventing you from knowing where exactly your coordinates were. After all, that is the sole purpose of the script: To hide your position and work your way as a Navigator.

At present, both Python and Stellarium have gone through many updates and the script does not work at all anymore. Since I have ZERO coding skills, I tried and successfully made ChatGPT into writing a new code that works.

Download the latest Python at:

https://www.python.org/downloads/

Current version is 3.11.3 as of May 2023. Once installed, it will run the script when you double-click it. The updated script is located at:

https://drive.google.com/file/d/1ycI...usp=share_link

Notes about the script:

Edit it using Python’s IDLE or with a text editor. It is a modified version of Mike’s file. Note that when editing your file’s location, you must type as “C:\\folder name” instead of “C:\foldername”. Two locations are to be typed in: Your Stellarium and the saved game from SH4 folders. The first saved game on the list in SH4 “Load saved game menu” will be the file that the Sub’s position will be extracted. Under Windows’ File Explorer options, set it to “Show hidden files” so you can reach the saved game’s location. Change the location of the folder as needed. Also note that the Submarine must have at least a waypoint set.

Download Stellarium:

http://stellarium.org/

About Stellarium:
Set the POV to “Perspective”. I prefer that one to be more realistic. Also, I set the “Ocean” as the default landscape. Cardinal points ON and celestial bodies’ names are OFF, so I can also practice identifying the bodies by myself. Stellarium has many options, so please read its guide and configure it in a way that suits you the most.

One interesting thing to be mentioned is that although the script does take away the current position from view you may also edit its config.ini and under [gui] you may add “flag_show_location = false” and the current position will be hidden too. This is under “Configuration Files” Part V-Appendices in the guide. This is useful if you will choose to hide the position when using other games (Such as SH5, Virtual Sailor NG etc.) but a specific script must be written for each game/sim.

Mostly the saved games have the position of your vessel/ship/submarine, and it’s just a matter into pasting the contents (if in ASCII/txt format) into ChatGPT and prompting it into writing a Python code.

I have also made available more celestial navigation material but SH4 "tailored". Download at:

https://drive.google.com/drive/folde...usp=share_link

Credits go to Mike 6SJ7GT.

Below is the code to be copied directly into Python’s IDLE if you wish.

import os
import sys
import time

####### Edit Locations here ############
# This assumes that your FIRST save game on the list in SH4 is the one to be scanned for its Submarine position.
# Change the directory on the next line to point to SaveData.map in your saved game directory. Turn on the "Show hidden files/folders in Explorer if needed.
savefolder = "C:\\Users\\YOUR_USERNAME_HERE\\Documents\\SH4\\da ta\\cfg\\SaveGames\\00000000"
# Set the Stellarium directory: Note:In the default c:\program files etc the access was denied. I installed Stellarium in another directory.
stellocation = "F:\\Stellarium2"

####### End Editable locations #########

savedgame = os.path.join(savefolder, "SaveData.map")
psettings = os.path.join(savefolder, "PlayerSettings.cfg.rep")


def nn(x):
if len(x) == 1:
num = "0" + x
else:
num = x
return num


# Read savegame.
with open(savedgame, 'r') as f:
content = f.read()

# Find lat & lon in the file
start = content.find("Waypoint")
longst = content.find("Pt0=", start)
longnd = content.find(",", longst)
latnd = content.find(",", longnd + 1)

# Convert to decimal degrees
try:
longitude = float(content[longst + 4:longnd]) / 120000
latitude = float(content[longnd + 1:latnd]) / 120000
except ValueError as e:
print("Error converting to float:", e)

# Get date & time (input)
gdate = input('GMT yyyy/mm/dd: ')
gtime = input('GMT hh:mm: ')
gdate = gdate.replace('/', '-')
# time.sleep(2)
with open(psettings, 'r') as f:
settings = f.read()

# Set the ocean label
if -90 < longitude <= 30:
ocean = "Atlantic"
elif 30 < longitude <= 105:
ocean = "Indian"
elif longitude <= -90 or longitude > 105:
ocean = "Pacific"

# Get the sub name
start = settings.find("UnitName=")
end = settings.find("\n", start)
boat = settings[start + 9:end]

# Write sh4.ssc script
longitude = str(longitude)
latitude = str(latitude)

with open(os.path.join(stellocation, "scripts", "startup.ssc"), 'w') as stella:
stella.write("core.setDate("" + gdate + "T" + gtime + ":00")\n")
stella.write(
"core.setObserverLocation(" + longitude + ", " + latitude + ", 0, 0, "" + boat + ", " + ocean + "")"
)

time.sleep(1)
# Open Stellarium and exit Python
prog = "start /d "" + stellocation + "" stellarium.exe" # --startup-script startup.ssc"
os.system(prog)
sys.exit()

Pilot_76 05-07-23 10:09 AM

The correct code to copy is :

"C:\\Users\\YOUR_USERNAME_HERE\\Documents\\SH4\\da ta\\cfg\\SaveGames\\00000000"

There is a space in "da ta" that for some reason when editing the post it won't go away.

The script itself is ok in the download link though.

propbeanie 05-07-23 10:28 AM

Excellent news Pilot_76! I had an SH4 install that I had nav working in a while back, but it has been trashed a while now, with all the various updates. Looking forward to trying this. Hopefully, I can increase my "skills" enough to where I don't smack land again... :arrgh!: :salute:

btw, that "space" character is probably from the "editor" here, needing to "break" the line.

Aktungbby 05-07-23 12:51 PM

Welcome back!
 
Pilot_76!:Kaleun_Salute:


All times are GMT -5. The time now is 12:08 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright © 1995- 2024 Subsim®
"Subsim" is a registered trademark, all rights reserved.