Search code examples
mathobfuscationformulabasicdeobfuscation

Strange BASIC math formula


I like converting old BASIC games - I ran across one that had this odd formula. Currently I am writing in Pascal, but I can write it in any language. After rummaging through the code, I could not find if this var in use, but still would like to know what kind of math shortcut BASIC was using back in the day.

d1 = 1-(( 0.23 + random / 10 ) * (-(d <= 50 )))

d1 is a dummy var, d = depth of sub

I broke it down into steps and found that part (-(d <= 50)) causes my compile to fail.

Can someone shed some light on to it?


Solution

  • -(d <= 50) should, AFAIK (boolean -> int conversion), return -1 if d <= 50 and 0 if d > 50. To sum up, if d > 50, the right part of multiplication will be equal to 0, so d1 will be equal to 1. You should write it using else or ternary construct (C-like pseudocode below):

    d1 = (d > 50) ? 1 : 1.23 + random / 10;
    

    Step by step explanation:

    d1 = 1-(( 0.23 + random / 10 ) * (-(d <= 50 )))
    

    then

    if ( d <= 50 )
      d1 = 1-(( 0.23 + random / 10 ) * (-TRUE)))
    else
      d1 = 1-(( 0.23 + random / 10 ) * (-FALSE)))
    

    then

    if ( d <= 50 )
      d1 = 1-(( 0.23 + random / 10 ) * (-1)))
    else
      d1 = 1-(( 0.23 + random / 10 ) * (-0)))
    

    then

    if ( d <= 50 )
      d1 = 1 - (( 0.23 + random / 10 ) * -1))
    else
      d1 = 1 - (( 0.23 + random / 10 ) * 0))
    

    then

    if ( d <= 50 )
      d1 = 1 - (-( 0.23 + random / 10 ))
    else
      d1 = 1 - (0)
    

    then

    if ( d <= 50 )
      d1 = 1 + ( 0.23 + random / 10 );
    else
      d1 = 1;
    

    then, finally

    d1 = (d > 50) ? 1 : 1.23 + random / 10;