Author Topic: random short desc using switch statement  (Read 6762 times)

Offline j_rathjens

  • Pottymouth
  • *
  • Posts: 8
    • View Profile
random short desc using switch statement
« on: November 15, 2007, 04:03:39 PM »
Here's the object, problem is it keeps making descs (at least as often as I check my inventory). Not sure if I need some sort of expression to save or store a value, but I'd thought maybe it would only be executed once on creation. Thanks.

Code: [Select]
#include <lib.h>
#include <damage_types.h>
#include <vendor_types.h>

inherit LIB_ITEM;



string adj;
string noun;


static void create() {
    item::create();
    SetKeyName("katana");
    SetId( ({ "katana", "sword" }) );
    SetAdjectives( ({0}) );
    SetShort((: GetName :));
    SetLong("A cheap and rather dull short sword.");
    SetMass(300);
    SetBaseCost("ryo",4);
    SetVendorType(VT_WEAPON);
    SetClass(30);
    SetDamageType(BLADE);
    SetWeaponType("blade");
}

void init(){
    ::init();
}

mixed GetName(){
    int rand1;
    int rand2;

    rand1 = random(10);
    rand2 = random(10);

    switch(rand1) {
case 0: adj = "Flowing"; break;
case 1: adj = "Running"; break;
case 2: adj = "Placid"; break;
case 3: adj = "Gushing"; break;
case 4: adj = "Spraying"; break;
case 5: adj = "Bursting"; break;
case 6: adj = "Roiling"; break;
case 7: adj = "Quenching"; break;
case 8: adj = "Serene"; break;
case 9: adj = "Cooling"; break;
case 10: adj = "Cleansing"; break;
    }

switch(rand2) {
case 0: noun = "Mist"; break;
case 1: noun = "Brook"; break;
case 2: noun = "River"; break;
case 3: noun = "Wave";  break;
case 4: noun = "Falls"; break;
case 5: noun = "Lake"; break;
case 6: noun = "Carp"; break;
case 7: noun = "Koi"; break;
case 8: noun = "Surf"; break;
case 9: noun = "Crest"; break;
case 10: noun = "Stream"; break;
}
return "a katana called " + adj + " " + noun;

}

Offline cratylus

  • Your favorite and best
  • Administrator
  • ***
  • Posts: 1020
  • Cratylus@Dead Souls <ds> np
    • View Profile
    • About Cratylus
Re: random short desc using switch statement
« Reply #1 on: November 15, 2007, 04:09:48 PM »
Code: [Select]
#include <lib.h>
#include <damage_types.h>
#include <vendor_types.h>

inherit LIB_ITEM;

string adj;
string noun;
string shortdesc;

string NewName();

static void create() {
    item::create();
    shortdesc = NewName();
    SetKeyName("katana");
    SetId( ({ "katana", "sword" }) );
    SetAdjectives( ({0}) );
    SetShort(shortdesc);
    SetLong("A cheap and rather dull short sword.");
    SetMass(300);
    SetBaseCost("ryo",4);
    SetVendorType(VT_WEAPON);
    SetClass(30);
    SetDamageType(BLADE);
    SetWeaponType("blade");
}

void init(){
    ::init();
}

string NewName(){
    int rand1;
    int rand2;

    rand1 = random(10);
    rand2 = random(10);

    switch(rand1) {
case 0: adj = "Flowing"; break;
case 1: adj = "Running"; break;
case 2: adj = "Placid"; break;
case 3: adj = "Gushing"; break;
case 4: adj = "Spraying"; break;
case 5: adj = "Bursting"; break;
case 6: adj = "Roiling"; break;
case 7: adj = "Quenching"; break;
case 8: adj = "Serene"; break;
case 9: adj = "Cooling"; break;
case 10: adj = "Cleansing"; break;
    }

switch(rand2) {
case 0: noun = "Mist"; break;
case 1: noun = "Brook"; break;
case 2: noun = "River"; break;
case 3: noun = "Wave";  break;
case 4: noun = "Falls"; break;
case 5: noun = "Lake"; break;
case 6: noun = "Carp"; break;
case 7: noun = "Koi"; break;
case 8: noun = "Surf"; break;
case 9: noun = "Crest"; break;
case 10: noun = "Stream"; break;
}
return "a katana called " + adj + " " + noun;

}

