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

Law and order to lua #3254

Merged
merged 17 commits into from
Nov 2, 2015
Merged

Conversation

impaktor
Copy link
Member

Move Law to Lua

This moves the law enforcement from Polit.cpp (which is almost empty now) to lua. I worked on this about 6 months ago, but I got stuck due to a segfault when loading a game, but it seems to have fixed itself now (after updating branch to master).

Update: as of 2015-08 I'm continuing this due to #887 being "fixed".

Changed/New features

Some of the changes in how police will behave now:

  1. Police number, and how quickly they react to your crimes (police launch probability) depends on system lawlessness.
  2. However, police will not launch until player has amassed high enough fines. I.e. threshold isn't fine = 0 anymore, but maxFineTolerated=1e3
  3. Lowered fine on piracy to 1e5, and weapons discharge to 1e2, allowing the latter to be below the maxFineTolerated level.
  4. Dumping illegal goods or goods with negative price (close to station) is an offense.
  5. Crime record now lists how many of each crime type the player has.
  6. Player has two crimerecords for each faction, one with "outstanding fines", and one "Crimerecord" with past history.
  7. A police ship destroyed at a station remains so until player hyperjumps back (same as before I believe); i.e. limited number of police ships in station.
  8. Police ships are different for different factions. Default: sinonatrix, CIW: pumpkinseed, SolFed: kanara.

Design / Code

I'm not sure about what code to put in which file, i.e. how to structure it. Current structure reflects a lot of the development history during my work on this.

  • I've introduced a libs/Legal.Lua that holds the crime constants, and a notifyOfCrime() which was previously in Polit.cpp.
  • I've introduced a modules/CrimeTracking to be symmetric with our modules/StatsTracking. It has the event triggers (onShip<ActionX>) for killing, firing, jettison, etc. and it holds the doLawAndOrder() (previously in SpaceStation.cpp).
  • libs/Player.lua has a local CrimeRecord class, which the player has two copies of for each faction (current crimes, and past crimes). There are also Player:AddCrime() and such.
  • In the old (current master) code, each space station upholds the law (every frame) by calling the function DoLawAndOrder. I could implement it the same way in libs/SpaceStation.lua, but thought it needs only be called once (about every 5 seconds), at which point a check is made to find the closest space station (calls a new method: Ship:GetLawEnforcingStations(), returns stations within "law range"), thus you will not be fined twice for one single illegal action done within range of both stations. (I still have to check the distance of the player to all stations every call.)
  • "law range" is a new attribute: Station.lawEnforcedRange (100 km).
  • Stations have a SpaceStation:LaunchPolice(targetShip) and Station:LandPolice().

Progress

For another separate pull request:

When this is merged, I'll be looking into:

  1. Label police ships after the police name used by the faction, requires JSON to faction (It is really cool when the launched police is labeled as "Confederate Police" instead of just "Police".) see: Descriptions of home systems are not translatable #2867
  2. Fix legal footer, Legal status doesn't work. #2863.
  3. Find a better way of compounding fines in NotifyOfCrime. For now: works as it used to.
  4. taking off when denied launch clearance is a crime
  5. Number of available police ships should depend on station size?
  6. Pay fines remotely by F4.
  7. IsCommodityLegal should be in lua?

@fluffyfreak
Copy link
Contributor

This looks good, gutting and removing Polit has been on the cards for far too long! :)

@impaktor impaktor force-pushed the lawAndOrder_to_lua branch 2 times, most recently from 87d9ec4 to 6196ca3 Compare July 7, 2015 12:16
@impaktor impaktor force-pushed the lawAndOrder_to_lua branch 2 times, most recently from 5387531 to a417616 Compare August 13, 2015 16:34
@bszlrd
Copy link
Contributor

bszlrd commented Sep 6, 2015

The two new police ship textures:
sinonatrix
pumpkin
This one doesn't have text, because the UVs are mirrored.

@impaktor
Copy link
Member Author

impaktor commented Sep 6, 2015

For the record, I've been working on this PR this summer, and have it almost done, (I poked @nozmajner about the police textures, thanks). What remains is just to clean up and push the last code changes from my local computer. I currently have very little time (or at least aught to have), so I'll see when this gets done.

EDIT: I updated the text in my first post a few weeks ago. Number 14 on the to-do list above is done. But to be cleaned and pushed.

For now, we just label the police ships by l.POLICE. Would be fun to have
faction specific name, but alas, those strings are not in translation system.
…l other

