Cleaned up ThiefControl code

Questions, comments, and news on the server side plug-ins and it's API
Post Reply
Enigma
Private First Class
Private First Class
Posts: 212
Joined: Sat Apr 23, 2005 3:13 am

Cleaned up ThiefControl code

Post 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.
Attachments
thiefControl.cpp
(2.38 KiB) Downloaded 170 times
User avatar
mrapple
Sergeant Major
Sergeant Major
Posts: 460
Joined: Wed May 27, 2009 11:59 am
Location: Unknown
Contact:

Re: Cleaned up ThiefControl code

Post by mrapple »

very nice job. I'll have to see if it compiles and works for me tomorrow.
Image
trepan
Dev Wizard
Dev Wizard
Posts: 704
Joined: Fri Feb 21, 2003 7:50 pm

Re: Cleaned up ThiefControl code

Post 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
User avatar
Spazzy McGee
Sergeant Major
Sergeant Major
Posts: 1405
Joined: Mon Mar 21, 2005 4:59 pm
Location: Planet MoFo, Sheffield Division; United Kingdom

Re: Cleaned up ThiefControl code

Post 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?
"Life is what happens to you while you're busy making other plans." - John Lennon
User avatar
joevano
General
General
Posts: 1863
Joined: Sat Jun 18, 2005 1:08 pm
Location: South Bend, Indiana, USA

Re: Cleaned up ThiefControl code

Post by joevano »

The server-side lua support was added back, client-side lua is still out.
There is nothing worse than aggressive stupidity. -- Johann Wolfgang von Goethe
"How many legs does a dog have if you call his tail a leg? Four. Calling a tail a leg doesn't make it a leg." -- Abraham Lincoln
User avatar
Spazzy McGee
Sergeant Major
Sergeant Major
Posts: 1405
Joined: Mon Mar 21, 2005 4:59 pm
Location: Planet MoFo, Sheffield Division; United Kingdom

Re: Cleaned up ThiefControl code

Post by Spazzy McGee »

Oh, sweet. Back to the LUA tutorials then :)
"Life is what happens to you while you're busy making other plans." - John Lennon
Enigma
Private First Class
Private First Class
Posts: 212
Joined: Sat Apr 23, 2005 3:13 am

Re: Cleaned up ThiefControl code

Post 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.
Enigma
Private First Class
Private First Class
Posts: 212
Joined: Sat Apr 23, 2005 3:13 am

Re: Cleaned up ThiefControl code

Post 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.
User avatar
blast
General
General
Posts: 4931
Joined: Fri Mar 21, 2003 3:49 pm
Location: playing.cxx
Contact:

Re: Cleaned up ThiefControl code

Post 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.
"In addition to knowing the secrets of the Universe, I can assure you that I am also quite potty trained." -Koenma (Yu Yu Hakusho)

Image
trepan
Dev Wizard
Dev Wizard
Posts: 704
Joined: Fri Feb 21, 2003 7:50 pm

Re: Cleaned up ThiefControl code

Post by trepan »

nice sig, blast ;)
User avatar
Spazzy McGee
Sergeant Major
Sergeant Major
Posts: 1405
Joined: Mon Mar 21, 2005 4:59 pm
Location: Planet MoFo, Sheffield Division; United Kingdom

Re: Cleaned up ThiefControl code

Post by Spazzy McGee »

Is/will the option to embed lua into map files still be available?
"Life is what happens to you while you're busy making other plans." - John Lennon
trepan
Dev Wizard
Dev Wizard
Posts: 704
Joined: Fri Feb 21, 2003 7:50 pm

Re: Cleaned up ThiefControl code

Post by trepan »

That can be done with current SVN HEAD code.
Enigma
Private First Class
Private First Class
Posts: 212
Joined: Sat Apr 23, 2005 3:13 am

Re: Cleaned up ThiefControl code

Post 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.
trepan
Dev Wizard
Dev Wizard
Posts: 704
Joined: Fri Feb 21, 2003 7:50 pm

Re: Cleaned up ThiefControl code

Post by trepan »

There are no plans to re-add the client-side lua code to bzflag.
Post Reply