Author Topic: Random NPC Heads Script *FIXED*  (Read 874 times)

Legacy_Snarkblat

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« on: June 24, 2011, 12:01:46 am »


               I've been working on a script that fires on a creatures on spawn event to give random body parts, skin color, hair etc.

So far I'm only testing the head part.  The script compiles fine but only gives the last random head in a sequence. For example: if I have 12 heads availiable, it always picks the 12th. If I have 6, it always picks the 6th. I feel like there's something extremely obvious I'm overlooking and I would appreciate any input you can give me.


//Random Head Script
void main()
{

      switch(Random(12))
      {
            case 0:  SetCreatureBodyPart(CREATURE_PART_HEAD, 1, OBJECT_SELF);
            case 1:  SetCreatureBodyPart(CREATURE_PART_HEAD, 2, OBJECT_SELF);
            case 2:  SetCreatureBodyPart(CREATURE_PART_HEAD, 3, OBJECT_SELF);
            case 3:  SetCreatureBodyPart(CREATURE_PART_HEAD, 4, OBJECT_SELF);
            case 4:  SetCreatureBodyPart(CREATURE_PART_HEAD, 5, OBJECT_SELF);
            case 5:  SetCreatureBodyPart(CREATURE_PART_HEAD, 6, OBJECT_SELF);
            case 6:  SetCreatureBodyPart(CREATURE_PART_HEAD, 7, OBJECT_SELF);
            case 7:  SetCreatureBodyPart(CREATURE_PART_HEAD, 8, OBJECT_SELF);
            case 8:  SetCreatureBodyPart(CREATURE_PART_HEAD, 9, OBJECT_SELF);
            case 9:  SetCreatureBodyPart(CREATURE_PART_HEAD, 10, OBJECT_SELF);
            case 10: SetCreatureBodyPart(CREATURE_PART_HEAD, 11, OBJECT_SELF);
            case 11: SetCreatureBodyPart(CREATURE_PART_HEAD, 12, OBJECT_SELF);
            case 12: SetCreatureBodyPart(CREATURE_PART_HEAD, 13, OBJECT_SELF);
      }
}
               
               

               


                     Modifié par Snarkblat, 26 juin 2011 - 05:53 .
                     
                  


            

Legacy_Snarkblat

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #1 on: June 24, 2011, 12:04:09 am »


               I should be clear I've also tested a script that separates it into race and gender and encountered the same problem.


