delete this post (moved)

Started by XeoNovaDan, March 03, 2017, 06:17:00 PM

Previous topic - Next topic

b0rsuk

#15

    weap_rng = [''] * 4
   
    if(weapon == "assaultrifle"):
            weap_rng = [70, 87, 77, 64]
    elif(weapon == "chargerifle"):
            weap_rng = [80, 83, 68, 53]
    elif(weapon == "greatbow"):
            weap_rng = [93, 85, 75, 50]
    elif(weapon == "heavysmg"):
            weap_rng = [89, 64, 37, 22]
    elif(weapon == "incendiarylauncher"):
            weap_rng = [79, 42, 18, 6]
    elif(weapon == "lmg"):
            weap_rng = [50, 64, 41, 22]
    elif(weapon == "machinepistol"):
            weap_rng = [73, 62, 43, 15]
    elif(weapon == "pila" or weapon == "pistol"): #Pila and pistol have the same accuracy values
            weap_rng = [91, 71, 50, 32]
    elif(weapon == "pumpshotgun"):
            weap_rng = [80, 87, 77, 64]
    elif(weapon == "shortbow"):
            weap_rng = [89, 64, 41, 22]
    elif(weapon == "sniperrifle"):
            weap_rng = [50, 86, 86, 88]
    elif(weapon == "survivalrifle"):
            weap_rng = [75, 96, 96, 90]


Two points here.

1.

weap_rng = [''] * 4 doesn't do anything unless none of the if statements matches. You absolutely don't have to "initialize an empty array" in Python, and not because these are lists and not arrays. In first line, you declare a variable and assign a list containing 4 empty strings to it. In the subsequent lines, you simply overwrite that variable, and the previous list is thrown away and never used. weap_rng = [70, 87, 77, 64] literally creates a new list containing the 4 numbers and assigns it to your variable.

2. This section is better replaced with a Python dictionary, like this:


WEAPON_RANGES = {
    "assaultrifle": (70, 87, 77, 64),
    "chargerifle": (80, 83, 68, 53),
    "greatbow":  (93, 85, 75, 50),
}


Then you could do:
weap_rng = WEAPON_RANGES[weapon]


No square brackets, because they're tuples, not lists. You could use lists, but there's no point. Tuples work the same except when you try to add new elements to them, and you're not going to suddenly add a 5th weapon range during the program operation, are you ? I wrote the name of the dictionary in all caps, because it's essentially a constant, and Python convention for constants is to name them in all caps. And putting them at the start of a file.

https://github.com/XeoNovaDan/RimWorld-Accuracy-Calculator-v0.1/blob/master/weap_acc_subfuncs.py#L82

You absolutely don't have to manually increment the counter variable like that if it comes from "for counter in range". In each iteration, Python provides you with a new value from the 'range' function. Your adding 1 to the variable at the end of an iteration does absolutely no harm, because nothing uses the increased variable. If you remove that line, your program will work the same.

https://github.com/XeoNovaDan/RimWorld-Accuracy-Calculator-v0.1/blob/master/pawn_acc_subfuncs.py#L65

Those if...break sections in your while loops are completely useless. You're giving the 'while' loop a condition how long it should spin. If the condition is no longer true, it will simply stop, and you don't have to break it manually.

https://github.com/XeoNovaDan/RimWorld-Accuracy-Calculator-v0.1/blob/master/weapon_accuracy.py#L55

Why not import it at the start of the file ?

https://github.com/XeoNovaDan/RimWorld-Accuracy-Calculator-v0.1/blob/master/weapon_accuracy.py#L68

In the long run it's a bad idea to put "magic numbers" in your code. Someone with less than perfect Rimworld knowledge won't know where the number came from. I played Rimworld much more than I care to admit, and I don't recognize many of your numbers. Instead, you could define these ranges at the start of the file, after imports:


TOUCH = 4
SHORT = 15
MEDIUM = 30
LONG = 50


Then you could write:

for counter in range(0, TOUCH + 1):
...
for counter in range(TOUCH, SHORT + 1):
...
for counter in range(SHORT, MEDIUM + 1):
...


Then your intentions are clearer. If you get excited over another game and return to your program six months later, you will instantly recognize what's going on.
You need to add 1 to the endpoint if you want your ranges to end with 4, 15, 30, 50.

In fact, it's best to do something like this for most numbers in your program. Not necessarily a separate variable for each number, that would be silly. But now you should know how to define dictionaries.

Hans Lemurson

Mental break: playing RimWorld
Hans Lemurson is hiding in his room playing computer games.
Final straw was: Overdue projects.

XeoNovaDan

#17
Indeed. I've learned a thing or two thanks to b0rsuk. :)
Implementing the changes as I go, also adding a third mode called 'custom', which allows for a bespoke experience. Was going to replace 'advanced' with it, but then I thought 'nah'.

I've probably learned more outside of school than I actually have in school xD

#Python101

XeoNovaDan

RimWorld Ranged Accuracy Calculator has officially been updated to v1.0!

There is a vast range of improvements and feature additions, which can be seen in the update log. Special thanks to b0rsuk for providing considerable amounts of feedback to improve this program, and also thanks to GiantSpaceHamster for initially suggesting to put this on GitHub :)

XeoNovaDan

Updated to v1.0.16_r3 (despite initially wanting to cease development, but I've worked around the trivial problem)

Changes
- Slight performance and optimisation improvements
     - Removed redundant variables
     - Some unnecessary lists are now tuples
- Corrected Sight contribution in the formula (0.9 -> 0.95)
- Annotated the raw code for improved legibility

XeoNovaDan

OP update: Sample output provided, categorised sections of the page

XeoNovaDan

Updated to v1.0.16_r4

Minor improvements as documented in the change log, but mainly an automatic bottleneck identifier.

XeoNovaDan

Updated to v1.1.16_r1 (23/3/2017)
Relatively minor update in one way, but major in another

Overview:
- CSV exporting
- Minor fixes

XeoNovaDan

v1.1.16_r2 is officially fully released!

Overview:
- Quality and Health can now be factored in weapon accuracy advanced mode
- Advanced mode bug fixing in fields that would cause exceptions
- Minor rephrasing done in places

GitHub saw this release several hours before Dropbox because school