2021-05-21 11:38:53

Hello all.
As of late, I’ve been playing around with basic python development, and have arrived at the point were I’m trying my hand at developing a little console based game, called rim-66, heavily inspired from elements of projects like the Wastes and War-Sim. That’s to say, a text based experience with random generation of things from weapons, to in game characters, to locations. For those interested, the base premise of the game is as follows.
In the late 21st century humanity has expanded throughout our local soler system, and is starting to establish colonies on distant exoplanets outside the range of influence of sol. On their way to set up a colony on a potential planet, 3 ships traveling through deep space have contact lost with them, disappear, and are reported MIA. As part of a rescue and investigation team, the player character is dispatched to the last reported coordinates of the 3 missing craft. Upon reaching the location, the crew is caught off guard by the sudden and unexplainable formation of a black hole, and caught on its event horizon, are subsequently dragged in. Emerging on the opposite end, the ship finds itself in orbit around an uncharted planet. However, having experienced system wide loss of life support systems and catastrophic malfunction, the onboard life-pods are jettisoned to the planet’s surface, with the player character being in one of these life-pods, and starting there at the beginning of the game.
So based off of that description, many of you’ve probably realised that the game’s also been inspired by Rim world and Sub Nautica. In any case, my main question and reason for creating this topic is to firstly ask if people have some suggestions for things I could try to implement in the game, and secondly to ask for the best way how I can go about randomly generating stuff.
Essentially what I’ve so far put together are a whole bunch of number generators which determine objects, their descriptions, and effects. With Weapons for example I’ve defined their overall elements such as , Gun.Name, Gun.Type, etc, and then I’ve created generators. Then an if statement checks the generated number, and switches that number to actual words. Then the generation gets kicked on over to a function housing more specific terms and elements for what it generated. for example.
Gun.Type1 = random.randint(1, 4)
If Gun.Type1 == 1:
Gun.Type1 = “submachinegun”
Submachinegun()
This method holds true for things like adjectives which determine what effects are tied to the generated weapon, what action it uses, for example bolt-action, fully-automatic, etc, round capacity and type, and a few other elements. Then, at the end of that, there are a few if triggers which look out for combinations that don’t quite fit, ever heard of a pump-action snipers machinegun, and changes it to a more acceptable term.
I’m using this similar method for most of the random generation in the game, and at this point my question arises. Firstly, is this a good system to be working with, or is this an abomination of code which should be put to the flame and incinerated, and secondly, how would I go about importing the generated weapons and stuff into a game. My idea is that I would like the game to generate x amount of weapons at game start, and then  only generate those x amount of weapons for the duration of that specific seed. An idea which I’m toying with is creating a number of values, which will be matched with weapons upon generation. For example,  I create a value called Weapon1, and then I tell the game to run the weapon generation process. The result is then saved to the Weapon1 value. The problem here is that I’m not sure if that is even feasible in python, and how to write it out even if it is.
Any suggestions would be very much welcomed, and I look forward to seeing any other recommendations and feedback that you guys might have.

Hit up my YouTube Channel at
https://www.youtube.com/channel/UCC9wWO … R85ILY1VQ/
Their you'll find some gaming videos, and what ever els my confused mess of a brain decides to upload. Hope you enjoy it and Stay safe, folks.

2021-05-21 18:06:14 (edited by magurp244 2021-05-21 18:33:17)

I wouldn't say its the "wrong" way, but it isn't necessarily the most efficient either. A better way would be to store the text values you want in an array, and then use the random number directly to reference it for storage. For example:

values = ["submachinegun","sniper","rocketlauncher"]
Gun.Type = values[random.randint(0,2)]

Or if the weapon in question is stored as a class, reference that directly:

values = [submachinegun(),sniper(),rocketlauncher()]
Gun.Type = values[random.randint(0,2)]
print(Gun.Type.id) #text stating gun type

This way there's no need for messy if statement checks, and you don't have to keep adding lines of code if you want to add more values, just append them to the existing values list. As for your question of generating X weapons per seed and only spawning those specific weapons throughout the game, yes you can totally do that in a number of ways. If you aren't dealing with a billion universes like No Mans Sky, the most straight forward way would be to generate those X weapons when generating your world and store them in a master list of items, then when spawning items in the world randomly select from that list, either removing them and treating them as unique items, or keeping them in the list to respawn as duplicate items as needed.

There's a number of interesting articles around on procedural generation you may find interesting, like procedurally generated cultures and religions with [Ultima Ratio Regum], or environments and items with [Cogmind], among others.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2021-05-21 20:19:57

Post #2 got it right, but here's a slightly easier way.

weapons = ["submachinegun","rocketlauncher"]
gun.type = random.choice(weapons)

