Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: A small Deity AI class system

by holyghost (Beadle)
on Nov 06, 2017 at 08:09 UTC ( [id://1202801]=note: print w/replies, xml ) Need Help??


in reply to Re: A small Deity AI class system
in thread A small Deity AI class system

I know about the rand() function, you get 10 cases in rollD10, without having to do a + 1 (it also returns faster). Zeroes could lead out to a non-hit e.g. on a Enemy Non Player Character. The meaning of the RNG class is to use it within e.g. the empower method which can be made through e.g. the water god Umberlee with swim and so on (derived classes from the Deity AI class.) Then the split method using ^### should split up the files for you in a small program that's left as an exercise. HTH

Replies are listed 'Best First'.
Re^3: A small Deity AI class system
by Your Mother (Archbishop) on Nov 06, 2017 at 15:09 UTC

    I really encourage you to Benchmark instead of assuming. You will get surprises somewhat often. And assuming a simple arithmetic op is a performance killer seems silly. It took 10 million iterations to even be able to get this to benchmark without complaining it wasn't enough to be sure; and while a fifteenth of a millionth of a second is indeed three times faster than a fifth of a millionth of a second… does it actually matter on any level for the application in question?

    moo@cow[483]~>perl pm-dice use strictures; use Benchmark "cmpthese"; sub d10 { my $self = shift; return rand(10); } sub d10_2 { rand(10) } sub d10_3 { 1 + int( rand(10) ) } sub d10_4 { 1 + rand(10) } cmpthese( 10_000_000, { orig => \&d10, orig_simple => \&d10_2, int_plus_one => \&d10_3, plus_one => \&d10_4, }); __END__ Rate orig int_plus_one plus_one orig_si +mple orig 4048583/s -- -19% -38% + -74% int_plus_one 5000000/s 24% -- -24% + -68% plus_one 6578947/s 62% 32% -- + -59% orig_simple 15873016/s 292% 217% 141% + --
Re^3: A small Deity AI class system
by holyghost (Beadle) on Nov 07, 2017 at 06:47 UTC
    I was wrong in saying that the RNG was faster with 0, this is not assembler shifted in perl. The system was made on FreeBSD 11.0 and Debian GNU/Linux. Now I've tried to make some main characters by rewriting the Deity class :
    package HollyGameAI::MutualExclusiveDeityAI; our @ISA = "MutualExclusiveAI"; sub MutualExclusiveDeityAI { my $class = shift; my $self = $class->SUPER::MutualExclusiveAI(@_); bless $self, ref($class) || $class; }
    Here are some examples for use as Deities for the main character _NOTE_ however that I do not know how to override the Factory with inherited methods inside the MutualExclusiveAI class, I cannot repost however with that as a question (the clue of the post however) :
    package HollyGameAI::MutualExclusiveAI; use lib "../HollyGameAI"; use Factory; sub MutualExclusiveAI { my ($class) = shift; my $self = { aiclass => HollyGameAI::Factory->Factory(@_) }; bless $self, ref($class) || $class; }
    Here are Moradin and Umberlee classes based on the system :
    package HollyGameAI::Umberlee; our @ISA = "MutualExclusiveDeityAI"; sub Umberlee { my $class = shift; my $self = $class->SUPER::MutualExclusiveDeityAI(qw(swim empow +er)); bless $self, ref($class) || $class; }
    package HollyGameAI::Moradin; our @ISA = "MutualExclusiveDeityAI"; sub Moradin { my $class = shift; my $self = $class->SUPER::MutualExclusiveDeityAI(qw(empower)); bless $self, ref($class) || $class; }
    Here is a better RNG, the meaning of the superfluous sets is to use the roll previous method without shifting in :
    package HollyGameAI::RNG; ### Random Number God, dice class sub RNG { my $class = shift; my $self = { dx => 0 }; return bless $self, ref($class) || $class; } sub set { my ($self, $dxx) = @_; $self->{dx} = $dxx; } sub rollDX { my $self = shift; return rand($self->{dx}); } sub rollD1 { my $self = shift; return rand(1); } sub rollD3 { my $self = shift; return rand(3); } sub rollD6 { my $self = shift; return rand(6); } sub rollD10 { my $self = shift; return rand(10); } sub rollD20 { my $self = shift; return rand(20); } sub rollPreviousDX { my $self = shift; return rand($self->{dx}); } sub roll { my ($self, $dxx) = @_; $self->set($dxx); given ($self->{dx}) { when ($_ = 0) { return 0; } when ($_ == 1) { return rollD1; } when ($_ == 3) { return rollD3; } when ($_ == 6) { return rollD6; } when ($_ == 10) { return rollD10; } when ($_ == 20) { return rollD20; } } return 0; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202801]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found