samedi 5 septembre 2015

Pascal: understanding if/then statements

I'm currently porting the VMS Pascal version of the classic Moria game, but I'm not sure if I'm understanding the limits of if/then statements (I've never programmed in Pascal before).

My understanding so far is that with no begin/end, then an if/then block only encloses one following statement. If that's the case, then in the following code;

if (i4 > 0) then
  with inventory[i4] do
    begin
      objdes(out_val,i4,false);
      msg_print('Your ' + out_val + ' glows faintly!');
      if (enchant(toac)) then
        begin
          flags := uand(%X'7FFFFFFF',flags);
          py_bonuses(blank_treasure,0);
        end
      else
        msg_print('The enchantment fails...');
    end;
  ident := true;

the ident := true; would be outside of the if (tval > 0) then block, meaning that even if i4 is 0, ident would still be set to true.

If that is correct, then does it mean the following code from UMoria (a C port) is wrong?

i_ptr = &inventory[INVEN_WIELD];
if (i_ptr->tval != TV_NOTHING) {
  objdes(tmp_str, i_ptr, FALSE);
  (void) sprintf(out_val, "Your %s glows faintly!", tmp_str);
  msg_print(out_val);
  if (enchant(&i_ptr->tohit, 10))
    {
      i_ptr->flags &= ~TR_CURSED;
      calc_bonuses();
    }
  else
    msg_print("The enchantment fails.");
  ident = TRUE;
}

...as the ident = TRUE; is inside the if block.

I've seen similar examples in several places -- I guess it's possible that these were changed for the C port -- but I'm hoping to get clarification before I change too much code.

Aucun commentaire:

Enregistrer un commentaire