Ticket #2206: correct_backtowork.6.diff
File correct_backtowork.6.diff, 9.7 KB (added by , 11 years ago) |
---|
-
binaries/data/mods/public/gui/session/input.js
2108 2108 // Filter out all entities that can't go back to work. 2109 2109 var workers = g_Selection.toList().filter(function(e) { 2110 2110 var state = GetEntityState(e); 2111 return (state && state.unitAI && state.unitAI. lastWorkOrder);2111 return (state && state.unitAI && state.unitAI.hasWorkOrders); 2112 2112 }); 2113 2113 2114 2114 Engine.PostNetworkCommand({"type": "back-to-work", "workers": workers}); -
binaries/data/mods/public/gui/session/utility_functions.js
294 294 }); 295 295 } 296 296 297 if (entState.unitAI && entState.unitAI. lastWorkOrder)297 if (entState.unitAI && entState.unitAI.hasWorkOrders) 298 298 { 299 299 commands.push({ 300 300 "name": "back-to-work", -
binaries/data/mods/public/simulation/components/Formation.js
115 115 for each (var ent in ents) 116 116 { 117 117 var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); 118 cmpUnitAI.UpdateWorkOrders(); 118 119 cmpUnitAI.SetFormationController(INVALID_ENTITY); 119 120 } 120 121 -
binaries/data/mods/public/simulation/components/GuiInterface.js
348 348 ret.unitAI = { 349 349 "state": cmpUnitAI.GetCurrentState(), 350 350 "orders": cmpUnitAI.GetOrders(), 351 " lastWorkOrder": cmpUnitAI.GetLastWorkOrder(),351 "hasWorkOrders": cmpUnitAI.HasWorkOrders(), 352 352 }; 353 353 // Add some information needed for ungarrisoning 354 354 if (cmpUnitAI.isGarrisoned && ret.player !== undefined) -
binaries/data/mods/public/simulation/components/Promotion.js
78 78 cmpPromotedUnitAI.Cheer(); 79 79 var orders = cmpCurrentUnitAI.GetOrders(); 80 80 cmpPromotedUnitAI.AddOrders(orders); 81 var lastWorkOrder = cmpCurrentUnitAI.GetLastWorkOrder();82 cmpPromotedUnitAI.Set LastWorkOrder(lastWorkOrder);81 var workOrders = cmpCurrentUnitAI.GetWorkOrders(); 82 cmpPromotedUnitAI.SetWorkOrders(workOrders); 83 83 84 84 Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: promotedUnitEntity }); 85 85 -
binaries/data/mods/public/simulation/components/UnitAI.js
2526 2526 this.lastFormationName = ""; 2527 2527 this.finishedOrder = false; // used to find if all formation members finished the order 2528 2528 2529 // To go back to work later2530 this. lastWorkOrder = undefined;2529 // Queue of remembered works 2530 this.workOrders = []; 2531 2531 2532 2532 // For preventing increased action rate due to Stop orders or target death. 2533 2533 this.lastAttacked = undefined; … … 2804 2804 Engine.PostMessage(this.entity, MT_UnitAIStateChanged, { "to": state }); 2805 2805 }; 2806 2806 2807 UnitAI.prototype.BackToWork = function()2808 {2809 if(this.lastWorkOrder)2810 {2811 this.ReplaceOrder(this.lastWorkOrder.type, this.lastWorkOrder.data);2812 return true;2813 }2814 else2815 return false;2816 };2817 2818 2807 /** 2819 2808 * Call when the current order has been completed (or failed). 2820 2809 * Removes the current order from the queue, and processes the … … 2831 2820 error("FinishOrder called for entity " + this.entity + " (" + template + ") when order queue is empty\n" + stack); 2832 2821 } 2833 2822 2834 // Remove the order from the queue , then forget it if it was a work to avoid trying to go back to it later.2823 // Remove the order from the queue 2835 2824 var finishedOrder = this.orderQueue.shift(); 2836 if(finishedOrder.type == "Gather" || finishedOrder.type == "Trade" || finishedOrder.type == "Repair")2837 this.lastWorkOrder = undefined;2838 2839 2825 this.order = this.orderQueue[0]; 2840 // If the worker was returning a resource, we shall2841 // -> (if the worker is gathering) keep remembering the "Gather" order which is this.order2842 // -> (if the worker was directly ordered to return a resource) forget the "ReturnResource" order which is finished2843 if(finishedOrder.type == "ReturnResource" && (!this.order || this.order.type != "Gather"))2844 this.lastWorkOrder = undefined;2845 2826 2846 2827 if (this.orderQueue.length) 2847 2828 { … … 2892 2873 { 2893 2874 var order = { "type": type, "data": data }; 2894 2875 this.orderQueue.push(order); 2895 2896 if(type == "Gather" || type == "Trade" || type == "Repair" || type == "ReturnResource")2897 this.lastWorkOrder = order;2898 2876 2899 2877 // If we didn't already have an order, then process this new one 2900 2878 if (this.orderQueue.length == 1) … … 2952 2930 2953 2931 UnitAI.prototype.ReplaceOrder = function(type, data) 2954 2932 { 2933 // Keep the previous orders if needed 2934 if (data && data.force) 2935 { 2936 // Handle formations 2937 if (this.IsFormationController()) 2938 { 2939 var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); 2940 cmpFormation.CallMemberFunction("UpdateWorkOrders", [type]); 2941 } 2942 else 2943 this.UpdateWorkOrders(type); 2944 } 2945 2955 2946 // Special cases of orders that shouldn't be replaced: 2956 2947 // 1. Cheering - we're invulnerable, add order after we finish 2957 2948 // 2. Packing/unpacking - we're immobile, add order after we finish (unless it's cancel) … … 2998 2989 return orders; 2999 2990 }; 3000 2991 3001 UnitAI.prototype. GetLastWorkOrder = function()2992 UnitAI.prototype.UpdateWorkOrders = function(type) 3002 2993 { 3003 return this.lastWorkOrder; 2994 var isWorkType = function(type){ 2995 return (type == "Gather" || type == "Trade" || type == "Repair" || type == "ReturnResource"); 2996 }; 2997 2998 if (isWorkType(type)) 2999 this.workOrders = []; 3000 else if (!this.workOrders.length) 3001 { 3002 // First if the unit is in a formation, get its workOrders from it 3003 if (this.IsFormationMember()) 3004 { 3005 var cmpUnitAI = Engine.QueryInterface(this.formationController, IID_UnitAI); 3006 for (var i = 0; i < cmpUnitAI.orderQueue.length; ++i) 3007 { 3008 var ordertype = cmpUnitAI.orderQueue[i].type; 3009 if (isWorkType(ordertype)) 3010 { 3011 this.workOrders = cmpUnitAI.orderQueue.slice(i); 3012 break; 3013 } 3014 } 3015 } 3016 3017 // If nothing found, take the unit orders 3018 if (!this.workOrders.length) 3019 { 3020 for (var i = 0; i < this.orderQueue.length; ++i) 3021 { 3022 var ordertype = this.orderQueue[i].type; 3023 if (isWorkType(ordertype)) 3024 { 3025 this.workOrders = this.orderQueue.slice(i); 3026 break; 3027 } 3028 } 3029 } 3030 } 3004 3031 }; 3005 3032 3006 UnitAI.prototype.SetLastWorkOrder = function(order) 3033 UnitAI.prototype.BackToWork = function() 3034 { 3035 if(this.workOrders.length > 0) 3036 { 3037 // Clear the order queue considering special orders not to avoid 3038 if (this.order && this.order.type == "Cheering") 3039 { 3040 var cheeringOrder = this.orderQueue.shift(); 3041 this.orderQueue = [ cheeringOrder ]; 3042 } 3043 else 3044 this.orderQueue = []; 3045 3046 this.AddOrders(this.workOrders); 3047 3048 // And if the unit is in a formation, remove it from the formation 3049 if (this.IsFormationMember()) 3050 Engine.QueryInterface(this.formationController, IID_Formation).RemoveMembers([this.entity]); 3051 3052 this.workOrders = []; 3053 return true; 3054 } 3055 else 3056 return false; 3057 }; 3058 3059 UnitAI.prototype.HasWorkOrders = function() 3007 3060 { 3008 this.lastWorkOrder = order;3061 return (this.workOrders.length > 0); 3009 3062 }; 3010 3063 3064 UnitAI.prototype.GetWorkOrders = function() 3065 { 3066 return this.workOrders; 3067 }; 3068 3069 UnitAI.prototype.SetWorkOrders = function(orders) 3070 { 3071 this.workOrders = orders; 3072 }; 3073 3011 3074 UnitAI.prototype.TimerHandler = function(data, lateness) 3012 3075 { 3013 3076 // Reset the timer -
binaries/data/mods/public/simulation/helpers/Commands.js
170 170 cmpUnitAI.ReturnResource(cmd.target, cmd.queued); 171 171 }); 172 172 break; 173 174 case "back-to-work": 175 var entities = FilterEntityList(cmd.workers, player, controlAllUnits); 176 for each (var ent in entities) 177 { 178 var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); 179 if(!cmpUnitAI || !cmpUnitAI.BackToWork()) 180 notifyBackToWorkFailure(player); 181 } 182 break; 173 183 174 184 case "train": 175 185 // Check entity limits … … 390 400 notifyUnloadFailure(player, garrisonHolder) 391 401 } 392 402 break; 393 394 case "back-to-work":395 var entities = FilterEntityList(cmd.workers, player, controlAllUnits);396 for each (var worker in entities)397 {398 var cmpUnitAI = Engine.QueryInterface(worker, IID_UnitAI);399 if (!cmpUnitAI || !cmpUnitAI.BackToWork())400 notifyBackToWorkFailure(player, worker)401 }402 break;403 403 404 404 case "formation": 405 405 GetFormationUnitAIs(entities, player, cmd.name).forEach(function(cmpUnitAI) { … … 530 530 /** 531 531 * Sends a GUI notification about worker(s) that failed to go back to work. 532 532 */ 533 function notifyBackToWorkFailure(player , worker)533 function notifyBackToWorkFailure(player) 534 534 { 535 535 var cmpPlayer = QueryPlayerIDInterface(player, IID_Player); 536 536 var notification = {"player": cmpPlayer.GetPlayerID(), "message": "Some unit(s) can't go back to work" };