]> ruin.nu Git - NDIRC.git/blob - PA.pm
checkPlanet and checkGal fixed for new command style
[NDIRC.git] / PA.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19 package NDIRC::PA;
20 use strict;
21 use warnings;
22 use ND::DB;
23 use ND::Include;
24 use NDIRC::Access;
25 use NDIRC::Misc;
26 use POSIX;
27 require Exporter;
28
29 our @ISA = qw/Exporter/;
30
31 our @EXPORT = qw/checkPlanet checkGal shipEff shipStop parseValue prettyValue calcXp/;
32
33 sub checkPlanet {
34         my ($msg) = @_;
35
36         my ($x,$y,$z,$nick);
37         if ($msg =~ /(\d+)\D+(\d+)\D+(\d+)/){
38                 $x = $1;
39                 $y = $2;
40                 $z = $3;
41         }elsif (officer() && defined $msg){
42                 $nick = $msg;
43         }else{
44                 $ND::server->command("notice $ND::nick usage .p X:Y:Z".(officer() ? ' or .p nick' : ''));
45                 return;
46         }
47         my $f = $ND::DBH->prepare(q{SELECT coords(x,y,z),ruler,planet,race,score,size,value,scorerank,sizerank,
48                 valuerank, xp, xprank, alliance, relationship, nick, planet_status, hit_us, channel
49                 FROM current_planet_stats WHERE (x = $1 AND y = $2 and z = $3) OR nick ILIKE $4 LIMIT 1
50         });
51         $f->execute($x,$y,$z,$nick);
52         if (my $planet = $f->fetchrow_hashref()){
53                 for (keys %{$planet}){
54                         $planet->{$_} = valuecolor(1,$planet->{$_});
55                 }
56                 my $ally = "";
57                 if (officer() || dc()){
58                         $ally = "Alliance=$planet->{alliance} ($planet->{relationship}), Nick=$planet->{nick} ($planet->{planet_status}), Channel: $planet->{channel}, Hostile Count: $planet->{hit_us},";
59                 }
60                 $ND::server->command("notice $ND::nick $planet->{coords} $planet->{ruler} OF $planet->{planet},$ally Race=$planet->{race}, Score=$planet->{score} ($planet->{scorerank}), Size=$planet->{size} ($planet->{sizerank}), Value=$planet->{value} ($planet->{valuerank}), XP=$planet->{xp} ($planet->{xprank})");
61         }else{
62                 $ND::server->command("notice $ND::nick Couldn't find planet: $msg");
63         }
64 }
65 sub checkGal {
66         my ($msg) = @_;
67
68         my ($x,$y,);
69         if ($msg =~ /(\d+)\D+(\d+)/){
70                 $x = $1;
71                 $y = $2;
72         }else{
73                 $ND::server->command("notice $ND::nick syntax: .g X:Y");
74         }
75         my $f = $ND::DBH->prepare("SELECT score,size,value FROM galaxies WHERE x = ? AND y = ? and tick = (SELECT max(tick) from galaxies)");
76         $f->execute($x,$y);
77         while (my @row = $f->fetchrow()){
78                 @row = map (valuecolor(1),@row);
79                 $ND::server->command("notice $ND::nick $x:$y  Score=$row[0], Size=$row[1], Value=$row[2]");
80         }
81 }
82
83 sub shipEff {
84         my ($msg,$value) = @_;
85         my ($amount,$ship);
86         if($msg =~ /^(-?\d+(?:\.\d+)?[hkMG]?) (\w+)$/){
87                 if (defined $value){
88                         $value = parseValue($1);
89                         $value *= -1.5 if $value < 0;
90                         my $feud = '';
91                 }else{
92                         $amount = parseValue($1);
93                 }
94                 $ship = "\%$2\%";
95         }else{
96                 my $command = (defined $value ? "veff" : "eff");
97                 $ND::server->command("notice $ND::nick syntax: .$command amount ship");
98                 return;
99         }
100         my $feud = '';
101
102         my @ship = $ND::DBH->selectrow_array(q{
103 SELECT name,target,"type",damage,metal+crystal+eonium,init,"class",guns,race
104 FROM ship_stats WHERE name ILIKE ?
105                 }, undef, $ship);
106         if (@ship){
107                 my $type = "kill";
108                 $type = "stun" if $ship[2] eq 'Emp';
109                 $type = "steal" if ($ship[2] eq 'Steal') or ($ship[2] eq 'Pod');
110
111                 if (defined $value){
112                         $amount = int(($value*100/$ship[4]));
113                         $feud = '(FEUD: '.prettyValue(int($amount/0.80)).') ';
114                 }
115                 $value = prettyValue(($amount*$ship[4]/100));
116                 my $text = prettyValue($amount)." $feud $ship[0] ($ship[5]:$value) will $type:";
117                 my $st = $ND::DBH->prepare(q{
118                         SELECT name,"class","type",armor,metal+crystal+eonium,init,target,eres,race
119                         FROM ship_stats WHERE "class" = ?
120                         });
121                 $st->execute($ship[1]);
122                 while (my @target = $st->fetchrow()){
123                         my $dead = $ship[2] eq 'Emp' ? int($amount*$ship[7]*(100-$target[7])/100) : int($amount*$ship[3]/$target[3]);
124                         $value = prettyValue($dead*$target[4]/100);
125                         if (($target[6] eq $ship[6]) and ($target[5] <= $ship[5])){
126                                 $target[5] = "${ND::C}04$target[5]$ND::C";
127                         }elsif(($target[6] eq $ship[6]) and ($target[5] > $ship[5])){
128                                 $target[5] = "${ND::C}12$target[5]$ND::C";
129                         }
130                         $target[0] = "${ND::C}04$target[0]$ND::C" if $target[2] eq 'Norm' || $target[2] eq 'Cloak';
131                         $target[0] = "${ND::C}12$target[0]$ND::C" if $target[2] eq 'Emp';
132                         $target[0] = "${ND::C}13$target[0]$ND::C" if $target[2] eq 'Steal';
133                         $text .= " $ND::B$dead$ND::B $target[0] ($target[5]:$value),";
134                 }
135                 chop $text;
136                 $ND::server->command("notice $ND::nick $text");
137         }
138         #print $text;
139 }
140
141 sub shipStop {
142         my ($msg,$value) = @_;
143         my ($amount,$ship);
144         if($msg =~ /^(-?\d+(?:\.\d+)?[hkMG]?) (\w+)$/){
145                 if (defined $value){
146                         $value = parseValue($1);
147                         $value *= -1.5 if $value < 0;
148                         my $feud = '';
149                 }else{
150                         $amount = parseValue($1);
151                 }
152                 $ship = "\%$2\%";
153         }else{
154                 my $command = (defined $value ? "vstop" : "stop");
155                 $ND::server->command("notice $ND::nick syntax: .$command amount ship");
156                 return;
157         }
158         my $feud = '';
159
160         my @ship = $ND::DBH->selectrow_array(q{
161 SELECT name,target,"type",armor,metal+crystal+eonium,init,"class",eres,race
162 FROM ship_stats WHERE name ILIKE ?
163                 }, undef, $ship);
164         if (@ship){
165                 $ship[0] = "${ND::C}04$ship[0]$ND::C" if $ship[2] eq 'Norm';
166                 $ship[0] = "${ND::C}12$ship[0]$ND::C" if $ship[2] eq 'Emp';
167                 $ship[0] = "${ND::C}13$ship[0]$ND::C" if $ship[2] eq 'Steal';
168
169                 if (defined $value){
170                         $amount = int(($value*100/$ship[4]));
171                         $feud = '(FEUD: '.prettyValue(int($amount/0.80)).') ';
172                 }
173                 $value = prettyValue(($amount*$ship[4]/100));
174                 my $text = "To stop $amount $feud $ship[0] ($ship[5]:$value) you need:";
175                 my $st = $ND::DBH->prepare(q{
176                         SELECT name,"class","type",damage,metal+crystal+eonium,init,target,guns,race
177                         FROM ship_stats WHERE "target" = ?
178                         });
179                 $st->execute($ship[6]);
180                 while (my @stopper = $st->fetchrow()){
181                         my $needed = $stopper[2] eq 'Emp' ? ceil($amount*100/(100-$ship[7])/$stopper[7]) : ceil($amount*$ship[3]/$stopper[3]);
182                         $value = prettyValue($needed*$stopper[4]/100);
183                         if (($stopper[1] eq $ship[1]) and ($ship[5] <= $stopper[5])){
184                                 $stopper[5] = "${ND::C}04$stopper[5]$ND::C";
185                         }elsif(($stopper[1] eq $ship[1]) and ($ship[5] > $stopper[5])){
186                                 $stopper[5] = "${ND::C}12$stopper[5]$ND::C";
187                         }
188                         $stopper[0] = "${ND::C}04$stopper[0]$ND::C" if $stopper[2] eq 'Norm' || $stopper[2] eq 'Cloak';
189                         $stopper[0] = "${ND::C}12$stopper[0]$ND::C" if $stopper[2] eq 'Emp';
190                         $stopper[0] = "${ND::C}13$stopper[0]$ND::C" if $stopper[2] eq 'Steal';
191                         $text .= " $ND::B$needed$ND::B $stopper[0] ($stopper[5]:$value),";
192                 }
193                 chop $text;
194                 $ND::server->command("notice $ND::nick $text");
195         }
196         #print $text;
197 }
198
199 sub calcXp {
200         my ($x,$y,$z,$roids,$cap) = @_;
201
202         my ($avalue,$ascore) = $ND::DBH->selectrow_array(q{
203                 SELECT value,score FROM current_planet_stats WHERE 
204                         id = (SELECT planet FROM users WHERE hostmask ILIKE ?);
205                 }, undef, $ND::address);
206         my ($tvalue,$tscore,$tsize) = $ND::DBH->selectrow_array(q{
207                 SELECT value,score,size FROM current_planet_stats WHERE 
208                 x = ? AND y = ? and z = ?;
209                 }, undef, $x,$y,$z);
210         $cap = 0.25 unless $cap;
211         unless($roids){
212                 $roids = int($tsize*$cap);
213         }elsif ($roids < 10){
214                 $tsize = ceil($tsize*.75**($roids-1));
215                 $roids = int($cap*$tsize);
216         }
217         $tsize -= $roids;
218         unless (defined $avalue && defined $ascore){
219                 $ND::server->command("notice $ND::target You don't have a planet specified");
220                 return;
221         }
222         unless (defined $tvalue && defined $tscore){
223                 $ND::server->command("notice $ND::target Doesn't seem to be a planet at $x:$y:$z");
224                 return;
225         }
226         my $xp = pa_xp($roids,$ascore,$avalue,$tscore,$tvalue);
227         my $score = 60 * $xp;
228         my $value = $roids*200;
229         my $totscore = prettyValue($score + $value);
230         $ND::server->command("notice $ND::target You will gain $ND::B$xp$ND::B XP, $ND::B$score$ND::B score, if you steal $roids roids ($ND::B$value$ND::B value), from $ND::B$x:$y:$z$ND::B, who will have $ND::B$tsize$ND::B roids left, total score gain will be: $ND::B$totscore$ND::B in total,");
231 }
232
233 sub findCovOpper {
234         my ($stolen) = @_;
235
236         my $tick = $ND::tick;
237         my $agents;
238
239         if ($stolen =~ /(\d+) (\d+) (\d+)/){
240                 $tick = $1;
241                 $agents = $2;
242                 $stolen = $3;
243         }elsif ($stolen =~ /(\d+) (\d+)/){
244                 $tick = $1;
245                 $stolen = $3;
246         }
247
248         my ($value,$score) = $ND::DBH->selectrow_array(q{
249                 SELECT value,score FROM planet_stats WHERE 
250                         id = (SELECT planet FROM users WHERE hostmask ILIKE ?) AND tick = ?;
251                 }, undef, $ND::address,$tick);
252         my ($coords) = $ND::DBH->selectrow_array(q{
253                 SELECT coords(p.x,p.y,p.z) FROM current_planet_stats p JOIN planet_stats ps using (id) WHERE 
254                 ps.tick = ? AND (2000*?*?/ps.value)::int = $stolen ;
255                 }, undef, $tick,$agents,$value,$stolen);
256         $ND::server->command("notice $ND::target The planet that cov opped you is: $coords");
257 }
258
259 1;