-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logic.py
executable file
·241 lines (192 loc) · 7.02 KB
/
Logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import Config
import Helpers
gameWorld = None
gameState = None
nextBotIndex = 0
startTime = 0
tickIndex = 0
ticksPerSecondCurrent = 0
activeBots = []
botsToAdd = []
botsToRemove = []
organicInWorld = []
organicToRemove = []
selectedBot = None
DEFAULT_START_BOT_QUANTITY = 0
worldInfo = {
"tick": tickIndex,
"ticksPerSecond": Config.MAX_TICKS_PER_SECOND,
"ticksPerSecondCurrent": ticksPerSecondCurrent,
"activeBots": 0,
"organicQuantity": 0,
"resetCount": 0
}
mousePos = (0, 0)
def Initialize():
from datetime import datetime
import World
global startTime, gameWorld, gameState, DEFAULT_START_BOT_QUANTITY
startTime = str(datetime.now().strftime('%d-%m-%Y %H:%M:%S'))
gameWorld = World.World()
gameState = World.WorldState.Running
DEFAULT_START_BOT_QUANTITY = int((gameWorld.worldSizeInCells[0] + gameWorld.worldSizeInCells[1]))
pass
def newBot(botPosition, botParent=None):
import Bot
global nextBotIndex
newbornBot = Bot.Bot(botPosition, nextBotIndex, botParent)
nextBotIndex += 1
gameWorld.createBot(newbornBot)
botsToAdd.append(newbornBot)
return newbornBot
def newOrgan(organPosition, botHead, newGenome):
import Bot
newbornOrgan = Bot.Organ(organPosition, botHead, newGenome)
gameWorld.createBot(newbornOrgan)
return newbornOrgan
def removeBot(bot):
import Organic
global selectedBot
organic = Organic.Organic(bot.position, (bot.health + bot.energy))
gameWorld.createOrganic(organic)
organicInWorld.append(organic)
activeBots.remove(bot)
if bot == selectedBot:
selectedBot = None
def attackBot(defenderCoord, force):
import Bot
defender = gameWorld.worldMatrix[defenderCoord[0]][defenderCoord[1]]
if isinstance(defender, Bot.Bot):
return defender.getDamage(force)
else:
return 0
def compareGenome(bot1, bot2):
if bot1.genomeSize == bot2.genomeSize:
genomeDifference = 0
for i in range(0, bot1.genomeSize-1):
if bot1.genome[i] != bot2.genome[i]:
genomeDifference += 1
if genomeDifference > Config.MAX_GENOME_DIFFERENCE:
return False
return True
else:
return False
def placeOrganic(organicPosition, food=64):
import Organic
organic = Organic.Organic(organicPosition, food)
gameWorld.createOrganic(organic)
organicInWorld.append(organic)
def removeOrganic(organic):
# organicCoord = (Helpers.loopValue(organicCoord[0], 0, gameWorld.worldSizeInCells[0]-1), organicCoord[1])
gameWorld.removeOrganic(organic)
try:
#if organic is not None:
organicInWorld.remove(organic)
except ValueError:
print("strange error")
except AttributeError:
print("strange error")
return organic.food
def fillWorldWithBots(botCount: int, parent: object = None):
import random
import World
for i in range(0, int(botCount/2)):
xCoord = random.randint(0, gameWorld.worldSizeInCells[0] - 1)
yCoord = random.randint(0, gameWorld.worldSizeInCells[1] - 1)
targetCell = gameWorld.whatIsOnTile((xCoord, yCoord))
if targetCell == World.WorldObject.Empty:
if i % 4 == 0:
newBot((xCoord, yCoord), parent)
else:
newBot((xCoord, yCoord), None)
def fillWorldWithOrganic(orgCount=int(DEFAULT_START_BOT_QUANTITY)):
import random
import World
for i in range(0, orgCount):
xCoord = random.randint(0, gameWorld.worldSizeInCells[0] - 1)
yCoord = random.randint(0, gameWorld.worldSizeInCells[1] - 1)
targetCell = gameWorld.whatIsOnTile((xCoord, yCoord))
if targetCell == World.WorldObject.Empty:
placeOrganic((xCoord, yCoord))
def updateNumbers():
global worldInfo
worldInfo["tick"] = tickIndex
worldInfo["ticksPerSecond"] = int(Config.MAX_TICKS_PER_SECOND)
worldInfo["ticksPerSecondCurrent"] = ticksPerSecondCurrent
worldInfo["activeBots"] = len(activeBots)
worldInfo["organicQuantity"] = len(organicInWorld)
def tick():
global tickIndex
tickIndex += 1
if Config.RESPAWN_ORGANIC is True and tickIndex % Config.RESPAWN_ORGANIC_COUNTDOWN is 0:
fillWorldWithOrganic(DEFAULT_START_BOT_QUANTITY)
for bot in activeBots:
bot.tick(tickIndex)
if bot.health <= 0:
if Config.GOD_MODE is False:
botsToRemove.append(bot)
for bot in botsToAdd:
activeBots.append(bot)
botsToAdd.clear()
for botToRemove in botsToRemove:
removeBot(botToRemove)
botsToRemove.clear()
if Config.ENABLE_ORGANIC_DISSAPEAR is True:
for org in organicInWorld:
org.timeLeft -= 1
if org.timeLeft <= 0:
organicToRemove.append(org)
for organic in organicToRemove:
removeOrganic(organic)
organicToRemove.clear()
updateNumbers()
# region Events
def handleLeftClick():
import Bot
import Interface
global selectedBot
clickedCell = gameWorld.cellByPixel(mousePos)
if clickedCell is not None:
if isinstance(gameWorld.worldMatrix[clickedCell[0]][clickedCell[1]], Bot.Bot):
if isinstance(selectedBot, Bot.Bot):
selectedBot.isSelected = False
selectedBot = gameWorld.worldMatrix[clickedCell[0]][clickedCell[1]]
selectedBot.isSelected = True
else:
if isinstance(selectedBot, Bot.Bot):
selectedBot.isSelected = False
selectedBot = None
elif Helpers.isCoordsInRect(mousePos, Interface.buttonSpeedDownRect) is True:
gameSpeedDown()
elif Helpers.isCoordsInRect(mousePos, Interface.buttonSpeedUpRect) is True:
gameSpeedUp()
elif Helpers.isCoordsInRect(mousePos, Interface.buttonPauseRect) is True:
switchGameState()
elif Helpers.isCoordsInRect(mousePos, Interface.buttonResetRect) is True:
reset()
elif Helpers.isCoordsInRect(mousePos, Interface.buttonTickRect) is True:
tick()
def gameSpeedUp():
Config.MAX_TICKS_PER_SECOND = Config.MAX_TICKS_PER_SECOND * 2
def gameSpeedDown():
Config.MAX_TICKS_PER_SECOND = Config.MAX_TICKS_PER_SECOND / 2
if Config.MAX_TICKS_PER_SECOND < 1:
Config.MAX_TICKS_PER_SECOND = 1
def switchGameState():
import World
global gameState
if gameState == World.WorldState.Running:
gameState = World.WorldState.Paused
elif gameState == World.WorldState.Paused:
gameState = World.WorldState.Running
def reset():
global tickIndex
worldInfo["resetCount"] += 1
activeBots.clear()
for org in organicInWorld:
gameWorld.removeOrganic(org)
organicInWorld.clear()
gameWorld.recreateWorld()
tickIndex = 0
fillWorldWithBots(DEFAULT_START_BOT_QUANTITY)
# endregion