From 6e21913ffe8caae80a985ad9350276c50528573e Mon Sep 17 00:00:00 2001 From: Darin McBride Date: Fri, 20 Nov 2015 13:47:31 -0700 Subject: [PATCH] Saben will destroy planets with a bit more randomness. Fixes #399. --- lib/Lacuna/AI/Saben.pm | 28 +++++++++++++++++++--------- lib/Lacuna/DB/Result/Map/Star.pm | 14 ++++++++++++++ var/www/public/changes.txt | 1 + 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/Lacuna/AI/Saben.pm b/lib/Lacuna/AI/Saben.pm index 5878d2130..183c8e247 100644 --- a/lib/Lacuna/AI/Saben.pm +++ b/lib/Lacuna/AI/Saben.pm @@ -155,16 +155,28 @@ sub destroy_world { return; } say "Looking for world to destroy..."; - my $targets = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->search({ - zone => $colony->zone, + my $targets = Lacuna->db->resultset('Map::Body')->search({ + "me.zone" => $colony->zone, size => { between => [46, 75] }, empire_id => undef, + # where there isn't a bhgneut law. + "bhgneut_laws.id" => undef, }, - { rows => 20} + { + join => { star => 'bhgneut_laws' }, + } ); - my $blownup = 0; - while (my $target = $targets->next) { - next if $target->is_bhg_neutralized; + + # two queries to find a random element which is supposed to + # be faster than order by rand(). Since we're only getting back + # one number and one row, this should be fairly light on the + # database communication level as well. + my $numtargets = $targets->count(); + my $targetidx = Lacuna::Util::randint(0,$numtargets - 1); + + my $target = $targets->search({}, { offset => $targetidx, rows => 1, order_by => 'me.id' })->first; + + if ($target) { say "Found ".$target->name; my @to_demolish = @{$target->building_cache}; $target->delete_buildings(\@to_demolish); @@ -175,10 +187,8 @@ sub destroy_world { }); say "Turned into ".$target->class; $colony->add_news(100, 'We are Sābēn. We have destroyed '.$target->name.'. Leave now.'); - $blownup = 1; - last; } - if ($blownup == 0) { + else { say "Nothing to destroy."; } } diff --git a/lib/Lacuna/DB/Result/Map/Star.pm b/lib/Lacuna/DB/Result/Map/Star.pm index d47d8a47e..788af4b83 100644 --- a/lib/Lacuna/DB/Result/Map/Star.pm +++ b/lib/Lacuna/DB/Result/Map/Star.pm @@ -18,6 +18,20 @@ __PACKAGE__->has_many('bodies', 'Lacuna::DB::Result::Map::Body', 'star_id'); __PACKAGE__->has_many('probes', 'Lacuna::DB::Result::Probes', 'star_id'); __PACKAGE__->has_many('laws', 'Lacuna::DB::Result::Laws', 'star_id'); __PACKAGE__->belongs_to('station', 'Lacuna::DB::Result::Map::Body', 'station_id', { on_delete => 'set null' }); +__PACKAGE__->has_many('bhgneut_laws', 'Lacuna::DB::Result::Laws', sub { + my $args = shift; + return ( + { + "$args->{foreign_alias}.station_id" => { -ident => "$args->{self_alias}.station_id" }, + "$args->{foreign_alias}.type" => 'BHGNeutralized', + }, + $args->{self_rowobj} && { + "$args->foreign_alias}.station_id" => $args->{self_rowobj}->station_id, + "$args->{foreign_alias}.type" => 'BHGNeutralized', + } + ) +}); + sub sqlt_deploy_hook { my ($self, $sqlt_table) = @_; diff --git a/var/www/public/changes.txt b/var/www/public/changes.txt index b68ce6d0d..8867e6fa6 100644 --- a/var/www/public/changes.txt +++ b/var/www/public/changes.txt @@ -8,6 +8,7 @@ - Fix: AIs' stations can now be destroyed. - Fix: Make owner of star a bit more predictable in case of ties. - Fix: Gauntlet now works at uni+1 level, too. + - Fix: Saben will destroy planets with a bit more randomness. 3.0915: - Fix: Reduce the wild resource fluctuations during planet ticking.