public void InitStats(MyStatsDefinition definition)
		{
			if (definition == null)
				return;
			
			foreach(var statId in definition.Stats)
			{
				MyEntityStatDefinition statDefinition = null;
				if (!MyDefinitionManager.Static.TryGetDefinition(statId, out statDefinition))
					continue;

			if (!statDefinition.Enabled || (!statDefinition.EnabledInCreative && MySession.Static.CreativeMode))
					continue;

				var builder = new MyObjectBuilder_EntityStat();
				builder.MaxValue = statDefinition.MaxValue;
				builder.MinValue = statDefinition.MinValue;
				builder.Value = statDefinition.MaxValue;
				AddStat(statId.SubtypeId, builder);
			}

			Type scriptType;
			foreach (var scriptName in definition.Scripts)
			{
				if (MyScriptManager.Static.StatScripts.TryGetValue(scriptName, out scriptType))
				{
					var script = (MyStatLogic)Activator.CreateInstance(scriptType);
					script.Init(Entity as MyCharacter, m_stats);
					m_scripts.Add(script);
				}
			}
		}
Beispiel #2
0
        public virtual MyObjectBuilder_EntityStat GetObjectBuilder()
        {
            var builder = new MyObjectBuilder_EntityStat();
            MyEntityStatDefinition definition = MyDefinitionManager.Static.GetDefinition(new MyDefinitionId(builder.TypeId, StatId)) as MyEntityStatDefinition;

            builder.SubtypeName = m_statId.ToString();
            Debug.Assert(definition != null);
            if (definition != null)
            {
                Debug.Assert(definition.MaxValue != 0);
                builder.Value = m_currentValue / (definition.MaxValue != 0 ? definition.MaxValue : 1);	// Save stat value relative to the definition maximum value
                builder.MaxValue = m_maxValue / (definition.MaxValue != 0 ? definition.MaxValue : 1);	// Save stat maximum value relative to the definition maximum value
            }
            else
            {
                builder.Value = m_currentValue / m_maxValue;
                builder.MaxValue = 1.0f;
            }

            builder.Effects = null;
            if (m_effects != null && m_effects.Count > 0)
            {
                int savedEffectCount = m_effects.Count;

                foreach (var effectPair in m_effects)
                {
                    if (effectPair.Value.Duration < 0)
                        --savedEffectCount;			// Don't save the permanent effects
                }
                if (savedEffectCount > 0)
                {
                    builder.Effects = new MyObjectBuilder_EntityStatRegenEffect[savedEffectCount];
                    int effectIndex = 0;
                    foreach (var effectPair in m_effects)
                    {
                        if (effectPair.Value.Duration >= 0)
                            builder.Effects[effectIndex++] = effectPair.Value.GetObjectBuilder();
                    }
                }
            }

            return builder;
        }
		private MyEntityStat AddStat(MyStringHash statId, MyObjectBuilder_EntityStat objectBuilder)
		{
			MyEntityStat stat = null;

			if (!m_stats.TryGetValue(statId, out stat))
			{
				stat = new MyEntityStat();
				stat.Init(objectBuilder);

				m_stats.Add(statId, stat);
			}
			else
				stat.ClearEffects();

			return stat;
		}
		public void InitStats(MyStatsDefinition definition)
		{
			if (definition == null || !definition.Enabled || (!definition.AvailableInSurvival && MySession.Static.SurvivalMode))
				return;

			foreach (var statId in definition.Stats)
			{
				MyEntityStatDefinition statDefinition = null;
				if (!MyDefinitionManager.Static.TryGetDefinition(statId, out statDefinition))
					continue;

				if (!statDefinition.Enabled
					|| (!statDefinition.EnabledInCreative && MySession.Static.CreativeMode)
					|| (!statDefinition.AvailableInSurvival && MySession.Static.SurvivalMode))
					continue;

				var nameHash = MyStringHash.GetOrCompute(statDefinition.Name);
				MyEntityStat existingStat = null;
				if (m_stats.TryGetValue(statId.SubtypeId, out existingStat) && existingStat.StatId == nameHash)
					continue;

				var builder = new MyObjectBuilder_EntityStat();
				builder.SubtypeName = statId.SubtypeName;
				builder.MaxValue = 1.0f;
				builder.Value = statDefinition.DefaultValue / statDefinition.MaxValue;
				AddStat(nameHash, builder);
			}

			if (Sync.IsServer)	// Only init scripts on server
			{
                // MW: remove all scripts because of the broken saves (Medieval character has multiple scripts (peasant's and player's))
                foreach (var script in m_scripts)
                {
                    script.Close();
                }
                m_scripts.Clear();

				foreach (var scriptName in definition.Scripts)
				{
					InitScript(scriptName);
				}
			}
			else
			{
				RequestStatActions();	// Only request the stat actions from the server
			}
		}
		private MyEntityStat AddStat(MyStringHash statId, MyObjectBuilder_EntityStat objectBuilder, bool forceNewValues = false)
		{
			MyEntityStat stat = null;

            if (m_stats.TryGetValue(statId, out stat))
            {
                if(!forceNewValues)
                    objectBuilder.Value = stat.CurrentRatio;

                stat.ClearEffects();
                m_stats.Remove(statId);
            }

            stat = new MyEntityStat();
            stat.Init(objectBuilder);

            m_stats.Add(statId, stat);

            return stat;
		}
        public override void Init(MyComponentDefinitionBase definition)
        {
            base.Init(definition);

            MyEntityStatComponentDefinition entityStatDefinition = definition as MyEntityStatComponentDefinition;
            Debug.Assert(entityStatDefinition != null);

            if (entityStatDefinition == null || !entityStatDefinition.Enabled || MySession.Static == null ||
                (!entityStatDefinition.AvailableInSurvival && MySession.Static.SurvivalMode))
            {
                if (Sync.IsServer) // Only init scripts on server
                {
                    m_statActionsRequested = true;
                }
                return;
            }

            foreach (var statId in entityStatDefinition.Stats)
            {
                MyEntityStatDefinition statDefinition = null;
                if (!MyDefinitionManager.Static.TryGetDefinition(statId, out statDefinition))
                    continue;

                if (!statDefinition.Enabled
                    || (!statDefinition.EnabledInCreative && MySession.Static.CreativeMode)
                    || (!statDefinition.AvailableInSurvival && MySession.Static.SurvivalMode))
                    continue;

                var nameHash = MyStringHash.GetOrCompute(statDefinition.Name);
                MyEntityStat existingStat = null;
                if (m_stats.TryGetValue(nameHash, out existingStat) && existingStat.StatDefinition.Id.SubtypeId == statDefinition.Id.SubtypeId)
                    continue;

                var builder = new MyObjectBuilder_EntityStat();
                builder.SubtypeName = statId.SubtypeName;
                builder.MaxValue = 1.0f;
                builder.Value = statDefinition.DefaultValue / statDefinition.MaxValue;
                AddStat(nameHash, builder);
            }

            if (Sync.IsServer)	// Only init scripts on server
            {
                Debug.Assert(m_scripts.Count == 0);
                foreach (var scriptName in entityStatDefinition.Scripts)
                {
                    InitScript(scriptName);
                }
                m_statActionsRequested = true;
            }
        }
		public void InitStats(MyStatsDefinition definition)
		{
			if (definition == null || !definition.Enabled || (!definition.AvailableInSurvival && MySession.Static.SurvivalMode))
				return;

			foreach (var statId in definition.Stats)
			{
				MyEntityStatDefinition statDefinition = null;
				if (!MyDefinitionManager.Static.TryGetDefinition(statId, out statDefinition))
					continue;

				if (!statDefinition.Enabled
					|| (!statDefinition.EnabledInCreative && MySession.Static.CreativeMode)
					|| (!statDefinition.AvailableInSurvival && MySession.Static.SurvivalMode))
					continue;

				var nameHash = MyStringHash.GetOrCompute(statDefinition.Name);
				MyEntityStat existingStat = null;
				if (m_stats.TryGetValue(statId.SubtypeId, out existingStat) && existingStat.StatId == nameHash)
					continue;

				var builder = new MyObjectBuilder_EntityStat();
				builder.SubtypeName = statId.SubtypeName;
				builder.MaxValue = 1.0f;
				builder.Value = 1.0f;
				AddStat(nameHash, builder);
			}

			if (Sync.IsServer)	// Only init scripts on server
			{
				foreach (var scriptName in definition.Scripts)
				{
					MyStatLogic script = null;
					if ((script = m_scripts.Find((otherScript) => { return otherScript.Name == scriptName; })) != null)
					{
						script.Close();
						m_scripts.Remove(script);	// On load we might've had less stats to consider (moving from creative to survival for example) so replace the old script
					}

					InitScript(scriptName);
				}
			}
			else
			{
				RequestStatActions();	// Only request the stat actions from the server
			}
		}