CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

House.cs (4986B)


      1 //
      2 // Copyright 2020 Electronic Arts Inc.
      3 //
      4 // The Command & Conquer Map Editor and corresponding source code is free 
      5 // software: you can redistribute it and/or modify it under the terms of 
      6 // the GNU General Public License as published by the Free Software Foundation, 
      7 // either version 3 of the License, or (at your option) any later version.
      8 
      9 // The Command & Conquer Map Editor and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 using MobiusEditor.Utility;
     16 using System;
     17 using System.Collections;
     18 using System.Collections.Generic;
     19 using System.ComponentModel;
     20 using System.Globalization;
     21 using System.Linq;
     22 
     23 namespace MobiusEditor.Model
     24 {
     25     public struct AlliesMask : IEnumerable<int>
     26     {
     27         public int Value { get; private set; }
     28 
     29         public AlliesMask(int value)
     30         {
     31             Value = value;
     32         }
     33 
     34         public void Set(int id)
     35         {
     36             if ((id < 0) || (id > 31))
     37             {
     38                 throw new ArgumentOutOfRangeException();
     39             }
     40 
     41             Value |= (1 << id);
     42         }
     43 
     44         public void Clear(int id)
     45         {
     46             if ((id < 0) || (id > 31))
     47             {
     48                 throw new ArgumentOutOfRangeException();
     49             }
     50 
     51             Value &= ~(1 << id);
     52         }
     53 
     54         public override bool Equals(object obj)
     55         {
     56             return Value.Equals(obj);
     57         }
     58 
     59         public override int GetHashCode()
     60         {
     61             return Value.GetHashCode();
     62         }
     63 
     64         public override string ToString()
     65         {
     66             return Value.ToString();
     67         }
     68 
     69         public IEnumerator<int> GetEnumerator()
     70         {
     71             for (int i = 0, mask = 1; i < 32; ++i, mask <<= 1)
     72             {
     73                 if ((Value & mask) != 0)
     74                 {
     75                     yield return i;
     76                 }
     77             }
     78         }
     79 
     80         IEnumerator IEnumerable.GetEnumerator()
     81         {
     82             return GetEnumerator();
     83         }
     84     }
     85 
     86     public class AlliesMaskTypeConverter : TypeConverter
     87     {
     88         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
     89         {
     90             return (context is MapContext) && (sourceType == typeof(string));
     91         }
     92 
     93         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
     94         {
     95             return (context is MapContext) && (destinationType == typeof(string));
     96         }
     97 
     98         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
     99         {
    100             if (!(value is AlliesMask) || !CanConvertTo(context, destinationType))
    101             {
    102                 return null;
    103             }
    104 
    105             var map = (context as MapContext).Map;
    106             var alliesMask = (AlliesMask)value;
    107 
    108             var allies = new List<string>(map.Houses.Length);
    109             foreach (var id in alliesMask)
    110             {
    111                 if (map.Houses.Where(h => h.Type.Equals((sbyte)id)).FirstOrDefault() is House house)
    112                 {
    113                     allies.Add(house.Type.Name);
    114                 }
    115             }
    116 
    117             return string.Join(",", allies);
    118         }
    119 
    120         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    121         {
    122             if (!CanConvertFrom(context, value?.GetType()))
    123             {
    124                 return null;
    125             }
    126 
    127             var map = (context as MapContext).Instance as Map;
    128             var alliesMask = new AlliesMask(0);
    129 
    130             var allies = (value as string).Split(',');
    131             foreach (var ally in allies)
    132             {
    133                 if (map.Houses.Where(h => h.Type.Equals(ally)).FirstOrDefault() is House house)
    134                 {
    135                     alliesMask.Set(house.Type.ID);
    136                 }
    137             }
    138 
    139             return alliesMask;
    140         }
    141     }
    142 
    143     public class House
    144     {
    145         public readonly HouseType Type;
    146 
    147         [NonSerializedINIKey]
    148         [DefaultValue(true)]
    149         public bool Enabled { get; set; }
    150 
    151         [TypeConverter(typeof(AlliesMaskTypeConverter))]
    152         public AlliesMask Allies { get; set; }
    153 
    154         [DefaultValue(150)]
    155         public int MaxBuilding { get; set; }
    156 
    157         [DefaultValue(150)]
    158         public int MaxUnit { get; set; }
    159 
    160         [DefaultValue("North")]
    161         public string Edge { get; set; }
    162 
    163         [DefaultValue(0)]
    164         public int Credits { get; set; } = 0;
    165 
    166         public House(HouseType type)
    167         {
    168             Type = type;
    169             Allies = new AlliesMask(1 << Type.ID);
    170         }
    171     }
    172 }