You can now resize the array without having to increase the number.

weapons = ["submachinegun", "rocketlauncher", "pistel", "grenade"]
gun.type = random.choice(weapons)

2021-05-21 20:24:26

If you're using python there's actually a shortcut for fairly simple cases. If your generation is going to get more complicated than this you may need additional data structures.

import random

values = ["submachinegun","sniper","rocketlauncher"]
Gun.Type = random.choice(values)
print(gun.type)

If you want to make certain things more likely to be chosen than others, you can use random.choices instead.

import random

values = ["submachinegun","sniper","rocketlauncher"]
Gun.Type = random.choices(values, [1, 1, 3])[0] #This lets you make repeated choices. Default is 1 but you still need to grab index 0
print(gun.type)

This would pick the rocketlauncher value 3/5 of the time and the others 1/5 of the time (the weights 1+1+3 sum to 5).

Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic.

2021-05-21 20:58:31

Wow, and here’s me prepping my mental stability to sit down and write something stupid like 10 lines of code for each of 60 different values that I wanted to randomly generate. This system of defining all the possible outcomes in one nice long line, and then having the random module pick between them sounds so much cleaner and straight forwards than how I was originally envisioning, and I’ll make it a point to shift the weapon name generation code to that cause, God, it was a mess, I’ll copy it down below for anyone who wants to marvel at its impracticability. However, before that the only issue that I’m  seeing with this method as far as actual generation, is when I’d want the generated strings to change the weapon’s base values. For example, I was setting it up so that if the RNG decided that a pistol was fully-automatic, chambered for 50bmg, and had a foldable shoulder stock, then the if statements which dictated all of the strings would also contain a number of changes that that string, if chosen, would apply to the weapon in question. With this new method on the other hand, I think that I’d still have to define if statements to check elements of the weapon, and make the appropriate alterations. In any case, here’s the original way I was going to go about this business, get ready.
# import all the needed modules
import cmd
import textwrap
import command
import os
import random
import time
import sys
from random import randint
class Gun(object):
    def __init__(self, Name1, Name2, Name3, Name4, Name5, Mode, Type1, Type2, Feed_systom, Ammo_Type, Ammo_Number, Body1, Body2, Body3, Attachment, Weight, Rainge, Recoil, DMG, Spread, Value):
        self.name1 = Name1
        self.name2 = Name2
        self.name3 = Name3
        self.name4 = Name4
        self.name5 = Name5
        self.mode = Mode
        self.type1 = Type1
        self.type2 = Type2
        self.feed_systom = Feed_systom
        self.ammo_type = Ammo_Type
        self.ammo_number = Ammo_Number
        self.body1 = Body1
        self.Body2 = Body2
        self.body3 = Body3
        self.attachment = Attachment
        self.weight = Weight
        self.rainge = Rainge
        self.recoil = Recoil
        self.dmg = DMG
        self.value = Value
