protected override void _ActionPerform()
 {
     NewDroidType = new DroidDesign();
     ResultUnit.TypeBase = NewDroidType;
     NewDroidType.CopyDesign(OldDroidType);
     NewDroidType.UpdateAttachments();
 }
Example #2
0
        private clsResult read_FMap_Objects(string text)
        {
            var returnResult = new clsResult("Reading objects", false);
            logger.Info("Reading objects");

            var objectsINI = IniReader.ReadString(text);

            var droidComponentUnknownCount = 0;
            var objectTypeMissingCount = 0;
            var objectPlayerNumInvalidCount = 0;
            var objectPosInvalidCount = 0;
            var designTypeUnspecifiedCount = 0;
            var unknownUnitTypeCount = 0;
            var maxUnknownUnitTypeWarningCount = 16;

            var droidDesign = default(DroidDesign);
            var newObject = default(clsUnit);
            var unitAdd = new clsUnitAdd();
            var unitTypeBase = default(UnitTypeBase);
            var isDesign = default(bool);
            var unitGroup = default(clsUnitGroup);
            var zeroPos = new XYInt(0, 0);
            UInt32 availableID = 0;

            unitAdd.Map = map;

            INIObject iniObject = default(INIObject);
            var invalid = true;
            var iniObjects = new List<INIObject> ();

            foreach (var iniSection in objectsINI) {
                iniObject = new INIObject ();
                iniObject.Type = UnitType.Unspecified;
                iniObject.Health = 1.0D;
                iniObject.WallType = -1;
                iniObject.TurretCodes = new string[Constants.MaxDroidWeapons];
                iniObject.TurretTypes = new enumTurretType[Constants.MaxDroidWeapons];
                for (var i = 0; i < Constants.MaxDroidWeapons; i++) {
                    iniObject.TurretTypes [i] = enumTurretType.Unknown;
                    iniObject.TurretCodes [i] = "";
                }
                invalid = false;
                foreach (var iniToken in iniSection.Data) {
                    if (invalid)
                    {
                        break;
                    }

                    try {
                        switch (iniToken.Name.ToLower()) {
                        case "type":
                            var typeTokens = iniToken.Data.Split (new string[] { ", " }, StringSplitOptions.None);
                            if (typeTokens.Length < 1) {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} type \"{1}\" is not valid.", iniSection.Name, iniToken.Data));
                                logger.Warn ("#{0} type \"{1}\" is not valid.", iniSection.Name, iniToken.Data);
                                continue;
                            }
                            switch (typeTokens [0].ToLower ()) {
                            case "feature":
                                iniObject.Type = UnitType.Feature;
                                iniObject.Code = typeTokens [1];
                                break;
                            case "structure":
                                iniObject.Type = UnitType.PlayerStructure;
                                iniObject.Code = typeTokens [1];
                                break;
                            case "droidtemplate":
                                iniObject.Type = UnitType.PlayerDroid;
                                iniObject.IsTemplate = true;
                                iniObject.Code = typeTokens [1];
                                break;
                            case "droiddesign":
                                iniObject.Type = UnitType.PlayerDroid;
                                break;
                            default:
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            break;
                        case "droidtype":
                            var droidType = App.GetTemplateDroidTypeFromTemplateCode (iniToken.Data);
                            if (droidType == null) {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            break;
                        case "body":
                            iniObject.BodyCode = iniToken.Data;
                            break;
                        case "propulsion":
                            iniObject.PropulsionCode = iniToken.Data;
                            break;
                        case "turrentcount":
                            iniObject.TurretCount = int.Parse(iniToken.Data);
                            if (iniObject.TurretCount < 0 || iniObject.TurretCount > Constants.MaxDroidWeapons) {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            break;
                        case "turret1":
                            var turret1Tokens = iniToken.Data.Split (new string[] { ", " }, StringSplitOptions.None);
                            if (turret1Tokens.Length < 2) {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            iniObject.TurretTypes[0] = App.GetTurretTypeFromName(turret1Tokens[0]);
                            iniObject.TurretCodes[1] = turret1Tokens[1];
                            break;
                        case "turret2":
                            var turret2Tokens = iniToken.Data.Split (new string[] { ", " }, StringSplitOptions.None);
                            if (turret2Tokens.Length < 2) {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            iniObject.TurretTypes[0] = App.GetTurretTypeFromName(turret2Tokens[0]);
                            iniObject.TurretCodes[1] = turret2Tokens[1];
                            break;
                        case "turret3":
                            var turret3Tokens = iniToken.Data.Split (new string[] { ", " }, StringSplitOptions.None);
                            if (turret3Tokens.Length < 2) {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            iniObject.TurretTypes[0] = App.GetTurretTypeFromName(turret3Tokens[0]);
                            iniObject.TurretCodes[1] = turret3Tokens[1];
                            break;
                        case "id":
                            iniObject.ID = uint.Parse(iniToken.Data);
                            break;
                        case "priority":
                            iniObject.Priority = int.Parse(iniToken.Data);
                            break;
                        case "pos":
                            iniObject.Pos = XYInt.FromString(iniToken.Data);
                            break;
                        case "heading":
                            if ( !IOUtil.InvariantParse(iniToken.Data, ref iniObject.Heading) )
                            {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            break;
                        case "unitgroup":
                            iniObject.UnitGroup = iniToken.Data;
                            break;
                        case "health":
                            if ( !IOUtil.InvariantParse(iniToken.Data, ref iniObject.Health) )
                            {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            break;
                        case "walltype":
                            if ( !IOUtil.InvariantParse(iniToken.Data, ref iniObject.WallType) )
                            {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            if (iniObject.WallType < 0 || iniObject.WallType > 3) {
                                iniObject.WallType = -1;
                            }
                            break;
                        case "scriptlabel":
                            iniObject.Label = iniToken.Data;
                            break;
                        default:
                            returnResult.WarningAdd(string.Format("Found an invalid key: {0} = {1}", iniToken.Name, iniToken.Data), false);
                            logger.Warn("Found an invalid key: {0} = {1}", iniToken.Name, iniToken.Data);
                            break;
                        }
                    }
                    catch (Exception ex) {
                        invalid = true;
                        returnResult.WarningAdd(
                            string.Format("#{0} invalid {2}: \"{3}\", got exception: {2}", iniSection.Name, iniToken.Name, iniToken.Data, ex.Message), false);
                        logger.WarnException(string.Format("#{0} invalid {2} \"{1}\"", iniSection.Name, iniToken.Name, iniToken.Data), ex);
                    }
                }

                if (!invalid) {
                    iniObjects.Add (iniObject);
                }
            }

            try {
                availableID = iniObjects.Max (w => w.ID) + 10;
                foreach (var iniObject2 in iniObjects)
                {
                    if ( iniObject2.Pos == null )
                    {
                        objectPosInvalidCount++;
                        continue;
                    }
                    else if ( !App.PosIsWithinTileArea(iniObject2.Pos, zeroPos, map.Terrain.TileSize) )
                    {
                        objectPosInvalidCount++;
                        continue;
                    }

                    unitTypeBase = null;
                    if ( iniObject2.Type != UnitType.Unspecified )
                    {
                        isDesign = false;
                        if ( iniObject2.Type == UnitType.PlayerDroid )
                        {
                            if ( !iniObject2.IsTemplate )
                            {
                                isDesign = true;
                            }
                        }
                        if ( isDesign )
                        {
                            droidDesign = new DroidDesign();
                            droidDesign.TemplateDroidType = iniObject2.TemplateDroidType;
                            if ( droidDesign.TemplateDroidType == null )
                            {
                                droidDesign.TemplateDroidType = App.TemplateDroidType_Droid;
                                designTypeUnspecifiedCount++;
                            }
                            if ( iniObject2.BodyCode != null )
                            {
                                droidDesign.Body = App.ObjectData.FindOrCreateBody(Convert.ToString(iniObject2.BodyCode));
                                if ( droidDesign.Body.IsUnknown )
                                {
                                    droidComponentUnknownCount++;
                                }
                            }
                            if ( iniObject2.PropulsionCode != null )
                            {
                                droidDesign.Propulsion = App.ObjectData.FindOrCreatePropulsion(iniObject2.PropulsionCode);
                                if ( droidDesign.Propulsion.IsUnknown )
                                {
                                    droidComponentUnknownCount++;
                                }
                            }
                            droidDesign.TurretCount = (byte)(iniObject2.TurretCount);
                            if ( iniObject2.TurretCodes[0] != "" )
                            {
                                droidDesign.Turret1 = App.ObjectData.FindOrCreateTurret(iniObject2.TurretTypes[0],
                                    Convert.ToString(iniObject2.TurretCodes[0]));
                                if ( droidDesign.Turret1.IsUnknown )
                                {
                                    droidComponentUnknownCount++;
                                }
                            }
                            if ( iniObject2.TurretCodes[1] != "" )
                            {
                                droidDesign.Turret2 = App.ObjectData.FindOrCreateTurret(iniObject2.TurretTypes[1],
                                    Convert.ToString(iniObject2.TurretCodes[1]));
                                if ( droidDesign.Turret2.IsUnknown )
                                {
                                    droidComponentUnknownCount++;
                                }
                            }
                            if ( iniObject2.TurretCodes[2] != "" )
                            {
                                droidDesign.Turret3 = App.ObjectData.FindOrCreateTurret(iniObject2.TurretTypes[2],
                                    Convert.ToString(iniObject2.TurretCodes[2]));
                                if ( droidDesign.Turret3.IsUnknown )
                                {
                                    droidComponentUnknownCount++;
                                }
                            }
                            droidDesign.UpdateAttachments();
                            unitTypeBase = droidDesign;
                        }
                        else
                        {
                            unitTypeBase = App.ObjectData.FindOrCreateUnitType(iniObject2.Code, iniObject2.Type, iniObject2.WallType);
                            if ( unitTypeBase.IsUnknown )
                            {
                                if ( unknownUnitTypeCount < maxUnknownUnitTypeWarningCount )
                                {
                                    returnResult.WarningAdd("\"{0}\" is not a loaded object.".Format2(iniObject2.Code));
                                }
                                unknownUnitTypeCount++;
                            }
                        }
                    }
                    if ( unitTypeBase == null )
                    {
                        objectTypeMissingCount++;
                        continue;
                    }

                    newObject = new clsUnit();
                    newObject.TypeBase = unitTypeBase;
                    newObject.Pos.Horizontal.X = iniObject2.Pos.X;
                    newObject.Pos.Horizontal.Y = iniObject2.Pos.Y;
                    newObject.Health = iniObject2.Health;
                    newObject.SavePriority = iniObject2.Priority;
                    newObject.Rotation = (int)(iniObject2.Heading);
                    if ( newObject.Rotation >= 360 )
                    {
                        newObject.Rotation -= 360;
                    }
                    if ( iniObject2.UnitGroup == null || iniObject2.UnitGroup == "" )
                    {
                        if ( iniObject2.Type != UnitType.Feature )
                        {
                            objectPlayerNumInvalidCount++;
                        }
                        newObject.UnitGroup = map.ScavengerUnitGroup;
                    }
                    else
                    {
                        if ( iniObject2.UnitGroup.ToLower() == "scavenger" )
                        {
                            newObject.UnitGroup = map.ScavengerUnitGroup;
                        }
                        else
                        {
                            UInt32 PlayerNum = 0;
                            try
                            {
                                if ( !IOUtil.InvariantParse(iniObject2.UnitGroup, ref PlayerNum) )
                                {
                                    throw (new Exception());
                                }
                                if ( PlayerNum < Constants.PlayerCountMax )
                                {
                                    unitGroup = map.UnitGroups[Convert.ToInt32(PlayerNum)];
                                }
                                else
                                {
                                    unitGroup = map.ScavengerUnitGroup;
                                    objectPlayerNumInvalidCount++;
                                }
                            }
                            catch ( Exception )
                            {
                                objectPlayerNumInvalidCount++;
                                unitGroup = map.ScavengerUnitGroup;
                            }
                            newObject.UnitGroup = unitGroup;
                        }
                    }
                    if ( iniObject2.ID == 0U )
                    {
                        // iniObject2.ID = availableID;
                        App.ZeroIDWarning(newObject, iniObject2.ID, returnResult);
                    }
                    unitAdd.NewUnit = newObject;
                    unitAdd.ID = iniObject2.ID;
                    unitAdd.Label = iniObject2.Label;
                    unitAdd.Perform();
                    App.ErrorIDChange(iniObject2.ID, newObject, "Read_FMap_Objects");
                    if ( availableID == iniObject2.ID )
                    {
                        availableID = newObject.ID + 1U;
                    }
                }

                if ( unknownUnitTypeCount > maxUnknownUnitTypeWarningCount )
                {
                    returnResult.WarningAdd (string.Format ("{0} objects were not in the loaded object data.", unknownUnitTypeCount));
                }
                if ( objectTypeMissingCount > 0 )
                {
                    returnResult.WarningAdd (string.Format ("{0} objects did not specify a type and were ignored.", objectTypeMissingCount));
                }
                if ( droidComponentUnknownCount > 0 )
                {
                    returnResult.WarningAdd (string.Format ("{0} components used by droids were loaded as unknowns.", droidComponentUnknownCount));
                }
                if ( objectPlayerNumInvalidCount > 0 )
                {
                    returnResult.WarningAdd (string.Format ("{0} objects had an invalid player number and were set to player 0.", objectPlayerNumInvalidCount));
                }
                if ( objectPosInvalidCount > 0 )
                {
                    returnResult.WarningAdd (string.Format ("{0} objects had a position that was off-map and were ignored.", objectPosInvalidCount));
                }
                if ( designTypeUnspecifiedCount > 0 )
                {
                    returnResult.WarningAdd (string.Format ("{0} designed droids did not specify a template droid type and were set to droid.", designTypeUnspecifiedCount));
                }
            }
            catch (Exception ex) {
                Debugger.Break ();
                returnResult.ProblemAdd (string.Format("Got a exeption: {0}", ex.Message));
                logger.ErrorException ("Got exception", ex);
            }

            return returnResult;
        }