-
Notifications
You must be signed in to change notification settings - Fork 1
/
unitHandler.py
316 lines (249 loc) · 13.5 KB
/
unitHandler.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from pandac.PandaModules import *
#from direct.gui.OnscreenText import OnscreenText
#from direct.actor.Actor import Actor
from direct.task.Task import Task
#from direct.showbase.DirectObject import DirectObject
from panda3d.ai import *
from direct.gui.OnscreenImage import OnscreenImage
from pandac.PandaModules import TransparencyAttrib
import copy
import astar
class world:
def __init__(self, mainClass):
self.AIWorld = AIWorld(render)
self.unitDict = mainClass.parserClass.unit
print self.unitDict
self.main = mainClass.parserClass.main # To redirect the unitID to the unitName
self.gameUnits = {} # A dictionary containing the info of all the in-game units, each with a unique ID as a key
self.unitUniqueID = 0
self.meshes = mainClass.grids
taskMgr.add(self.tskWorld, 'world update')
def addUnit2(self, unitID, mainClass):
self.addUnit(unitID, mainClass.mouseClass.getMousePos(), mainClass)
def addUnit(self, unitID, position, mainClass):
self.gameUnits[self.unitUniqueID] = Unit(unitID, position, mainClass)
self.unitUniqueID += 1
#print self.gameUnits,self.gameUnits[self.unitUniqueID]
def addUnit_temp(self, unitID, position, mainClass):
self.gameUnits[self.unitUniqueID] = copy.copy(self.unitDict[self.main['units'][unitID]])
self.gameUnits[self.unitUniqueID].uID = self.unitUniqueID
self.gameUnits[self.unitUniqueID].modelNode = loader.loadModel(self.gameUnits[self.unitUniqueID].model)
self.gameUnits[self.unitUniqueID].modelNode.setName('unit ' + str(self.unitUniqueID).zfill(3))
self.gameUnits[self.unitUniqueID].modelNode.reparentTo(render)
self.gameUnits[self.unitUniqueID].modelNode.setPos(position)
self.gameUnits[self.unitUniqueID].modelNode.setCollideMask(BitMask32.bit(1))
self.gameUnits[self.unitUniqueID].select = OnscreenImage(image = 'data/models/game/selected.png')
self.gameUnits[self.unitUniqueID].select.setScale(float(self.gameUnits[self.unitUniqueID].selectScale))
self.gameUnits[self.unitUniqueID].select.reparentTo(self.gameUnits[self.unitUniqueID].modelNode)
self.gameUnits[self.unitUniqueID].select.setZ(float(self.gameUnits[self.unitUniqueID].modelHeight)/2)
self.gameUnits[self.unitUniqueID].select.setTransparency(TransparencyAttrib.MAlpha)
self.gameUnits[self.unitUniqueID].select.setBillboardPointEye()
self.gameUnits[self.unitUniqueID].select.hide()
self.gameUnits[self.unitUniqueID].groundRay = CollisionRay()
self.gameUnits[self.unitUniqueID].groundRay.setOrigin(0,0,100)
self.gameUnits[self.unitUniqueID].groundRay.setDirection(0,0,-1)
self.gameUnits[self.unitUniqueID].groundCol = CollisionNode('unit Ray')
self.gameUnits[self.unitUniqueID].groundCol.addSolid(self.gameUnits[self.unitUniqueID].groundRay)
self.gameUnits[self.unitUniqueID].groundCol.setTag('units','ray1')
self.gameUnits[self.unitUniqueID].groundCol.setFromCollideMask(BitMask32.bit(0))
# self.gameUnits[self.unitUniqueID].groundCol.setIntoCollideMask(BitMask32.allOff())
self.gameUnits[self.unitUniqueID].groundColNp = self.gameUnits[self.unitUniqueID].modelNode.attachNewNode(self.gameUnits[self.unitUniqueID].groundCol)
self.gameUnits[self.unitUniqueID].groundColNp.setPos(0,0,0)
self.gameUnits[self.unitUniqueID].groundHandler = CollisionHandlerFloor()
self.gameUnits[self.unitUniqueID].groundHandler.setMaxVelocity(100)
base.cTrav2.addCollider(self.gameUnits[self.unitUniqueID].groundColNp, self.gameUnits[self.unitUniqueID].groundHandler)
self.gameUnits[self.unitUniqueID].groundHandler.addCollider(self.gameUnits[self.unitUniqueID].groundColNp, self.gameUnits[self.unitUniqueID].modelNode)
self.gameUnits[self.unitUniqueID].AI = AICharacter(self.gameUnits[self.unitUniqueID].fullName,
self.gameUnits[self.unitUniqueID].modelNode,
self.gameUnits[self.unitUniqueID].mass*2,
self.gameUnits[self.unitUniqueID].startForce*2,
self.gameUnits[self.unitUniqueID].maxForce*2)
self.AIWorld.addAiChar(self.gameUnits[self.unitUniqueID].AI)
self.gameUnits[self.unitUniqueID].AIBehaviors = self.gameUnits[self.unitUniqueID].AI.getAiBehaviors()
if (self.gameUnits[self.unitUniqueID].moveType == 'ground'):
self.gameUnits[self.unitUniqueID].aStar = astar.aStar(self.meshes.landMesh, mainClass)
elif (self.gameUnits[self.unitUniqueID].moveType == 'water'):
self.gameUnits[self.unitUniqueID].aStar = astar.aStar(self.meshes.waterMesh, mainClass)
elif (self.gameUnits[self.unitUniqueID].moveType == 'air'):
self.gameUnits[self.unitUniqueID].aStar = astar.aStar(self.meshes.airMesh, mainClass)
self.unitUniqueID += 1
def moveTo(self, mainClass, destination, uID, userJob = False): # Movees the unit
def getHeight(position, mainClass):
mainClass.heightColNp.setPos(position[0], position[1], 100)
base.cTrav2.traverse(render)
entries = []
for i in range(mainClass.heightHandler.getNumEntries()):
entry = mainClass.heightHandler.getEntry(i)
entries.append(entry)
entries.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
x.getSurfacePoint(render).getZ()))
if (len(entries)>0):
return entries[0].getSurfacePoint(render).getZ()
else:
return 3
# print self.gameUnits[uID].modelNode.getPos()
if (userJob == True):
self.gameUnits[uID].job = ["moving",destination]
taskMgr.add(self.tskMove, "Moving task", extraArgs = [uID])
tempDest = self.gameUnits[uID].aStar.moveTo(self.gameUnits[uID].modelNode, destination)
if (len(tempDest) >= 1):
self.gameUnits[uID].AIBehaviors.pathFollow(1)
self.gameUnits[uID].AIBehaviors.addToPath(Vec3(destination[0], destination[1], getHeight(destination, mainClass)))
for point in tempDest:
self.gameUnits[uID].AIBehaviors.addToPath(Vec3(4*point[0], 4*point[1], getHeight(point, mainClass)-3))
self.gameUnits[uID].AIBehaviors.startFollow()
else:
return None
# else:
def tskMove(self, unitNumber):
if (self.gameUnits[unitNumber].AIBehaviors.behaviorStatus('pathfollow') == 'done') or (self.gameUnits[unitNumber].AIBehaviors.behaviorStatus('pathfollow') == 'paused'):
self.gameUnits[unitNumber].job = False
return Task.done
else:
return Task.cont
def tskWorld(self, task): # A task to update the units in the world
self.AIWorld.update()
base.cTrav2.traverse(render)
for i in self.gameUnits:
# print self.gameUnits[i].uID
if (self.gameUnits[i].AIBehaviors.behaviorStatus('pathfollow') == 'done'):
self.gameUnits[i].AIBehaviors.pauseAi("pathfollow")
elif (self.gameUnits[i].AIBehaviors.behaviorStatus('pathfollow') == 'active'):
self.gameUnits[i].modelNode.setP(0)
return Task.cont
def getNearestAvailable(self, job):
#searchX = 1000
#searchY = 1000
search = 1000
if (len(job.position) == 2):
position = (job.position[0]*4, job.position[1]*4)
else:
position = job.position
for uID in self.gameUnits:
unit = self.gameUnits[uID]
print uID, unit.job
if (unit.job == False) and (unit.dig != None):
tempPos = abs(job.position[0] - unit.modelNode.getX()) + abs(job.position[1]-unit.modelNode.getY())
if (tempPos < search):
search = tempPos
number = unit.uID
#if ((unit.modelNode.getX() + unit.modelNode.getY())/2 < (searchX +searchY)/2):
# searchX = unit.modelNode.getX()
# searchY = unit.modelNode.getY()
# number = unit.uID
return number
# else:
# return None
def doJob(self, job, unitNumber, mainClass):
def getNearTile(tileX,tileY):
print mainClass.mapLoaderClass.tileArray[tileY+1][tileX].solid, mainClass.mapLoaderClass.tileArray[tileY-1][tileX].solid, mainClass.mapLoaderClass.tileArray[tileY][tileX-1].solid, mainClass.mapLoaderClass.tileArray[tileY][tileX+1].solid
if (mainClass.mapLoaderClass.tileArray[tileY+1][tileX].solid == False):
return (tileX,tileY+1)
elif (mainClass.mapLoaderClass.tileArray[tileY-1][tileX].solid == False):
return (tileX,tileY-1)
elif (mainClass.mapLoaderClass.tileArray[tileY][tileX+1].solid == False):
return (tileX+1,tileY)
elif (mainClass.mapLoaderClass.tileArray[tileY][tileX-1].solid == False):
return (tileX-1,tileY)
else:
print 'NOOOO'
def toTile(numberToRound):
numberToRound = round(numberToRound)
if (numberToRound % 4 == 1):
numberToRound -= 1
elif (numberToRound % 4 == 2):
numberToRound -= 2
elif (numberToRound % 4 == 3):
numberToRound += 1
else:
pass
return int(numberToRound)
if (job.type == "drill"):
tempTile = getNearTile(job.position[0],job.position[1])#(toTile(job.position[0]),toTile(job.position[1]))
print '##'+str(tempTile)
if (tempTile != None):
# mainClass.priorities.taskDict.remove(job)
self.moveTo(mainClass, (tempTile[0]*4, tempTile[1]*4), unitNumber)
self.gameUnits[unitNumber].job = ["drill",(job.position[0],job.position[1])]
taskMgr.add(self.mineWall, "Unit finishing", extraArgs = [job, mainClass, unitNumber])
def mineWall(self, job, mainClass, unitNumber):
# print self.gameUnits[unitNumber].job
if (self.gameUnits[unitNumber].job[0] == "drill"):
# print self.gameUnits[unitNumber].AIBehaviors.behaviorStatus('pathfollow')
if (self.gameUnits[unitNumber].AIBehaviors.behaviorStatus('pathfollow') == 'done') or (self.gameUnits[unitNumber].AIBehaviors.behaviorStatus('pathfollow') == 'paused'):
# if (self.gameUnits[unitNumber].job != False):
mainClass.mineWall(mainClass.mapLoaderClass.tileArray[self.gameUnits[unitNumber].job[1][1]][self.gameUnits[unitNumber].job[1][0]])
self.gameUnits[unitNumber].job = False
# else:
# print 'eh?'
return Task.done
else:
return Task.cont
elif (self.gameUnits[unitNumber].job == "selected"):
return Task.cont
else:
mainClass.priorities.taskDict["drill"].append(job)
return Task.done
class Unit:
def __init__(self, unitID, position, mainClass):
# self = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]])
self.model = mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].model
self.fullName = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].fullName)
self.HP = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].HP)
self.info = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].info)
self.moveType = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].moveType)
self.model = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].model)
self.radius = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].radius)
self.mass = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].mass)
self.startForce = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].startForce)
self.maxForce = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].maxForce)
self.dig = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].dig)
self.reinforce = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].reinforce)
self.shovel = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].shovel)
self.hold = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].hold)
self.modelHeight = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].modelHeight)
self.selectScale = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]].selectScale)
self.job = False
self.uID = mainClass.unitHandler.unitUniqueID
print self.uID
self.modelNode = loader.loadModel(self.model)
self.modelNode.setName('unit ' + str(self.uID).zfill(3))
self.modelNode.reparentTo(render)
self.modelNode.setPos(position)
self.modelNode.setCollideMask(BitMask32.bit(1))
self.select = OnscreenImage(image = 'data/models/game/selected.png')
self.select.setScale(float(self.selectScale))
self.select.reparentTo(self.modelNode)
self.select.setZ(float(self.modelHeight)/2)
self.select.setTransparency(TransparencyAttrib.MAlpha)
self.select.setBillboardPointEye()
self.select.hide()
self.groundRay = CollisionRay()
self.groundRay.setOrigin(0,0,100)
self.groundRay.setDirection(0,0,-1)
self.groundCol = CollisionNode('unit Ray')
self.groundCol.addSolid(self.groundRay)
self.groundCol.setTag('units','ray1')
self.groundCol.setFromCollideMask(BitMask32.bit(0))
self.groundCol.setIntoCollideMask(BitMask32.allOff())
self.groundColNp = self.modelNode.attachNewNode(self.groundCol)
self.groundColNp.setPos(0,0,0)
self.groundHandler = CollisionHandlerFloor()
self.groundHandler.setMaxVelocity(100)
base.cTrav2.addCollider(self.groundColNp, self.groundHandler)
self.groundHandler.addCollider(self.groundColNp, self.modelNode)
self.AI = AICharacter(self.fullName,
self.modelNode,
self.mass*2,
self.startForce*2,
self.maxForce*2)
mainClass.unitHandler.AIWorld.addAiChar(self.AI)
self.AIBehaviors = self.AI.getAiBehaviors()
if (self.moveType == 'ground'):
self.aStar = astar.aStar(mainClass.unitHandler.meshes.landMesh, mainClass)
elif (self.moveType == 'water'):
self.aStar = astar.aStar(mainClass.unitHandler.meshes.waterMesh, mainClass)
elif (self.moveType == 'air'):
self.aStar = astar.aStar(mainClass.unitHandler.meshes.airMesh, mainClass)
print self
# mainClass.unitHandler.unitUniqueID += 1