FeXoR Posted February 29, 2012 Report Share Posted February 29, 2012 There are some functions i missed inside the rmgen .js files.Here are some suggestions i find useful and put inside my random map as helper functions.The "getDirection" function is a bit fuzzy but works (consistent with the unit placement orientation AFAIK).Might be written more sober though.I skipped checking the given operands in any way but that might be wise...// Function to get the distance between 2 pointsfunction getDistance(point1, point2) {return Math.pow(Math.pow(point1[0] - point2[0], 2) + Math.pow(point1[1] - point2[1], 2), 1/2)};// Function to get the distance between 2 points given in seperate coordinatesfunction getDistanceXZ(x1, z1, x2, z2) {return getDistance([x1, z1], [x2, z2])};// Function to get the direction from one point to anotherfunction getDirection(point1, point2){ var vector = [point2[0] - point1[0], point2[1] - point1[1]]; var output = Math.acos(vector[0]/getDistance(point1, point2)); if (vector[1] > 0) {output = PI + (PI - Math.acos(vector[0]/getDistance(point1, point2)))}; return (output + PI/2) % (2*PI);};// Function to get the direction from one point to another given in seperate coordinatesfunction getDirectionXZ(x1, z1, x2, z2) {return getDirection([x1, z1], [x2, z2])}; Quote Link to comment Share on other sites More sharing options...
FeXoR Posted February 29, 2012 Author Report Share Posted February 29, 2012 (edited) The getDirection function fails if both points are the same.So here's a version that don't...// Function to get the direction from one point to anotherfunction getDirection(point1, point2){ var vector = [point2[0] - point1[0], point2[1] - point1[1]]; var output = 0; if (vector[0] !== 0 || vector[1] !== 0) { output = Math.acos(vector[0]/getDistance(point1, point2)); if (vector[1] > 0) {output = PI + (PI - Math.acos(vector[0]/getDistance(point1, point2)))}; }; return (output + PI/2) % (2*PI);}; Edited March 4, 2012 by FeXoR Quote Link to comment Share on other sites More sharing options...
FeXoR Posted February 29, 2012 Author Report Share Posted February 29, 2012 Inside the 'if' statement the 'var' in front of 'output' is not needed, but does'nt hurt AFAIK...And sry for my poor english. Quote Link to comment Share on other sites More sharing options...
quantumstate Posted February 29, 2012 Report Share Posted February 29, 2012 You should definitely remove the second var, it will confuse people who are used to block level scoping. Also in terms of formatting you should either use the style:if (foo){ bar;}or if (foo) bar;(Obviously you cannot use the second if you have multiple statements.)The function is pretty confusing to read, using arccos() is unconventional. I would recommend using Math.atan2(), it will return the correct angle in the range -pi to pi.I would also suggest renaming the function to getAngle(), this is less ambiguous. Quote Link to comment Share on other sites More sharing options...
Spahbod Posted March 1, 2012 Report Share Posted March 1, 2012 Nice functions. But the thing we usually use for direction is tangent itself not the angle. So it is better not to use arcs in a separate function to save performance. Quote Link to comment Share on other sites More sharing options...
FeXoR Posted March 1, 2012 Author Report Share Posted March 1, 2012 (edited) You should definitely remove the second var, it will confuse people who are used to block level scoping.I removed the "var" in my script, just didn't want to spam posts.Also in terms of formatting you should either use the style:if (foo){ bar;}or if (foo) bar;(Obviously you cannot use the second if you have multiple statements.) THX, I'm not familiar with javascript. I don't exactly know when to use ";" at the end of the line and it seldom raises warnings when not present. I saw some missing ones in other scripts so if I do something wrong, plz let me know.The function is pretty confusing to read, using arccos() is unconventional. I would recommend using Math.atan2(), it will return the correct angle in the range -pi to pi.I would also suggest renaming the function to getAngle(), this is less ambiguous.Nice functions. But the thing we usually use for direction is tangent itself not the angle. So it is better not to use arcs in a separate function to save performance.Oh, well, figured out the function myself because I didn't find one in the helper functions in the rmgen .js scripts and needed it.The main thing is a function with the same functionality despite the name and the exact code.So please add something like that in the scripts.If I find the time I will adjust the code to you'r recommendations. Edited March 1, 2012 by FeXoR Quote Link to comment Share on other sites More sharing options...
Ykkrosh Posted March 1, 2012 Report Share Posted March 1, 2012 Be careful that trigonometric functions in JS don't return precisely identical results on different platforms (Windows vs Linux etc), so they'll probably cause out-of-sync errors in multiplayer. It's best to avoid them entirely if at all possible (and prefer using vectors maths rather than trigonometry); otherwise we'll probably have to replace the standard versions of the functions with 'safe' (but likely slow and imprecise) versions. Quote Link to comment Share on other sites More sharing options...
quantumstate Posted March 1, 2012 Report Share Posted March 1, 2012 I removed the "var" in my script, just didn't want to spam posts.You can edit your post.THX, I'm not familiar with javascript. I don't exactly know when to use ";" at the end of the line and it seldom raises warnings when not present. I saw some missing ones in other scripts so if I do something wrong, plz let me know.There should be a ; after every statement in javascript. It would be good for you to learn about this, knowing the language well will allow oyu to write better code. Try reading https://developer.mozilla.org/en/JavaScript/Guide/Statements Quote Link to comment Share on other sites More sharing options...
Spahbod Posted March 1, 2012 Report Share Posted March 1, 2012 Oh, well, figured out the function myself because I didn't find one in the helper functions in the rmgen .js scripts and needed it.The main thing is a function with the same functionality despite the name and the exact code.So please add something like that in the scripts.If I find the time I will adjust the code to you'r recommendations. As I said before I am creating more functions for rmgen. But currently my rmgen library and random map scripts are full of buggy things related to the new features I'm planning for alpha 10. Quote Link to comment Share on other sites More sharing options...
Ykkrosh Posted March 1, 2012 Report Share Posted March 1, 2012 There should be a ; after every statement in javascript.Depends what you mean by "should" . I think the language rule is basically that you can always replace a semicolon by a newline, but our (rather incomplete) coding conventions are that you should always include an explicit semicolon. Quote Link to comment Share on other sites More sharing options...
FeXoR Posted March 1, 2012 Author Report Share Posted March 1, 2012 (edited) Be careful that trigonometric functions in JS don't return precisely identical results on different platforms (Windows vs Linux etc), so they'll probably cause out-of-sync errors in multiplayer. It's best to avoid them entirely if at all possible (and prefer using vectors maths rather than trigonometry); otherwise we'll probably have to replace the standard versions of the functions with 'safe' (but likely slow and imprecise) versions.Uh, didn't think of it! But isn't the map generated on the hosting computer and then passed to the other players?And that kind of functions are used in many (if not all) RMGs like sin and cos.I don't what to argue about how to implement them (and that my code sucks and is only meant as an example) but I feel they are really needed!About the ";" thx for the statements, I'll read it when I have time (x)) but I closely listen to your advises!As I said before I am creating more functions for rmgen. But currently my rmgen library and random map scripts are full of buggy things related to the new features I'm planning for alpha 10.I have no problem with that, I'll be patient and rewrite my files when it's added.But until then I "have" to use my own functions even if my RMGs only work in single player... because I'm hot now and I admire it Is there anything wrong with that? Edited March 1, 2012 by FeXoR Quote Link to comment Share on other sites More sharing options...
Ykkrosh Posted March 1, 2012 Report Share Posted March 1, 2012 But isn't the map generated on the hosting computer and then passed to the other players?No - every player runs the script independently. (The generated map data can be quite large, so it would be slow to upload it to every player.)And that kind of functions are used in many (if not all) RMGs like sin and cos.That's potentially problematic. There's some examples here of where functions return different results; presumably the values often get rounded to integer tiles, but in some cases the difference will cause the value to cross the rounding boundary and cause OOS. (The OOS check doesn't verify the terrain data directly, so you'll only notice when the difference influences the behaviour of an entity.) Quote Link to comment Share on other sites More sharing options...
FeXoR Posted March 1, 2012 Author Report Share Posted March 1, 2012 (edited) So the thing to do is only setting objects on tiles (eventually by rounding but only if necessary), never on coordinates (pair of floats) and hope that no missmatch will occur.Things to avoid are then:- ceil and floor (small missmatches of a natural number would often cause inconsistence)- Using polar coordinates for entity/terrain placement (as I have done in my RMG, even if I round afterwards this would not be wise)...and for sure other things I've done allrdy. I will learn... Edited March 1, 2012 by FeXoR Quote Link to comment Share on other sites More sharing options...
FeXoR Posted March 3, 2012 Author Report Share Posted March 3, 2012 Be careful that trigonometric functions in JS don't return precisely identical results on different platforms (Windows vs Linux etc), so they'll probably cause out-of-sync errors in multiplayer. It's best to avoid them entirely if at all possible (and prefer using vectors maths rather than trigonometry); otherwise we'll probably have to replace the standard versions of the functions with 'safe' (but likely slow and imprecise) versions.After a little wile I noticed that it's quite impossible avoiding placing objects on coordinates (z and z in floats).So where can I find a documentation of that 'vector maths' you are suggesting.I only found vector math libraries but don't know if there's a default lib in JS for it and which one you use allrdy (if so).I noticed though that may functions in the rmgen libs use trigonometric functions.Please, answer this one quick so I avoid doing things I have to rewrite.Thanks. Quote Link to comment Share on other sites More sharing options...
quantumstate Posted March 3, 2012 Report Share Posted March 3, 2012 After a little wile I noticed that it's quite impossible avoiding placing objects on coordinates (z and z in floats).So where can I find a documentation of that 'vector maths' you are suggesting.I only found vector math libraries but don't know if there's a default lib in JS for it and which one you use allrdy (if so).I noticed though that may functions in the rmgen libs use trigonometric functions.Please, answer this one quick so I avoid doing things I have to rewrite.The trig functions are being replaced with safe versions, I have done sin and cos, and will be doing atan2 sometime soon. The replacements should be pretty accurate (about 10 decimal places) and reasonably fast, if you avoid using them in big loops then it should be fine.Vector maths is a general area of mathematics, for map making you would only need a basic knowledge though, I don't know where you would go to learn it though. I don't think there are any libraries used in javascript for the game, I just wrote the operations myself when working on the advanced attack code. Quote Link to comment Share on other sites More sharing options...
FeXoR Posted March 4, 2012 Author Report Share Posted March 4, 2012 The trig functions are being replaced with safe versions, I have done sin and cos, and will be doing atan2 sometime soon. The replacements should be pretty accurate (about 10 decimal places) and reasonably fast, if you avoid using them in big loops then it should be fine.Vector maths is a general area of mathematics, for map making you would only need a basic knowledge though, I don't know where you would go to learn it though. I don't think there are any libraries used in javascript for the game, I just wrote the operations myself when working on the advanced attack code.I don't have problems with vector maths, I have the problem that I don't want to what Ykkrosh told me to avoid. And I don't know how to avoid it.If you wrote functions with about 4 decimal points higher accuracy (The error in trigonometric functions in JS in different OS I rad about in posts was about 10**(-6)) wouldn't it be a good idea to include them in other areas of the game as well? Quote Link to comment Share on other sites More sharing options...
historic_bruno Posted March 4, 2012 Report Share Posted March 4, 2012 If you wrote functions with about 4 decimal points higher accuracy (The error in trigonometric functions in JS in different OS I rad about in posts was about 10**(-6)) wouldn't it be a good idea to include them in other areas of the game as well?We've got replacements for sin, cos, atan, and atan2 now, see Math.js. They automatically replace the originals in simulation, random maps, etc. Quote Link to comment Share on other sites More sharing options...
FeXoR Posted March 4, 2012 Author Report Share Posted March 4, 2012 (edited) We've got replacements for sin, cos, atan, and atan2 now, see Math.js. They automatically replace the originals in simulation, random maps, etc. THX, THX, THX!!!! Edited March 4, 2012 by FeXoR Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.