Ticket #4158: 4158.diff
File 4158.diff, 3.8 KB (added by , 7 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Timer.js
19 19 return this.time; 20 20 }; 21 21 22 /** 23 * Returns the duration of the latest turn of the game, in integer milliseconds. 24 */ 22 25 Timer.prototype.GetLatestTurnLength = function() 23 26 { 24 27 return this.turnLength; … … 32 35 */ 33 36 Timer.prototype.SetTimeout = function(ent, iid, funcname, time, data) 34 37 { 35 var id = ++this.id; 36 this.timers.set(id, [ent, iid, funcname, this.time + time, 0, data]); 38 let id = ++this.id; 39 40 this.timers.set(id, 41 { 42 "entity": ent, 43 "iid": iid, 44 "functionName": funcname, 45 "time": this.time + time, 46 "repeatTime": 0, 47 "data": data 48 }); 49 37 50 return id; 38 51 }; 39 52 … … 50 63 { 51 64 if (typeof repeattime != "number" || !(repeattime > 0)) 52 65 error("Invalid repeattime to SetInterval of "+funcname); 53 var id = ++this.id; 54 this.timers.set(id, [ent, iid, funcname, this.time + time, repeattime, data]); 66 67 let id = ++this.id; 68 69 this.timers.set(id, 70 { 71 "entity": ent, 72 "iid": iid, 73 "functionName": funcname, 74 "time": this.time + time, 75 "repeatTime": 0, 76 "data": data 77 }); 78 55 79 return id; 56 80 }; 57 81 … … 66 90 67 91 Timer.prototype.OnUpdate = function(msg) 68 92 { 69 var dt = Math.round(msg.turnLength * 1000); 70 this.time += dt; 71 this.turnLength = dt; 93 this.turnLength = Math.round(msg.turnLength * 1000); 94 this.time += this.turnLength; 72 95 73 96 // Collect the timers that need to run 74 97 // (We do this in two stages to avoid deleting from the timer list while 75 98 // we're in the middle of iterating through it) 76 var run = []; 99 let run = []; 100 77 101 for (let id of this.timers.keys()) 78 { 79 if (this.timers.get(id)[3] <= this.time) 102 if (this.timers.get(id).time <= this.time) 80 103 run.push(id); 81 } 82 for ( vari = 0; i < run.length; ++i)104 105 for (let i = 0; i < run.length; ++i) 83 106 { 84 var id = run[i]; 107 let id = run[i]; 108 let timer = this.timers.get(id); 85 109 86 var t = this.timers.get(id);87 if (!t )88 continue; // an earlier timer might have cancelled this one, so skip it110 // An earlier timer might have cancelled this one, so skip it 111 if (!timer) 112 continue; 89 113 90 var cmp = Engine.QueryInterface(t[0], t[1]); 91 if (!cmp) 114 // The entity was probably destroyed; clean up the timer 115 let cmpTimer = Engine.QueryInterface(timer.entity, timer.iid); 116 if (!cmpTimer) 92 117 { 93 // The entity was probably destroyed; clean up the timer94 118 this.timers.delete(id); 95 119 continue; 96 120 } 97 121 98 try { 99 var lateness = this.time - t[3]; 100 cmp[t[2]](t[5], lateness); 101 } catch (e) { 102 var stack = e.stack.trimRight().replace(/^/mg, ' '); // indent the stack trace 103 error("Error in timer on entity "+t[0]+", IID "+t[1]+", function "+t[2]+": "+e+"\n"+stack+"\n"); 122 try 123 { 124 let lateness = this.time - timer.time; 125 cmpTimer[timer.functionName](timer.data, lateness); 104 126 } 105 106 // Handle repeating timers 107 if (t[4]) 127 catch (e) 108 128 { 109 // Add the repeat time to the execution time 110 t[3] += t[4]; 111 // Add it to the list to get re-executed if it's soon enough 112 if (t[3] <= this.time) 113 run.push(id); 129 // Indent the stack trace 130 let stack = e.stack.trimRight().replace(/^/mg, ' '); 131 error("Error in timer on entity " + timer.entity + ", IID " + timer.iid + ", function " + timer.functionName + ": " + e + "\n" + stack + "\n"); 114 132 } 115 else 133 134 // Non-repeating time - delete it 135 if (!timer.repeatTime) 116 136 { 117 // Non-repeating time - delete it118 137 this.timers.delete(id); 138 continue; 119 139 } 140 // Handle repeating timers 141 // Add the repeat time to the execution time 142 timer.time += timer.repeatTime; 143 // Add it to the list to get re-executed if it's soon enough 144 if (timer.time <= this.time) 145 run.push(id); 120 146 } 121 147 }; 122 148