From d9d6856ce9d8fe8ca0d518cbaef5ef25614e31ef Mon Sep 17 00:00:00 2001
From: Jonathan Harris <jonathan@marginal.org.uk>
Date: Sat, 1 Aug 2015 23:46:05 +0100
Subject: [PATCH] Fix for Electronic Countermeasure in ship loadout.

---
 loadout.py    | 41 +++++++++++++++++++++--------------------
 outfitting.py | 31 ++++++++++++++++++-------------
 2 files changed, 39 insertions(+), 33 deletions(-)

diff --git a/loadout.py b/loadout.py
index a84b26d6..d9c1adb1 100644
--- a/loadout.py
+++ b/loadout.py
@@ -1,4 +1,4 @@
-# Export ship loadout
+# Export ship loadout in E:D Shipyard format
 
 from collections import defaultdict
 import os
@@ -47,34 +47,35 @@ def export(data):
     for slot in sorted(data['ship']['modules']):
 
         v = data['ship']['modules'][slot]
-        if not v or not v.get('module'):
-            continue
         try:
+            if not v: continue
+
             module = outfitting.lookup(v['module'])
             if not module: continue
+
+            cr = class_rating(module)
+
+            # Specials
+            if module['name'] in ['Fuel Tank', 'Cargo Rack']:
+                name = '%s (Capacity: %d)' % (module['name'], 2**int(module['class']))
+            else:
+                name = module['name']
+
+            for s in slot_map:
+                if slot.startswith(s):
+                    loadout[slot_map[s]].append(cr + name)
+                    break
+            else:
+                if slot.startswith('Slot'):
+                    loadout[slot[-1]].append(cr + name)
+                elif __debug__: print 'Loadout: Unknown slot %s' % slot
+
         except AssertionError as e:
             if __debug__: print 'Loadout: %s' % e
             continue	# Silently skip unrecognized modules
         except:
             if __debug__: raise
 
-        cr = class_rating(module)
-
-        # Specials
-        if module['name'] in ['Fuel Tank', 'Cargo Rack']:
-            name = '%s (Capacity: %d)' % (module['name'], 2**int(module['class']))
-        else:
-            name = module['name']
-
-        for s in slot_map:
-            if slot.startswith(s):
-                loadout[slot_map[s]].append(cr + name)
-                break
-        else:
-            if slot.startswith('Slot'):
-                loadout[slot[-1]].append(cr + name)
-            elif __debug__: print 'Loadout: Unknown slot %s' % slot
-
     # Construct description
     string = '[%s]\n' % ship
     for slot in ['H', 'L', 'M', 'S', 'U', None, 'BH', 'RB', 'TM', 'FH', 'EC', 'PC', 'SS', 'FS', None, '9', '8', '7', '6', '5', '4', '3', '2', '1']:
diff --git a/outfitting.py b/outfitting.py
index a12cf3e6..caee04bd 100755
--- a/outfitting.py
+++ b/outfitting.py
@@ -29,7 +29,7 @@ weapon_map = {
     'BeamLaser'                      : 'Beam Laser',
     ('BeamLaser','Heat')             : 'Retributor Beam Laser',
     'Cannon'                         : 'Cannon',
-    'DrunkMissileRack'               : 'Pack-hound Missile Rack',
+    'DrunkMissileRack'               : 'Pack-Hound Missile Rack',
     'DumbfireMissileRack'            : 'Missile Rack',
     'MineLauncher'                   : 'Mine Launcher',
     ('MineLauncher','Impulse')       : 'Impulse Mine Launcher',	# Not seen in game?
@@ -152,14 +152,17 @@ weaponoldvariant_map = {
     'SS' : 'Scatter Spray',
 }
 
+countermeasure_map = {
+    'ChaffLauncher'            : ('Chaff Launcher', 'I'),
+    'ElectronicCountermeasure' : ('Electronic Countermeasure', 'F'),
+    'HeatSinkLauncher'         : ('Heat Sink Launcher', 'I'),
+    'PlasmaPointDefence'       : ('Point Defence', 'I'),
+}
+
 utility_map = {
     'CargoScanner'             : 'Cargo Scanner',
-    'ChaffLauncher'            : 'Chaff Launcher',
     'CloudScanner'             : 'Frame Shift Wake Scanner',
     'CrimeScanner'             : 'Kill Warrant Scanner',
-    'ElectronicCountermeasure' : 'Electronic Countermeasure',
-    'HeatSinkLauncher'         : 'Heat Sink Launcher',
-    'PlasmaPointDefence'       : 'Point Defence',
     'ShieldBooster'            : 'Shield Booster',
 }
 
@@ -172,7 +175,7 @@ rating_map = {
 }
 
 standard_map = {
-    'Armour'           : 'Bulkheads',
+    # 'Armour'         : handled separately
     'Engine'           : 'Thrusters',
     'FuelTank'         : 'Fuel Tank',
     'Hyperdrive'       : 'Frame Shift Drive',
@@ -260,17 +263,19 @@ def lookup(module):
             new['guidance'] = missiletype_map[name[1]]
         new['class'] = weaponclass_map[name[3]]
 
+    # Countermeasures - e.g. Hpt_PlasmaPointDefence_Turret_Tiny
+    elif name[0]=='Hpt' and name[1] in countermeasure_map:
+        new['category'] = 'utility'
+        new['name'], new['rating'] = countermeasure_map[len(name)>4 and (name[1],name[4]) or name[1]]
+        new['class'] = weaponclass_map[name[-1]]
+
     # Utility - e.g. Hpt_CargoScanner_Size0_Class1
     elif name[0]=='Hpt' and name[1] in utility_map:
         new['category'] = 'utility'
         new['name'] = utility_map[len(name)>4 and (name[1],name[4]) or name[1]]
-        if name[-1] in weaponclass_map:	# e.g. Hpt_PlasmaPointDefence_Turret_Tiny
-            new['class'] = weaponclass_map[name[-1]]
-            new['rating'] = 'I'
-        else:
-            if not name[2].startswith('Size') or not name[3].startswith('Class'): raise AssertionError('%s: Unknown class/rating "%s/%s"' % (module['id'], name[2], name[3]))
-            new['class'] = name[2][4:]
-            new['rating'] = rating_map[name[3][5:]]
+        if not name[2].startswith('Size') or not name[3].startswith('Class'): raise AssertionError('%s: Unknown class/rating "%s/%s"' % (module['id'], name[2], name[3]))
+        new['class'] = name[2][4:]
+        new['rating'] = rating_map[name[3][5:]]
 
     elif name[0]=='Hpt':
         raise AssertionError('%s: Unknown weapon "%s"' % (module['id'], name[1]))