pioneerspacesim/pioneer — pull request #3254 by impaktor (2014-11-21 17:32:15+00:00)
Title: WIP Law and order to lua

# WIP: 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).

Anyway. I'm putting it here so that you see what is being worked on, and if anyone wants to give feedback on the code. I will push / rebase commits as I make progress.

It's WIP mainly due to number 10 in "progress" list below: it has some instability when a ship is destroyed it can sometimes happen that `onShipDestroyed` in `modules/CrimeTracking` triggers a crash when calling `ship:IsPlayer()` or `attacker:IsPlayer()` depending on the ship destoryed. I don't know if it is my code or if I'm triggering #887.

## Changed/New features

Some of the changes in how police will behave now:

1. A player with high fines will not be given launch clearance.
2. Police number, and how quickly they react to your crimes (police launch probability) depends on system lawlessness.
3. However, police will not launch until player has amassed high enough fines. I.e. threshold isn't `fine = 0` anymore, but `maxFineTolerated=1e4`
4. Lowered fine on piracy to `1e5`, and weapons discharge to `1e2`, allowing the latter to be below the `maxFineTolerated` level.
5. Dumping goods with negative price (close to station) is an offense.
6. Crime record now lists _how many_ of each crime type the player has.
7. A police ship destroyed at a station remains so until player hyperjumps back (same as before I believe).
8. Police ships can be different for different factions.

## Design / Code

I suspect that there is probably some "code ugliness" and better ways to do things then the way I've done it. Suggestions (or even better: commits!) are welcome.

I've introduced a `data/libs/Constants.lua`. It works, but might be "ugly" and benefit from some more object oriented encapsulated approach? It is used in `notifyOfCrime` to get the base fine level, and in `Police.lua` to translate the crime string.

Another thing I have been twisting my brain over:

- `notifyOfCrime` (previously in `Polit.cpp`) is now done in a `modules/CrimeTracking`. I put it there to be symmetric with our `modules/StatsTracking`. This is where there are triggers/events (`onShip`) for killing, firing, jettison, etc. I'm not sure if there is another more "natural" place to put it. Maybe it should not be a module but a lib? It does not have its own `lang/modules-crimetracking` folder, as is currently uses `ui-core`.

- `doLawAndOrder` (previously in `SpaceStation.cpp`) is now also done in a `modules/CrimeTracking/`. In the old (current master) code, each space station upholds the law (every frame) by calling the function. 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

[x] 1. Remove all crime handling from c++
[x] 2. Implement crime handling in Lua (kind of done), closes #1055
[x] 3. Crime record shows number of violations of each type
[x] 4. Police ship set by faction definition, fixes #1237
[x] 5. Deny launch clearance if player has unpaid fines, fixes #62
[x] 6. Dumping cargo with negative price (close to station) is a crime, fixes #873
[x] 7. Police is launched first after some threshold of illegal actions is passed, closes #1731
[ ] 8. The way I implemented crime constants in Lua (e.q. MURDER, ...) should be more OO?
[ ] 9. Better methods for getting at the player's crimes in the `Player.record` table?
[ ] 10. Fix frequent crashes when a police ship is dead
[ ] 11. Implement a `Player:ClearCrimeFine`, and clean up comments in `lib/Player.lua`
[ ] 12. Test-test-test: Save/Load when chased by police and killed police and test again

## For another separate pull request:
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: #2867
2. Fix legal footer, #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?