Simple point script

Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 38
  1. #16
    I'm overrated. Fraysa is offline
    MemberRank
    Apr 2008 Join Date
    4,891Posts

    Re: Simple point script

    Quote Originally Posted by xStr0nGx View Post
    You shouldn't use ternary for such a long thing. Thats when arrays (as mentioned above), ifs, switch-case come to the script.

    Ternary in my opinion should be used for short terms as its very ineffeicient and very hard for the reader to get whats going on when its huge.

    Fraysa's method is what I would use for an instance.
    I believe the compiler would optimize the same for ternary or if statements. The only disadvantage is readability, but that wouldn't really matter because the majority of this community doesn't care about that.

    Please base your statements with proofs or show why are you stating that.

  2. #17
    unknowndog NmZero is offline
    MemberRank
    Jul 2012 Join Date
    unknowndogLocation
    202Posts

    Re: Simple point script

    Quote Originally Posted by Fraysa View Post
    I believe the compiler would optimize the same for ternary or if statements. The only disadvantage is readability, but that wouldn't really matter because the majority of this community doesn't care about that.

    Please base your statements with proofs or show why are you stating that.
    I think he meant to say:
    "I dont personally like using ternary
    EXCEPT
    when using long sentecnes (or w/e)
    for example:"
    Code:
    options += "\r\n#L"+x+"##i"+ITEMS[s][x][0]+"# "+cr()+"#t"+ITEMS[s][x][0]+"##k for "+(cm.getPlayer().isGM() === false ? ""+ITEMS[s][x][1]+" EP" : ""+cr()+"FREE")+"#l";
    He never said "ternary is bad"

  3. #18
    I'm overrated. Fraysa is offline
    MemberRank
    Apr 2008 Join Date
    4,891Posts

    Re: Simple point script

    Quote Originally Posted by nmzero View Post
    i think he meant to say:
    "i dont personally like using ternary
    except
    when using long sentecnes (or w/e)
    for example:"
    Code:
    options += "\r\n#l"+x+"##i"+items[s][x][0]+"# "+cr()+"#t"+items[s][x][0]+"##k for "+(cm.getplayer().isgm() === false ? ""+items[s][x][1]+" ep" : ""+cr()+"free")+"#l";
    he never said "ternary is bad"
    Quote Originally Posted by xStr0nGx View Post
    it's very inefficient
    #tooshort

  4. #19
    Moderator Eric is offline
    ModeratorRank
    Jan 2010 Join Date
    DEV CityLocation
    3,188Posts

    Re: Simple point script

    Quote Originally Posted by Fraysa View Post
    I believe the compiler would optimize the same for ternary or if statements. The only disadvantage is readability, but that wouldn't really matter because the majority of this community doesn't care about that.

    Please base your statements with proofs or show why are you stating that.
    Oh, good one. I completely missed that part but yes, I had researched this myself and it has been said numerous times that ternary operations don't differ in performance from an if-else statement. Which is why in my eyes, if a ternary operation and a if-else statement are equal in performance, and a ternary is shorter than doing several if-else-if-else statements, I will result in using a ternary over if statements and even switch (however, I have not checked performance between switch and if-else, though I have heard that switch is faster than if-else).

  5. #20
    C# developer xStr0nGx is offline
    MemberRank
    Dec 2013 Join Date
    UnknownLocation
    659Posts

    Re: Simple point script

    Quote Originally Posted by Fraysa View Post
    If one want to become a greater coder, his codes should function and be readable

    as the end-users (the developers who may receive jobs from others on resuming this development) will find it easier

    and be much more thankful than the codes that werent coded this way.


    But yeah, every developer got his own conventions. However if he is to release something, I personally would

    expect a readable code and not a complex one (massive lines of code without side-comments).

  6. #21
    Novice Sunnyday is offline
    MemberRank
    Dec 2014 Join Date
    wonderlandLocation
    3Posts

    Re: Simple point script

    Good job m8

  7. #22
    Proficient Member ALotOfPosts is offline
    MemberRank
    Sep 2014 Join Date
    181Posts

    Re: Simple point script

    Quote Originally Posted by chunkarama View Post
    Oh, good one. I completely missed that part but yes, I had researched this myself and it has been said numerous times that ternary operations don't differ in performance from an if-else statement. Which is why in my eyes, if a ternary operation and a if-else statement are equal in performance, and a ternary is shorter than doing several if-else-if-else statements, I will result in using a ternary over if statements and even switch (however, I have not checked performance between switch and if-else, though I have heard that switch is faster than if-else).
    Switch is the same, at least in old Javas. At least the compiler is supposed to implement it the same. There have been some tests that show that switch is faster, but they seem pretty inconclusive. There's some interesting ways to set up tests that it would give you data that you want. See this for some weirdness.

    As a side note about readable code and shorter code:
    Please stop thinking shorter code is better and ternary operations are that good.

    Ternary is pretty terrible if they consist of long clauses. Ternary is extremely bad looking if you chain them together. I'm not saying I don't use them, because I have used them plenty of times. It does the same thing as an if statement but at least if statements are more clear and readable. Take the example earlier. It takes a much longer time to figure out what's going on in
    Code:
    options += "\r\n#l"+x+"##i"+items[s][x][0]+"# "+cr()+"#t"+items[s][x][0]+"##k for "+(cm.getplayer().isgm() === false ? ""+items[s][x][1]+" ep" : ""+cr()+"free")+"#l";
    than if you did
    Code:
    options += "\r\n#l"+x+"##i"+items[s][x][0]+"# "+cr()+"#t"+items[s][x][0]+"##k for ";
    if (!c.getPlayer().isGM()) {
         options += items[s][x][1]+" ep";
    } else {
         options += cr()+"free")+"#l";
    }
    There's no performance difference in either. One of them is a lot easier to understand and debug. Long ternary operations are among the worst to debug along with undescriptive variable names and uncommented code chunks. Personally I've never seen it used in any production/deployed code because it wouldn't pass any reviews. There are many times when shorter isn't better. Compilers these days do very well in optimization and so the user should never try to optimize by him/herself.

    You can do things like x << 1 instead of x * 2, to change a function call into a bit manipulation to "optimize" your code, but the compiler will already do that. Even things like int x = 5 + 5; The compiler will probably evaluate that during compile time.

    Anyway going back to the branch issue from the url above, as you noticed that sorting the array makes it a lot faster, but sorting the array adds code lines. Obviously sometimes adding additional code lines makes the code run faster. Shorter code almost never really implies better code, especially when algorithms are around Sometimes a trivial method could have a runtime complexity of n^2 (pure for loops), but then when you think about clever ways to approach a problem, you can bring a problem down to log n, exponentially better.

    I know @xStr0nGx is hated around here, but I'd have to agree with him. Sometimes when you release things you want to make things readable for everyone. You've all seen XxOsirisxX's one line for loops. They're cool in concept, but not really for practical use.

  8. #23
    unknowndog NmZero is offline
    MemberRank
    Jul 2012 Join Date
    unknowndogLocation
    202Posts

    Re: Simple point script

    Quote Originally Posted by ALotOfPosts View Post
    I know @xStr0nGx is hated around here, but I'd have to agree with him. Sometimes when you release things you want to make things readable for everyone. You've all seen XxOsirisxX's one line for loops. They're cool in concept, but not really for practical use.
    I have to agree as well, but when you are making new things yourself you tend to see them more clearly; since you have written the line/code yourself. This can sometimes make it hard to detect what is normally readable for the public in my opinion.
    I don't know if the following example also applies for higher 'skill level' but here is what I mean:

    this is how somebody who never heard of loops would write a npc script that sells things:

    PHP Code:
    function action(mts) {
    //mode blabla
    if (status === 0) {
      
    cm.sendSimple("#L1# selection 1#l\r\n#L2#selection 2#l etc etc etc");
    } else if (
    status === 1) {
      if (
    selection === 1) {
        
    cm.warp(91000001);
      else if (
    selection === 2) {
        
    cm.warp(91000002);
    // etc etc etc
    }

    here is how anybody who heard of loops would make this:

    PHP Code:
    function action(mts) {
    //mode blabla
    if (status === 0) {
      var 
    text "blabla\r\n";
      for (var 
    110i++)
        
    text += "#L"+i+"#selection "+i+"#l";
    } else if (
    status === 1) {
      var 
    teleport 91000000 i;
      
    cm.warp(teleport);

    If a person who never heard of loops opened both scripts, which one would be more readable for him?
    script 1 ofcourse

    anyway, I get that:
    PHP Code:
    options += "\r\n#l"+x+"##i"+items[s][x][0]+"# "+cr()+"#t"+items[s][x][0]+"##k for "+(cm.getplayer().isgm() === false ""+items[s][x][1]+" ep" ""+cr()+"free")+"#l"
    is a little bit unreadable for everybody on first sight.
    All I'm saying is that wether its readable is also an opinion.

  9. #24
    Proficient Member ALotOfPosts is offline
    MemberRank
    Sep 2014 Join Date
    181Posts

    Re: Simple point script

    That is true but a better comparison is doing:

    for (var i = 1; i < 10; i++, text += "#L"+i+"#selection "+i+"#l");

    Also starting at 1 is an interesting choice if you're doing a less than in the second part. Some people may be confused because they wouldn't be sure if you want to go from 0-10, 1-10, 1-9, but that's not really an issue.

    For the original for loop, I'm pretty sure anyone would be ok with it if there was a comment added or if there was an example of what was supposed to be there. The code is clear enough. There really is just no reason to do cond1 ? a : cond2? b : cond3 ? c : cond4 ? d : cond5? e : f.

    There's also the SuperLol's status changer, which is status = 2 * mode - 1; for status++ if mode = 1, status-- if mode = 0. This just gets interesting when someone packet edits mode to be something else. You can probably skip a few statuses and if key variables are set during those times, it might be exploitable.


    function
    cr() {
    var
    colors = ["b", "d", "g", "r", "k"];
    return
    "#" + colors[(Math.floor(Math.random() * 5))];
    }


    The way you are using cr() is a bit excessive. You may want to look into making a function that takes a string as an argument and returns a randomized color string. Probably a syntax error somewhere here but you get the idea.


    function
    cr(strings) {
    var text = "";
    var
    colors = ["b", "d", "g", "r", "k"];
    for (var i = 0; i < strings.length(); i++) {
    text += "#" + colors[(Math.floor(Math.random() * 5))] + strings.charAt(i) + "#";
    }

    return text;
    }

  10. #25
    unknowndog NmZero is offline
    MemberRank
    Jul 2012 Join Date
    unknowndogLocation
    202Posts

    Re: Simple point script

    Quote Originally Posted by ALotOfPosts View Post
    That is true but a better comparison is doing:

    for (var i = 1; i < 10; i++, text += "#L"+i+"#selection "+i+"#l");
    I didn't even know this was allowed/possible.

    Quote Originally Posted by ALotOfPosts View Post
    Also starting at 1 is an interesting choice if you're doing a less than in the second part. Some people may be confused because they wouldn't be sure if you want to go from 0-10, 1-10, 1-9, but that's not really an issue.
    The id 91000000 is main free market map, the 1 to 24 are free market rooms, so basically this could be used to warp to a room without getting to main fm first. But like you said, thats not really the issue here.

    Quote Originally Posted by ALotOfPosts View Post

    function
    cr() {
    var
    colors = ["b", "d", "g", "r", "k"];
    return
    "#" + colors[(Math.floor(Math.random() * 5))];
    }


    The way you are using cr() is a bit excessive. You may want to look into making a function that takes a string as an argument and returns a randomized color string. Probably a syntax error somewhere here but you get the idea.


    function
    cr(strings) {
    var text = "";
    var
    colors = ["b", "d", "g", "r", "k"];
    for (var i = 0; i < strings.length(); i++) {
    text += "#" + colors[(Math.floor(Math.random() * 5))] + strings.charAt(i) + "#";
    }

    return text;
    }
    PHP Code:
    function caw(string) { //caw = color all words ^_^
        
    var colors = ["b""d""g""r""k"];
        var 
    color string.replace(/ /g"_");
        var 
    amount color.split("_").length 1;
        for (var 
    0amounti++)
            
    color color.replace("_"" #" colors[(Math.floor(Math.random() * 5))]);
        return 
    color;


  11. #26
    Account Upgraded | Title Enabled! AristoCat is offline
    MemberRank
    Apr 2012 Join Date
    947Posts

    Re: Simple point script

    wouldnt it be easier to
    PHP Code:
    function sendNext_color(str) {
        var 
    colored "";
        var 
    splitted str.split(" ");
        for 
    each (var i in splitted)
            
    colored += cr() + i;
        
    sendNext(colored);

    not sure if works didnt test
    Last edited by AristoCat; 12-03-15 at 03:51 PM.

  12. #27
    unknowndog NmZero is offline
    MemberRank
    Jul 2012 Join Date
    unknowndogLocation
    202Posts

    Re: Simple point script


    meh, idk :p

  13. #28
    C# developer xStr0nGx is offline
    MemberRank
    Dec 2013 Join Date
    UnknownLocation
    659Posts

    Re: Simple point script

    Quote Originally Posted by NmZero View Post

    meh, idk :p

    PHP Code:
    function sendNext_color(preText) {
        var 
    coloredText "";
        var 
    words preText.split(" ");
        for 
    each (var word in words)
            
    coloredText += cr() + word;
        
    sendNext(coloredText);

    Is what he meant.

  14. #29
    unknowndog NmZero is offline
    MemberRank
    Jul 2012 Join Date
    unknowndogLocation
    202Posts

    Re: Simple point script

    Its getting a bit off topic here, but why is strong so hated. He made a good point.
    more half offtopic things:
    PHP Code:
    function color(str) {
        var 
    colored "";
        var 
    splitted str.split(" ");
        for 
    each (var i in splitted)
            
    colored += cr() + " "+i;
        return 
    colored;

    correct code + can be used for any cm. text

    I've been trying to do this as well
    PHP Code:
    function cal(string) { //cal = color all letters ^_^
        
    var text "";
        var 
    colors = ["b""d""g""r""k"];
        
    //text += string.charAt(17-2);
        
    for (var 0string.length(); i++)
            
    // if (string.charAt(i - 1) !== "")
                
    text += "#" colors[(Math.floor(Math.random() * 5))] + string.charAt(i);
            
    // else
                // text += string.charAt(i);
        
    return text;
        
    // var colors = ["b", "d", "g", "r", "k"];
        // var color = string.replace(/""/g, "_");
        // var amount = color.split("_").length - 1;
        // for (var i = 0; i < amount; i++)
            // color = color.replace("_", "#" + colors[(Math.floor(Math.random() * 5))]);
        // return color;

    Color each letter differently, the only problem here is that it uses the standard string which includes \r\n multiple times, the result is this:


    The commented things are the things I've tried, but so far I've had no succes :c

    on topic:
    updated script with aristocat's color method

  15. #30
    Novice Xenoverz is offline
    MemberRank
    Mar 2015 Join Date
    WonderlandLocation
    1Posts

    Re: Simple point script

    Looks nice !



Page 2 of 3 FirstFirst 123 LastLast

Advertisement