Offline Alkiera

  • Acquaintance
  • *
  • Posts: 8
    • View Profile
Re: random short desc using switch statement
« Reply #2 on: November 15, 2007, 04:42:10 PM »
j_,

The difference is the use of the (: :) operator.  That doesn't call just call the function and insert the value; it interprets the function, and then attaches the compiled function code to whatever variable SetShort() sets.  Whenever anything uses GetShort() to read that variable, instead of just getting a string, it's getting a reference to that function call, and it calls the function.

Not sure what name LPC uses, but some languages would call that a closure; it calls the function in question later, not when it evaluates the call.  They're weird, and so is the (: :) operator.  8)

Alkiera

Offline wodan

  • BFF
  • ***
  • Posts: 434
  • Drink and code, you know you want to!
    • View Profile
Re: random short desc using switch statement
« Reply #3 on: November 16, 2007, 04:53:54 AM »
why the switch{case} thing though? you could just put them all in an array and index that with the random number, or if you're using fluffos with the dwlib package, element_of(array).

Actually, I should probably move some stuff from dwlib to contrib, as some of those are not all that dw specific :)

Offline detah

  • BFF
  • ***
  • Posts: 190
  • Ruler of 2D
    • View Profile
Re: random short desc using switch statement
« Reply #4 on: November 16, 2007, 08:14:19 AM »
why the switch{case} thing though? you could just put them all in an array and index that with the random number, or if you're using fluffos with the dwlib package, element_of(array).

Actually, I should probably move some stuff from dwlib to contrib, as some of those are not all that dw specific :)

First I would like to remind you that, switch is superior to multiple if statements. I have done enough testing with those two to prove that to myself.  You didnt make that mistake. I think wodan makes a good point in your case here. I strongly suspect that the appropriate and most efficient code here is via string arrays. Your code will be more compact (much shorter), and I feel it would be just as fast (possibly faster) than a switch.

Offline Atomic

  • BFF
  • ***
  • Posts: 115
  • To infinity and beyond!
    • View Profile
Re: random short desc using switch statement
« Reply #5 on: November 16, 2007, 08:49:30 AM »
why the switch{case} thing though? you could just put them all in an array and index that with the random number, or if you're using fluffos with the dwlib package, element_of(array).

Actually, I should probably move some stuff from dwlib to contrib, as some of those are not all that dw specific :)

First I would like to remind you that, switch is superior to multiple if statements. I have done enough testing with those two to prove that to myself.  You didnt make that mistake. I think wodan makes a good point in your case here. I strongly suspect that the appropriate and most efficient code here is via string arrays. Your code will be more compact (much shorter), and I feel it would be just as fast (possibly faster) than a switch.

I'm following the discussion with great interest, but I'm not quite following your reply. First you say switch is superior to multiple if-statements, ok,
then you conclude with saying that if using ....(not sure what term/method you mean here)...would be just as fast (possibly faster) than a switch.
Which one do you mean? (multiple) IF's, Switch or Array?  ???
Always remember that the early bird gets the worm first, but the fi...*SNAP*...second mouse gets the cheese.

Offline Atomic

  • BFF
  • ***
  • Posts: 115
  • To infinity and beyond!
    • View Profile
Re: random short desc using switch statement
« Reply #6 on: November 16, 2007, 08:51:38 AM »
Great, I'm jumping to false conclusions again.  :D
I re-read twice and I read you now Detah. My apologies.

String array... could you perhaps give such an example compared to the code above? (Still slowly learning)
Always remember that the early bird gets the worm first, but the fi...*SNAP*...second mouse gets the cheese.

Offline detah

  • BFF
  • ***
  • Posts: 190
  • Ruler of 2D
    • View Profile
Re: random short desc using switch statement
« Reply #7 on: November 16, 2007, 09:17:44 AM »
int x;
string array adjective;

x=random(11);
adjective=({"Flowing","Running","Placid","Gushing","Spraying","Bursting","Roiling","Quenching","Serene","Cooling","Cleansing"});
adjective
  • ; //Now this will generate a random adjective from the array