def Weapon_n():
    Gun.Name1 = random.randint(1, 40)
    if Gun.Name1 == 1:
        Gun.Name1 = "A"
    elif Gun.Name1 == 2:
        Gun.Name1 = "B"
    elif Gun.Name1 == 3:
        Gun.Name1 = "C"
    elif Gun.Name1 == 4:
        Gun.Name1 = "D"
    elif Gun.Name1 == 5:
        Gun.Name1 = "E"
    elif Gun.Name1 == 6:
        Gun.Name1 = "F"
    elif Gun.Name1 == 7:
        Gun.Name1 = "G"
    elif Gun.Name1 == 8:
        Gun.Name1 = "H"
    elif Gun.Name1 == 9:
        Gun.Name1 = "I"
    elif Gun.Name1 == 10:
        Gun.Name1 = "J"
    elif Gun.Name1 == 11:
        Gun.Name1 = "K"
    elif Gun.Name1 == 12:
        Gun.Name1 = "L"
    elif Gun.Name1 == 13:
        Gun.Name1 = "M"
    elif Gun.Name1 == 14:
        Gun.Name1 = "N"
    elif Gun.Name1 == 15:
        Gun.Name1 = "O"
    elif Gun.Name1 == 16:
        Gun.Name1 = "P"
    elif Gun.Name1 == 17:
        Gun.Name1 = "Q"
    elif Gun.Name1 == 18:
        Gun.Name1 = "R"
    elif Gun.Name1 == 19:
        Gun.Name1 = "S"
    elif Gun.Name1 == 20:
        Gun.Name1 = "T"
    elif Gun.Name1 == 21:
        Gun.Name1 = "U"
    elif Gun.Name1 == 22:
        Gun.Name1 = "V"
    elif Gun.Name1 == 23:
        Gun.Name1 = "W"
    elif Gun.Name1 == 24:
        Gun.Name1 = "X"
    elif Gun.Name1 == 25:
        Gun.Name1 = "Y"
    elif Gun.Name1 == 26:
        Gun.Name1 = "Z"
    elif Gun.Name1 == 27:
        Gun.Name1 = "Winchester "
    elif Gun.Name1 == 28:
        Gun.Name1 = "Browning "
    elif Gun.Name1 == 29:
        Gun.Name1 = "Ruger"
    elif Gun.Name1 == 30:
        Gun.Name1 = "Colt "
    elif Gun.Name1 == 31:
        Gun.Name1 = "Beretta "
    elif Gun.Name1 == 32:
        Gun.Name1 = "Taurus "
    elif Gun.Name1 == 33:
        Gun.Name1 = "H&K "
    elif Gun.Name1 == 34:
        Gun.Name1 = "High Point "
    elif Gun.Name1 == 35:
        Gun.Name1 = "Mouser "
    elif Gun.Name1 == 36:
        Gun.Name1 = "Kel-Tec "
    elif Gun.Name1 == 37:
        Gun.Name1 = "Kalashnikov "
    elif Gun.Name1 == 38:
        Gun.Name1 = "Armor-Light "
    elif Gun.Name1 == 39:
        Gun.Name1 = "Glock "
    elif Gun.Name1 == 40:
        Gun.Name1 = "SIG Sour "
    Gun.Name2 = random.randint(1, 26)
    if Gun.Name2 == 1:
        Gun.Name2 = "A"
    elif Gun.Name2 == 2:
        Gun.Name2 = "B"
    elif Gun.Name2 == 3:
        Gun.Name2 = "C"
    elif Gun.Name2 == 4:
        Gun.Name2 = "D"
    elif Gun.Name2 == 5:
        Gun.Name2 = "E"
    elif Gun.Name2 == 6:
        Gun.Name2 = "F"
    elif Gun.Name2 == 7:
        Gun.Name2 = "G"
    elif Gun.Name2 == 8:
        Gun.Name2 = "H"
    elif Gun.Name2 == 9:
        Gun.Name2 = "I"
    elif Gun.Name2 == 10:
        Gun.Name2 = "J"
    elif Gun.Name2 == 11:
        Gun.Name2 = "K"
    elif Gun.Name2 == 12:
        Gun.Name2 = "L"
    elif Gun.Name2 == 13:
        Gun.Name2 = "M"
    elif Gun.Name2 == 14:
        Gun.Name2 = "N"
    elif Gun.Name2 == 15:
        Gun.Name2 = "O"
    elif Gun.Name2 == 16:
        Gun.Name2 = "P"
    elif Gun.Name2 == 17:
        Gun.Name2 = "Q"
    elif Gun.Name2 == 18:
        Gun.Name2 = "R"
    elif Gun.Name2 == 19:
        Gun.Name2 = "S"
    elif Gun.Name2 == 20:
        Gun.Name2 = "T"
    elif Gun.Name2 == 21:
        Gun.Name2 = "U"
    elif Gun.Name2 == 22:
        Gun.Name2 = "V"
    elif Gun.Name2 == 23:
        Gun.Name2 = "W"
    elif Gun.Name2 == 24:
        Gun.Name2 = "X"
    elif Gun.Name2 == 25:
        Gun.Name2 = "Y"
    elif Gun.Name2 == 26:
        Gun.Name2 = "Z"
    Gun.Name3 = random.randint(1, 2)
    if Gun.Name3 == 1:
        Gun.Name3 = "-"
    elif Gun.Name3 == 2:
        Gun.Name3 = ""
    Gun.Name4 = random.randint(1, 1000)
    Gun.Name5 = random.randint(1, 10)
    if Gun.Name5 == 1:
        Gun.Name5 = ""
    elif Gun.Name5 == 2:
        Gun.Name5 = " Mini"
    elif Gun.Name5 == 3:
        Gun.Name5 = " Scorpion"
    elif Gun.Name5 == 4:
        Gun.Name5 = " Grizzly"
    elif Gun.Name5 == 5:
        Gun.Name5 = " Government"
    elif Gun.Name5 == 6:
        Gun.Name5 = " Falcon"
    elif Gun.Name5 == 7:
        Gun.Name5 = " Dragoon"
    elif Gun.Name5 == 8:
        Gun.Name5 = " Liberty"
    elif Gun.Name5 == 9:
        Gun.Name5 = " Legion"
    elif Gun.Name5 == 10:
        Gun.Name5 = ""
    Weapon_class()

#setting up the weapon classes

