Scripting Atrocities



  • I've seen my fair share of hacky code, hell I've even written up stuff that now makes me want to puke to death and hide myself from the world. (Read: Any code written prior to two months ago.)

    This master-failure, however, does not come from me (thankfully). Rather it's from a community extension to a game called “Garrysmod” (the author is so humble indeed).

     

    Now, you might say the following:

    “Bah, it's a 3rd party fan extension, it's alright if it's a bit hacky.”

    - Or -

    “Give the author a break, he's trying to make something neat.”

    I'd agree in most cases, but this one is almost on relative par with <font color="#000080">Codethulhu</font>.

     

    Start things off, let's put the following code in context. The objective is to make a 'microwave rifle' that makes NPC's head swell up and explode. (It can also throw 'microwave grenades'.) Personally I found this a hilarious little time waster until I started looking at the code. First red flag went up when I saw this:

    First glance....

    That's right, he put all the weapon code into a single file. Most Scripted WEaPons (SWEPs, I didn't choose these names and I'm not making is up)are divided into three files:

     

    cl_init.lua: Anything that has to do with client-side stuff (fx, etc..)

    shared.lua: Anything that “needs to be shared both by the server and client”

    init.lua: Anything that has to do with server-side stuff (synchronization, etc..)

     

    So apparently the entire thing needs to be done both on the client and the server. Humm, strange, but ok, if that's what it takes. I was perplexed but lenient, but soon any forgiveness that was in my mind quickly vanished. I'd try to put this one into words, but words are inadequate, so I'll just show you and explain under each snippet (in case you don't know <font color="#000080">Lua</font>).



    Exhibit A (Or “What's scope indenting?):

    Make sense?

    I know it's very hard, if at all possible, to read the actual code, but that is not the point of this exhibit. If anyone can decode a rational scheme of indentation in this picture, please feel free to email me: evilpineapple <atz> cox <d0tz> n3t (If you're human you should be able to decode this, no spaces.)

     

    Exhibit B (Or, “Rude Goldberg Style”):

    What a mess...

    Rudey would be proud of the author, he goes through all the pain of creating a custom entity type (another file, 1/1/2 kb because it's effectively empty) so he can do all the processing of a 'microwave grenade' in the gun entity code instead of in the grenade entity code. To make this quick let me present a condensed list of these worse-then-failures present in the picture above:

     

    CurTime() Spam: Repeated call to C function when he could have stashed the value as a local

    table.Count(Target): Counts a target... So he can loop 1 to N... When there is a dedicated function (pairs, or ipairs) for table iteration... Right...

    string.find(Offer:GetClass(), "npc_*"): This guy will haunt us into exhibit C

     

    Fun Side Effect: The grenade mysteriously stops working when the player dies and the gun entity is removed.


    Exhibit C (Or “The If-Then Jungle”):

    Logic dictates...that logic did not dictate this document.

    Now you obviously can't read this, but each of those randomly indented sections of code consists of:

    Once again, let me condense this:

     

    'Offer:GetClass()' : He calls this 53 times instead of calling it once and stashing it to a local

    string.find(): He then proceeds to call a string search operation for the exact entity names he wants to filter for. Obviously it's better to search for npc_antlion and then for npc_antlion_worker then to just search for antlion and finish it there.

    Total lack of possibly offending elseifs: If a string search succeeds for one entity name, apparently it's better to keep searching all the others anyways, just in case GetClass changes it's return value in the middle of a Lua callback.

     

    Obviously either the author was naïve (an idiot) or I'm missing something blindly obvious. Note that he could have also coded it as a table with string indexes corresponding to the entity values he wanted. If he'd done that, 200 lines could be condensed into:

    local specs = EntitySpecifications[Offer:GetClass()]

    And bailed out if it was nil. Please also note he does a search for “npc_*” prior to doing all of these ifs. Obviously that was too easy a solution or something.

    Fun Fact: Because “ npc_antlion” is a subset of “npc_antlion_worker” the second string search will never be called if it is an antlion. I guess we all get lucky sometimes.


    Exhibit D (Or “LET'S DO IT AGAIN!”):

    Apparently unsatisfied at his previous set of atrocities he proceeds to do the same again. Remember that initial search for 'npc_*' I mentioned? Well, regardless of if it is an NPC or not, we must check if it's a ragdoll as well!

    Simple!

    See Exhibit C's list of atrocities. Substitute :GetClass() by :GetModel().


    Exhibit E (Or “Just in case, let's copy and paste it around.”):

    Now that we're out of the Think() function (that gets called every frame), let's move onto PrimaryAttack().

    Looks familiar? And a bit farther down:

    Yep, that's right. He copied and pasted that blasphemous code from the Think() function into PrimaryAttack() and added a line or two of code here and there. I can't tell any more if it was supposed to be originally in PrimaryAttack or in Think, but at this point I don't think it matters.

    See Exhibit C and D for atrocities committed.


    Exhibit F (Or “Why bother with the custom entity I made, I'll just use prop_physics.”):

    Remember when I rante-, err, talked, about that grenade processing code in the gun entity and how he should have put it in the grenade entity's code? Well, it turns out he isn't even using it. Poor, littl' WaveGrenade never ever gets used... How sad...

    Interestingly enough he uses all three lua files in it anyway...


    Exhibit G (Or “There's one thing done right. Or rather, there's one thing done right by someone else.”):

    This is the only elegant looking piece of the whole thing. It's also the only one that he didn't write (other then replacing the author & email field), so everything works out in the end.


    Conclusion (Or “Pray he doesn't get hired by M$”):

    There are other atrocities I could point out, like how he spams fx entities and eats up all the ParticleEmitters without releasing them (in a reasonable amount of time). Or how he's said with a sad face that these errors that people get (like 'Out of ParticleEmitters') aren't his fault, that they're an engine issue and he can't fix them. Or how he keeps state information for the gun in a global weapon table so the gun cannot be effectively used by more then one player at once, etc... But all of these pale before one simple fact:


    He's the “b3sT LUA God GM0d h@S Evah S33n!” – Unnamed Follower of His


    <font size="3"> </font><font style="font-size: 16pt;" size="4">However!</font><font size="3"> </font><font size="3">There is one absolutely redeeming fact:</font>

    <font size="3">He released a 'fix'.</font>


    <font size="3"> </font>

    <font size="3"> And we call all now rest safe in the knowledge that things always improve:</font>


    <font size="3">See! No more string.finds! Of course, that doesn't bring much comfort, especially in the light of this:</font>

    <font size="3"> Your eyes aren't lying to you, he really is iterating through all entities present in the game, testing each one by one, to see if it's a vehicle. There are about 1000 to 2000 entities in a game, and he could just have done ents.FindByClass(“prop_vehicle_*”) instead. The only thing I don't get is why he is testing a boolean to see if it's true if the result of such a comparison is the value true. And why he is using network variables on what should be a purely server-side script...</font>


    PS: Anyone want to see his Time-Grenade code? He's back to his old tricks!


    Pardon any mistakes in spelling, it's midnight and I'm getting really annoyed at the fly who's buzzing in the room.




  • I don't know what should shock me more. His code or your way to spend time.



  • /me sings "Refactor! Refactor! Refactor the pain away!"



    I've never really understood the fascination of using randomly indented code, I've seen it before (double-spaced too), probably a side effect of copy-pasting from tutorials/forum postings. shrugs

    Ultimately though he'll be hired by some company that doesn't care about the efficiency of the code written and everyone will suffer.



  • For those that want to know what that comment says, I took the liberty of translating it for you:
    Works

    Yeah, thanks for telling us what the code apparently is supposed to do: work.



  • Next time someone asks me to "send teh codez", I'll send them snippets of this monstruosity. Just for kicks.



  • I'm with HAX on this. TRWTF is that you were willing to spend god knows how much time deconstructing this guy's (admittedly crappy) Lua code just to tell us how much it sucks.



  • And with screenshots of text with ugly syntax highlighting, too...



  • Christ you people are a bunch of whiners.  I thought it was a fine WTF.  The post was a bit long and image-heavy, but it seemed like the best way to convey the sheer lunacy of the code in question.  Not sure what all the Lua hate is about, either, but it seems like a decent language to me.

     

    Anyway, nice first post, OP.



  • @morbiuswilters said:

    Christ you people are a bunch of whiners.  I thought it was a fine WTF.

    Seconded.

    Oh, shit, I just agreed with morbiuswilters. Are there plagues of darkness outside?



  • tl;dr



  • @CDarklock said:

    @morbiuswilters said:

    Christ you people are a bunch of whiners.  I thought it was a fine WTF.

    Seconded.

    Oh, shit, I just agreed with morbiuswilters. Are there plagues of darkness outside?

    Yes, and they are demanding equal recognition before the law, economic opportunities and an end to hate speech like yours.



  • I'm glad to see Lua getting some representation in the WTF world. For a long time it's just been the choice of people who just wanted to add a fast, flexible, powerful dynamic language to their C code. Finally, some real morons are getting into it. Thanks, World of Warcraft!



  • @morbiuswilters said:

    Yes, and they are demanding equal recognition before the law, economic opportunities and an end to hate speech like yours.

    But they're retarded, and they smell funny.



  • @jimheem said:

    tl;dr

     

    then don't fucking post.



  • I'll shed some light on the OP.

    OlivierHamel aka LuaPineapple aka LuaPine is a very much _disliked_ member of the Facepunch community.

    I don't know which is more WTF worthy, him posting here, or his "attempt" to disassemble other peoples code when in fact his code is just as bad.

     

    There is _nothing_ wrong with putting an entire SWEP into the shared.lua file.

     

    [23:23]    <Rambo_Sechs>    LPine is teh greatest Lua God
    [23:24]    <Rambo_Sechs>    That's right, he put all the weapon code into a single file. Most Scripted WEaPons (SWEPs, I didn't choose these names and I'm not making is up)are divided into three files:
    [23:24]    <Rambo_Sechs>    lol.
    [23:24]    <Rambo_Sechs>    you can cram an entier swep into one file if you jus tmark shit as server/client only
    [23:25]    <SamuraiMushroom>    rofl
    [23:25]    <SamuraiMushroom>    even people who don't even know him are telling him he's an idiot
    [23:25]    <Rambo_Sechs>    Wow, you're dissecting a shitty 5-second Gmod SWEP, LPine. Good to see



  • @Marine said:

    OlivierHamel aka LuaPineapple aka LuaPine is a very much _disliked_ member of the Facepunch community.

    Nobody cares about your faggy* little mod community flamewars.

     

    @Marine said:

    I don't know which is more WTF worthy, him posting here, or his "attempt" to disassemble other peoples code when in fact his code is just as bad.

    Then post his code or STFU.  This isn't "The Daily Butthurt Feud Over A Shitty Mod That Nobody Plays".

     

    @Marine said:

    Filed under: ... I dont know how to use the tag cloud so i'm going to spam it up instead

    Is it really that difficult?  Don't all tag clouds behave the same?

    <input name="ctl00$ctl00$bcr$bcr$ctl00$PostList$ctl16$ctl23$ctl01" id="ctl00_ctl00_bcr_bcr_ctl00_PostList_ctl16_ctl23_ctl01_State" value="value:Filed%20under%3A%20%3Ca%20href%3D%22%2Ftags%2FCommunity%2Bserver%2Bis%2Ba%2Bpile%2Bof%2Bshit%2Fdefault.aspx%22%20rel%3D%22tag%22%3ECommunity%20server%20is%20a%20pile%20of%20shit%3C%2Fa%3E%2C%20%3Ca%20href%3D%22%2Ftags%2FI%2Bdont%2Bknow%2Bhow%2Bto%2Buse%2Bthe%2Btag%2Bcloud%2Bso%2Bi_2700_m%2Bgoing%2Bto%2Bspam%2Bit%2Bup%2Binstead%2Fdefault.aspx%22%20rel%3D%22tag%22%3EI%20dont%20know%20how%20to%20use%20the%20tag%20cloud%20so%20i'm%20going%20to%20spam%20it%20up%20instead%3C%2Fa%3E" type="hidden">

     

    <font size="1">* I've heard this used on broadcast TV so I consider it fair game now.</font>



  • Not to say what you should or shouldn't post, but I wouldn't get worked up about finding WTFs in a minor game mod. The idea of creating a game mod will inspire many a young 'un to take up code who otherwise wouldn't. Consider it in the spirit of not mocking student code on this site. Either that, or minor game mods are fertile WTF ground.

    Back in the day (early 2000s) I frequented a newsfroup for a certain popular modable online game. I let it be known that I was a developer (I was more chatty online than I am now). Anyway, I got a couple of unsolicited 'so how would I take up coding?' emails.



  • @Wang Broom said:

    Oliver Olivier Hamel you are a shitbrick.

    I'm starting to think he is, just for introducing so many retards to this site.



  • He goes by the name "Oliver" on the community where he came from. So how about you stop correcting trivial typos you fucking pleb.



  •  This thread fails. Hard.



  • @Wang Broom said:

    He goes by the name "Oliver" on the community where he came from. So how about you stop correcting trivial typos you fucking pleb.

     

    You're doing so well here. Join up, insult everyone, bitch about genuinely shitty code being posted on a site mostly concerned with laughing at shitty code.

    So where's this terrible code by Oliver/Olivier, or whatever the fuck his/her name is?

    By the way, no one knows or cares who your community members are in person. We're laughing at the code. No one from the forum is coming round to your house to insult your software until you cry.



  • Yee-haw! Break out the popcorn, it's time for a roast!



  • @Wang Broom said:

    He goes by the name "Oliver" on the community where he came from. So how about you stop correcting trivial typos you fucking pleb.

    Wait, so is it a typo or isn't it?  And which is worse: correcting trivial typos or working your already over-taxed and cholesterol-laden heart into a coronary over the triviality of someone mocking the shitty code for a faggy little game mod nobody gives a fuck about?  Maybe you should turn off the game, stand upon your lardy hocks and waddle your way outdoors for some fresh air?  Instead of getting emotionally invested in the community of basement-dwelling nerds surrounding a video game maybe you could do something just a tad less gay, like taking up watercolors or glory-holing anonymous men in a bus stop restroom?

     

    The possibilities are as limitless as the elastic band on your pants.



  •  For the record, some people like myself are tired of taking a bad rap for liking/playing/modding garrys mod just because of facepunch.  Facepunch is just above /b/ in terms of forum quality IMO, and should be avoided by anyone wanting actual help, not just slings of insults for not knowing everything about their stupid little community immediately after walking in.

     We're not ALL just a punch of basement dwelling idiots trying to enhance our e-peens by making TEH BEST L33T M0D EVAR!!!.  Some of us just like playing with the thing.



  • @Wang Broom said:

    oh wow a joke about me being fat

    No.  High cholesterol != fat.  If I hadn't known this before, this was conveyed very clearly by my last roommate in school - weighing in at 250, his cholesterol was at something around 115 - and his ratio was good, too, despite the fact he didn't exercise.  Meanwhile, at a scrawny 160 pounds (for someone over 6' tall), I watched my diet, I ran for about an hour every day, and my cholesterol was at 200.

    I've known someone who was skinnier than me, who watched his diet, worked out every day for over an hour, and was on statins, and he still had a cholesterol level of 230 (note: a big improvement; he was over 350 when he was first checked.)

    The only valid association I know of between the two is that many fast food restaurants serve food which is bad for both proper cholesterol levels and proper weight.

    @Wang Broom said:

    i'm saying Oliver is a hypocritical sack of shit.

    And we're saying, "Show us the code."



  • @morbiuswilters said:

    Don't all tag clouds behave the same?

    You mean there's *[b]others[/b]*?  I thought that was a lovely dailyWTF special, just for its own extra WTF charm.

    By all rights, that should be a contender for the most horrible thing I've read all day.  Sad to say, it doesn't even make the top 10 today.

    With any luck, if you confirm that there are, in fact, other tag clouds, I can read it tomorrow, and it could be the most horrible thing I read tomorrow.

    Oh god, I've been here too long.  I need a better definition of luck.



  •  *Puts on bulleproof vest & another kevlar jacket*

    Let me make this quick:

    * FP sucks, MC's conclusion is/was correct

    * Lua does not suck, it's just been horribly miss-applied to applications and not properly used

    * It doesn't matter that this WTF comes from from hobby code, it's still a WTF for me when I started wondering about the bugs that cropped up and I took a look at the code to see if I could fix it. This eventually resulted me in re-writing the whole damn thing into a MP compatible version using a table DB for referencing brain-explosion fx specifics, as well as being a shitload more efficient too

    * I admit I'm a hypocritical sack of shit in some respects, and since I first started programming in earnest in GM, it won't take much work to find some utter shit I showed to the world when I was starting out. While we're on the subject, let's see that first 'encrypter' (massive case character substitution) program that was your first piece of work and see if you're proud of it. Within ~two years I went from things not far better then this (but all my things were better then this AFAIK) to writing a full-physics, 64 player, Freespace 2 clone gamemode (incomplete), an online distribution system which streams and updates addons directly to users by a click of a button with an easy GUI (90% complete), and writing my own engine which is using a hybrid raytrace-rasterizer renderer and volumetric-consistent deformable physics engine & unique fast creation/update spatial partitioning structure with variable resolution and periodic self-refining (I'll admit the whole thing is incomplete and that right now it explodes if you even just sneeze on it)

    * I admit that me writing this up is a WTF in itself, I was bored and I wanted to write an article and as I said to the editor of TDWTF, "I think it's shit, but you said to send it anyways"

    *  I'd like to apologize to TDWTF & it's forums, I'm sorry for attracting this batch of moronic suckups from FP

    *  Yes, there are grammatical mistakes in this text, such as time agreement, etc...

    * I hope I gave a WTF to some of you at least

     

    PS: Contrary to popular belief, hacks are not always horrible crimes against humanity, it always depends on when, where, and on what

    PPS: It's 'Olivier', get it right at least!

    PPPS: No one can write god-like code 100% of the time, but no one should write code like this more then .1% of the time on personal hack-assistance code as well.



  •  'So where's this terrible code by Oliver/Olivier, or whatever the fuck his/her name is?'

    Just look at anything older then three/four months old that I released. Anything older then that is pure shit in my opinion.

    That said, the initial beta of my Catmull-rom Spline Cameras is probably the most recent thing that I wouldn't spit on. It's got hacks in quite a few places and inelegancies up the arse but it's far more coherent then much of what's release for GMod anyways. I probably went overboard on features though, leading to moderate code-bloat.

    'He goes by the name "Oliver" on the community where he came from.'

    No I don't, FP can screw itself right off the map of the internet and keep beating it's members harder then the prisoners at Guantanimo Bay for all I care. I do not associate myself with them anymore.

     

    On topic:

    I have a new theory: His entire code was one massive attempt at obfuscation to prevent reverse engineering.

    Though I somehow doubt it....

     

    PS: There is one obvious typo in the post above this, 'full-physics' was supposed to be 'full-physics rewritten'



  • @Olivier Hamel said:

    No I don't, FP can screw itself right off the map of the internet and keep beating it's members harder then the prisoners at Guantanimo Bay for all I care.

    The rest of your posts are fine, but I'd just like to point out that nobody at GTMO is being beaten.  It's actually a remarkably well-run prison that shows far more respect to the human scum they are housing than I would be capable of.



  • Olivier - You have made your point rather nicely in my opinion. Everyone deserves a chance to defend themselves. Don't stoop any lower though, they're not worth it.



  •  Yah I know, I was just using it as a comparative of a horrible place to be, FP is basically a Nazi Concentration Camp which has some kind of total ban on humour of any kind.

    "Olivier - You have made your point rather nicely in my opinion. Everyone deserves a chance to defend themselves. Don't stoop any lower though, they're not worth it."

    Humm, you are correct. I'll shall let them return to their concentra-, errrr, Facepunch.

     

    Everyone else: Enjoy this WTF if you will.



  • @Wang Broom said:

     godwin's law at the speed of light, ladies and gentlemen!

    So now Godwin's Law defies Einstein's law?



  •  another tepid burn by mobiuswilters



  • @Wang Broom said:

     another tepid burn by mobiuswilters





    What can I say, he's good at it :)



    I want to hate morbius but I can't... he's like the little troll that could...



    Now, back on topic - Wang (brooms handles are wood, which is a common reference of head, so from now on I will, with a bit of a stretch, refer to you as dickhead) ahem, dickhead, you said yourself that the SWEP is poorly coded - yes, the post was a bit overdone but it's valid. Please, fuck off and don't come back.



    toot toot!



  • @tgape said:

    No.  High cholesterol != fat.  If I hadn't known this before, this was conveyed very clearly by my last roommate in school - weighing in at 250, his cholesterol was at something around 115 - and his ratio was good, too, despite the fact he didn't exercise.  Meanwhile, at a scrawny 160 pounds (for someone over 6' tall), I watched my diet, I ran for about an hour every day, and my cholesterol was at 200.
     

    If you were still in school, I assume you are/were under 35.

    If you're young (in this case, about under 35) high cholesterol is rarely a problem. If someones cholesterol is extremely high or he has vascular problems, it is almost always caused by a (often genetic) disease or factor. Extreme obesity excluded.

    Cholesterol only becomes a problem after it sticks to little defects in the blood vessels. These defects naturally occur, but are greatly increased by high blood pressure.

    So I wouldn't worry about your cholesterol just yet, just stay in shape and watch your bp and you're fine.



  • @morbiuswilters said:

    So now Godwin's Law defies Einstein's law?
     

    Please use your time machine and prevent this thread from ever being born morbius.



  • @dtech said:

    @morbiuswilters said:

    So now Godwin's Law defies Einstein's law?
     

    Please use your time machine and prevent this thread from ever being born morbius.

     

    I wasn't able to stop the thread from being born, but I did stop by the early 90s to pick up enough Crystal Pepsi for everyone.



  • @morbiuswilters said:

     

    I wasn't able to stop the thread from being born, but I did stop by the early 90s to pick up enough Crystal Pepsi for everyone.

     

    Oh man, that stuff was the shit.  *drinks like a crack addict*



  • @morbiuswilters said:

    I did stop by the early 90s to pick up enough Crystal Pepsi for everyone.

    OMG OMG OMG I LOVE CRYSTAL PEPSI!!!!!!

    No, seriously, I love Crystal Pepsi. I miss it.

    But does anyone remember Jooky? That shit was a party in a can.



  • @CDarklock said:

    @morbiuswilters said:

    I did stop by the early 90s to pick up enough Crystal Pepsi for everyone.

    OMG OMG OMG I LOVE CRYSTAL PEPSI!!!!!!

    No, seriously, I love Crystal Pepsi. I miss it.

    But does anyone remember Jooky? That shit was a party in a can.

    This is the one that I miss:

    New York Seltzer

    This is Raspberry flavored New York Seltzer, and, unlike Jooky, it was refreshingly non-fictitious.... I could drink that stuff all day long.


Log in to reply