Random shop name

Wrote new code? Fixed a bug? Want to discuss technical stuff? Feel free to post it here.

Moderator: Moderators

Message
Author
Kissa2k
Human
Human
Posts: 46
Joined: 27 Apr 2008, 12:52
Noob?: No
Location: Russia

Random shop name

#1 Post by Kissa2k »

With this patch Openkore will open shop with a random name from the control/shop.txt
For example:
My Shop;;My cool shop;;My sex-shop

Jellopy 3
Andre Card 200,000 5

Code: Select all

Index: src/FileParsers.pm
===================================================================
--- src/FileParsers.pm   (revision 6342)
+++ src/FileParsers.pm   (working copy)
@@ -425,8 +425,8 @@
       $line =~ s/[\r\n\x{FEFF}]//g;
       next if $line =~ /^$/ || $line =~ /^#/;
 
-      if (!$shop->{title}) {
-         $shop->{title} = $line;
+      if (!$shop->{title_line}) {
+         $shop->{title_line} = $line;
          next;
       }
 
Index: src/Misc.pm
===================================================================
--- src/Misc.pm   (revision 6342)
+++ src/Misc.pm   (working copy)
@@ -4152,7 +4152,7 @@
       return;
    }
 
-   if (!$shop{title}) {
+   if (!$shop{title_line}) {
       error T("Your shop does not have a title.\n");
       return;
    }
@@ -4194,7 +4194,10 @@
 
 sub openShop {
    my @items = makeShop();
+   my @shopnames;
    return unless @items;
+   @shopnames = split(/;;/, $shop{title_line});
+   $shop{title} = $shopnames[int rand($#shopnames)];
    $shop{title} = ($config{shopTitleOversize}) ? $shop{title} : substr($shop{title},0,36);
    $messageSender->sendOpenShop($shop{title}, \@items);
    message TF("Shop opened (%s) with %d selling items.\n", $shop{title}, @items.""), "success"; 

Click
Human
Human
Posts: 39
Joined: 15 Apr 2008, 06:08
Noob?: Yes
Location: Russia, Velikiy Novgorod
Contact:

Re: Random shop name

#2 Post by Click »

svn 6344

iamanoob
Plain Yogurt
Plain Yogurt
Posts: 82
Joined: 04 Apr 2008, 09:49

Re: Random shop name

#3 Post by iamanoob »

um...

ive tested this and doesnt print this 'Four'

this is my shop.txt

Code: Select all

One;;Two;;Three;;Four

Jellopy		3
Andre Card	200,000		5
so there are four shop names
and i did closeshop and openshop repeatedly XD
and still this 'Four' shop name didnt popped

Code: Select all

	@shopnames = split(/;;/, $shop{title_line});
	message TF ("$#shopnames\n"),"info";
	$shop{title} = $shopnames[int rand($#shopnames)];
ive added 'message TF ("$#shopnames\n"),"info";' this line for me to know how many are randomized
and it only printed '3' which is supposed to be 4

so I've changed the code

Code: Select all

$shop{title} = $shopnames[int rand($#shopnames)];
to this

Code: Select all

$shop{title} = $shopnames[int rand($#shopnames + 1)];
am i right?
Image
DARKest Ninja

Kissa2k
Human
Human
Posts: 46
Joined: 27 Apr 2008, 12:52
Noob?: No
Location: Russia

Re: Random shop name

#4 Post by Kissa2k »

iamanoob, yes it is.
$#shopnames contain index of last element in array but arrays in perl starts from zero. ;)

sli
Perl Monk
Perl Monk
Posts: 810
Joined: 04 Apr 2008, 17:26
Noob?: No

Re: Random shop name

#5 Post by sli »

The technical term for that is offset. Since you skip nothing to get to the first item in an array, its offset is zero.

Image
cs : ee : realist

Click
Human
Human
Posts: 39
Joined: 15 Apr 2008, 06:08
Noob?: Yes
Location: Russia, Velikiy Novgorod
Contact:

Re: Random shop name

#6 Post by Click »

fix 6345

iamanoob
Plain Yogurt
Plain Yogurt
Posts: 82
Joined: 04 Apr 2008, 09:49

Re: Random shop name

#7 Post by iamanoob »

sli wrote:The technical term for that is offset. Since you skip nothing to get to the first item in an array, its offset is zero.
aww...
ok.. O.o
i dont know that offset thingy...
and i dont know perl
but thank you sli and Kissa2k
this patch is really useful
Image
DARKest Ninja

Post Reply