Do the same for the noun. Then you can generate a single sentence for the random ShortDesc, using Cratylus' suggested structure above. Im pretty sure that selecting an element from an array is equal to or faster than a switch statement. Given that the above code is much more compact, Id go with the array approach.

Offline detah

  • BFF
  • ***
  • Posts: 190
  • Ruler of 2D
    • View Profile
Re: random short desc using switch statement
« Reply #8 on: November 16, 2007, 10:07:52 AM »
Code: [Select]
int x;
string array adjective;

x=random(11);
adjective=({"Flowing","Running","Placid","Gushing","Spraying","Bursting","Roiling","Quenching","Serene","Cooling","Cleansing"});
adjective[x]; //Now this will generate a random adjective from the array

UPDATE: Ive just added the code tags. Should display correctly now.

Offline petriomelony

  • Acquaintance
  • *
  • Posts: 34
    • View Profile
Re: random short desc using switch statement
« Reply #9 on: November 16, 2007, 01:25:00 PM »
wow, this is exactly the same sorta thing that i was looking for :) sweet deal

ps, personally i'd want a "bursting koi".

Offline j_rathjens

  • Pottymouth
  • *
  • Posts: 8
    • View Profile
Re: random short desc using switch statement
« Reply #10 on: November 16, 2007, 04:08:40 PM »
Thanks to Crat for his correction, and thanks very much to Alkiera. I'd gotten the distinct feeling (with no real justification for the idea) that it wasn't just producing names every cycle, and I kept wondering why the function would just be referenced over and over. I've got a lot to learn yet.

This was more of a prototypical object to see (from a player/inventory perspective) if the 'named sword' concept would fly aesthetically. Some combinations come out more relevant and profound-ish than others ('Bursting Koi', 'Spraying Carp' might be my favorite  :) ) Eventually this might be integrated into weapon objects themselves (when my LP catches up) to help with parsing woes and differentiate (katana especially) from one another, maybe tagging in random traits like hi (fullers) and moroha (double-edged) into the long desc. Complicated. :-\ For me. :(

Thanks also for the alternate method with the arrays, Detah, and everyone for their responses.

Offline Nulvect

  • BFF
  • ***
  • Posts: 127
    • View Profile
Re: random short desc using switch statement
« Reply #11 on: November 16, 2007, 08:37:12 PM »
A slightly more maintainable way:

Code: [Select]
string *adjectives = ({
  "flowing",
  "running",
  "placid",
  "gushing",
  "spraying",
  "bursting",
  "roiling",
  "quenching",
  "serene",
  "cooling",
  "cleansing",
});
string word = capitalize(adjectives[random(sizeof(adjectives))]);

This way you can easily add another line using ed if you need to do so, and you don't need to worry about the size of the array for picking a random word.

Offline Atomic

  • BFF
  • ***
  • Posts: 115
  • To infinity and beyond!
    • View Profile
Re: random short desc using switch statement
« Reply #12 on: November 20, 2007, 03:30:42 AM »
A slightly more maintainable way:

Code: [Select]
string *adjectives = ({
  "flowing",
  "running",
  "placid",
  "gushing",
  "spraying",
  "bursting",
  "roiling",
  "quenching",
  "serene",
  "cooling",
  "cleansing",
});
string word = capitalize(adjectives[random(sizeof(adjectives))]);

This way you can easily add another line using ed if you need to do so, and you don't need to worry about the size of the array for picking a random word.

Cleaner indeed and less maintenance-sensitive. Kudos!
Always remember that the early bird gets the worm first, but the fi...*SNAP*...second mouse gets the cheese.

Offline petriomelony

  • Acquaintance
  • *
  • Posts: 34
    • View Profile
Re: random short desc using switch statement
« Reply #13 on: November 20, 2007, 01:48:31 PM »
hmm.. i'm finding (with crat's code structure and my own item) that the random values don't persist... when i quit and log back in, the values are different, and when i reload the item, the values are different... is there a simple way to change this?

Offline detah

  • BFF
  • ***
  • Posts: 190
  • Ruler of 2D
    • View Profile
Re: random short desc using switch statement
« Reply #14 on: November 20, 2007, 01:53:45 PM »
make the variables global. ie. do not put them in the create nor init function. Then they will not get changed when you reload.