All of the questions that I've asked recently about Python have been for this project. I have realised that the reason I'm asking so many questions may not be because I'm so new to Python (but I know a good bit of PHP) and is probably not because Python has some inherent flaw.
Thus I will now say what the project is and what my current idea is and you can either tell me I'm doing it all wrong, that there's a few things I need to learn or that Python is simply not suited to dealing with this type of project and language XYZ would be better in this instance or even that there's some open source project I might want to get involved in.
The project
I run a free turn based strategy game (think the campaign mode from the total war series but with even more complexity and depth) and am creating a combat simulator for it (again, think total war as an idea of how it'd work). I'm in no way deluded enough to think that I'll ever make anything as good as the Total war games alone but I do think that I can automate a process that I currently do by hand.
What will it do
It will have to take into account a large range of variables for the units, equipment, training, weather, terrain and so on and so forth. I'm aware it's a big task and I plan to do it a piece at a time in my free time. I've zero budget but it's a hobby that I'm prepared to put time into (and have already).
My current stumbling block
In PHP everything can access everything else, "wrong" though some might consider this it's really really handy for this. If I have an array of equipment for use by the units, I can get hold of that array from anywhere. With Python I have to remake that array each time I import the relevant data file and this seems quite a silly solution for a language that from my experience is well thought out. I've put in a system of logging function calls and class creation (because I know from a very basic version of this that I did in PHP once that it'll help a lot down the line) and the way that I've kept the data in one place is to pass each of my classes an instance to my logging list, smells like a hack to me but it's the only way I've gotten it to work.
Thus I conclude I'm missing something and would very much appreciate the insight of anybody willing to give it. Thank you.
Code samples
This creates a list of formations, so far there's only one value (besides the name) but I anticipate adding more on which is why they're a list of classes rather than just a standard list. This is found within data.py
formations = []
formationsHash = []
def createFormations(logger):
"""This creates all the formations that will be used"""
# Standard close quarter formation, maximum number of people per square metre
formationsHash.append('Tight')
formations.append(Formation(logger, 'Tight', tightness = 1))
# Standard ranged combat formation, good people per square metre but not too cramped
formationsHash.append('Loose')
formations.append(Formation(logger, 'Loose', tightness = 0.5))
# Standard skirmishing formation, very good for moving around terrain and avoiding missile fire
formationsHash.append('Skirmish')
formations.append(Formation(logger, 'Skirmish', tightness = 0.1))
# Very unflexible but good for charges
formationsHash.append('Arrowhead')
formations.append(Formation(logger, 'Arrowhead', tightness = 1))
def getFormation(searchFor):
"""Returns the fomation object with this name"""
indexValue = formationsHash.index(searchFor)
return formations[indexValue]
I don't have a code sample of when I'd need to access it because I've not gotten as far as making it but I anticipate the code looking something like the following:
Python
tempFormation = data.getFormation(unit.formationType)
tempTerrain = data.getTerrain(unit.currentTerrain)
unit.attackDamage = unit.attackDamage * tempTerrain.tighnessBonus(tempFormation.tightness)
The unit contains an integer that links to the index/key of the relevant terrain, formation and whatnot in the master list. Temporary variables are used to make the 3rd line shorter but in the long run will possibly cause issues if I forget to get one and it's use a value from earlier which is then incorrect (that's where the logging comes in handy).
PHP
$unit->attackDamage *= $terrain[$unit->currentTerrain]->tighnessBonus($unit->currentTerrain)
The unit class contains the index (probably a string) of the relevant terrain it's on and the formation it's in.
Maybe this will show some massive flaw in my understanding of Python (6 months vs the 3 years of PHP).