Page 1 of 1

Cleaned up ThiefControl code

Posted: Fri Jul 24, 2009 10:58 pm
by Enigma
Since I had modified the ThiefControl plug-in once before, I decided to clean up the code.

Here is a list of everything I changed.
  • Removed the constructor - it was empty.
  • Removed the virtual destructor, as it is not needed; the class is not meant to be subclassed.
  • Added a function dropThief(), since there was duplicate code in three places, violating the DRY principle.
  • Used bz_getPlayerTeam() instead of bz_BasePlayerRecord. Now there is no memory management involved.
  • Removed the "default" switch, since all the enumerations are handled.
  • Removed the allowTransfer variable, as it was only used once.
All of these changes removed 30 lines of code :-).

I did not test it, but it actually compiled on the first try. You might have to re-indent the code to conform to the "tab stop" rule.

[Edit] I changes some variable names.

Re: Cleaned up ThiefControl code

Posted: Sat Jul 25, 2009 4:50 am
by mrapple
very nice job. I'll have to see if it compiles and works for me tomorrow.

Re: Cleaned up ThiefControl code

Posted: Sat Jul 25, 2009 5:16 am
by trepan
To give yall a comparative preview, here's what the bz3.0 lua version might look like:

Code: Select all

if (WantConfig) then
  return {
    name     = 'thief_control', -- the only required field
    desc     = 'Thief Control  (based on Enigma\'s C++ plugin)',
    author   = 'trepan',
    date     = 'Jul 25, 2009',
    license  = 'LGPL 2.1',
    niceness = 0,
  }
end
   
local teamFlags = { ['R*'] = 1, ['G*'] = 2, ['B*'] = 3, ['P*'] = 4 }

function CallIn.FlagTransfer(srcID, dstID, flagType)
  local gameType = bz.GetGameType()
  local srcTeam, dstTeam = bz.GetPlayerTeam(srcID), bz.GetPlayerTeam(dstID)

  if ((srcTeam ~= dstTeam)       or
      (dstTeam == BZ.TEAM.ROGUE) or
      (gameType == BZ.GAME.OPEN) or
      ((gameType == BZ.GAME.CTF) and teamFlags[flagType])) then
    return -- carry on, nothing to see here
  end

  bz.SendMessage(BZ.PLAYER.SERVER, dstID, dropMsg,
                 "Flag dropped. Don't steal from teammates!")
  return 'drop'
end

Re: Cleaned up ThiefControl code

Posted: Sat Jul 25, 2009 11:46 am
by Spazzy McGee
trepan wrote:To give yall a comparative preview, here's what the bz3.0 lua version might look like:
I thought lua was dropped from 3.0, for later review?

Re: Cleaned up ThiefControl code

Posted: Sat Jul 25, 2009 12:05 pm
by joevano
The server-side lua support was added back, client-side lua is still out.

Re: Cleaned up ThiefControl code

Posted: Sat Jul 25, 2009 12:22 pm
by Spazzy McGee
Oh, sweet. Back to the LUA tutorials then :)

Re: Cleaned up ThiefControl code

Posted: Mon Jul 27, 2009 1:57 am
by Enigma
Hm... looks like I am learning Lua. I just installed lua, so I can follow the tutorials and learn the language before moving to bzfs plug-ins. From what I've seen so far, I think I'm going to like this language.

In an excerpt from the book Programming in Lua, I feel like they had C++ in mind when they wrote this paragraph: "Currently, many programming languages are concerned with how to help you write programs with hundreds of thousands of lines. For that, they offer you packages, namespaces, complex type systems, a myriad of constructions, and thousands of documentation pages to be studied."

I am actually looking forward to the client-side plug-ins more than the server-side plug-ins, as I don't run a server: I can't really use the code I write.

Re: Cleaned up ThiefControl code

Posted: Mon Jul 27, 2009 2:03 am
by Enigma
I should also mention that I wasn't the first person to write the ThiefControl plug-in. I added different behaviors for each game type. The original one simply blocked thief steals for players on the same team, regardless of the game type.

Re: Cleaned up ThiefControl code

Posted: Mon Jul 27, 2009 2:08 am
by blast
Enigma wrote:I am actually looking forward to the client-side plug-ins more than the server-side plug-ins, as I don't run a server: I can't really use the code I write.
Client-side plugins were removed.

Re: Cleaned up ThiefControl code

Posted: Mon Jul 27, 2009 2:29 am
by trepan
nice sig, blast ;)

Re: Cleaned up ThiefControl code

Posted: Mon Jul 27, 2009 10:15 am
by Spazzy McGee
Is/will the option to embed lua into map files still be available?

Re: Cleaned up ThiefControl code

Posted: Mon Jul 27, 2009 2:18 pm
by trepan
That can be done with current SVN HEAD code.

Re: Cleaned up ThiefControl code

Posted: Tue Jul 28, 2009 2:09 am
by Enigma
blast wrote:
Enigma wrote:I am actually looking forward to the client-side plug-ins more than the server-side plug-ins, as I don't run a server: I can't really use the code I write.
Client-side plugins were removed.
Oh I know. It was my understanding that they might be added at a later date.

Re: Cleaned up ThiefControl code

Posted: Tue Jul 28, 2009 2:26 am
by trepan
There are no plans to re-add the client-side lua code to bzflag.