crime handling and saving/loading of crime record in c++ (most of Polit),
removed crime adding from Ship.cpp as well.

SAVEBUMP needed
@impaktor impaktor changed the title WIP Law and order to lua Law and order to lua Sep 11, 2015
@impaktor impaktor removed the WIP label Sep 11, 2015
@impaktor
Copy link
Member Author

This has been fermenting for over a year now (first commit: 2014 April), and I'm almost approaching issue-1719 status, but now I think I'm ready for this to be reviewed. I don't know how many times I've rebased this, but it's a lot, so the commits should make sense.

I don't expect anyone to read all code, but I would like:

  • Please re-read the original top post (I've updated it)
  • Please (if you have an opinion) look at the balance or "which-function-goes-where" in the files: libs/Legal.lua, modules/CrimeTracking/CrimeTracking.lua (and libs/Player.lua). My current thinking is that at least Legal.lua and CrimeTracking.lua could be combined or something? @laarmen, @johnbartholomew ?
  • If anyone care, play around with it. Specifically, test save/load, jump between factions, and blow up police and save->load.
  • Yes, I know the Police screen isn't updated real-time when you pay your fines, one has to go to some other tab then back again. I don't consider it a bug, more a lack-of-feature-I-didn't-care-to-fix. It's all in Lua, so anyone can play around with it.

So 90% of this work (c++ -> lua) is nothing anyone will notice, but at least below is some eye-candy. Hmmm. Or perhaps eye-oatmeal.

I'VE BEEN BAD:
screenshot-20150818-164622

BUT I RIGHTED MY WRONGS:
screenshot-20150818-164710

BUT OLD HABITS DIE HARD:
screenshot-20150818-164803

@fluffyfreak
Copy link
Contributor

Excellent work!

I don't think I can bring myself to stare at any code tonight after doing it all day for work but will try to look into it & testing next week / Sunday.

Everyone else chip in where possible!

@impaktor
Copy link
Member Author

Thanks @fluffyfreak! By the way, we're getting OpenGL errors on the new police ships, I'm guessing you can understand that more easily than me or @nozmajner.

I can add that since now the code is in Lua, it's easy for anyone to play around with how fines should work, how they're accumulated, and also how/when police should be dispatched. I wrote that part over a year ago, and I'm slightly foggy on it, and it can be tweaked later, but since I lowered fine for "unlawful firing" and put a threshold for when police will start shooting at you, you now have to shoot quite a bit to raise the police.

@laarmen
Copy link
Contributor

laarmen commented Sep 12, 2015 via email

@fluffyfreak
Copy link
Contributor

@impaktor & @nozmajner I think the OpenGL errors are coming from the pumpkinseed_police_diff.dds texture which is 1024x1025 <-- note the "5" instead of a "4" in there.

DDS textures using DXT1, DXT1a or DXT5 must be either power of two, or divisible by 4 in each dimension. So 1028 might be ok, or 1020, but 1025 isn't.

NB: Just to confirm, it is the texture, I resized it locally, recompressed it etc and have tested a few times without the INVALID_ARGUMENT appearing again. @nozmajner just needs to shrink/crop the texture my 1 pixel vertically and make sure that the UV mapping is still correct.

@fluffyfreak
Copy link
Contributor

Also @laarmen might have a good point, we've got until the end of the month so getting more eyeballs on it and then fixing the obvious issues could be one approach.

Or wait until next month and merge right at the start to give maximum time.

So far I've not noticed any problems but I was only looking at the opengl issue thus far. I haven't been able to get the Police to attack me despite shooting at a ground station for a while though.

Will test some more :)

@fluffyfreak
Copy link
Contributor

Ok, I have 278 Unlawful weapon discharge and a $178 fine but no police have come after me yet.

@impaktor
Copy link
Member Author

