Insane Loop Control



  • <FONT face="Book Antiqua" size=2>This is a few years old but I just came across this site recently. This code was from an existing code base that was being updated for a new game. I changed the name of one of the variables to make it less obvious which game it is (it was very popular) because it would be potentially embarrasing for the programmer who wrote it. ;)</FONT>

     
    <FONT face="Book Antiqua" size=2>Note that this is just one of many examples of similar occurrences. Most of them did not survive until the end but I'd be surprised if they were all gone by the time we shipped...</FONT>
     
    <FONT face="Book Antiqua" size=2>do
    {
    // some code
    } while (is_ocean(x,y) || (rocky_at(x, y) > 1) ||
               (alt_at(x,y) >= ALT_HILLS) || (anything_at(x,y) >= 0) ||
               (y < 2) || (y >= g_app.m_world.ys - 2) ||
               (decent < (12 - (timeout/80))) ||
               (river < (5 - (timeout/120))) ||
               (base_dist < (game.turns?12:6)*g_app.m_world.root/MAP_ROOT) ||
               (base_dist < ((16 - (timeout/64)))*g_app.m_world.root/MAP_ROOT) ||
               (!game.turns && (timeout < 900) && (REGION.size < 36 - (timeout/64))) ||
               ((REGION.fertile * (g_app.m_world.wld[WATER]+1)/2) < (24 + (is_human(who)?(6-DIFF)*8:8) - (game.turns/16) - (timeout/16))) ||
               ((REGION.size < (5-(is_human(who) ? DIFF : 3))*48 - game.turns/16 - timeout/4)) ||
               (shared && ((REGION.size / (bit_count(shared) + 1)) < (64 - (timeout/64)))) ||
               ((site_at(x, y) < 15 - (timeout/64)) && !game.turns && !rules(FLEXIBLE)) ||
               (landmark_dist < (1024-timeout)/128) || !landmark_dist ||
               (game.turns && (map_loc(x,y)->terr > 0)) ||
               //(is_human(who) && !net_game && !game.turns && (vector_dist(x, y, landmark_x, landmark_y) > 8) && (vector_dist(x, y, crater_x, crater_y) > 8) && (timeout < 800)) ||
               ((base >= 0) && (WHOM == by) && (region_at(BASE.x, BASE.y) == reg)));
    </FONT>
     
    <FONT face="Book Antiqua" size=2></FONT> 


  • awesome...

    i just pray that no patches have to be released (screw maintaining THAT!)
    and that the testers are top notch (i can see wierd conditions getting thru the gaps in THAT!)



  • Well it's not pretty but at least the ANDs and ORs are clearly visible,
    and if the actual rules are that complicated then there are likely to
    be gaps in the logic however you code it.



  • There are actually even more WTFs in that:

    DIFF is a variable

    REGION is a macro defined as regions[reg]

    BASE is a macro defined as bases[base]

    WHOM is also macro but I forget the actual definition.

     

    This was "interesting" code to work with...



  • That looks like a lot of fun. Is it a multiplayer RTS or something?



  • What's great is the fact that the second-to-last line is commented out. Good luck catching that without syntax highlighting... :)

    Hmm... is it a world generator or a path finder? I think the first.

    Civilization perhaps? :)



  • @gonzo said:

    That looks like a lot of fun. Is it a multiplayer RTS or something?

    No need for multiplayer, and I'd suggest a turn-based stragegy game, something akin to Civ/Colonize (because there are loads of references to turns, and the routine checks for human player)



  • @masklinn said:

    No need for multiplayer

    there's this though:

    //is_human(who) && !net_game



  • @gonzo said:

    @masklinn said:

    No need for multiplayer

    there's this though:

    //is_human(who) && !net_game

    True that, I had missed it (noticed it after my last reply but alas the forum wouldn't allow me to edit my post), consider the previous answer edited to remove the offending section.



  • Probably civ becuase the poster is from hunt valley, home of firaxis games (makers of civ)



  • Makes me wonder what kind of code was snipped out. Did you notice how many different things are tested disjointly? That means the code should be performed while x,y is in an ocean (assuming the names are meaningful), or while the "descent" is high enough, or region fertility is good enough, or ... Really amazing.



  • My guess too is it's CIV,

     

    I'm no genious programmer, hell I don't even have a programming job!!, but just from reading the types I'm guessing it can only be!!

    I think the realy WTF here is they SHOULD be named and shamed for programming like that!!!



  • @Cratig said:

    I'm no genious programmer, hell I don't even have a programming job!!

    I wonder why!!!



  • I think the code itself isn't fundamentally wrong, it's just unclear. The best way to handle it would be to break it up into nameable logical chunks sharing variables and make those functions of those variables (a function of x,y, and other necessaries; a function of timeout & co.; etc.) and replace parts of the expression with calls to those.

    It isn't a WTF so much as it is more imposing to the maintainer than it ought to be.



  • @zip said:

    @Cratig said:

    I'm no genious programmer, hell I don't even have a programming job!!

    I wonder why!!!

    [:P]



  • A quick lesson in operator precedence wouldn't hurt either.



  • Also, it seems like some kind of listener, in which case the code doesn't strike me as very efficient.



  • I like the magic numbers. Obviously, the region shouldn't be fertile, or it's size should be 5 minus DIFF or 3 based on whether the player is human, times 48, minus the number of turns and a timeout, divided by some random number.



  • @Cratig said:

    My guess too is it's CIV,





    Then the real WTF here is... why haven't they released the entire code
    base to the dedicated fanset of that game and Civ2? There's a petition
    in the works to get the code released so we can fix all the bugs...



  • My guess: Sid Meier's Alpha Centauri.
    <font face="Book Antiqua">(vector_dist(x, y, crater_x, crater_y) > 8) reminds me of the crater area for some reason.  Also, the </font><font face="Book Antiqua">rules(FLEXIBLE) reminds me of one of the options when you create a new game.</font>
    <font face="Book Antiqua">Same with landmarks.  I'd guess it has something to do with searching for where to build the first/next base.  The timeout definitely makes me believe that it's a search algorithm that may not have a best item to find, since if the timeout is low, it may skip the search and use whatever is already found.  Also, it looks closer during different points in the game.  There are a few more items, but I think that's enough.
    </font>


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.