Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tradeships #4984

Merged
merged 5 commits into from
May 2, 2021
Merged

Improve tradeships #4984

merged 5 commits into from
May 2, 2021

Conversation

Gliese852
Copy link
Contributor

@Gliese852 Gliese852 commented Oct 26, 2020

tradesh_git.mp4

TODO

  • create a pre-calculation of the ship's path according to the specified parameters

  • create a function that spawns ships "on the way" with checking for possible collisions at the insertion point

  • improve the method of creating merchant ships, using these functions (TradeShips.lua)

Commits

Add class for pre-calculating spaceship path

A one-dimensional case is considered, given the parameters of the ship
and the length of the path, a flight plan is calculated, which allows
determining the time, speed, and mass of the ship at any point on the
way. Also added a simple simulator for testing purposes.

Add function PutShipOnRoute

Function description:

The ship rearranged from its current position to a given body in space, for a given part of the path, as if it were flying in a straight line, consuming fuel and changing speed. The ship's current speed is ignored and is considered to be equal to target's.
This function changes the coordinates, the mass of fuel in the tank, and the speed of the given ship.

This function will be used so that when the game starts, or the player jump into the system, the NPC ships looked as if they had been moving on their routes for a long time.

Improve Tradeships.lua

Preparations:

  • Add method Ship:GetCargo
  • Add method Ship:GetDurationForDistance
  • Add method Ship:Create
  • Add method ui.treeNode & ui.treePop
  • Add method LuaObject::CreateInLua
  • Add functions for convenient selection of a random array element
  • Add some numerical utilites
  • Add a tool to easily draw a table from an array
  • Fix ui.Format.Number
  • Turn off the output when the ship changes frames
  • Reduce the detection range of hyperspace clouds
  • Remove the call LuaEvent::Emit immediately after exiting hyperspace
  • Split Tradeships.lua into 5 modules

Tradeships.lua

  • For current system, calculate all possible routes of tradeships, durations, flows; arrange ships in space in such equilibrium state, as if the have been flying about their business for a long time.

  • Calculate the average spawn interval for new ships, and parking intervals, so that the stations are not overcrowded or empty.

  • Сreate a tab in the debug window with full information on routes, ships, stations, remote systems.

Fix some bugs appeared

  • Move the drawing of the system map pigui to a separate handler to ensure that it will always be drawn strictly after the SystemView object.
  • Fix the setting of the m_colliding parameter of the ship during deserialization.

Reduce the flow of tradeships

  • reduce the flow to the most popular station in system by 40%.
  • reduce the maximum station load by 25%
  • reduce the maximum ships limit by 40%

This is to improve the performance. although these parameters are independent, it is better to change them synchronously. For example, if you just reduce the allowable number of ships, in all systems this number can become the same. If the load on the station is not reduced, the ships will stay there for too long.

Details on the forum: https://pioneerspacesim.net/forum/viewtopic.php?f=3&t=650

@The-EG
Copy link
Contributor

The-EG commented Oct 26, 2020

Just a heads up: adding sources below src/ automatically adds them to the build, which I'm guessing you didn't intend for src/ship/PPTester since it has a main. Also, it looks like you're missing #include <algorithm> for std::min.

@Gliese852
Copy link
Contributor Author

@The-EG Thanks! To be honest, I have not yet figured out what to do with PPTester.cpp, probably this is a draft for a unit test, if I figure out how to make it properly. Or just throw it away.

@impaktor
Copy link
Member

impaktor commented Nov 4, 2020

@Gliese852 Perhaps it's something we can add to the Ctrl-i menu? (I don't know what it does, but I like in-game debug tools!)

@Gliese852
Copy link
Contributor Author

@impaktor I just can't think of how it can be useful in debugging. Rather, it can be useful for navigation in space. With minor modifications it will be possible to calculate the time of arrival at the station, for example.

@impaktor
Copy link
Member

Yay! The two new Github Actions ran (gcc & clang).

@Gliese852
Copy link
Contributor Author

@impaktor it's strange that the checks passed, locally I get a linking error, because there are two main functions

@The-EG
Copy link
Contributor

The-EG commented Nov 23, 2020

Odd that the checks aren't at least warning about the duplicate main symbols, but it's definitely there.
The appveyor build actually picked up the main defined in PPTester.cpp by the looks of it. That's supposed to be the modelcompiler...I'm guessing GCC and clang are doing something similar.

@Gliese852
Copy link
Contributor Author

I decided to replace a separate test file PPTester.cpp with the ability to output a test report directly inside the game.
if you define the value PRECALCPATH_TESTMODE, while calling the functions of this class, a report similar to this will be output:

--------------------------------------------------------------------------------
Testing PrecalcPath::setTime()
.
Ship parameters:
EV:             18200000.00
F:               6100000.00
acap:                   inf
mass:             188000.00
fuel:              48000.00
margin:                0.90
.
Path and point parameters:
Stotal:     139533955305.25
V0:                    0.00
time:              96876.48
.
parameter |      calculated |       simulated |   deviation | result
----------|-----------------|-----------------|-------------|-------
Duration  |       126804.42 |       126804.50 |    0.00006% | OK
Vmax      |      2201119.79 |      2201119.26 |    0.00002% | OK
V         |      1080400.80 |      1080399.30 |    0.00014% | OK
fuel      |        16635.80 |        16635.79 |    0.00004% | OK
dist      | 123206922481.00 | 123207023018.29 |    0.00008% | OK
state     |              -1 |              -1 |             | OK
.
Test passed successfully!
--------------------------------------------------------------------------------

@Gliese852
Copy link
Contributor Author

now it's even playable

@Gliese852 Gliese852 force-pushed the tradeships branch 2 times, most recently from dd73b6c to 0539fd2 Compare March 7, 2021 22:49
@Gliese852 Gliese852 marked this pull request as ready for review March 7, 2021 23:13
@Gliese852 Gliese852 changed the title WIP: Generate tradeships at different points of their routes Generate tradeships at different points of their routes Mar 7, 2021
@Gliese852 Gliese852 changed the title Generate tradeships at different points of their routes Improve tradeships Mar 7, 2021
@sturnclaw
Copy link
Member

@Gliese852 given that the TradeShips.lua file is now well over 1800 lines of Lua code (even with debug code), I would highly recommend that you create a folder for it in data/modules/ and split the file up into several sub-files. It massively helps readability to have similar functionality grouped into smaller files, and also means we can eventually promote functionality that's common to multiple modules to a top-level library.

Copy link
Member

@sturnclaw sturnclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things stood out to me; I'll wait to review the Lua until it's a little bit more manageable (and I have more time).

@Gliese852
Copy link
Contributor Author

@Gliese852 given that the TradeShips.lua file is now well over 1800 lines of Lua code (even with debug code), I would highly recommend that you create a folder for it in data/modules/ and split the file up into several sub-files. It massively helps readability to have similar functionality grouped into smaller files, and also means we can eventually promote functionality that's common to multiple modules to a top-level library.

@Web-eWorks divided Tradeships.lua into 5 modules, and brought something to the libs

@Gliese852 Gliese852 requested a review from sturnclaw March 11, 2021 23:13
Copy link
Member

@sturnclaw sturnclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slowly but surely making my way through this review. Here's a batch of change requests; I'll dig into the meat of the PR further tomorrow.

Copy link
Member

@sturnclaw sturnclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet more changes - I'm sure some of these are pointing at code you didn't write, but it's a good time to do some cleanup of the module anyways.

@Gliese852 Gliese852 force-pushed the tradeships branch 3 times, most recently from d40efe8 to f32efbe Compare April 9, 2021 21:54
Copy link
Member

@sturnclaw sturnclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet another batch of changes; only three files are left to review but I'm out of time to review those right now.

@Gliese852
Copy link
Contributor Author

@Web-eWorks probably I will put the changes that I make in separate commits, and then I will squash them all where necessary

@Gliese852 Gliese852 force-pushed the tradeships branch 2 times, most recently from 88d7c05 to c87c21e Compare April 10, 2021 21:50
Copy link
Member

@sturnclaw sturnclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't had a chance to playtest this (or anything else) for a while now, but the code is good to go! Thank you @Gliese852 for working so hard to make the code easy to maintain and understand for contributors (and for putting up with my pedantic style suggestions 😄) - this PR is quite a big one, but you've done an excellent job with it.

@Gliese852
Copy link
Contributor Author