Yes, so the threshold for launching police is currently 1000, and fine for shooting in Sol is 100, (on New Hope it's 89). I think I should lower the threshold to 300, i.e. a three strike policy for lawlessness = 0 system.

I also agree with letting the players test it, and find the bugs. Ideally, I think it would be good with a "pioneer stable" version, and a "latest" version, my thinking is that the "stable" version is best for new players, since it's a bit of a bummer to try pioneer for the first time and end up with bugs and crashes.

Also, please note: this PR does not include any save-bump commit, so up to the merger to fix that.

Do we know of any other save-bumping code that might pop-up in the near future?

Ok, I have 278 Unlaful weapon discharge and a $178 fine but no police have come after me yet.

I'll look into that, what system?

@fluffyfreak
Copy link
Contributor

Lanner as mod

Rolling contract is kind of what I'm on now too, I expect that it will last until March/April next year but that's only based on what we've discussed there's nothing in writing. Such is life!

Now then the bug: yes I see it even with the latest merged in (hoped for fixes) which is pretty bad.
I don't understand the Lua save system at all unfortunately but I can at least confirm it for you :/

@fluffyfreak
Copy link
Contributor

Is there a way to write to the output log from the Lua save methods so that you can see if they're being recorded/restored? Perhaps figure out where they are themselves called from and put some logging in there to figure out if they are being called, and if they are why they're not saving/restoring.

@impaktor
Copy link
Member Author

I'm not sure my description for reproducing this bug is 100% deterministic. Seems like sometimes it works, sometimes not. I'm printing out debug statements, so I can see the bug happening "real time". I.e. when saving manually the save system always calls the serializer of the lua-class, as it should, and it prints out "...SERIALIZED!" (I've pushed a debug commit). When autosave does a save, it prints out "SAVED ".

So just by doing:

  1. save manually
  2. exit game,

I can get two different printouts:

without bug triggered:

I get one print from CrimeRecord serializer for each save call (1 manual, 1 from autosave), and then AutoSave notifies me it's been called and saved to a file called _exit.

TradeShips: Removed 0 ships before serialization
...SERIALIZED!
TradeShips: Removed 0 ships before serialization
...SERIALIZED!
...SAVED!   _exit

with bug triggered:

First printout if from my manual save. Second is from AutoSave, but somewhere down the line it failed to call the serializer of the class:

TradeShips: Removed 0 ships before serialization
...SERIALIZED!
TradeShips: Removed 0 ships before serialization
...SAVED!   _exit

So I'm guessing somewhere between LuaGame.cpp and LuaSerializer.cpp something strange is happening. I cant poke this more tonight.

@laarmen
Copy link
Contributor

laarmen commented Oct 23, 2015 via email

@impaktor
Copy link
Member Author

That would be greatly appreciated @laarmen, because I'm at a loss here!

It might be interesting to also have debug printout for the Equipment serialization, since that might also have the bug, and if it doesn't then that's narrowed it down further. My CrimeRecord class is, as of my recent fix of the bug fluffy found, basically just an exact copy of your Equipment implementation.

@johnbartholomew
Copy link
Contributor

Best guess about the bug is that it's because both the AutoSave module and the Player module have registered handlers for the "onGameEnd" event. The Player module onGameEnd handler clears out the player's data -- if that runs before AutoSave onGameEnd then the _exit save will have no crime record.

Should be fairly easy to fix -- don't try to clean up any state in onGameEnd; instead, reset the state in onGameStart (and then inject anything needed from loaded_data). It's not a very nice system, but should work ok.

@impaktor
Copy link
Member Author

I believe you are as usual, spot on!

And as usual, I was way off. (How come I always look for bugs in the wrong place... until I find it? 😜 )

I've put a printout in OnGameEnd and indeed it is called before the autosave, which then has no CrimeRecord object to serialize. (with the printout I can't seem to get the bug not to happen, to really make sure they're called in the opposite order)

impaktor and others added 11 commits October 30, 2015 16:03
and a policeDispatched table added.
Differentiate current factions by using newly added police ships,
and change default from Kanara to:

- Default: sinonatrix_police
- Federation: kanara
- CIW: pumpkinseed_police
...Since when just echoing it to comms
it's not seen behind the Lobby window.
…and one

crime record for the authority to remember the past crimes. Police now also
display list of both (current) "Outstanding fines", and (past) "Crime Record"
In the process, use the functionality for lua classes from utils
@impaktor
Copy link
Member Author

Rebased (probably for the +40th time), and cleaned/squashed up previous fixes, (except for the last one, since that commit would have to be split in two and then each part merged into two different commits, and I don't feel much like doing that).

So, to the best of my knowledge, this PR is now done.

@impaktor impaktor merged commit b3be4d3 into pioneerspacesim:master Nov 2, 2015
impaktor added a commit that referenced this pull request Nov 2, 2015
@impaktor impaktor deleted the lawAndOrder_to_lua branch November 2, 2015 10:30
@impaktor impaktor mentioned this pull request Jan 20, 2020
7 tasks
@impaktor impaktor mentioned this pull request Feb 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants