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 | # |
||
405 | @public(AL_ADMIN) |
||
406 | def processBATTLEPhase(self, tran, obj, data): |
||
407 | system = obj |
||
408 | #@log.debug('ISystem', 'BATTLE - system', obj.oid) |
||
409 | # we are processing fleets, planets, ... |
||
410 | objects = obj.planets[:] + obj.fleets[:] |
||
411 | # shuffle them to prevent predetermined one-sided battles (temporary hack) |
||
412 | random.shuffle(objects) |
||
413 | # store owners of objects |
||
414 | # find enemies and allies |
||
415 | attack = {} |
||
416 | allies = {} |
||
417 | owners = {} |
||
418 | ownerIDs = {} |
||
419 | systemAtt = {} |
||
420 | systemDef = {} |
||
421 | hasMine = {} |
||
422 | isOwnedObject = 0 |
||
423 | for objID in objects: |
||
424 | attack[objID] = [] |
||
425 | allies[objID] = [] |
||
426 | owner = tran.db[objID].owner |
||
427 | owners[objID] = owner |
||
428 | ownerIDs[owner] = owner |
||
429 | if owner != OID_NONE: |
||
430 | isOwnedObject = 1 |
||
431 | for owner in ownerIDs: |
||
432 | tempAtt, tempDef = self.getSystemCombatBonuses(tran, system, owner) |
||
433 | systemAtt[owner] = tempAtt |
||
434 | systemDef[owner] = tempDef |
||
435 | hasMine[owner] = self.getSystemMineSource(tran, system, owner) |
||
436 | if not isOwnedObject: |
||
437 | #@log.debug('ISystem', 'No combat') |
||
438 | # reset combat counters |
||
439 | system.combatCounter = 0 |
||
440 | return |
||
441 | # first - direct ones |
||
442 | index = 1 |
||
443 | for obj1ID in objects: |
||
444 | obj1 = tran.db[obj1ID] |
||
445 | if obj1.owner == OID_NONE: |
||
446 | index += 1 |
||
447 | continue |
||
448 | commander = tran.db[obj1.owner] |
||
449 | # relationships |
||
450 | #for obj2ID in objects[index:]: |
||
451 | for obj2ID in objects: |
||
452 | obj2 = tran.db[obj2ID] |
||
453 | if obj2.owner == OID_NONE or obj1 is obj2: |
||
454 | continue |
||
455 | if obj1.owner == obj2.owner: |
||
456 | allies[obj1ID].append(obj2ID) |
||
457 | allies[obj2ID].append(obj1ID) |
||
458 | continue |
||
459 | # planet and military object |
||
460 | elif obj1.type == T_PLANET and obj2.isMilitary and \ |
||
461 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_ALLOW_MILITARY_SHIPS): |
||
462 | #@log.debug("ISystem pl - mil", obj1ID, obj2ID) |
||
463 | if obj2ID not in attack[obj1ID]: |
||
464 | attack[obj1ID].append(obj2ID) |
||
465 | if obj1ID not in attack[obj2ID]: |
||
466 | attack[obj2ID].append(obj1ID) |
||
467 | # planet and civilian object |
||
468 | elif obj1.type == T_PLANET and not obj2.isMilitary and \ |
||
469 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_ALLOW_CIVILIAN_SHIPS): |
||
470 | #@log.debug("ISystem pl - civ", obj1ID, obj2ID) |
||
471 | if obj2ID not in attack[obj1ID]: |
||
472 | attack[obj1ID].append(obj2ID) |
||
473 | if obj1ID not in attack[obj2ID]: |
||
474 | attack[obj2ID].append(obj1ID) |
||
475 | # military and military object |
||
476 | elif obj1.isMilitary and obj2.isMilitary and \ |
||
477 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_ALLOW_MILITARY_SHIPS): |
||
478 | #@log.debug("ISystem mil - mil", obj1ID, obj2ID) |
||
479 | if obj2ID not in attack[obj1ID]: |
||
480 | attack[obj1ID].append(obj2ID) |
||
481 | if obj1ID not in attack[obj2ID]: |
||
482 | attack[obj2ID].append(obj1ID) |
||
483 | # military and civilian object |
||
484 | elif obj1.isMilitary and not obj2.isMilitary and \ |
||
485 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_ALLOW_CIVILIAN_SHIPS): |
||
486 | #@log.debug("ISystem mil - civ", obj1ID, obj2ID) |
||
487 | if obj2ID not in attack[obj1ID]: |
||
488 | attack[obj1ID].append(obj2ID) |
||
489 | if obj1ID not in attack[obj2ID]: |
||
490 | attack[obj2ID].append(obj1ID) |
||
491 | # planet and fleet |
||
492 | #elif obj1.type == T_PLANET and obj2.type == T_FLEET and \ |
||
493 | # self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_MUTUAL_DEFENCE): |
||
494 | # allies[obj1ID].append(obj2ID) |
||
495 | # allies[obj2ID].append(obj1ID) |
||
496 | # fleet and fleet |
||
497 | #elif obj1.type == T_FLEET and obj2.type == T_FLEET and \ |
||
498 | # self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_MUTUAL_OFFENCE): |
||
499 | # allies[obj1ID].append(obj2ID) |
||
500 | # allies[obj2ID].append(obj1ID) |
||
501 | # asteroid |
||
502 | if obj2.type == T_ASTEROID: |
||
503 | attack[obj1ID].append(obj2ID) |
||
504 | attack[obj2ID].append(obj1ID) |
||
505 | index += 1 |
||
506 | #@log.debug('ISystem', 'Targets:', targets) |
||
507 | #@log.debug('ISystem', 'Allies:', allies) |
||
508 | # find indirect a/e |
||
509 | #for objID in objects: |
||
510 | # iTargets = [] |
||
511 | # iAllies = [] |
||
512 | # # find indirect a/e |
||
513 | # todo = allies[objID][:] |
||
514 | # while todo: |
||
515 | # id = todo.pop(0) |
||
516 | # iTargets.extend(targets[id]) |
||
517 | # for tmpID in allies[id]: |
||
518 | # if tmpID not in iAllies: |
||
519 | # todo.append(tmpID) |
||
520 | # iAllies.append(tmpID) |
||
521 | # # remove allies from targets |
||
522 | # for id in iAllies: |
||
523 | # if id in iTargets: |
||
524 | # iTargets.remove(id) |
||
525 | # # IMPORTATNT preffer NOT to fire at possible allies |
||
526 | # # add my targets |
||
527 | # #for id in targets[objID]: |
||
528 | # # if id not in iTargets: |
||
529 | # # iTargets.append(id) |
||
530 | # # that's all folks |
||
531 | # for id in iTargets: |
||
532 | # if objID not in attack[id]: |
||
533 | # attack[id].append(objID) |
||
534 | # if id not in attack[objID]: |
||
535 | # attack[objID].append(id) |
||
536 | # NOT VALID: objects with action ACTION_ATTACK will attack only their targets |
||
537 | # check, if there are any targets |
||
538 | isCombat = 0 |
||
539 | for objID in objects: |
||
540 | if attack[objID]: |
||
541 | isCombat = 1 |
||
542 | break #end loop |
||
543 | if not isCombat: |
||
544 | #@log.debug('ISystem', 'No combat') |
||
545 | # reset combat counters |
||
546 | system.combatCounter = 0 |
||
547 | for fleetID in system.fleets: |
||
548 | tran.db[fleetID].combatCounter = 0 |
||
549 | return |
||
550 | # increase combat counters |
||
551 | system.combatCounter += 1 |
||
552 | for fleetID in system.fleets: |
||
553 | tran.db[fleetID].combatCounter += 1 |
||
554 | # debug |
||
555 | log.debug('ISystem', 'Final attacks in system %d:' % system.oid, attack) |
||
556 | # mines detonate before battle |
||
557 | shots = {} |
||
558 | targets = {} |
||
559 | firing = {} |
||
560 | damageCaused = {} |
||
561 | killsCaused = {} |
||
562 | damageTaken = {} |
||
563 | shipsLost = {} |
||
564 | minesTriggered = {} |
||
565 | fleetOwners = {} |
||
566 | isCombat = False |
||
567 | isMineCombat = False |
||
568 | for owner in ownerIDs: |
||
569 | if owner not in hasMine: # no planets |
||
570 | continue |
||
571 | if not hasMine[owner]: # no planet with control structure |
||
572 | continue |
||
573 | controlPlanetID = hasMine[owner][0] # there is list returned, all planets have same effect |
||
574 | if len(self.getMines(system, owner)) == 0: |
||
575 | continue # no mines, something broke |
||
576 | if len(attack[controlPlanetID]) == 0: |
||
577 | continue # no targets |
||
578 | isMineFired = True |
||
579 | mineTargets = copy.copy(attack[controlPlanetID]) |
||
580 | while isMineFired: |
||
581 | while len(mineTargets) > 0: |
||
582 | targetID = random.choice(mineTargets) # select random target |
||
583 | targetobj = tran.db.get(targetID, None) |
||
584 | try: |
||
585 | if targetobj.type == T_FLEET: |
||
586 | fleetOwners[targetID] = targetobj.owner |
||
587 | break # target found |
||
588 | mineTargets.remove(targetID) # remove an object type that a mine can't hit from the temporary targets list |
||
589 | except: |
||
590 | mineTargets.remove(targetID) # remove a dead fleet from the temporary targets list |
||
591 | |||
592 | if len(mineTargets) == 0: |
||
593 | break # no fleet targets for mines |
||
594 | temp, temp, firing[targetID] = self.cmd(targetobj).getPreCombatData(tran, targetobj) # fix firing for "surrender to" section |
||
595 | damage, att, ignoreshield, mineID = self.cmd(obj).fireMine(system, owner) |
||
596 | if not damage: # no more mines |
||
597 | isMineFired = False |
||
598 | break |
||
599 | log.debug('ISystem', 'Mine Shooting (damage, att, ignore shield):', damage, att, ignoreshield) |
||
600 | isMineCombat = True |
||
601 | minesTriggered[mineID] = minesTriggered.get(mineID, 0) + 1 |
||
602 | # Process Combat |
||
603 | # for now we assume only one ship can be destroyed by one mine |
||
604 | dmg, destroyed = self.cmd(targetobj).applyMine(tran, targetobj, att, damage, ignoreshield) |
||
605 | #log.debug('ISystem-Mines', 'Actual Damage Done:',dmg) |
||
606 | if dmg > 0: |
||
607 | damageTaken[targetID] = damageTaken.get(targetID, 0) + dmg |
||
608 | shipsLost[targetID] = shipsLost.get(targetID, 0) + destroyed |
||
609 | killsCaused[mineID] = killsCaused.get(mineID, 0) + destroyed |
||
610 | if dmg > 0: |
||
611 | damageCaused[mineID] = damageCaused.get(mineID, 0) + dmg |
||
612 | # send messages about mine effects to the owner of the minefield |
||
613 | # collect hit players |
||
614 | players = {} |
||
615 | for triggerID in firing.keys(): |
||
616 | players[owners[triggerID]] = None |
||
617 | controllerPlanet = tran.db.get(controlPlanetID, None) |
||
618 | damageCausedSum = 0 |
||
619 | killsCausedSum = 0 |
||
620 | for mineID in damageCaused.keys(): |
||
621 | damageCausedSum = damageCausedSum + damageCaused.get(mineID, 0) |
||
622 | killsCausedSum = killsCausedSum + killsCaused.get(mineID, 0) |
||
623 | Utils.sendMessage(tran, controllerPlanet, MSG_MINES_OWNER_RESULTS, system.oid, (players.keys(),(damageCaused, killsCaused, minesTriggered),damageCausedSum,killsCausedSum)) |
||
624 | # send messages to the players whose fleets got hit by minefields |
||
625 | for targetID in damageTaken.keys(): |
||
626 | targetFleet = tran.db.get(targetID, None) |
||
627 | if targetFleet: |
||
628 | Utils.sendMessage(tran, targetFleet, MSG_MINES_FLEET_RESULTS, system.oid, (damageTaken[targetID], shipsLost[targetID])) |
||
629 | else: |
||
630 | targetFleet = IDataHolder() |
||
631 | targetFleet.oid = fleetOwners[targetID] |
||
632 | Utils.sendMessage(tran, targetFleet, MSG_MINES_FLEET_RESULTS, system.oid, (damageTaken[targetID], shipsLost[targetID])) |
||
633 | Utils.sendMessage(tran, targetFleet, MSG_DESTROYED_FLEET, system.oid, ()) |
||
634 | damageCaused = {} |
||
635 | killsCaused = {} |
||
636 | damageTaken = {} |
||
637 | shipsLost = {} |
||
638 | # now to battle |
||
639 | for objID in objects: |
||
640 | obj = tran.db.get(objID, None) |
||
641 | # get shots from object, should be sorted by weaponClass |
||
642 | # shots = [ shot, ...], shot = (combatAtt, weaponID) |
||
643 | # get target classes and numbers |
||
644 | # (class1, class2, class3, class4) |
||
645 | # cls0 == fighters, cls1 == midships, cls2 == capital ships, cls3 == planet installations |
||
646 | #@log.debug(objID, obj.name, "getting pre combat data") |
||
647 | if obj: # source already destroyed; ignore |
||
648 | shots[objID], targets[objID], firing[objID] = self.cmd(obj).getPreCombatData(tran, obj) |
||
649 | if firing[objID]: |
||
650 | isCombat = True |
||
651 | if not isCombat and not isMineCombat: |
||
652 | # no shots has been fired |
||
653 | #@log.debug('ISystem', 'No combat') |
||
654 | # reset combat counters |
||
655 | system.combatCounter = 0 |
||
656 | for fleetID in system.fleets: |
||
657 | tran.db[fleetID].combatCounter = 0 |
||
658 | return |
||
659 | #@log.debug("Shots:", shots) |
||
660 | #@log.debug("Targets", targets) |
||
661 | if isCombat: |
||
662 | for shotIdx in (3, 2, 1, 0): |
||
663 | for objID in objects: |
||
664 | # obj CAN be deleted at this point |
||
665 | obj = tran.db.get(objID, None) |
||
666 | if obj == None: |
||
667 | continue # source already destroyed; move to next source |
||
668 | # if object is fleet, then it's signature is max |
||
669 | if obj and obj.type == T_FLEET: |
||
670 | obj.signature = Rules.maxSignature |
||
671 | # target preselection |
||
672 | totalClass = [0, 0, 0, 0] |
||
673 | total = 0 |
||
674 | for targetID in attack[objID]: |
||
675 | totalClass[0] += targets[targetID][0] |
||
676 | totalClass[1] += targets[targetID][1] |
||
677 | totalClass[2] += targets[targetID][2] |
||
678 | totalClass[3] += targets[targetID][3] |
||
679 | total = totalClass[0] + totalClass[1] + totalClass[2] + totalClass[3] |
||
680 | # process shots |
||
681 | for combatAtt, weaponID in shots[objID][shotIdx]: |
||
682 | weapon = Rules.techs[weaponID] |
||
683 | weaponClass = weapon.weaponClass |
||
684 | if total == 0: |
||
685 | # there are no targets |
||
686 | break |
||
687 | #@log.debug('ISystem', 'Processing shot', objID, weapon.name, weaponClass) |
||
688 | # process from weaponClass up |
||
689 | # never shoot on smaller ships than weaponClass |
||
690 | applied = 0 |
||
691 | for tmpWpnClass in xrange(weaponClass, 4): |
||
692 | #@log.debug('ISystem', 'Trying target class', tmpWpnClass, totalClass[tmpWpnClass]) |
||
693 | # select target |
||
694 | if totalClass[tmpWpnClass]: |
||
695 | target = Utils.rand(0, totalClass[tmpWpnClass]) |
||
696 | #@log.debug('ISystem', 'Target rnd num', target, totalClass[tmpWpnClass]) |
||
697 | for targetID in attack[objID]: |
||
698 | if target < targets[targetID][tmpWpnClass]: |
||
699 | #@log.debug(objID, 'attacks', targetID, tmpWpnClass) |
||
700 | # targetID can be deleted at this point |
||
701 | anObj = tran.db.get(targetID, None) |
||
702 | if anObj: |
||
703 | dmg, destroyed, destroyedClass = self.cmd(anObj).applyShot(tran, anObj, systemDef[owners[targetID]], combatAtt + systemAtt[owners[objID]], weaponID, tmpWpnClass, target) |
||
704 | #@log.debug("ISystem result", dmg, destroyed, destroyedClass, tmpWpnClass) |
||
705 | #@print objID, 'dmg, destroyed', dmg, destroyed |
||
706 | damageTaken[targetID] = damageTaken.get(targetID, 0) + dmg |
||
707 | if destroyed > 0: |
||
708 | shipsLost[targetID] = shipsLost.get(targetID, 0) + destroyed |
||
709 | total -= destroyed |
||
710 | totalClass[destroyedClass] -= destroyed |
||
711 | if dmg > 0 and obj: |
||
712 | obj.combatExp += dmg |
||
713 | damageCaused[objID] = damageCaused.get(objID, 0) + dmg |
||
714 | applied = 1 |
||
715 | else: |
||
716 | continue # target already destroyed, move to next target |
||
717 | break |
||
718 | else: |
||
719 | #@log.debug('ISystem', 'Lovering target by', targets[targetID][tmpWpnClass]) |
||
720 | target -= targets[targetID][tmpWpnClass] |
||
721 | if applied: |
||
722 | break |
||
723 | # send messages and modify diplomacy relations |
||
724 | # distribute experience pts |
||
725 | for objID in objects: |
||
726 | obj = tran.db.get(objID, None) |
||
727 | if obj: |
||
728 | self.cmd(obj).distributeExp(tran, obj) |
||
729 | if attack[objID]: |
||
730 | source = obj or tran.db[owners[objID]] |
||
731 | # collect players |
||
732 | players = {} |
||
733 | for attackerID in attack[objID]: |
||
734 | players[owners[attackerID]] = None |
||
735 | d1 = damageTaken.get(objID,0) |
||
736 | d2 = damageCaused.get(objID,0) |
||
737 | l = shipsLost.get(objID, 0) |
||
738 | if d1 or d2 or l: |
||
739 | # send only if damage is taken/caused |
||
740 | Utils.sendMessage(tran, source, MSG_COMBAT_RESULTS, system.oid, (d1, d2, l, players.keys())) |
||
741 | if not obj: |
||
742 | # report DESTROYED status |
||
743 | Utils.sendMessage(tran, source, MSG_DESTROYED_FLEET, system.oid, ()) |
||
744 | # modify diplomacy relations |
||
745 | objOwner = tran.db[owners[objID]] |
||
746 | for attackerID in attack[objID]: |
||
747 | attOwner = tran.db.get(owners[attackerID], None) |
||
748 | # owner of the fleet |
||
749 | rel = self.cmd(objOwner).getDiplomacyWith(tran, objOwner, attOwner.oid) |
||
750 | rel.relChng = Rules.relLostWhenAttacked |
||
751 | # attacker |
||
752 | rel = self.cmd(attOwner).getDiplomacyWith(tran, attOwner, objOwner.oid) |
||
753 | rel.rechChng = Rules.relLostWhenAttacked |
||
754 | # check if object surrenders |
||
755 | for objID in objects: |
||
756 | # object surrender IFF it and its allies had target and was not able |
||
757 | # to fire at it, planet is not counted as ally in this case |
||
758 | obj = tran.db.get(objID, None) |
||
759 | if firing[objID] and obj: |
||
760 | continue |
||
761 | surrenderTo = [] |
||
762 | for attID in attack[objID]: |
||
763 | if firing[attID] and tran.db.has_key(attID): |
||
764 | surrenderTo.append(tran.db[attID].owner) |
||
765 | for allyID in allies[objID]: |
||
766 | if not tran.db.has_key(allyID): |
||
767 | continue |
||
768 | ally = tran.db[allyID] |
||
769 | if firing[allyID] and ally.type != T_PLANET: |
||
770 | surrenderTo = [] |
||
771 | break |
||
772 | if surrenderTo: |
||
773 | index = Utils.rand(0, len(surrenderTo)) |
||
774 | if obj: |
||
775 | if self.cmd(obj).surrenderTo(tran, obj, surrenderTo[index]): |
||
776 | winner = tran.db[surrenderTo[index]] |
||
777 | source = tran.db.get(owners[objID], None) |
||
778 | log.debug('ISystem', 'BATTLE - surrender', objID, surrenderTo[index], surrenderTo) |
||
779 | if source: |
||
780 | Utils.sendMessage(tran, source, MSG_COMBAT_LOST, system.oid, winner.oid) |
||
781 | Utils.sendMessage(tran, winner, MSG_COMBAT_WON, system.oid, source.oid) |
||
782 | else: |
||
783 | Utils.sendMessage(tran, winner, MSG_COMBAT_WON, system.oid, obj.oid) |
||
784 | else: |
||
785 | winner = tran.db[surrenderTo[index]] |
||
786 | source = tran.db[owners[objID]] |
||
787 | log.debug('ISystem', 'BATTLE - surrender', objID, surrenderTo[index], surrenderTo) |
||
788 | Utils.sendMessage(tran, source, MSG_COMBAT_LOST, system.oid, winner.oid) |
||
789 | Utils.sendMessage(tran, winner, MSG_COMBAT_WON, system.oid, source.oid) |
||
790 | return |
||
791 | |||
1060 |