Ticket #3748: UnitAI_set_isIdle_on_INDIVIDUAL.IDLE.enter_v2.patch

File UnitAI_set_isIdle_on_INDIVIDUAL.IDLE.enter_v2.patch, 3.5 KB (added by elexis, 8 years ago)
  • binaries/data/mods/public/simulation/components/UnitAI.js

    UnitAI.prototype.UnitFsmSpec = {  
    14381438                    if (cmpFormation)
    14391439                        animationName = cmpFormation.GetFormationAnimation(this.entity, animationName);
    14401440                }
    14411441                this.SelectAnimation(animationName);
    14421442
    1443                 // If we have some orders, it is because we are in an intermediary state
    1444                 // from FinishOrder (SetNextState("IDLE") is only executed when we get
    1445                 // a ProcessMessage), and thus we should not start another order which could
    1446                 // put us in a weird state
    14471443                if (this.orderQueue.length > 0 && !this.IsGarrisoned())
    1448                     return false;
     1444                    warn("INDIVIDUAL.IDLE.enter while having orders and not being garrisoned!");
    14491445
    14501446                // If the unit is guarding/escorting, go back to its duty
    14511447                if (this.isGuardOf)
    14521448                {
    14531449                    this.Guard(this.isGuardOf, false);
    14541450                    return true;
    14551451                }
    14561452
    1457                 // The GUI and AI want to know when a unit is idle, but we don't
    1458                 // want to send frequent spurious messages if the unit's only
    1459                 // idle for an instant and will quickly go off and do something else.
    1460                 // So we'll set a timer here and only report the idle event if we
    1461                 // remain idle
    1462                 this.StartTimer(1000);
    1463 
    14641453                // If a unit can heal and attack we first want to heal wounded units,
    14651454                // so check if we are a healer and find whether there's anybody nearby to heal.
    14661455                // (If anyone approaches later it'll be handled via LosHealRangeUpdate.)
    14671456                // If anyone in sight gets hurt that will be handled via LosHealRangeUpdate.
    14681457                if (this.IsHealer() && this.FindNewHealTargets())
    UnitAI.prototype.UnitFsmSpec = {  
    14721461                // so immediately check whether there's anybody nearby to attack.
    14731462                // (If anyone approaches later, it'll be handled via LosRangeUpdate.)
    14741463                if (this.FindNewTargets())
    14751464                    return true; // (abort the FSM transition since we may have already switched state)
    14761465
     1466                if (this.isIdle)
     1467                    warn("INDIVIDUAL.IDLE.enter while isIdle!");
     1468
    14771469                // Nobody to attack - stay in idle
     1470                this.isIdle = true;
     1471                Engine.PostMessage(this.entity, MT_UnitIdleChanged, { "idle": true });
    14781472                return false;
    14791473            },
    14801474
    14811475            "leave": function() {
    14821476                var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    14831477                if (this.losRangeQuery)
    14841478                    cmpRangeManager.DisableActiveQuery(this.losRangeQuery);
    14851479                if (this.losHealRangeQuery)
    14861480                    cmpRangeManager.DisableActiveQuery(this.losHealRangeQuery);
    14871481
    1488                 this.StopTimer();
     1482                if (!this.isIdle)
     1483                    warn("INDIVIDUAL.IDLE.leave while not isIdle!");
    14891484
    1490                 if (this.isIdle)
    1491                 {
    1492                     this.isIdle = false;
    1493                     Engine.PostMessage(this.entity, MT_UnitIdleChanged, { "idle": this.isIdle });
    1494                 }
     1485                this.isIdle = false;
     1486                Engine.PostMessage(this.entity, MT_UnitIdleChanged, { "idle": false });
    14951487            },
    14961488
    14971489            "LosRangeUpdate": function(msg) {
    14981490                if (this.GetStance().targetVisibleEnemies)
    14991491                {
    UnitAI.prototype.UnitFsmSpec = {  
    15111503            },
    15121504
    15131505            "MoveCompleted": function() {
    15141506                this.SelectAnimation("idle");
    15151507            },
    1516 
    1517             "Timer": function(msg) {
    1518                 if (!this.isIdle)
    1519                 {
    1520                     this.isIdle = true;
    1521                     Engine.PostMessage(this.entity, MT_UnitIdleChanged, { "idle": this.isIdle });
    1522                 }
    1523             },
    15241508        },
    15251509
    15261510        "WALKING": {
    15271511            "enter": function () {
    15281512                this.SelectAnimation("move");