1. Hey Guest, is it this your first time on the forums?

    Visit the Beginner's Box

    Introduce yourself, read some of the ins and outs of the community, access to useful links and information.

    Dismiss Notice

Adventures in modding

Discussion in 'Modding [KAG]' started by JaytleBee, May 16, 2016.

  1. JaytleBee

    JaytleBee Ballista Bolt Thrower
    1. [Server] Sandbox Reborn

    Messages:
    117
    (this posts borders on offtopic, so mods, please move it if it doesn't belong here)

    Okay, so you probably know this: You're just minding your own mods, maybe basing them off someone else's or base code and just see something that simply doesn't make sense/is extremely redundant. Here's the place to post that.

    Here's a start:
    1.PNG
    Wow, someone seems to be a lazy typer

    3.PNG
    Cause for loops are too mainstream

    4.PNG
    the image says it all
     
  2. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    While I'll agree that the last case in particular is blatant copy-paste that's been missed, the first one "Lazy typer" increases the legibility of any code using it, which is an improvement in itself. Which would you rather be reading, if(tokens.length() >= MAP_NAME && hasCommand(player, "loadmap")) or if(tokens.length() >= MAP_NAME && getSecurity().checkAccess_Command(player, "loadmap")) ?

    That said, I think it's good to have a topic where you can go through code that you feel doesn't make sense and ask questions about what's going on there - often times there's "a reason", but whether that's a good one or not is always up for discussion ;) Always good to have a dialogue going.
     
  3. makmoud98

    makmoud98 You are already DEAD Forum Moderator Staff Alumni Tester

    Messages:
    586
    No not really, this is just no reason to waste more lines of code by creating a security and boolean variable beforehand. Nothing very complicated in there.

    Nothing wrong with a while loop here, it literally does the same thing a for loop could've done. While loops can be especially useful when you don't know how many times it needs to iterate, but not in this case. Still though, nothing wrong here.

    The weird thing here is that mapname and map_name are being declared twice. I'll admit though, that I sometimes do the same thing here because sometimes it is easier to copy and paste the code I just wrote than to move shit around.
     
  4. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    Silly mak; the weird thing is that the two sections are exact duplicates and the second will never be run, the commands are the same :)
     
  5. makmoud98

    makmoud98 You are already DEAD Forum Moderator Staff Alumni Tester

    Messages:
    586
    haha i completely missed that part, i just assumed that there was something slightly different
     
  6. JaytleBee

    JaytleBee Ballista Bolt Thrower
    1. [Server] Sandbox Reborn

    Messages:
    117
    Of course the while loop does the same thing, but here you have to look at more code to see at what step it starts (which is declared ~5 lines up) and where step is changed (which, in a for loop is usually only in the header)
    Working code != Good code
     
  7. Asu

    Asu THD Team THD Team Forum Moderator

    Messages:
    1,580
    :(

    Edit : On a serious note, I don't think you got this from the latest version of moarCommands but rather on the legacy branch. The code got lots of style & optimization improvements in the rewrite branch that never really came out. This function actually proved out to be useful because IIRC this specific one was adapted to have more flexibility (i.e. it checks for a seclev called "mc_all").
    Edit : Wait is this even my mod?
    Edit : Ok, this isn't my mod. *fades out*
     
    Last edited: May 17, 2016
  8. zerd

    zerd Arsonist
    1. SIEGE Clan - SIEGE

    Messages:
    47
    I think the main problem with the first "lazy typer" example is that the command name given to the function, is not the command name it actually checks for. Instead it uses a hardcoded string value simply consisting of "command".
     
    JaytleBee likes this.
  9. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    The while loop one is either taken directly from vanilla or a mod that copies it directly.

    Actually makes sense from a seclev point of view (as if you want to have 1000 extra seclevs), but I'll be honest and say I missed this :)
     
  10. JaytleBee

    JaytleBee Ballista Bolt Thrower
    1. [Server] Sandbox Reborn

    Messages:
    117
    The while loop is actually the only vanilla thing in here
     
  11. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    Nothing terribly wrong with these examples, in my opinion, but there are some underlying annoyances in KAG's code (some which are also visible here). The mixing of class methods, global functions and script functions for instance, or the inconsistency in naming conventions. And of course, difficult/un-intuitive names. There's more, but I just can't think of it right now.

    There are reasons for things like this. Some of it is bound to happen as soon as the project is worked on by more than one person, even if they coordinate well. Also as a project ages, the work to keep things tidy increases, and it might still get scrapped down the line so you have to weigh the cost versus benefit. In KAG's case, there's also the fact that somewhere along the development process, they decided to add scripting support. Maintenance of the C++ code is probably not aligned that well with what is happening with the scripts, i.e. not usually touched. It's in the script the logic resides. I don't know how much they had to tweak the engine for Trench Run for example, but not much by my guess. Anyway, people might feel a certain aversion too, over time, against touching old and possibly hairy code. These things shown here is nothing in comparison.
     
  12. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    fwiw this one (though there are some that break the rule) is actually just a strange convention. Methods that returns something is in lowerCamelCase. Stuff that doesn't is UpperCamelCase. Server-only stuff gets server_ appended and then (usually) is UpperCamelCase. Its madness, but there's a pattern to it; you can quiz MM on the logic.

    Otherwise, yeah you're generally right. The core of the engine code is pretty much set in stone, or at least set in spaghetti. The recent slew of rendering bugs is testament to that. Furai fixed the windows server not downloading API-hosted mods recently, which resulted in the windows server crashing :) Fixing that meant everything "works as expected" but it's like playing whack-a-bug.

    The scripts themselves also languish over time though - noone wants to touch the builder code any more, for example.

    Not a lot changed in the "core" of the engine for TR - but a lot of stuff was exposed. The map system in TR totally replaces all the tiles in KAG and their behaviours. TCPR support was tightened up a little to allow communicating with the TR backend (longer lines supported, random disconnects fixed, works on windows). Some weird bugs with adding/removing shapes from objects was fixed. Being able to change the map-edge behaviour was fixed. Shaders were exposed to scripts + received an "extra texture" input. The rendering system was partly forked to allow rendering to a lower-resolution framebuffer.
     
    Asu likes this.
  13. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    Ooookay. Yes. Sometimes you have project members who are being clever. Hehe. I would probably be one of those myself ::D:
     
  14. Asu

    Asu THD Team THD Team Forum Moderator

    Messages:
    1,580
    Please don't. It's totally unobvious and I'm pretty sure there wasn't more than two modders that ever knew this here (at least until Geti said it). I always had to check the method exact names because I never knew what was the exact syntax.
     
    JaytleBee likes this.
  15. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    On notation choices - offtopic but worth talking about since it's something new programmers often get hung up on. Like most things, it's a totally religious argument and there's no "right answer".

    If there is a right answer though, I don't think that it's the system used in crimson ;)

    I'm currently of a fan of underscore_lowercase for methods and variables, _underscore_prefix for "dont use directly" or "read only" variables (private if you want but these can exist in structs too) and UpperCamelCase for types. UNDERSCORE_CAPS for macros.

    I'm slowly coming around to the idea of underscore_lowercase for everything except macros though; screw that shift key. It gives a little less info away, but fits in well with standard cpp stuff and is harder to make typing mistakes with.

    Never got into the typename_t pattern tbh, gives me too much win32 hungarian notation PTSD.

    The main thing with any of it is being consistent. If you want your LPCSImpossibleToRead type names go for it. If you ReallyLoveJavaStyleNotation then hey, noone's stopping you. Just make sure that you dont jump schizophrenically between multiple styles - the crimson "rules" feel that way to anyone who doesn't know them, which is a sign they aren't great ones to follow.

    Same goes for indentation - it "doesn't really matter" as long as you have a pattern, and that pattern is easy enough to understand.
     
    Asu and makmoud98 like this.