Randomized character location based on odds?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
twistkill
Newbie
Posts: 14
Joined: Sat Feb 24, 2018 4:02 am
Contact:

Randomized character location based on odds?

#1 Post by twistkill »

Hey guys, new to this forum, new to ren'py, sorta! I am making a dating sim and one thing that's holding me back is character location.

There are 4 possible locations for Kate- home, office, mall, park.
There are 3 times of the day - morning, afternoon, night.
I want to build a sort-of random schedule based on odds for the player to find Kate at different locations.

For example, the chances of finding Kate at home in the morning is 50%, else 50% chance she is at the office. She cannot be at the mall or park in the morning.
The chances of meeting Kate at office in the afternoon is very high, about 80% else there is 10% chance you will meet her in the park or 10% she is in the mall.
At night, there is 60% chance you will find her at home, 10% chance she is at the mall and 30% chance she could be at the park.

Let know if one of you code ninjas can help me out with a sample code or an example. Any help will be much appreciated.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Randomized character location based on odds?

#2 Post by Ocelot »

Something like that:

Code: Select all

init python:
  # Takes a list of (value, weight) pairs
 # and returns one value
  def weighted_random(pairs):
    total_weight = sum(n for _, n in pairs)
    rvalue =  renpy.random.randint(1, total_weight)
    for (value, weight) in pairs:
          rvalue -= weight
          if rvalue <= 0: 
            return value

define location_chances = { 
  'morning':   [('home', 50), ('office', 50),],
  'afternoon': [              ('office', 80), ('mall', 10), ('park', 10),],
  'night':     [('home', 60),                 ('mall', 10), ('park', 30),],
}

default time_of_day = 'morning'

default kate_location = weighted_random(location_chances[time_of_day])

# usage:
# Whenever you want to change Kate location
$ kate_location = weighted_random(location_chances[time_of_day])
# Assuming you store time of day in varable. You can also use it like:
$ kate_location = weighted_random(location_chances['night'])
< < insert Rick Cook quote here > >

twistkill
Newbie
Posts: 14
Joined: Sat Feb 24, 2018 4:02 am
Contact:

Re: Randomized character location based on odds?

#3 Post by twistkill »

Ocelot wrote: Sat Feb 24, 2018 7:25 am Something like that...
Thank you so much. Your code gives me a good idea how to use renpy.random. I will give it a shot and get back on how it went. Thanks again!

KingsCard
Newbie
Posts: 14
Joined: Sat Feb 17, 2018 8:58 am
Contact:

Re: Randomized character location based on odds?

#4 Post by KingsCard »

Hello Ocelot,

Thank you for your code, it's working well ! I was wondering if it could be customized in game with variables.

Like if an event occur, you can't go to work anymore. So it would be convenient to be able to change the character schedule, change the odds or add a location.

Do you think it's possible ?

KingsCard
Newbie
Posts: 14
Joined: Sat Feb 17, 2018 8:58 am
Contact:

Re: Randomized character location based on odds?

#5 Post by KingsCard »

Hi, can someone help me with this dict ? I tried to understand by my own but I'm in a dead end.

Ideally, I just need a way to add or modify a key and its value to a certain period of time.

Post Reply

Who is online

Users browsing this forum: Google [Bot], Ocelot