[SOLVED] Help writing a function using lists and things

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
LyannaCore
Newbie
Posts: 22
Joined: Wed Jun 21, 2017 1:45 pm
Contact:

[SOLVED] Help writing a function using lists and things

#1 Post by LyannaCore »

I seem to be unable to resist complicating my life, but my desires often outstrip my abilities.
This time I am trying to create a function to determine how far a destination is, and calculate various things relating to that.

I created a series of lists that contain distances between a current location, and potential destinations. So for example, if the player is at "earth" and wanted to go to mars, they would use the 5th number. (6th item? Still confused about that)

Code: Select all

default travel_distance_rounded = [   #so     me     ve     ea     ma     ab     ju     gr     tr     sa     ur     ne     pl
    #"starting location",            distance to destination in AU
    ["sol",                          0.00,  0.50,  0.75,  1.00,  1.50,  3.00,  5.00,  6.00,  6.00, 10.00, 19.00, 30.00, 40.00],
    ["mercury",                      0.50,  0.00,  0.25,  0.75,  2.25,  4.25,  5.25,  5.25,  5.50,  9.50, 18.50, 29.50, 39.50],
    ["venus",                        0.75,  0.25,  0.00,  0.25,  0.75,  2.25,  4.25,  5.25,  5.25,  9.25, 18.25, 29.25, 39.25],
    ["earth",                        1.00,  0.50,  0.25,  0.00,  0.50,  2.00,  4.00,  5.00,  5.00,  9.00, 18.00, 29.00, 39.00],
    ["mars",                         1.50,  1.00,  0.75,  0.50,  0.00,  1.50,  3.50,  4.50,  4.50,  8.50, 17.50, 28.50, 38.50],
    ["asteroid belt",                3.00,  2.50,  2.25,  2.00,  1.50,  0.00,  2.00,  3.00,  3.00,  7.00, 16.00, 27.00, 37.00],
    ["jupiter",                      5.00,  4.50,  4.25,  4.00,  3.50,  2.00,  0.00,  1.00,  1.00,  5.00, 14.00, 25.00, 35.00],
    ["greeks",                       6.00,  5.50,  5.25,  5.00,  4.50,  3.00,  1.00,  0.00,  1.00,  4.00, 13.00, 24.00, 34.00],
    ["trojans",                      6.00,  5.50,  5.25,  5.00,  4.50,  3.00,  1.00,  1.00,  0.00,  4.00, 13.00, 24.00, 34.00],
    ["saturn",                      10.00,  9.50,  9.25,  9.00,  8.50,  7.00,  5.00,  4.00,  4.00,  0.00,  9.00, 20.00, 30.00],
    ["uranus",                      19.00, 18.50, 18.25, 18.00, 17.50, 16.00, 14.00, 13.00, 13.00,  9.00,  0.00, 11.00, 21.00],
    ["neptune",                     30.00, 29.50, 29.25, 29.00, 28.50, 27.00, 25.00, 24.00, 24.00, 20.00, 11.00,  0.00, 10.00],
    ["pluto",                       40.00, 39.50, 39.25, 39.00, 38.50, 37.00, 35.00, 34.00, 34.00, 30.00, 21.00, 10.00,  0.00],
]
Then I figured there should be a way to determine which destination corresponds to which number in the initial list, and made this one:

Code: Select all

default destination_number = [
    ["sol",             1],
    ["mercury",         2],
    ["venus",           3],
    ["earth",           4],
    ["mars",            5],
    ["asteroid belt",   6],
    ["jupiter",         7],
    ["greeks",          8],
    ["trojans",         9],
    ["saturn",          10],
    ["uranus",          11],
    ["neptune",         12],
    ["pluto",           13],
]
This is what I have roughed out so far, but I'm unsure how to go about a lot of it, or even if I am on the right track. Any suggestions/help would be appreciated.

Code: Select all

init python:
    def distance_checker():
        global travel_distance_rounded
        global destination_number
        global player_destination
        global player_location
        global engine_power
        global ship_fuel

        AU = 150,000,000

        #compare player_location with travel_distance_rounded to find the correct list.
        #compare player_destination with destination_number to get the number: dest_number.
        dest_number =
        
        #use the dest_number to determine the item number of the list: dist_number
        dist_number =
        
        #1 fuel is worth 0.25, so divide dist_number by 0.25 to determine required fuel: req_fuel.
        req_fuel = (dist_number / 0.25)
        
        #multiply dist_number and AU to determine distance in kilometers: travel_distance
        travel_distance = (dist_number * AU)
        
        #multiply engine_power by dist_number to get travel time: travel_time
        travel_time = (engine_power * dist_number)


        if ship_fuel >= (req_fuel + 1):
            ship_fuel -= req_fuel
            jump player_destination
        elif ship_fuel == req_fuel:
            call fuel_question
        else:
            narrator("You don't have enough fuel for that destination.", interact=True)