void main()
{

     if ((GetRacialType(OBJECT_SELF)==RACIAL_TYPE_HUMAN))
     {

     if (GetGender(OBJECT_SELF)==GENDER_FEMALE)
     {
     switch(Random(12))
     {
           case 0:  SetCreatureBodyPart(CREATURE_PART_HEAD, 1, OBJECT_SELF);
           case 1:  SetCreatureBodyPart(CREATURE_PART_HEAD, 2, OBJECT_SELF);
           case 2:  SetCreatureBodyPart(CREATURE_PART_HEAD, 3, OBJECT_SELF);
           case 3:  SetCreatureBodyPart(CREATURE_PART_HEAD, 4, OBJECT_SELF);
           case 4:  SetCreatureBodyPart(CREATURE_PART_HEAD, 5, OBJECT_SELF);
           case 5:  SetCreatureBodyPart(CREATURE_PART_HEAD, 6, OBJECT_SELF);
           case 6:  SetCreatureBodyPart(CREATURE_PART_HEAD, 7, OBJECT_SELF);
           case 7:  SetCreatureBodyPart(CREATURE_PART_HEAD, 8, OBJECT_SELF);
           case 8:  SetCreatureBodyPart(CREATURE_PART_HEAD, 9, OBJECT_SELF);
           case 9:  SetCreatureBodyPart(CREATURE_PART_HEAD, 10, OBJECT_SELF);
           case 10: SetCreatureBodyPart(CREATURE_PART_HEAD, 11, OBJECT_SELF);
           case 11: SetCreatureBodyPart(CREATURE_PART_HEAD, 12, OBJECT_SELF);
           case 12: SetCreatureBodyPart(CREATURE_PART_HEAD, 13, OBJECT_SELF);

     }
     }
  else
     {
     switch(Random(6))
           {
           case 0:  SetCreatureBodyPart(CREATURE_PART_HEAD, 1, OBJECT_SELF);
           case 1:  SetCreatureBodyPart(CREATURE_PART_HEAD, 2, OBJECT_SELF);
           case 2:  SetCreatureBodyPart(CREATURE_PART_HEAD, 3, OBJECT_SELF);
           case 3:  SetCreatureBodyPart(CREATURE_PART_HEAD, 4, OBJECT_SELF);
           case 4:  SetCreatureBodyPart(CREATURE_PART_HEAD, 5, OBJECT_SELF);
           case 5:  SetCreatureBodyPart(CREATURE_PART_HEAD, 6, OBJECT_SELF);
           case 6:  SetCreatureBodyPart(CREATURE_PART_HEAD, 7, OBJECT_SELF);

     }

           }
           }
else if ((GetRacialType(OBJECT_SELF)==RACIAL_TYPE_ELF))
  {
   if (GetGender(OBJECT_SELF)==GENDER_FEMALE)
     {
     switch(Random(5))
     {
           case 0:  SetCreatureBodyPart(CREATURE_PART_HEAD, 1, OBJECT_SELF);
           case 1:  SetCreatureBodyPart(CREATURE_PART_HEAD, 2, OBJECT_SELF);
           case 2:  SetCreatureBodyPart(CREATURE_PART_HEAD, 3, OBJECT_SELF);
           case 3:  SetCreatureBodyPart(CREATURE_PART_HEAD, 4, OBJECT_SELF);
           case 4:  SetCreatureBodyPart(CREATURE_PART_HEAD, 5, OBJECT_SELF);
           case 5:  SetCreatureBodyPart(CREATURE_PART_HEAD, 6, OBJECT_SELF);
     }
     }
  else
     {
     switch(Random(5))
           {
           case 0:  SetCreatureBodyPart(CREATURE_PART_HEAD, 1, OBJECT_SELF);
           case 1:  SetCreatureBodyPart(CREATURE_PART_HEAD, 2, OBJECT_SELF);
           case 2:  SetCreatureBodyPart(CREATURE_PART_HEAD, 3, OBJECT_SELF);
           case 3:  SetCreatureBodyPart(CREATURE_PART_HEAD, 4, OBJECT_SELF);
           case 4:  SetCreatureBodyPart(CREATURE_PART_HEAD, 5, OBJECT_SELF);
           case 5:  SetCreatureBodyPart(CREATURE_PART_HEAD, 6, OBJECT_SELF);

     }

           }
           }

}
               
               

               
            

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #2 on: June 24, 2011, 12:06:35 am »


               You must break your cases in switches or they wont work properly, try this:
case 0:  SetCreatureBodyPart(CREATURE_PART_HEAD, 1, OBJECT_SELF); break;
( add to all cases )

Also if you have more then 1 line in a case (or two, if you count break), you need to put {} for the lines like this:

switch ()
{
  case 1:
  {}
  break;
  case 2:
........
}

I hate switches and almost never use them. <><>

               


                     Modifié par Xardex, 23 juin 2011 - 11:12 .
                     
                  


            

Legacy_Calvinthesneak

  • Hero Member
  • *****
  • Posts: 1159
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #3 on: June 24, 2011, 12:08:57 am »


               How does the random function work, I know for some things like C++ random needs to be multiplied by 10 to make it a full integer.. I spose I should go look up the Lexicon.

http://www.nwnlexico...ion.random.html
               
               

               
            

Legacy_Snarkblat

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #4 on: June 24, 2011, 12:10:38 am »


               Xardex: Oh my god, thank you! I can't believe I overlooked that. I did as you said and it worked beautifully. Now my city is populated with random NPCs running around. I can't wait to implement this code for other things as well, hair color, skin.

Thanks again <3>

               


                     Modifié par Snarkblat, 23 juin 2011 - 11:12 .
                     
                  


            

Legacy_kalbaern

  • Hero Member
  • *****
  • Posts: 1531
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #5 on: June 24, 2011, 12:12:16 am »


               Yup, do as suggested above and ... add one to all of your random numbers. 0 thru 5 is not random 5, its random 6. Random 5 is 0 thru 4.
               
               

               
            

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #6 on: June 24, 2011, 12:15:11 am »


               Well, the page you linked explains it better then I could...

Generally if I want a random integer I do " Random(x)+1 "
               
               

               


                     Modifié par Xardex, 23 juin 2011 - 11:16 .
                     
                  


            

Legacy_Snarkblat

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #7 on: June 24, 2011, 12:18:45 am »


               

Xardex wrote...

Well, the page you linked explains it better then I could...

Generally if I want a random integer I do " Random(x)+1 "


