Пример #1
0
        public void Load(IList <ISmartScriptLine> lines, IList <IConditionLine> conditions)
        {
            int?            entry  = null;
            SmartScriptType?source = null;

            var conds = ParseConditions(conditions);
            SortedDictionary <long, SmartEvent> triggerIdToActionParent = new();
            SortedDictionary <long, SmartEvent> triggerIdToEvent        = new();
            Dictionary <int, SmartEvent>        linkToSmartEventParent  = new();

            // find double links (multiple events linking to same event, this is not supported by design)
            var doubleLinks = lines
                              .Where(line => line.Link > 0)
                              .GroupBy(link => link.Link)
                              .Where(pair => pair.Count() > 1)
                              .Select(pair => pair.Key)
                              .ToHashSet();

            Dictionary <int, int>?linkToTriggerTimedEventId = null;

            if (doubleLinks.Count > 0)
            {
                int nextFreeTriggerTimedEvent = lines.Where(e => e.Id == SmartConstants.EventTriggerTimed)
                                                .Select(e => (int)e.EventParam1)
                                                .DefaultIfEmpty(0)
                                                .Max() + 1;

                linkToTriggerTimedEventId = doubleLinks.Select(linkId => (linkId, nextFreeTriggerTimedEvent++))
                                            .ToDictionary(pair => pair.linkId, pair => pair.Item2);
            }

            SmartEvent?lastEvent = null;

            foreach (ISmartScriptLine line in lines)
            {
                SmartEvent?currentEvent = null;

                if (!entry.HasValue)
                {
                    entry = line.EntryOrGuid;
                }
                else
                {
                    Debug.Assert(entry.Value == line.EntryOrGuid);
                }

                if (!source.HasValue)
                {
                    source = (SmartScriptType)line.ScriptSourceType;
                }
                else
                {
                    Debug.Assert((int)source.Value == line.ScriptSourceType);
                }

                if (source == SmartScriptType.TimedActionList && lastEvent != null &&
                    line.EventParam1 == 0 && line.EventParam2 == 0)
                {
                    currentEvent = lastEvent;
                }
                else if (!linkToSmartEventParent.TryGetValue(line.Id, out currentEvent))
                {
                    lastEvent = currentEvent = SafeEventFactory(line);

                    if (currentEvent != null)
                    {
                        if (currentEvent.Id == SmartConstants.EventTriggerTimed)
                        {
                            triggerIdToEvent[currentEvent.GetParameter(0).Value] = currentEvent;
                        }
                        else if (currentEvent.Id == SmartConstants.EventLink && doubleLinks.Contains(line.Id))
                        {
                            smartFactory.UpdateEvent(currentEvent, SmartConstants.EventTriggerTimed);
                            currentEvent.GetParameter(0).Value = linkToTriggerTimedEventId ![line.Id];