Example #1
0
        public void GetMultiblockConfig()
        {
#warning TODO: Restrict barrel placement to directly in front of the main body

            List <Sandbox.ModAPI.IMySlimBlock> connectedBlocks = new List <Sandbox.ModAPI.IMySlimBlock>();

            Sandbox.ModAPI.IMyCubeGrid grid = (Sandbox.ModAPI.IMyCubeGrid)MDBody.CubeGrid;
            Matrix          localMatrix     = MDBody.LocalMatrix;
            List <Vector3I> searchQueue     = new List <Vector3I>()
            {
                FindOffset(localMatrix, new Vector3I(2, 0, 1)),  //right port
                FindOffset(localMatrix, new Vector3I(-2, 0, 1)), //left port
                FindOffset(localMatrix, new Vector3I(0, 0, 3)),  //front port
            };

            while (searchQueue.Count > 0)
            {
                Vector3I searchLocation = searchQueue[0];

                Sandbox.ModAPI.IMySlimBlock block = grid.GetCubeBlock(searchLocation);

                if (block != null)
                {
                    if (!connectedBlocks.Contains(block))
                    {
                        if (block.GetObjectBuilder().SubtypeId.ToString().Contains("MassDriver"))
                        {
                            connectedBlocks.Add(block);
                        }

                        switch (block.GetObjectBuilder().SubtypeId.ToString())
                        {
                        case "MassDriverCord":
                            searchQueue.Add(FindOffset(block.FatBlock.LocalMatrix, new Vector3I(0, 0, 1)));       //front port
                            searchQueue.Add(FindOffset(block.FatBlock.LocalMatrix, new Vector3I(0, 0, -1)));      //back port
                            break;

                        case "MassDriverCordSupport":
                            searchQueue.Add(FindOffset(block.FatBlock.LocalMatrix, new Vector3I(0, 0, 1)));       //front port
                            searchQueue.Add(FindOffset(block.FatBlock.LocalMatrix, new Vector3I(0, 0, -1)));      //back port
                            break;

                        case "MassDriverCordTurn":
                            searchQueue.Add(FindOffset(block.FatBlock.LocalMatrix, new Vector3I(1, 0, 0)));       //right port
                            searchQueue.Add(FindOffset(block.FatBlock.LocalMatrix, new Vector3I(0, -1, 0)));      //bottom port
                            break;

                        case "MassDriverCapacitor":
                            //need to change the translation matrix a little because of the block's even-block-count depth and height
                            Vector3 fixedCapTranslation = block.FatBlock.LocalMatrix.Translation + block.FatBlock.LocalMatrix.Forward + block.FatBlock.LocalMatrix.Down;
                            Matrix  fixedCapMatrix      = block.FatBlock.LocalMatrix;
                            fixedCapMatrix.Translation = fixedCapTranslation;

                            searchQueue.Add(FindOffset(fixedCapMatrix, new Vector3I(0, 0, 2)));      //front port
                            searchQueue.Add(FindOffset(fixedCapMatrix, new Vector3I(0, 0, -3)));     //back port
                            searchQueue.Add(FindOffset(fixedCapMatrix, new Vector3I(0, -1, 0)));     //bottom port
                            break;

                        case "MassDriverBarrelSector":
                            searchQueue.Add(FindOffset(block.FatBlock.LocalMatrix, new Vector3I(0, 0, 2)));      //front port
                            searchQueue.Add(FindOffset(block.FatBlock.LocalMatrix, new Vector3I(0, 0, -2)));     //back port
                            break;

                        case "MassDriverBarrelTip":
                            searchQueue.Add(FindOffset(block.FatBlock.LocalMatrix, new Vector3I(0, 0, -2)));     //back port
                            break;

                        default:

                            break;
                        }
                    }
                }

                searchQueue.RemoveAt(0);
            }

            //count attached parts
            //
            int barrelCount      = connectedBlocks.FindAll(b => b.GetObjectBuilder().SubtypeId.ToString().Contains("MassDriverBarrel")).Count;
            int compulsatorCount = connectedBlocks.FindAll(b => b.GetObjectBuilder().SubtypeId.ToString() == "MassDriverCapacitor").Count;

            //build list of connected batteries
            //
            compulsators = new List </*Sandbox.ModAPI.*/ IMyBatteryBlock>();
            foreach (var batt in connectedBlocks.FindAll(b => b.GetObjectBuilder().SubtypeId.ToString() == "MassDriverCapacitor"))
            {
                compulsators.Add(batt.FatBlock as /*Sandbox.ModAPI.*/ IMyBatteryBlock);
            }

            //update custom name and info instance
            //
            MDBody.SetCustomName("Mass Driver (" + barrelCount.ToString() + "B|" + compulsatorCount.ToString() + "C)");

            if (Data.Drivers.Find(d => d.Id == MDBody.EntityId) == null) //add entry for damage handler
            {
                Data.Drivers.Add(new MDInfo(MDBody.EntityId, compulsatorCount));
            }
            else
            {
                MDInfo existingEntry = Data.Drivers.Find(d => d.Id == MDBody.EntityId);
                existingEntry.DamageMultiplier = compulsatorCount;
            }

            MyAPIGateway.Utilities.ShowMessage("", Data.Drivers.Count().ToString());
        }