@Web-eWorks Thank you very much for taking so much of your time to review!
I will squash all tempourary commits soon.

I played for a few hours, mostly courier missions, everything seems to work stably.
Savegame size with 700 ships in space is about 500 kb.
FPS dropped significantly but your 'shipfest' branch will fix the problem.
If the FPS turns out to be uncomfortable afterwards, it is quite easy to reduce the flow of ships in the master.
There are some non-critical issues with autopilot, but fixes are almost ready in #5127

A one-dimensional case is considered, given the parameters of the ship
and the length of the path, a flight plan is calculated, which allows
determining the time, speed, and mass of the ship at any point on the
way.

Also added a simple simulator for testing purposes.
Function description:

The ship rearranged from its current position to a given body in space, for a
given part of the path, as if it were flying in a straight line, consuming
fuel and changing speed. The ship's current speed is ignored and is
considered to be equal to target's.
This function changes the coordinates, the mass of fuel in the tank, and the
speed of the given ship.

This function will be used so that when the game starts, or the player
jump into the system, the NPC ships looked as if they had been moving on
their routes for a long time.
- Preparations:

Add method Ship:GetCargo
Add method Ship:GetDurationForDistance
Add method Ship:Create
Add method ui.treeNode & ui.treePop
Add method LuaObject::CreateInLua
Add functions for convenient selection of a random array element
Add some numerical utilites
Add a tool to easily draw a table from an array
Fix ui.Format.Number
Turn off the output when the ship changes frames
Reduce the detection range of hyperspace clouds
Remove the call LuaEvent::Emit immediately after exiting hyperspace
Split Tradeships.lua into 5 modules

- Tradeships

For current system, calculate all possible routes of tradeships,
durations, flows; arrange ships in space in such equilibrium state,
as if the have been flying about their business for a long time.

Calculate the average spawn interval for new ships, and parking
intervals, so that the stations are not overcrowded or empty.

Сreate a tab in the debug window with full information on
routes, ships, stations, remote systems.
- Move the drawing of the system map pigui to a separate handler to
  ensure that it will always be drawn strictly after the SystemView object
- Fix the setting of the m_colliding parameter of the ship during
  deserialization
@Gliese852
Copy link
Contributor Author

Squashed all tempourary commits

@fluffyfreak
Copy link
Contributor

Don't worry about the VS failure, I'll fix that up once it's merged.

@sturnclaw
Copy link
Member

Building in RelWithDebInfo mode, I'm noticing a major frame drop in Sol when turning ship orbit display on in the System View. While I'd like to further improve that particular view's CPU usage, for now I'd suggest dropping the number of tradeships down to ~300 (from 500 in Core.lua) in deference to the members of our community with lower-spec PCs. I'd love to make that number configurable (and be able to bring it back up) but right now I don't think this is quite performant enough to merge as the 'default'.

Otherwise, this is an amazing PR and I really love the way the tradeship debug view looks with the new debugger color scheme!
Screenshot from 2021-05-01 19-55-34

@impaktor
Copy link
Member

impaktor commented May 2, 2021

What's RelWithDebInfo?

OK, so just drop default number of ships and this will be ready for merge?

@sturnclaw
Copy link
Member

What's RelWithDebInfo?

OK, so just drop default number of ships and this will be ready for merge?

Release with Debug Info - it's a default Cmake build target that compiles with optimizations and debug symbols (what we ship in releases).

Yes, just drop the default number of ships until such a time as we're able to optimize some of the remaining bottlenecks in the code, and this PR is ready to merge.

- reduce the flow to the most popular station in system by 40%.
- reduce the maximum station load by 25%
- reduce the maximum ships limit by 40%

This is to improve the performance. although these parameters are
independent, it is better to change them synchronously.  For example, if
you just reduce the allowable number of ships, in all systems this
number can become the same.  If the load on the station is not reduced,
the ships will stay there for too long.
@Gliese852
Copy link
Contributor Author

@Web-eWorks I'll probably make this change in a separate commit to clarify how the flow is set up.

@sturnclaw sturnclaw merged commit 9795b9d into pioneerspacesim:master May 2, 2021
@sturnclaw
Copy link
Member

Merged! Thank you for all of the work on this @Gliese852!
@fluffyfreak whenever you've recovered, the VS files need updating for the new files involved in the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants