Conditions | 101 |
Total Lines | 386 |
Code Lines | 269 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ige.ospace.ISystem.ISystem.processBATTLEPhase() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # |
||
260 | @public(Const.AL_ADMIN) |
||
261 | def processBATTLEPhase(self, tran, obj, data): |
||
262 | system = obj |
||
263 | #@log.debug('ISystem', 'BATTLE - system', obj.oid) |
||
264 | # we are processing fleets, planets, ... |
||
265 | objects = obj.planets[:] + obj.fleets[:] |
||
266 | # shuffle them to prevent predetermined one-sided battles (temporary hack) |
||
267 | random.shuffle(objects) |
||
268 | # store owners of objects |
||
269 | # find enemies and allies |
||
270 | attack = {} |
||
271 | allies = {} |
||
272 | owners = {} |
||
273 | ownerIDs = {} |
||
274 | systemAtt = {} |
||
275 | systemDef = {} |
||
276 | hasMine = {} |
||
277 | isOwnedObject = 0 |
||
278 | for objID in objects: |
||
279 | attack[objID] = [] |
||
280 | allies[objID] = [] |
||
281 | owner = tran.db[objID].owner |
||
282 | owners[objID] = owner |
||
283 | ownerIDs[owner] = owner |
||
284 | if owner != Const.OID_NONE: |
||
285 | isOwnedObject = 1 |
||
286 | for owner in ownerIDs: |
||
287 | tempAtt, tempDef = self.getSystemCombatBonuses(tran, system, owner) |
||
288 | systemAtt[owner] = tempAtt |
||
289 | systemDef[owner] = tempDef |
||
290 | hasMine[owner] = self.getSystemMineSource(tran, system, owner) |
||
291 | if not isOwnedObject: |
||
292 | #@log.debug('ISystem', 'No combat') |
||
293 | # reset combat counters |
||
294 | system.combatCounter = 0 |
||
295 | return |
||
296 | # first - direct ones |
||
297 | index = 1 |
||
298 | for obj1ID in objects: |
||
299 | obj1 = tran.db[obj1ID] |
||
300 | if obj1.owner == Const.OID_NONE: |
||
301 | index += 1 |
||
302 | continue |
||
303 | commander = tran.db[obj1.owner] |
||
304 | # relationships |
||
305 | #for obj2ID in objects[index:]: |
||
306 | for obj2ID in objects: |
||
307 | obj2 = tran.db[obj2ID] |
||
308 | if obj2.owner == Const.OID_NONE or obj1 is obj2: |
||
309 | continue |
||
310 | if obj1.owner == obj2.owner: |
||
311 | allies[obj1ID].append(obj2ID) |
||
312 | allies[obj2ID].append(obj1ID) |
||
313 | continue |
||
314 | # planet and military object |
||
315 | elif obj1.type == Const.T_PLANET and obj2.isMilitary and \ |
||
316 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, Const.PACT_ALLOW_MILITARY_SHIPS): |
||
317 | #@log.debug("ISystem pl - mil", obj1ID, obj2ID) |
||
318 | if obj2ID not in attack[obj1ID]: |
||
319 | attack[obj1ID].append(obj2ID) |
||
320 | if obj1ID not in attack[obj2ID]: |
||
321 | attack[obj2ID].append(obj1ID) |
||
322 | # planet and civilian object |
||
323 | elif obj1.type == Const.T_PLANET and not obj2.isMilitary and \ |
||
324 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, Const.PACT_ALLOW_CIVILIAN_SHIPS): |
||
325 | #@log.debug("ISystem pl - civ", obj1ID, obj2ID) |
||
326 | if obj2ID not in attack[obj1ID]: |
||
327 | attack[obj1ID].append(obj2ID) |
||
328 | if obj1ID not in attack[obj2ID]: |
||
329 | attack[obj2ID].append(obj1ID) |
||
330 | # military and military object |
||
331 | elif obj1.isMilitary and obj2.isMilitary and \ |
||
332 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, Const.PACT_ALLOW_MILITARY_SHIPS): |
||
333 | #@log.debug("ISystem mil - mil", obj1ID, obj2ID) |
||
334 | if obj2ID not in attack[obj1ID]: |
||
335 | attack[obj1ID].append(obj2ID) |
||
336 | if obj1ID not in attack[obj2ID]: |
||
337 | attack[obj2ID].append(obj1ID) |
||
338 | # military and civilian object |
||
339 | elif obj1.isMilitary and not obj2.isMilitary and \ |
||
340 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, Const.PACT_ALLOW_CIVILIAN_SHIPS): |
||
341 | #@log.debug("ISystem mil - civ", obj1ID, obj2ID) |
||
342 | if obj2ID not in attack[obj1ID]: |
||
343 | attack[obj1ID].append(obj2ID) |
||
344 | if obj1ID not in attack[obj2ID]: |
||
345 | attack[obj2ID].append(obj1ID) |
||
346 | # planet and fleet |
||
347 | #elif obj1.type == Const.T_PLANET and obj2.type == Const.T_FLEET and \ |
||
348 | # self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_MUTUAL_DEFENCE): |
||
349 | # allies[obj1ID].append(obj2ID) |
||
350 | # allies[obj2ID].append(obj1ID) |
||
351 | # fleet and fleet |
||
352 | #elif obj1.type == Const.T_FLEET and obj2.type == Const.T_FLEET and \ |
||
353 | # self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_MUTUAL_OFFENCE): |
||
354 | # allies[obj1ID].append(obj2ID) |
||
355 | # allies[obj2ID].append(obj1ID) |
||
356 | # asteroid |
||
357 | if obj2.type == Const.T_ASTEROID: |
||
358 | attack[obj1ID].append(obj2ID) |
||
359 | attack[obj2ID].append(obj1ID) |
||
360 | index += 1 |
||
361 | #@log.debug('ISystem', 'Targets:', targets) |
||
362 | #@log.debug('ISystem', 'Allies:', allies) |
||
363 | # find indirect a/e |
||
364 | #for objID in objects: |
||
365 | # iTargets = [] |
||
366 | # iAllies = [] |
||
367 | # # find indirect a/e |
||
368 | # todo = allies[objID][:] |
||
369 | # while todo: |
||
370 | # id = todo.pop(0) |
||
371 | # iTargets.extend(targets[id]) |
||
372 | # for tmpID in allies[id]: |
||
373 | # if tmpID not in iAllies: |
||
374 | # todo.append(tmpID) |
||
375 | # iAllies.append(tmpID) |
||
376 | # # remove allies from targets |
||
377 | # for id in iAllies: |
||
378 | # if id in iTargets: |
||
379 | # iTargets.remove(id) |
||
380 | # # IMPORTATNT preffer NOT to fire at possible allies |
||
381 | # # add my targets |
||
382 | # #for id in targets[objID]: |
||
383 | # # if id not in iTargets: |
||
384 | # # iTargets.append(id) |
||
385 | # # that's all folks |
||
386 | # for id in iTargets: |
||
387 | # if objID not in attack[id]: |
||
388 | # attack[id].append(objID) |
||
389 | # if id not in attack[objID]: |
||
390 | # attack[objID].append(id) |
||
391 | # NOT VALID: objects with action ACTION_ATTACK will attack only their targets |
||
392 | # check, if there are any targets |
||
393 | isCombat = 0 |
||
394 | for objID in objects: |
||
395 | if attack[objID]: |
||
396 | isCombat = 1 |
||
397 | break #end loop |
||
398 | if not isCombat: |
||
399 | #@log.debug('ISystem', 'No combat') |
||
400 | # reset combat counters |
||
401 | system.combatCounter = 0 |
||
402 | for fleetID in system.fleets: |
||
403 | tran.db[fleetID].combatCounter = 0 |
||
404 | return |
||
405 | # increase combat counters |
||
406 | system.combatCounter += 1 |
||
407 | for fleetID in system.fleets: |
||
408 | tran.db[fleetID].combatCounter += 1 |
||
409 | # debug |
||
410 | log.debug('ISystem', 'Final attacks in system %d:' % system.oid, attack) |
||
411 | # mines detonate before battle |
||
412 | shots = {} |
||
413 | targets = {} |
||
414 | firing = {} |
||
415 | damageCaused = {} |
||
416 | killsCaused = {} |
||
417 | damageTaken = {} |
||
418 | shipsLost = {} |
||
419 | minesTriggered = {} |
||
420 | fleetOwners = {} |
||
421 | isCombat = False |
||
422 | isMineCombat = False |
||
423 | for owner in ownerIDs: |
||
424 | if owner not in hasMine: # no planets |
||
425 | continue |
||
426 | if not hasMine[owner]: # no planet with control structure |
||
427 | continue |
||
428 | controlPlanetID = hasMine[owner][0] # there is list returned, all planets have same effect |
||
429 | if len(self.getMines(system, owner)) == 0: |
||
430 | continue # no mines, something broke |
||
431 | if len(attack[controlPlanetID]) == 0: |
||
432 | continue # no targets |
||
433 | isMineFired = True |
||
434 | mineTargets = copy.copy(attack[controlPlanetID]) |
||
435 | while isMineFired: |
||
436 | while len(mineTargets) > 0: |
||
437 | targetID = random.choice(mineTargets) # select random target |
||
438 | targetobj = tran.db.get(targetID, None) |
||
439 | try: |
||
440 | if targetobj.type == Const.T_FLEET: |
||
441 | fleetOwners[targetID] = targetobj.owner |
||
442 | break # target found |
||
443 | mineTargets.remove(targetID) # remove an object type that a mine can't hit from the temporary targets list |
||
444 | except: |
||
445 | mineTargets.remove(targetID) # remove a dead fleet from the temporary targets list |
||
446 | |||
447 | if len(mineTargets) == 0: |
||
448 | break # no fleet targets for mines |
||
449 | temp, temp, firing[targetID] = self.cmd(targetobj).getPreCombatData(tran, targetobj) # fix firing for "surrender to" section |
||
|
|||
450 | damage, att, ignoreshield, mineID = self.cmd(obj).fireMine(system, owner) |
||
451 | if not damage: # no more mines |
||
452 | isMineFired = False |
||
453 | break |
||
454 | log.debug('ISystem', 'Mine Shooting (damage, att, ignore shield):', damage, att, ignoreshield) |
||
455 | isMineCombat = True |
||
456 | minesTriggered[mineID] = minesTriggered.get(mineID, 0) + 1 |
||
457 | # Process Combat |
||
458 | # for now we assume only one ship can be destroyed by one mine |
||
459 | dmg, destroyed = self.cmd(targetobj).applyMine(tran, targetobj, att, damage, ignoreshield) |
||
460 | #log.debug('ISystem-Mines', 'Actual Damage Done:',dmg) |
||
461 | if dmg > 0: |
||
462 | damageTaken[targetID] = damageTaken.get(targetID, 0) + dmg |
||
463 | shipsLost[targetID] = shipsLost.get(targetID, 0) + destroyed |
||
464 | killsCaused[mineID] = killsCaused.get(mineID, 0) + destroyed |
||
465 | if dmg > 0: |
||
466 | damageCaused[mineID] = damageCaused.get(mineID, 0) + dmg |
||
467 | # send messages about mine effects to the owner of the minefield |
||
468 | # collect hit players |
||
469 | players = {} |
||
470 | for triggerID in firing.keys(): |
||
471 | players[owners[triggerID]] = None |
||
472 | controllerPlanet = tran.db.get(controlPlanetID, None) |
||
473 | damageCausedSum = 0 |
||
474 | killsCausedSum = 0 |
||
475 | for mineID in damageCaused.keys(): |
||
476 | damageCausedSum = damageCausedSum + damageCaused.get(mineID, 0) |
||
477 | killsCausedSum = killsCausedSum + killsCaused.get(mineID, 0) |
||
478 | Utils.sendMessage(tran, controllerPlanet, Const.MSG_MINES_OWNER_RESULTS, system.oid, (players.keys(),(damageCaused, killsCaused, minesTriggered),damageCausedSum,killsCausedSum)) |
||
479 | # send messages to the players whose fleets got hit by minefields |
||
480 | for targetID in damageTaken.keys(): |
||
481 | targetFleet = tran.db.get(targetID, None) |
||
482 | if targetFleet: |
||
483 | Utils.sendMessage(tran, targetFleet, Const.MSG_MINES_FLEET_RESULTS, system.oid, (damageTaken[targetID], shipsLost[targetID])) |
||
484 | else: |
||
485 | targetFleet = IDataHolder() |
||
486 | targetFleet.oid = fleetOwners[targetID] |
||
487 | Utils.sendMessage(tran, targetFleet, Const.MSG_MINES_FLEET_RESULTS, system.oid, (damageTaken[targetID], shipsLost[targetID])) |
||
488 | Utils.sendMessage(tran, targetFleet, Const.MSG_DESTROYED_FLEET, system.oid, ()) |
||
489 | damageCaused = {} |
||
490 | killsCaused = {} |
||
491 | damageTaken = {} |
||
492 | shipsLost = {} |
||
493 | # now to battle |
||
494 | for objID in objects: |
||
495 | obj = tran.db.get(objID, None) |
||
496 | # get shots from object, should be sorted by weaponClass |
||
497 | # shots = [ shot, ...], shot = (combatAtt, weaponID) |
||
498 | # get target classes and numbers |
||
499 | # (class1, class2, class3, class4) |
||
500 | # cls0 == fighters, cls1 == midships, cls2 == capital ships, cls3 == planet installations |
||
501 | #@log.debug(objID, obj.name, "getting pre combat data") |
||
502 | if obj: # source already destroyed; ignore |
||
503 | shots[objID], targets[objID], firing[objID] = self.cmd(obj).getPreCombatData(tran, obj) |
||
504 | if firing[objID]: |
||
505 | isCombat = True |
||
506 | if not isCombat and not isMineCombat: |
||
507 | # no shots has been fired |
||
508 | #@log.debug('ISystem', 'No combat') |
||
509 | # reset combat counters |
||
510 | system.combatCounter = 0 |
||
511 | for fleetID in system.fleets: |
||
512 | tran.db[fleetID].combatCounter = 0 |
||
513 | return |
||
514 | #@log.debug("Shots:", shots) |
||
515 | #@log.debug("Targets", targets) |
||
516 | if isCombat: |
||
517 | for shotIdx in (3, 2, 1, 0): |
||
518 | for objID in objects: |
||
519 | # obj CAN be deleted at this point |
||
520 | obj = tran.db.get(objID, None) |
||
521 | if obj == None: |
||
522 | continue # source already destroyed; move to next source |
||
523 | # if object is fleet, then it's signature is max |
||
524 | if obj and obj.type == Const.T_FLEET: |
||
525 | obj.signature = Rules.maxSignature |
||
526 | # target preselection |
||
527 | totalClass = [0, 0, 0, 0] |
||
528 | total = 0 |
||
529 | for targetID in attack[objID]: |
||
530 | totalClass[0] += targets[targetID][0] |
||
531 | totalClass[1] += targets[targetID][1] |
||
532 | totalClass[2] += targets[targetID][2] |
||
533 | totalClass[3] += targets[targetID][3] |
||
534 | total = totalClass[0] + totalClass[1] + totalClass[2] + totalClass[3] |
||
535 | # process shots |
||
536 | for combatAtt, weaponID in shots[objID][shotIdx]: |
||
537 | weapon = Rules.techs[weaponID] |
||
538 | weaponClass = weapon.weaponClass |
||
539 | if total == 0: |
||
540 | # there are no targets |
||
541 | break |
||
542 | #@log.debug('ISystem', 'Processing shot', objID, weapon.name, weaponClass) |
||
543 | # process from weaponClass up |
||
544 | # never shoot on smaller ships than weaponClass |
||
545 | applied = 0 |
||
546 | for tmpWpnClass in xrange(weaponClass, 4): |
||
547 | #@log.debug('ISystem', 'Trying target class', tmpWpnClass, totalClass[tmpWpnClass]) |
||
548 | # select target |
||
549 | if totalClass[tmpWpnClass]: |
||
550 | target = Utils.rand(0, totalClass[tmpWpnClass]) |
||
551 | #@log.debug('ISystem', 'Target rnd num', target, totalClass[tmpWpnClass]) |
||
552 | for targetID in attack[objID]: |
||
553 | if target < targets[targetID][tmpWpnClass]: |
||
554 | #@log.debug(objID, 'attacks', targetID, tmpWpnClass) |
||
555 | # targetID can be deleted at this point |
||
556 | anObj = tran.db.get(targetID, None) |
||
557 | if anObj: |
||
558 | dmg, destroyed, destroyedClass = self.cmd(anObj).applyShot(tran, anObj, systemDef[owners[targetID]], combatAtt + systemAtt[owners[objID]], weaponID, tmpWpnClass, target) |
||
559 | #@log.debug("ISystem result", dmg, destroyed, destroyedClass, tmpWpnClass) |
||
560 | #@print objID, 'dmg, destroyed', dmg, destroyed |
||
561 | damageTaken[targetID] = damageTaken.get(targetID, 0) + dmg |
||
562 | if destroyed > 0: |
||
563 | shipsLost[targetID] = shipsLost.get(targetID, 0) + destroyed |
||
564 | total -= destroyed |
||
565 | totalClass[destroyedClass] -= destroyed |
||
566 | if dmg > 0 and obj: |
||
567 | obj.combatExp += dmg |
||
568 | damageCaused[objID] = damageCaused.get(objID, 0) + dmg |
||
569 | applied = 1 |
||
570 | else: |
||
571 | continue # target already destroyed, move to next target |
||
572 | break |
||
573 | else: |
||
574 | #@log.debug('ISystem', 'Lovering target by', targets[targetID][tmpWpnClass]) |
||
575 | target -= targets[targetID][tmpWpnClass] |
||
576 | if applied: |
||
577 | break |
||
578 | # send messages and modify diplomacy relations |
||
579 | # distribute experience pts |
||
580 | for objID in objects: |
||
581 | obj = tran.db.get(objID, None) |
||
582 | if obj: |
||
583 | self.cmd(obj).distributeExp(tran, obj) |
||
584 | if attack[objID]: |
||
585 | source = obj or tran.db[owners[objID]] |
||
586 | # collect players |
||
587 | players = {} |
||
588 | for attackerID in attack[objID]: |
||
589 | players[owners[attackerID]] = None |
||
590 | d1 = damageTaken.get(objID,0) |
||
591 | d2 = damageCaused.get(objID,0) |
||
592 | l = shipsLost.get(objID, 0) |
||
593 | if d1 or d2 or l: |
||
594 | # send only if damage is taken/caused |
||
595 | Utils.sendMessage(tran, source, Const.MSG_COMBAT_RESULTS, system.oid, (d1, d2, l, players.keys())) |
||
596 | if not obj: |
||
597 | # report DESTROYED status |
||
598 | Utils.sendMessage(tran, source, Const.MSG_DESTROYED_FLEET, system.oid, ()) |
||
599 | # modify diplomacy relations |
||
600 | objOwner = tran.db[owners[objID]] |
||
601 | for attackerID in attack[objID]: |
||
602 | attOwner = tran.db.get(owners[attackerID], None) |
||
603 | # owner of the fleet |
||
604 | rel = self.cmd(objOwner).getDiplomacyWith(tran, objOwner, attOwner.oid) |
||
605 | rel.relChng = Rules.relLostWhenAttacked |
||
606 | # attacker |
||
607 | rel = self.cmd(attOwner).getDiplomacyWith(tran, attOwner, objOwner.oid) |
||
608 | rel.rechChng = Rules.relLostWhenAttacked |
||
609 | # check if object surrenders |
||
610 | for objID in objects: |
||
611 | # object surrender IFF it and its allies had target and was not able |
||
612 | # to fire at it, planet is not counted as ally in this case |
||
613 | obj = tran.db.get(objID, None) |
||
614 | if firing[objID] and obj: |
||
615 | continue |
||
616 | surrenderTo = [] |
||
617 | for attID in attack[objID]: |
||
618 | if firing[attID] and tran.db.has_key(attID): |
||
619 | surrenderTo.append(tran.db[attID].owner) |
||
620 | for allyID in allies[objID]: |
||
621 | if not tran.db.has_key(allyID): |
||
622 | continue |
||
623 | ally = tran.db[allyID] |
||
624 | if firing[allyID] and ally.type != Const.T_PLANET: |
||
625 | surrenderTo = [] |
||
626 | break |
||
627 | if surrenderTo: |
||
628 | index = Utils.rand(0, len(surrenderTo)) |
||
629 | if obj: |
||
630 | if self.cmd(obj).surrenderTo(tran, obj, surrenderTo[index]): |
||
631 | winner = tran.db[surrenderTo[index]] |
||
632 | source = tran.db.get(owners[objID], None) |
||
633 | log.debug('ISystem', 'BATTLE - surrender', objID, surrenderTo[index], surrenderTo) |
||
634 | if source: |
||
635 | Utils.sendMessage(tran, source, Const.MSG_COMBAT_LOST, system.oid, winner.oid) |
||
636 | Utils.sendMessage(tran, winner, Const.MSG_COMBAT_WON, system.oid, source.oid) |
||
637 | else: |
||
638 | Utils.sendMessage(tran, winner, Const.MSG_COMBAT_WON, system.oid, obj.oid) |
||
639 | else: |
||
640 | winner = tran.db[surrenderTo[index]] |
||
641 | source = tran.db[owners[objID]] |
||
642 | log.debug('ISystem', 'BATTLE - surrender', objID, surrenderTo[index], surrenderTo) |
||
643 | Utils.sendMessage(tran, source, Const.MSG_COMBAT_LOST, system.oid, winner.oid) |
||
644 | Utils.sendMessage(tran, winner, Const.MSG_COMBAT_WON, system.oid, source.oid) |
||
645 | return |
||
646 | |||
907 |