Ticket #1391: shipgarrison-v4.diff

File shipgarrison-v4.diff, 5.8 KB (added by mimo, 11 years ago)

change the name of the method as suggested in comment #13

  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    328328        }
    329329    },
    330330
     331    "Order.PickUpUnit": function(msg) {
     332        if (this.MoveToTarget(this.order.data.target))
     333        {
     334            this.SetNextState("INDIVIDUAL.PICKUP.APPROACHING");
     335        }
     336        else
     337        {
     338            // We are already at the target, or can't move at all
     339            this.StopMoving();
     340            this.SetNextState("INDIVIDUAL.PICKUP.LOADING");
     341        }
     342    },
     343
    331344    "Order.Flee": function(msg) {
    332345        // We use the distance between the enities to account for ranged attacks
    333346        var distance = DistanceBetweenEntities(this.entity, this.order.data.target) + (+this.template.FleeDistance);
     
    597610            return;
    598611        }
    599612
     613        // If to be garrisoned inside a ship, warn it so it can take needed action
     614        var cmpIdentity = Engine.QueryInterface(this.order.data.target, IID_Identity);
     615        if (cmpIdentity && cmpIdentity.HasClass("Ship"))
     616            Engine.PostMessage(this.order.data.target, MT_TransportRequired, { "entity": this.entity });
     617
    600618        if (this.MoveToTarget(this.order.data.target))
    601619        {
    602620            this.SetNextState("INDIVIDUAL.GARRISON.APPROACHING");
     
    724742                    // The target was destroyed
    725743                    this.FinishOrder();
    726744                else
     745                {
    727746                    // Out of range; move there in formation
    728747                    this.PushOrderFront("WalkToTargetRange", { "target": msg.data.target, "min": 0, "max": 10 });
     748                    // If to be garrisoned inside a ship, warn it so it can take needed action
     749                    var cmpIdentity = Engine.QueryInterface(msg.data.target, IID_Identity);
     750                    {
     751                        var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
     752                        var entity = cmpFormation.GetPrimaryMember();
     753                        Engine.PostMessage(msg.data.target, MT_TransportRequired, { "entity": entity });
     754                    }
     755                }
    729756                return;
    730757            }
    731758
     
    22282255                        {
    22292256                            // Unable to reach the target, try again
    22302257                            // (or follow if it's a moving target)
     2258                            // But first if garrisoning inside a ship, warn it again
     2259                            // in case the ship's orders have been changed
     2260                            var cmpIdentity = Engine.QueryInterface(target, IID_Identity);
     2261                            if (cmpIdentity && cmpIdentity.HasClass("Ship"))
     2262                                Engine.PostMessage(target, MT_TransportRequired, { "entity": this.entity });
     2263
    22312264                            if (this.MoveToTarget(target))
    22322265                            {
    22332266                                this.SetNextState("APPROACHING");
     
    23072340                // Ignore attacks while unpacking
    23082341            },
    23092342        },
     2343
     2344        "PICKUP": {
     2345            "APPROACHING": {
     2346                "enter": function() {
     2347                    this.SelectAnimation("move");
     2348                },
     2349
     2350                "MoveCompleted": function() {
     2351                    this.SetNextState("LOADING");
     2352                },
     2353               
     2354                "leave": function() {
     2355                }
     2356            },
     2357
     2358            "LOADING": {
     2359                "enter": function() {
     2360                    this.StartTimer(0, 1000);
     2361                    this.SelectAnimation("idle");
     2362                },
     2363
     2364                "Timer": function() {
     2365                    var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI);
     2366                    // Stop waiting if the target does not exist any more, or is already garrisoned or its orders have changed
     2367                    if (!cmpUnitAI || cmpUnitAI.isGarrisoned || !cmpUnitAI.order || cmpUnitAI.order.type != "Garrison"
     2368                        || cmpUnitAI.order.data.target != this.entity)
     2369                        this.FinishOrder();
     2370                    // Or if the capacity is already full
     2371                    var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
     2372                    if (cmpGarrisonHolder.GetEntities().length >= cmpGarrisonHolder.GetCapacity())
     2373                        this.FinishOrder();
     2374                },
     2375
     2376                "leave": function() {
     2377                    this.StopTimer();
     2378                },
     2379            },
     2380        },
    23102381    },
    23112382
    23122383    "ANIMAL": {
     
    25852656        this.SetupRangeQueries();
    25862657};
    25872658
     2659UnitAI.prototype.OnTransportRequired = function(msg)
     2660{
     2661    var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
     2662    if (cmpGarrisonHolder && cmpGarrisonHolder.GetEntities().length < cmpGarrisonHolder.GetCapacity())
     2663    {
     2664        // First check if we already have such a requirement
     2665        for each (var order in this.orderQueue)
     2666        {
     2667            if (order.type == "PickUpUnit" && order.data.target == msg.entity)
     2668                return;
     2669        }
     2670        // Insert the PickUp order after the last forced order
     2671        this.PushOrderAfterForced("PickUpUnit", { "target": msg.entity });
     2672    }
     2673};
     2674
    25882675// Wrapper function that sets up the normal, healer, and Gaia range queries.
    25892676UnitAI.prototype.SetupRangeQueries = function()
    25902677{
     
    28502937    }
    28512938};
    28522939
     2940/**
     2941 * Insert an order after the last forced order onto the queue
     2942 * and after the other orders of the same type
     2943 */
     2944UnitAI.prototype.PushOrderAfterForced = function(type, data)
     2945{
     2946    if (!this.order || ((!this.order.data || !this.order.data.force) && this.order.type != type))
     2947    {
     2948        this.PushOrderFront(type, data);
     2949    }
     2950    else
     2951    {
     2952        for (var i = 1; i < this.orderQueue.length; ++i)
     2953        {
     2954            if (this.orderQueue[i].data && this.orderQueue[i].data.force)
     2955                continue;
     2956            if (this.orderQueue[i].type == type)
     2957                continue;
     2958            this.orderQueue.splice(i, 0, {"type": type, "data": data});
     2959            return;
     2960        }
     2961        this.PushOrder(type, data);
     2962    }
     2963};
     2964
    28532965UnitAI.prototype.ReplaceOrder = function(type, data)
    28542966{
    28552967    // Special cases of orders that shouldn't be replaced:
  • binaries/data/mods/public/simulation/components/interfaces/UnitAI.js

     
    1111// Message of the form { "to": orderData }.
    1212// sent whenever the unit changes state
    1313Engine.RegisterMessageType("UnitAIOrderDataChanged");
     14
     15// Message of the form { "entity": entity },
     16// sent whenever a transport (ship) is required
     17Engine.RegisterMessageType("TransportRequired");