Skip to content

Commit

Permalink
Saben will destroy planets with a bit more randomness.
Browse files Browse the repository at this point in the history
Fixes #399.
  • Loading branch information
dmcbride committed Nov 20, 2015
1 parent 4d2879f commit 6e21913
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/Lacuna/AI/Saben.pm
Expand Up @@ -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);
Expand All @@ -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.";
}
}
Expand Down
14 changes: 14 additions & 0 deletions lib/Lacuna/DB/Result/Map/Star.pm
Expand Up @@ -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) = @_;
Expand Down
1 change: 1 addition & 0 deletions var/www/public/changes.txt
Expand Up @@ -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.
Expand Down

0 comments on commit 6e21913

Please sign in to comment.