Could you explain that a little further? You say you don't use switches. Is there a simpler way to do my above script without switches? Because when it comes to randomizing colors... there's 175 of them. I don't want to have to write 175 lines of code.

Edit: I read the link but I'm afraid it's still a little bit lost on me.
               
               

               


                     Modifié par Snarkblat, 23 juin 2011 - 11:20 .
                     
                  


            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #8 on: June 24, 2011, 12:21:46 am »


               SetCreatureBodyPart(CREATURE_PART_HEAD, Random(5)+1, OBJECT_SELF);
               
               

               
            

Legacy_Snarkblat

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #9 on: June 24, 2011, 12:27:44 am »


               

GhostOfGod wrote...

SetCreatureBodyPart(CREATURE_PART_HEAD, Random(5)+1, OBJECT_SELF);





Brilliant... I just tested it out and every NPC spawned with a beautiful and rather "unique" appearance. '<img'> 

Here's a screenshot of in-game. oi51.tinypic.com/fz0n0l.jpg

Cudos to you, sir. You've just saved me a lot of grief. 
               
               

               
            

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #10 on: June 24, 2011, 12:36:49 am »


               Switches are a neat way of doing things, and to my understanding good performance vice... Its a personal preference, I only use them if I have to. Obviously the piece from Ghost is even better in this case, but if you have more then two variables to randomize depending on a third one, like randomizing the 3 weapon parts depending on base item, you have to use switches.

If you plan on randomizing the clothing aswell, I'll be very intrested in how you managed if you didn't either use switches or create a boatload of if statements.
               
               

               


                     Modifié par Xardex, 23 juin 2011 - 11:41 .
                     
                  


            

Legacy_kalbaern

  • Hero Member
  • *****
  • Posts: 1531
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #11 on: June 24, 2011, 12:40:41 am »


               

int iHair;
        switch(Random(6))
            {
            case 0:  iHair = 003; break;
            case 1:  iHair = 007; break;
            case 2:  iHair = 015; break;
            case 3:  iHair = 009; break;
            case 4:  iHair = 167; break;
            case 5:  iHair = 124; break;
            }
int nHairColor = iHair;
SetCreatureBodyPart(CREATURE_PART_HEAD, Random(5)+1, OBJECT_SELF);
SetColor(OBJECT_SELF,COLOR_CHANNEL_HAIR,nHairColor);

That should give you 6 different hair colorings.
               
               

               
            

Legacy_Snarkblat

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #12 on: June 24, 2011, 01:17:12 am »


               I've compiled the working script and will post here for anyone else who wants to use it. Thanks to all who helped out. ^-^

I'm also using a Commoner Walker system which automatically assigns random clothing, but I can't for the life of me remember what it's called.

//Random NPC Script
void main()
{

   SetCreatureBodyPart(CREATURE_PART_HEAD, Random(20)+1, OBJECT_SELF);
   SetCreatureBodyPart(CREATURE_PART_RIGHT_FOREARM, Random(2)+1, OBJECT_SELF);
   SetCreatureBodyPart(CREATURE_PART_RIGHT_FOREARM, Random(2)+1, OBJECT_SELF);
   SetCreatureBodyPart(CREATURE_PART_RIGHT_BICEP, Random(2)+1, OBJECT_SELF);
   SetCreatureBodyPart(CREATURE_PART_LEFT_BICEP, Random(2)+1, OBJECT_SELF);
   SetColor(OBJECT_SELF, COLOR_CHANNEL_HAIR, Random(175)+1);
   SetColor(OBJECT_SELF, COLOR_CHANNEL_SKIN, Random(175)+1);
   SetColor(OBJECT_SELF, COLOR_CHANNEL_TATTOO_1, Random(175)+1);
   SetColor(OBJECT_SELF, COLOR_CHANNEL_TATTOO_2, Random(175)+1);




}
               
               

               
            

Legacy_Snarkblat

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #13 on: June 24, 2011, 01:33:48 am »


               Hmm apparently the Random function also works for random portrait selection as well, but there's no assignment according to race or gender. Has anyone found a solution to this?

SetPortraitId(OBJECT_SELF, Random(60)+1);
               
               

               
            

Legacy_DM_Vecna

  • Hero Member
  • *****
  • Posts: 501
  • Karma: +0/-0
Random NPC Heads Script *FIXED*
« Reply #14 on: June 24, 2011, 03:34:39 am »


               You can have lots of fun with this. In my PW I have all kinds of randomness running including a demographics for the region that they spawned in. This all runs from one male and one female NPC