def Weapon_class():
    Gun.Type1 = random.randint(1, 8)
    if Gun.Type1 == 1:
        Gun.Type1 = "pistol"
        Gun.Range = 20
        Gun.Recoil = 1
        Gun.Spread = 0
        Gun.DMG = 3
        Gun.Weight = 1
        Gun.Value = 2
        Pistol()
    elif Gun.Type1 == 2:
        Gun.Type1 = "rifle"
        Gun.Range = 60
        Gun.Recoil = 3
        Gun.Spread = 0
        Gun.DMG = 6
        Gun.Weight = 3
        Gun.Value = 5
    elif Gun.Type1 == 3:
        Gun.Type1 = "shotgun"
        Gun.Range = 30
        Gun.Recoil = 5
        Gun.DMG = 8
        Gun.Spread = 4
        Gun.Weight = 3
        Gun.Value = 7
    elif Gun.Type1 == 4:
        Gun.Type1 = "carbine"
        Gun.Range = 50
        Gun.Recoil = 3
        Gun.Spread = 0
        Gun.DMG = 5
        Gun.Weight = 2
        Gun.Value = 4
    elif Gun.Type1 == 5:
        Gun.Type1 = "submachinegun"
        Gun.Range = 40
        Gun.Recoil = 4
        Gun.Spread = 2
        Gun.DMG = 4
        Gun.Weight = 2.5
        Gun.Value = 5
    elif Gun.Type1 == 6:
        Gun.Type1 = "machinegun"
        Gun.Range = 40
        Gun.Recoil = 4
        Gun.Spread = 3
        Gun.DMG = 4
        Gun.Weight = 10
        Gun.Value = 8
    elif Gun.Type1 == 7:
        Gun.Type1 = "machine pistol"
        Gun.Range = 25
        Gun.Recoil = 2
        Gun.Spread = 2
        Gun.DMG = 3
        Gun.Weight = 1
        Gun.Value = 5
    elif Gun.Type1 == 8:
        Gun.Type1 = "magnum"
        Gun.Range = 20
        Gun.Recoil = 2
        Gun.Spread = 0
        Gun.DMG = 4
        Gun.Weight = 1
        Gun.Value = 2

#setting up the main pistol class

def Pistol():
    Gun.Mode = random.randint(1, 4)
    if Gun.Mode == 1:
        Gun.Mode = "semi-automatic"
        Gun.Range += 5
        Gun.Recoil -= 1
        Gun.Value += 1
    elif Gun.Mode == 2:
        Gun.Mode = "Fully-automatic"
        Gun.Range -= 5
        Gun.Spread += 2
        Gun.Recoil += 1
        Gun.Value += 1

Hit up my YouTube Channel at
https://www.youtube.com/channel/UCC9wWO … R85ILY1VQ/
Their you'll find some gaming videos, and what ever els my confused mess of a brain decides to upload. Hope you enjoy it and Stay safe, folks.

2021-05-21 21:28:53 (edited by magurp244 2021-05-21 21:34:24)

Part of the trick is to design for randomization, which can involve designing things so they can be standardized. In your case, you want to have certain weapon types have certain presets that change the overall weapon, or even a specific set of random variations, like with automatic weapons. Now there are a number of ways you could do this, one of which would be to use nested arrays, or a 3D array.

So, lets say we take the list of gun type values:

values = ["submachinegun","sniper","rocketlauncher"]

Now, you can either recreate that list to contain other lists, or have a secondary list of subvalues, like so:

values = [["submachinegun","pump-action", "semi-auto", "full-auto"], ["Sniper","piercing","explosive","tranq"], ["Rocketlauncher","High-Explosive","Phophorus","Piercing"]]
choice = random.randint(0,2)
Gun.Type = values[choice][0]
Gun.Property = values[choice][random.randint(1,3)]

Or:

values = ["submachinegun","sniper","rocketlauncher"]
properties = [["pump-action", "semi-auto", "full-auto"], ["piercing","explosive","tranq"], ["High-Explosive","Phophorus","Piercing"]]
choice = random.randint(0,2)
Gun.Type = values[choice]
Gun.Property = properties[choice][random.randint(0,2)]

Or even:

values = ["submachinegun","sniper","rocketlauncher"]
properties = ["pump-action", "semi-auto", "full-auto", "piercing","explosive","tranq", "High-Explosive","Phophorus","Piercing"]
choice = random.randint(0,2)
Gun.Type = values[choice]
Gun.Property = properties[random.randint(0,8)]

If you wanted to have things like a full auto sniper rifle, etc. I'd probably define a general type as a class and have it contain a list of such offsets, then have it randomly choose them when the class is created automatically.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer