1
\$\begingroup\$

I am working on creating a salvage mechanic for my game. I want different units to calculate salvage after an encounter to randomly determine how much each one was able to scavenge from fallen allies or attackers.

As I have it setup up now its calculate as a group.
IE: If I have group of units whom collectively can salvage value of 5 and for each point of salvage I can potentially gain 1-4 piece of salvage then I have a range of 5-20 pieces of salvage (provided there are 20 or more pieces of salvage to collect). I am simply multiplying the total salvage value of the group by a randomly determined number in a range of 1-4.

My question is, how can I get each unit to calculate its salvage individually from its own range, rather than as a group as I'm doing now to give greater variance in the amount of salvage generated?

I am rather new and any suggestion or insight would be appreciated.

Below is the code for a demo I setup for the salvage system:

import random

parts_inventory = 0
undif_unit_count = 999
haulers = 2
foragers = 1
salvage_value = (haulers * 2) + foragers
temp_salvage = 0
salvage_random = random.randint(1, 4)

attack = random.randint(1, 6)
fatalities = attack
salvage_pool = fatalities * 4

print("There were {} units destroyed resulting in {} pieces of potential salvage".format(attack, salvage_pool))
print("Your units can haul away {} peice of salvage".format(salvage_value))


def salvage_operation(parts_inventory=parts_inventory):
    print("Encounter Haul {}".format(salvage_value))
    if salvage_value <= salvage_pool:
        print("You gained {} salvaged parts.".format(salvage_value))
        parts_inventory = parts_inventory + salvage_value
    if salvage_value > salvage_pool:
        print("You gained {} salvaged parts.".format(salvage_pool))
        parts_inventory = parts_inventory + salvage_pool
salvage_operation()
\$\endgroup\$
0
\$\begingroup\$

this is not how you would normally write Python

from random import randint

def units_scrap_rolls(current_salvage, haulers, foragers):
  rolls = [(haulers, 6), (foragers, 4)]  
  for unit_count, roll_max in rolls:
    for i in range(unit_count):
      current_salvage += randint(1, 6)
  return current_salvage

salvage
salvage = print(units_scrap_rolls(salvage, 2, 1))
print(salvage)
  • Notice the function has a return value
  • Using Python range() instead of while
  • Concise code
  • No default values for the variables if they aren't used
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Here is a solution I managed to come up with after talking with a friend involving making a function to essentially roll dice for each unit via a loop.

import random

haulers = 2
foragers = 1
temp_salvage = 0
count = 0

def units_scrap_rolls(temp_salvage=temp_salvage, haulers=haulers, foragers=foragers, count=count):
    if haulers > 0:
        number_of_rolls = haulers
        count = number_of_rolls
        while number_of_rolls >= count and count > 0:
            temp_salvage = temp_salvage + random.randint(1, 6)
            count = count - 1
        print ("After the Haulers were done, you had {} salvage.".format(temp_salvage))
    if foragers > 0:
        number_of_rolls = foragers
        count = number_of_rolls
        while number_of_rolls >= count and count > 0:
            temp_salvage = temp_salvage + random.randint(1, 4)
            count = count - 1

    print("In total you gained {} pieces of salvage.".format(temp_salvage))

units_scrap_rolls()
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.