Ticket #1720: UnitAI.js.patch

File UnitAI.js.patch, 3.7 KB (added by svott, 4 years ago)

to #comment3

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

     
    32153215    this.lastHealed = undefined;
    32163216
    32173217    this.SetStance(this.template.DefaultStance);
     3218
     3219    this.isOnPatrol = false;
    32183220};
    32193221
    32203222UnitAI.prototype.IsTurret = function()
     
    35623564        var template = cmpTemplateManager.GetCurrentTemplateName(this.entity);
    35633565        error("FinishOrder called for entity " + this.entity + " (" + template + ") when order queue is empty\n" + stack);
    35643566    }
     3567   
     3568    //warn(this.GetCurrentState())
     3569    // break automatic patrol order if the order queue is reset by a new single order
     3570    if (this.orderQueue.length < 2) {
     3571        this.isOnPatrol = false;
     3572    }
    35653573
     3574    if (this.isOnPatrol) {
     3575        // repeat walk to current position after doing other orders
     3576        this.orderQueue.push(this.order);
     3577    }
     3578    else {
     3579        this.isOnPatrol = this.CheckPatrolWalk();
     3580    }
     3581
     3582    // debug print
     3583    /*
     3584    let s =""
     3585    for each (let o in this.orderQueue)
     3586        s += (""+o.data.x).substring(0,3) + "/" + (""+o.data.z).substring(0,3) + " -> ";
     3587    warn(s)
     3588    */
     3589
     3590    // next order
    35663591    this.orderQueue.shift();
    35673592    this.order = this.orderQueue[0];
    35683593
     
    36073632    }
    36083633};
    36093634
     3635// activate a automatically patrol if the first and last order have the same coordinates.
     3636UnitAI.prototype.CheckPatrolWalk = function()
     3637{
     3638    if (this.orderQueue.length < 2)
     3639    {
     3640        // a patrol is not possible if less then 2 walk commands are given
     3641        return false;
     3642    }
     3643
     3644    let finishedOrder = this.order;
     3645    let lastOrder = this.orderQueue.slice(-1)[0];
     3646    let epsilon = 2; // TODO remove to a better place?
     3647
     3648    if ((finishedOrder.type != "WalkAndFight" && lastOrder.type != "WalkAndFight") &&
     3649        (finishedOrder.type != "Walk" && lastOrder.type != "Walk"))
     3650    {
     3651        // a none-walking order cannot be a patrol by nature
     3652        warn(finishedOrder.type + " / " + lastOrder.type)
     3653        return false;
     3654    }
     3655
     3656    // do not this check because it is not possible when pressing the shift-key
     3657    /*for each(let o in this.orderQueue)
     3658    {
     3659        if (o.type != "Walk") {
     3660            warn("not all are walks")
     3661            return false;
     3662        }
     3663    }*/
     3664
     3665    if (finishedOrder.data.x === undefined /*|| finishedOrder.data.y === undefined*/ || finishedOrder.data.z === undefined)
     3666    {
     3667        warn("missing data finishedOrder")
     3668        return false;
     3669    }
     3670
     3671    if (lastOrder.data.x === undefined /*|| finishedOrder.data.y === undefined*/ || lastOrder.data.z === undefined)
     3672    {
     3673        warn("missing data lastOrder")
     3674        return false;
     3675    }
     3676
     3677    warn(finishedOrder.data.x + " / " + finishedOrder.data.z)
     3678    warn(lastOrder.data.x + " / " + lastOrder.data.z)
     3679   
     3680    // compare coordinates of current and last order
     3681    // TODO x and y are always positive?
     3682    if (Math.abs(finishedOrder.data.x - lastOrder.data.x) < epsilon && Math.abs(finishedOrder.data.z - lastOrder.data.z) < epsilon)
     3683    {
     3684        warn("PATROL");
     3685        return true;
     3686    }
     3687
     3688    // check neighbours
     3689    /*for (let i = 0; i < this.orderQueue - 1; i++)
     3690    {
     3691        let a = this.orderQueue[i];
     3692        let b = this.orderQueue[i+1];
     3693
     3694        if (Math.abs(a.data.x - b.data.x) < epsilon && Math.abs(a.data.z - b.data.z) < epsilon)
     3695        {
     3696            warn("HERE 2")
     3697            return true;
     3698        }
     3699    }*/
     3700
     3701    return false;
     3702}
     3703
    36103704/**
    36113705 * Add an order onto the back of the queue,
    36123706 * and execute it if we didn't already have an order.