label fuel_question:
    "You have exactly enough fuel to reach that destination, but it's recomended that you carry some reserve for emergencies."
    menu:
        "Do you still wish to procede?"
        "Engage!":
            jump player_destination
        "Reconsider":
            return
Last edited by LyannaCore on Wed Aug 08, 2018 10:45 pm, edited 1 time in total.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Help writing a function using lists and things

#2 Post by Remix »

Firstly, you have the bodies in the first big list, so just a quick:

# All we need is them in order, no need for a numeric too.
default destination_number = [ k[0] for k in travel_distance_rounded ]

Calculate distance first...

#multiply distance by AU to determine distance in kilometers: travel_distance
travel_distance = [ k[1:] for k in travel_distance_rounded if k[0] == player_location ][0][ destination_number.index( player_destination ) ] * AU
#
# Basically we grab a temp list by checking player_location, e.g. if on earth we would get:
# [ [1.00, 0.50, 0.25, 0.00, 0.50, 2.00, 4.00, 5.00, 5.00, 9.00, 18.00, 29.00, 39.00] ]
# First item (the [0]) to get:
# [ 1.00, 0.50, 0.25, 0.00, 0.50, 2.00, 4.00, 5.00, 5.00, 9.00, 18.00, 29.00, 39.00 ]
# Index from our list above, e.g. so = 0
# = 1.0 AU

#1 fuel is worth 0.25, so multiply dist_number by 4.0 to determine required fuel: req_fuel.
req_fuel = travel_distance * 4.0

I think my comprehensions are valid there...
Frameworks & Scriptlets:

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Help writing a function using lists and things

#3 Post by philat »

What I imagine is a more long-hand way of expressing Remix's idea above (because I'm not a programmer and compressing logic too much makes it hard for me to read ;) ) Also, you don't need the whole matrix, just a list of absolute positions which can be used to calculate relative distance.

Code: Select all

                    #so     me     ve     ea     ma     ab     ju     gr     tr     sa     ur     ne     pl
default positions = [0.00,  0.50,  0.75,  1.00,  1.50,  3.00,  5.00,  6.00,  6.00, 10.00, 19.00, 30.00, 40.00]

# changed this to a dictionary
default position_index = {
    "sol":             0,
    "mercury":         1,
    "venus":           2,
    "earth":           3,
    "mars":            4,
    "asteroid belt":   5,
    "jupiter":         6,
    "greeks":          7,
    "trojans":         8,
    "saturn":          9,
    "uranus":          10,
    "neptune":         11,
    "pluto":           12 }

init python:
    def distance_checker(player, dest):
        # use the player's location (a string) and destination (also a string)
        # ex: distance_checker("sol", "asteroid belt")

        # you only need to declare global if you intend to CHANGE a variable (only accessing a variable doesn't matter)
        
        AU = 150,000,000

        # grab player/dest positions
        # ex: position_index["sol"] would return 0. positions[0] is 0.00
        player_position = positions[position_index[player]] 
        dest_position = positions[position_index[dest]]

        # calculate distance (use abs to force a positive number)
        dist_number = abs(player_position - dest_position)
    
        #1 fuel is worth 0.25, so divide dist_number by 0.25 to determine required fuel: req_fuel.
        req_fuel = (dist_number / 0.25)

        # I'm not sure what you're trying to do with travel_distance and travel_time
        #multiply dist_number and AU to determine distance in kilometers: travel_distance
        travel_distance = (dist_number * AU)
        #multiply engine_power by dist_number to get travel time: travel_time
        travel_time = (engine_power * dist_number) 

        # you can't use renpy script inside a function
        # probably simpler to return something and use that in renpy script
        if ship_fuel >= (req_fuel + 1):
            return req_fuel # return the req_fuel so you can subtract it from ship_fuel outside of the function
        if ship_fuel == req_fuel:
            return None

label start:
    $ can_travel = distance_checker("sol", "asteroid belt")
    if can_travel:
    	$ ship_fuel -= can_travel
        jump "asteroid belt"
    elif can_travel == None:
        call fuel_question
    else:
        narrator "You don't have enough fuel for that destination."

LyannaCore
Newbie
Posts: 22
Joined: Wed Jun 21, 2017 1:45 pm
Contact:

Re: Help writing a function using lists and things

#4 Post by LyannaCore »

Thank you both for the help, I got both solutions to work and learned a lot about how to do things in different ways.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]