From 3f4af78ccbe9d1c77e7b264f0c7417371aaf5741 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Sat, 24 Apr 2010 11:45:40 -0500 Subject: [PATCH] better abstraction of interception chance --- lib/Lacuna/DB/Body/Planet.pm | 45 ++++++++++++++++++++++++----- lib/Lacuna/DB/Building.pm | 6 ++-- lib/Lacuna/DB/Building/Shipyard.pm | 4 +-- lib/Lacuna/DB/Building/SpacePort.pm | 2 +- lib/Lacuna/DB/Spies.pm | 10 ++++--- 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/lib/Lacuna/DB/Body/Planet.pm b/lib/Lacuna/DB/Body/Planet.pm index 555bbada..56b1639c 100644 --- a/lib/Lacuna/DB/Body/Planet.pm +++ b/lib/Lacuna/DB/Body/Planet.pm @@ -177,8 +177,8 @@ has determine_espionage => ( $self->thieves(\@thieves); $self->saboteurs(\@saboteurs); $self->interceptors(\@interceptors); - $self->chance_of_theft(($steal > 90) ? 90 : $steal ); - $self->chance_of_sabotage(($sabotage > 90) ? 90 : $sabotage ); + $self->theft_score( $steal ); + $self->sabotage_score( $sabotage ); return 1; }, ); @@ -239,13 +239,13 @@ sub defeat_theft { my $spy = $self->thieves->[0]; my $interceptor = $self->interceptors->[0]; if ($event < 5) { - $self->chance_of_theft( $self->chance_of_theft - $spy->offense ); + $self->theft_score( $self->theft_score - $spy->offense ); $self->kill_a_spy($spy, $interceptor); delete $self->thieves->[0]; $self->add_news(70,'%s police caught and killed a thief on %s during the commission of the crime.', $self->empire->name, $self->name); } elsif ($event < 40) { - $self->chance_of_theft( $self->chance_of_theft - $spy->offense ); + $self->theft_score( $self->theft_score - $spy->offense ); $self->capture_a_spy($spy, $interceptor); delete $self->thieves->[0]; $self->add_news(40,'%s announced the incarceration of a thief on %s today.', $self->empire->name, $self->name); @@ -264,13 +264,13 @@ sub defeat_sabotage { my $spy = $self->saboteurs->[0]; my $interceptor = $self->interceptors->[0]; if ($event < 10) { - $self->chance_of_sabotage( $self->chance_of_sabotage - $spy->offense ); + $self->sabotage_score( $self->sabotage_score - $spy->offense ); $self->kill_a_spy($spy, $interceptor); delete $self->saboteurs->[0]; $self->add_news(70,'%s told us that a lone saboteur was killed on %s before he could carry out his plot.', $self->empire->name, $self->name); } elsif ($event < 50) { - $self->chance_of_sabotage( $self->chance_of_sabotage - $spy->offense ); + $self->sabotage_score( $self->sabotage_score - $spy->offense ); $self->capture_a_spy($spy, $interceptor); delete $self->saboteurs->[0]; $self->add_news(40,'A saboteur was apprehended on %s today by %s authorities.', $self->name, $self->empire->name); @@ -293,7 +293,19 @@ sub pick_a_spy_per_empire { return values %empires; } -has chance_of_theft => ( +sub chance_of_theft { + my $self = shift; + $self->determine_espionage; + my $chance = $self->theft_score - $self->interception_score; + return ($chance > 90) ? 90 : $chance; +} + +sub check_theft { + my $self = shift; + return ($self->chance_of_theft > randint(1,100)); +} + +has theft_score => ( is => 'rw', default => 0, ); @@ -303,12 +315,29 @@ has thieves => ( default => sub { [] }, ); +sub chance_of_sabotage { + my $self = shift; + $self->determine_espionage; + my $chance = $self->sabotage_score - $self->interception_score; + return ($chance > 90) ? 90 : $chance; +} + +sub check_sabotage { + my $self = shift; + return ($self->chance_of_sabotage > randint(1,100)); +} + has saboteurs => ( is => 'rw', default => sub { [] }, ); -has chance_of_sabotage => ( +has sabotage_score => ( + is => 'rw', + default => 0, +); + +has interception_score => ( is => 'rw', default => 0, ); diff --git a/lib/Lacuna/DB/Building.pm b/lib/Lacuna/DB/Building.pm index 764f1ea6..960b16ee 100644 --- a/lib/Lacuna/DB/Building.pm +++ b/lib/Lacuna/DB/Building.pm @@ -3,7 +3,6 @@ package Lacuna::DB::Building; use Moose; extends 'SimpleDB::Class::Item'; use Lacuna::Constants ':all'; -use Lacuna::Util qw(randint); use List::Util qw(shuffle); __PACKAGE__->set_domain_name('building'); @@ -597,7 +596,7 @@ sub start_upgrade { $body->clear_last_in_build_queue; # steal it - if ($body->chance_of_theft > randint(1,100)) { + if ($body->checkt_theft) { my @random = shuffle @{$body->thieves}; $random[0]->steal_a_building($self); } @@ -612,10 +611,9 @@ sub finish_upgrade { my ($self) = @_; $self->build_queue->delete; my $body = $self->body; - $body->determine_espionage; # blow it up - if ($self->level > 0 && $body->chance_of_sabotage > randint(1,100)) { + if ($self->level > 0 && $body->check_sabotage) { $self->build_queue_id(''); $self->put; $self->send_blow_up_a_building(); diff --git a/lib/Lacuna/DB/Building/Shipyard.pm b/lib/Lacuna/DB/Building/Shipyard.pm index b1f89eb7..a169b489 100644 --- a/lib/Lacuna/DB/Building/Shipyard.pm +++ b/lib/Lacuna/DB/Building/Shipyard.pm @@ -2,7 +2,7 @@ package Lacuna::DB::Building::Shipyard; use Moose; extends 'Lacuna::DB::Building'; -use Lacuna::Util qw(to_seconds format_date randint); +use Lacuna::Util qw(to_seconds format_date); use DateTime; __PACKAGE__->add_attributes( @@ -240,7 +240,7 @@ sub check_for_completed_ships { if (!$spaceport->is_full) { $body->determine_espionage; #deal with saboteurs - if ($body->chance_of_sabotage > randint(1,100)) { + if ($body->check_sabotage) { $self->send_blow_up_a_ship($completed_ship->{type}); my $spies = $body->pick_a_spy_per_empire($body->saboteurs); foreach my $spy (@{$spies}) { diff --git a/lib/Lacuna/DB/Building/SpacePort.pm b/lib/Lacuna/DB/Building/SpacePort.pm index 529d1014..5680ad39 100644 --- a/lib/Lacuna/DB/Building/SpacePort.pm +++ b/lib/Lacuna/DB/Building/SpacePort.pm @@ -54,7 +54,7 @@ sub send_spy_pod { $self->put; # steal it - if ($body->chance_of_theft > randint(1,100)) { + if ($body->check_theft) { my @random = shuffle @{$body->thieves}; $spy = pop @random; $body->thieves(\@random); diff --git a/lib/Lacuna/DB/Spies.pm b/lib/Lacuna/DB/Spies.pm index 0637f116..8ab71640 100644 --- a/lib/Lacuna/DB/Spies.pm +++ b/lib/Lacuna/DB/Spies.pm @@ -63,6 +63,7 @@ sub assign { sub steal_a_building { my ($self, $building) = @_; + my $body = $building->body; if ($building->level == 0) { $self->from_body->add_free_build($building->class, 1); } @@ -74,7 +75,7 @@ sub steal_a_building { filename => 'building_theft_report.txt', params => [$building->level + 1, $building->name, $self->name], ); - $building->body->chance_of_theft( $building->body->chance_of_theft - 5); + $body->interception_score ( $body->interception_score + 5); } sub steal_a_ship { @@ -108,7 +109,8 @@ sub steal_a_ship { params => [$type, $body->name], ); $body->add_news(60,'In a daring escape a thief absconded with a %s from %s today.', $type, $body->name); - $body->chance_of_theft( $body->chance_of_theft - $self->offense - 10); + $body->theft_score( $body->theft_score - $self->offense); + $body->interception_score ( $body->interception_score + 10); } sub sabotage_a_building { @@ -119,7 +121,7 @@ sub sabotage_a_building { filename => 'sabotage_report.txt', params => [$building->name, $body->name, $self->name], ); - $body->chance_of_sabotage( $body->chance_of_sabotage - 20); + $body->interception_score( $body->interception_score + 20); } sub sabotage_a_ship { @@ -131,7 +133,7 @@ sub sabotage_a_ship { filename => 'sabotage_report.txt', params => [$type, $body->name, $self->name], ); - $body->chance_of_sabotage( $body->chance_of_sabotage - 10); + $body->interception_score( $body->interception_score + 10); } -- 2.51.2