Guest User

Untitled

a guest
Jan 13th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 54.96 KB | None | 0 0
  1. diff -r a8a4d5213b1e -r f6c2d27eb104 master/master.cpp
  2. --- a/master/master.cpp Sat Jan 12 13:30:52 2013 +0100
  3. +++ b/master/master.cpp Sun Jan 13 11:34:27 2013 +0100
  4. @@ -1116,6 +1116,12 @@
  5.     }
  6.  
  7.  
  8. +   TNL_IMPLEMENT_RPC_OVERRIDE(MasterServerConnection, c2mRequestMOTD, ())
  9. +   {
  10. +      sendMotd();
  11. +   }
  12. +
  13. +
  14.     // Game server wants to know if user name has been verified
  15.     TNL_IMPLEMENT_RPC_OVERRIDE(MasterServerConnection, s2mRequestAuthentication, (Vector<U8> id, StringTableEntry name))
  16.     {
  17. diff -r a8a4d5213b1e -r f6c2d27eb104 master/master.h
  18. --- a/master/master.h   Sat Jan 12 13:30:52 2013 +0100
  19. +++ b/master/master.h   Sun Jan 13 11:34:27 2013 +0100
  20. @@ -251,6 +251,9 @@
  21.     TNL_DECLARE_RPC_OVERRIDE(s2mSendLevelInfo, (string hash, string levelName, string creator,
  22.                                                 StringTableEntry gametype, bool hasLevelGen, U8 teamCount, S32 winningScore, S32 gameDurationInSeconds));
  23.  
  24. +   // Send message-of-the-day
  25. +   TNL_DECLARE_RPC_OVERRIDE(c2mRequestMOTD, ());
  26. +
  27.     // Send high scores stats to client
  28.     TNL_DECLARE_RPC_OVERRIDE(c2mRequestHighScores, ());
  29.  
  30. diff -r a8a4d5213b1e -r f6c2d27eb104 master/masterInterface.cpp
  31. --- a/master/masterInterface.cpp    Sat Jan 12 13:30:52 2013 +0100
  32. +++ b/master/masterInterface.cpp    Sun Jan 13 11:34:27 2013 +0100
  33. @@ -84,6 +84,11 @@
  34.     NetClassGroupMasterMask, RPCGuaranteedOrdered, RPCDirClientToServer, 0) {}
  35.  
  36.  
  37. +TNL_IMPLEMENT_RPC(MasterServerInterface, c2mRequestMOTD,
  38. +      (),
  39. +      (),
  40. +      NetClassGroupMasterMask, RPCGuaranteedOrdered, RPCDirClientToServer, 2) {}
  41. +
  42.  TNL_IMPLEMENT_RPC(MasterServerInterface, m2cSetMOTD,
  43.     (TNL::StringPtr masterName, TNL::StringPtr motdString),
  44.     (masterName, motdString),
  45. diff -r a8a4d5213b1e -r f6c2d27eb104 master/masterInterface.h
  46. --- a/master/masterInterface.h  Sat Jan 12 13:30:52 2013 +0100
  47. +++ b/master/masterInterface.h  Sun Jan 13 11:34:27 2013 +0100
  48. @@ -132,9 +132,11 @@
  49.        StringTableEntry levelName, StringTableEntry levelType,
  50.        U32 botCount, U32 playerCount, U32 maxPlayers, U32 infoFlags));
  51.  
  52. +   /// Request the Message-of-the-Day
  53. +   TNL_DECLARE_RPC(c2mRequestMOTD, ());
  54.  
  55.     /// m2cSetMOTD is sent to a client when the connection is established.  The
  56. -   /// client's game string is used to pick which MOTD will be sent.
  57. +   /// client's BUILD_VERSION is used to pick which MOTD will be sent.
  58.     TNL_DECLARE_RPC(m2cSetMOTD, (TNL::StringPtr masterName, TNL::StringPtr motdString));
  59.  
  60.     // If the user has a verified name, send this message back so they know the "correct spelling" of the name
  61. diff -r a8a4d5213b1e -r f6c2d27eb104 tnl/tnlNetConnection.h
  62. --- a/tnl/tnlNetConnection.h    Sat Jan 12 13:30:52 2013 +0100
  63. +++ b/tnl/tnlNetConnection.h    Sun Jan 13 11:34:27 2013 +0100
  64. @@ -244,6 +244,7 @@
  65.  
  66.        ReasonConnectionsForbidden,   // Used when rejecting data connections when data connection capability is disabled
  67.        ReasonBanned,                 // You made the admin mad...
  68. +      ReasonAnonymous,              // Anonymous connections are terminated quickly, only for simple data retrieval
  69.        TerminationReasons,           // Must be last of enumerated reasons!
  70.        ReasonNone
  71.     };
  72. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/ClientGame.cpp
  73. --- a/zap/ClientGame.cpp    Sat Jan 12 13:30:52 2013 +0100
  74. +++ b/zap/ClientGame.cpp    Sun Jan 13 11:34:27 2013 +0100
  75. @@ -1259,8 +1259,12 @@
  76.  
  77.           mSeenTimeOutMessage = true;
  78.           break;
  79. +
  80.        case NetConnection::ReasonSelfDisconnect:
  81. -         break;  // no errors when client disconnect (when quitting bitfighter)
  82. +         // no errors when client disconnect (when quitting bitfighter)
  83. +      case NetConnection::ReasonAnonymous:
  84. +         // Anonymous connections are disconnected quickly, usually after retrieving some data
  85. +         break;
  86.  
  87.        default:  // Not handled
  88.           ui->setMessage(2, "Unable to connect to the master server, with error code:");
  89. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/EngineeredItem.cpp
  90. --- a/zap/EngineeredItem.cpp    Sat Jan 12 13:30:52 2013 +0100
  91. +++ b/zap/EngineeredItem.cpp    Sun Jan 13 11:34:27 2013 +0100
  92. @@ -1689,10 +1689,20 @@
  93.  // Combined Lua / C++ default constructor
  94.  Turret::Turret(lua_State *L) : Parent(TEAM_NEUTRAL, Point(0,0), Point(1,0))
  95.  {
  96. +   // TODO: This does not work; we need to snap the turret to a nearby wall and compute the normal angle.
  97. +   // It is likely that setPos is similarly broken.  And maybe more.
  98. +   if(L)
  99. +   {
  100. +      static LuaFunctionArgList constructorArgList = { {{ END }, { GEOM, TEAM_INDX, END }}, 2 };
  101. +      S32 profile = checkArgList(L, constructorArgList, "Turret", "constructor");
  102. +      findMountPoint(Game::getAddTarget(), getPos());
  103. +      if(profile == 1)
  104. +      {
  105. +         findMountPoint(gServerGame, getPointOrXY(L, 1));
  106. +         setTeam(getInt(L, 2));
  107. +      }
  108. +   }
  109.     initialize();
  110. -
  111. -   if(L)
  112. -      findMountPoint(Game::getAddTarget(), getPos());
  113.  }
  114.  
  115.  
  116. @@ -2039,6 +2049,8 @@
  117.  /////
  118.  // Lua interface
  119.  /**
  120. +  *  @luafunc Turret::Turret()
  121. +  *  @luafunc Turret::Turret(geom, team)
  122.    *  @luaclass Turret
  123.    *  @brief Mounted gun that shoots at enemy ships and other objects.
  124.    */
  125. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/EventManager.cpp
  126. --- a/zap/EventManager.cpp  Sat Jan 12 13:30:52 2013 +0100
  127. +++ b/zap/EventManager.cpp  Sun Jan 13 11:34:27 2013 +0100
  128. @@ -44,16 +44,16 @@
  129.  
  130.  
  131.  struct Subscription {
  132. -   const char *scriptId;
  133. +   LuaScriptRunner *subscriber;
  134.     LuaBase::ScriptContext context;
  135.  };
  136.  
  137.  
  138.  // Statics:
  139.  bool EventManager::anyPending = false;
  140. -Vector<Subscription> EventManager::subscriptions[EventTypes];
  141. -Vector<Subscription> EventManager::pendingSubscriptions[EventTypes];
  142. -Vector<const char *> EventManager::pendingUnsubscriptions[EventTypes];
  143. +static Vector<Subscription>      subscriptions         [EventManager::EventTypes];
  144. +static Vector<Subscription>      pendingSubscriptions  [EventManager::EventTypes];
  145. +static Vector<LuaScriptRunner *> pendingUnsubscriptions[EventManager::EventTypes];
  146.  
  147.  bool EventManager::mConstructed = false;  // Prevent duplicate instantiation
  148.  
  149. @@ -113,7 +113,7 @@
  150.  }
  151.  
  152.  
  153. -void EventManager::subscribe(const char *subscriber, EventType eventType, LuaBase::ScriptContext context, bool failSilently)
  154. +void EventManager::subscribe(LuaScriptRunner *subscriber, EventType eventType, LuaBase::ScriptContext context, bool failSilently)
  155.  {
  156.     // First, see if we're already subscribed
  157.     if(isSubscribed(subscriber, eventType) || isPendingSubscribed(subscriber, eventType))
  158. @@ -122,7 +122,7 @@
  159.     lua_State *L = LuaScriptRunner::getL();
  160.  
  161.     // Make sure the script has the proper event listener
  162. -   bool ok = LuaScriptRunner::loadFunction(L, subscriber, eventDefs[eventType].function);     // -- function
  163. +   bool ok = LuaScriptRunner::loadFunction(L, subscriber->getScriptId(), eventDefs[eventType].function);     // -- function
  164.  
  165.     if(!ok)
  166.     {
  167. @@ -137,7 +137,7 @@
  168.     removeFromPendingUnsubscribeList(subscriber, eventType);
  169.  
  170.     Subscription s;
  171. -   s.scriptId = subscriber;
  172. +   s.subscriber = subscriber;
  173.     s.context = context;
  174.  
  175.     pendingSubscriptions[eventType].push_back(s);
  176. @@ -147,7 +147,7 @@
  177.  }
  178.  
  179.  
  180. -void EventManager::unsubscribe(const char *subscriber, EventType eventType)
  181. +void EventManager::unsubscribe(LuaScriptRunner *subscriber, EventType eventType)
  182.  {
  183.     if((isSubscribed(subscriber, eventType) || isPendingSubscribed(subscriber, eventType)) && !isPendingUnsubscribed(subscriber, eventType))
  184.     {
  185. @@ -159,10 +159,10 @@
  186.  }
  187.  
  188.  
  189. -void EventManager::removeFromPendingSubscribeList(const char *subscriber, EventType eventType)
  190. +void EventManager::removeFromPendingSubscribeList(LuaScriptRunner *subscriber, EventType eventType)
  191.  {
  192.     for(S32 i = 0; i < pendingSubscriptions[eventType].size(); i++)
  193. -      if(strcmp(pendingSubscriptions[eventType][i].scriptId, subscriber) == 0)
  194. +      if(pendingSubscriptions[eventType][i].subscriber == subscriber)
  195.        {
  196.           pendingSubscriptions[eventType].erase_fast(i);
  197.           return;
  198. @@ -170,10 +170,10 @@
  199.  }
  200.  
  201.  
  202. -void EventManager::removeFromPendingUnsubscribeList(const char *subscriber, EventType eventType)
  203. +void EventManager::removeFromPendingUnsubscribeList(LuaScriptRunner *subscriber, EventType eventType)
  204.  {
  205.     for(S32 i = 0; i < pendingUnsubscriptions[eventType].size(); i++)
  206. -      if(strcmp(pendingUnsubscriptions[eventType][i], subscriber) == 0)
  207. +      if(pendingUnsubscriptions[eventType][i] == subscriber)
  208.        {
  209.           pendingUnsubscriptions[eventType].erase_fast(i);
  210.           return;
  211. @@ -181,10 +181,10 @@
  212.  }
  213.  
  214.  
  215. -void EventManager::removeFromSubscribedList(const char *subscriber, EventType eventType)
  216. +void EventManager::removeFromSubscribedList(LuaScriptRunner *subscriber, EventType eventType)
  217.  {
  218.     for(S32 i = 0; i < subscriptions[eventType].size(); i++)
  219. -      if(strcmp(subscriptions[eventType][i].scriptId, subscriber) == 0)
  220. +      if(subscriptions[eventType][i].subscriber == subscriber)
  221.        {
  222.           subscriptions[eventType].erase_fast(i);
  223.           return;
  224. @@ -193,39 +193,39 @@
  225.  
  226.  
  227.  // Unsubscribe an event bypassing the pending unsubscribe queue, when we know it will be OK
  228. -void EventManager::unsubscribeImmediate(const char *subscriber, EventType eventType)
  229. +void EventManager::unsubscribeImmediate(LuaScriptRunner *subscriber, EventType eventType)
  230.  {
  231. -   removeFromSubscribedList(subscriber, eventType);
  232. -   removeFromPendingSubscribeList(subscriber, eventType);
  233. +   removeFromSubscribedList        (subscriber, eventType);
  234. +   removeFromPendingSubscribeList  (subscriber, eventType);
  235.     removeFromPendingUnsubscribeList(subscriber, eventType);    // Probably not really necessary...
  236.  }
  237.  
  238.  
  239.  // Check if we're subscribed to an event
  240. -bool EventManager::isSubscribed(const char *subscriber, EventType eventType)
  241. +bool EventManager::isSubscribed(LuaScriptRunner *subscriber, EventType eventType)
  242.  {
  243.     for(S32 i = 0; i < subscriptions[eventType].size(); i++)
  244. -      if(strcmp(subscriptions[eventType][i].scriptId, subscriber) == 0)
  245. +      if(subscriptions[eventType][i].subscriber == subscriber)
  246.           return true;
  247.  
  248.     return false;
  249.  }
  250.  
  251.  
  252. -bool EventManager::isPendingSubscribed(const char *subscriber, EventType eventType)
  253. +bool EventManager::isPendingSubscribed(LuaScriptRunner *subscriber, EventType eventType)
  254.  {
  255.     for(S32 i = 0; i < pendingSubscriptions[eventType].size(); i++)
  256. -      if(strcmp(pendingSubscriptions[eventType][i].scriptId, subscriber) == 0)
  257. +      if(pendingSubscriptions[eventType][i].subscriber == subscriber)
  258.           return true;
  259.  
  260.     return false;
  261.  }
  262.  
  263.  
  264. -bool EventManager::isPendingUnsubscribed(const char *subscriber, EventType eventType)
  265. +bool EventManager::isPendingUnsubscribed(LuaScriptRunner *subscriber, EventType eventType)
  266.  {
  267.     for(S32 i = 0; i < pendingUnsubscriptions[eventType].size(); i++)
  268. -      if(strcmp(pendingUnsubscriptions[eventType][i], subscriber) == 0)
  269. +      if(pendingUnsubscriptions[eventType][i] == subscriber)
  270.           return true;
  271.  
  272.     return false;
  273. @@ -265,23 +265,7 @@
  274.     lua_State *L = LuaScriptRunner::getL();
  275.  
  276.     for(S32 i = 0; i < subscriptions[eventType].size(); i++)
  277. -   {
  278. -      try
  279. -      {
  280. -         // Passing nothing
  281. -         bool error = LuaScriptRunner::loadFunction(L, subscriptions[eventType][i].scriptId, eventDefs[eventType].function);
  282. -         if(error)
  283. -            throw LuaException("Could not load function!");
  284. -
  285. -         fire(L, 0, subscriptions[eventType][i].context);
  286. -      }
  287. -      catch(LuaException &e)
  288. -      {
  289. -         handleEventFiringError(L, subscriptions[eventType][i], eventType, e.what());
  290. -         LuaObject::clearStack(L);
  291. -         return;
  292. -      }
  293. -   }
  294. +      fire(L, subscriptions[eventType][i].subscriber, eventDefs[eventType].function, subscriptions[eventType][i].context);
  295.  }
  296.  
  297.  
  298. @@ -298,20 +282,8 @@
  299.  
  300.     for(S32 i = 0; i < subscriptions[eventType].size(); i++)
  301.     {
  302. -      try  
  303. -      {
  304. -         // Passing time
  305. -         LuaScriptRunner::loadFunction(L, subscriptions[eventType][i].scriptId, eventDefs[eventType].function);
  306. -         lua_pushinteger(L, deltaT);
  307. -
  308. -         fire(L, 1, subscriptions[eventType][i].context);
  309. -      }
  310. -      catch(LuaException &e)
  311. -      {
  312. -         handleEventFiringError(L, subscriptions[eventType][i], eventType, e.what());
  313. -         LuaObject::clearStack(L);
  314. -         return;
  315. -      }
  316. +      lua_pushinteger(L, deltaT);   // -- deltaT
  317. +      fire(L, subscriptions[eventType][i].subscriber, eventDefs[eventType].function, subscriptions[eventType][i].context);
  318.     }
  319.  }
  320.  
  321. @@ -326,27 +298,15 @@
  322.  
  323.     for(S32 i = 0; i < subscriptions[eventType].size(); i++)
  324.     {
  325. -      try
  326. -      {
  327. -         // Passing ship
  328. -         LuaScriptRunner::loadFunction(L, subscriptions[eventType][i].scriptId, eventDefs[eventType].function);
  329. -         ship->push(L);
  330. -
  331. -         fire(L, 1, subscriptions[eventType][i].context);
  332. -      }
  333. -      catch(LuaException &e)
  334. -      {
  335. -         handleEventFiringError(L, subscriptions[eventType][i], eventType, e.what());
  336. -         LuaObject::clearStack(L);
  337. -         return;
  338. -      }
  339. +      ship->push(L);                // -- ship
  340. +      fire(L, subscriptions[eventType][i].subscriber, eventDefs[eventType].function, subscriptions[eventType][i].context);
  341.     }
  342.  }
  343.  
  344.  
  345.  // Note that player can be NULL, in which case we'll pass nil to the listeners
  346.  // callerId will be NULL when player sends message
  347. -void EventManager::fireEvent(const char *callerId, EventType eventType, const char *message, LuaPlayerInfo *player, bool global)
  348. +void EventManager::fireEvent(LuaScriptRunner *sender, EventType eventType, const char *message, LuaPlayerInfo *playerInfo, bool global)
  349.  {
  350.     if(suppressEvents(eventType))  
  351.        return;
  352. @@ -355,36 +315,25 @@
  353.  
  354.     for(S32 i = 0; i < subscriptions[eventType].size(); i++)
  355.     {
  356. -      if(callerId && strcmp(callerId, subscriptions[eventType][i].scriptId) == 0)    // Don't alert bot about own message!
  357. +      if(sender == subscriptions[eventType][i].subscriber)    // Don't alert sender about own message!
  358.           continue;
  359.  
  360. -      try
  361. -      {
  362. -         // Passing msg, player, isGlobal
  363. -         LuaScriptRunner::loadFunction(L, subscriptions[eventType][i].scriptId, eventDefs[eventType].function);
  364. -         lua_pushstring(L, message);
  365. +      lua_pushstring(L, message);   // -- message
  366.  
  367. -         if(player)
  368. -            player->push(L);
  369. -         else
  370. -            lua_pushnil(L);
  371. +      if(playerInfo)
  372. +         playerInfo->push(L);       // -- message, playerInfo
  373. +      else
  374. +         lua_pushnil(L);            
  375.  
  376. -         lua_pushboolean(L, global);
  377. +      lua_pushboolean(L, global);   // -- message, player, isGlobal
  378.  
  379. -         fire(L, 3, subscriptions[eventType][i].context);
  380. -      }
  381. -      catch(LuaException &e)
  382. -      {
  383. -         handleEventFiringError(L, subscriptions[eventType][i], eventType, e.what());
  384. -         LuaObject::clearStack(L);
  385. -         return;
  386. -      }
  387. +      fire(L, subscriptions[eventType][i].subscriber, eventDefs[eventType].function, subscriptions[eventType][i].context);
  388.     }
  389.  }
  390.  
  391.  
  392.  // onPlayerJoined, onPlayerLeft
  393. -void EventManager::fireEvent(const char *callerId, EventType eventType, LuaPlayerInfo *player)
  394. +void EventManager::fireEvent(LuaScriptRunner *player, EventType eventType, LuaPlayerInfo *playerInfo)
  395.  {
  396.     if(suppressEvents(eventType))  
  397.        return;
  398. @@ -393,23 +342,11 @@
  399.  
  400.     for(S32 i = 0; i < subscriptions[eventType].size(); i++)
  401.     {
  402. -      if(strcmp(callerId, subscriptions[eventType][i].scriptId) == 0)    // Don't trouble bot with own joinage or leavage!
  403. +      if(player == subscriptions[eventType][i].subscriber)    // Don't trouble player with own joinage or leavage!
  404.           continue;
  405.  
  406. -      try  
  407. -      {
  408. -         // Passing player
  409. -         LuaScriptRunner::loadFunction(L, subscriptions[eventType][i].scriptId, eventDefs[eventType].function);
  410. -         player->push(L);
  411. -
  412. -         fire(L, 1, subscriptions[eventType][i].context);
  413. -      }
  414. -      catch(LuaException &e)
  415. -      {
  416. -         handleEventFiringError(L, subscriptions[eventType][i], eventType, e.what());
  417. -         LuaObject::clearStack(L);
  418. -         return;
  419. -      }
  420. +      playerInfo->push(L);          // -- playerInfo
  421. +      fire(L, subscriptions[eventType][i].subscriber, eventDefs[eventType].function, subscriptions[eventType][i].context);
  422.     }
  423.  }
  424.  
  425. @@ -427,13 +364,12 @@
  426.        try  
  427.        {
  428.           // Passing ship, zone, zoneType, zoneId
  429. -         LuaScriptRunner::loadFunction(L, subscriptions[eventType][i].scriptId, eventDefs[eventType].function);
  430. -         ship->push(L);
  431. -         zone->push(L);
  432. -         lua_pushinteger(L, zone->getObjectTypeNumber());
  433. -         lua_pushinteger(L, zone->getUserAssignedId());
  434. +         ship->push(L);                                     // -- ship
  435. +         zone->push(L);                                     // -- ship, zone  
  436. +         lua_pushinteger(L, zone->getObjectTypeNumber());   // -- ship, zone, zone->objTypeNumber
  437. +         lua_pushinteger(L, zone->getUserAssignedId());     // -- ship, zone, zone->objTypeNumber, zone->id
  438.  
  439. -         fire(L, 4, subscriptions[eventType][i].context);
  440. +         fire(L, subscriptions[eventType][i].subscriber, eventDefs[eventType].function, subscriptions[eventType][i].context);
  441.        }
  442.        catch(LuaException &e)
  443.        {
  444. @@ -445,12 +381,12 @@
  445.  }
  446.  
  447.  
  448. -// Actually fire the event, called by one fo the fireEvent() methods above
  449. -void EventManager::fire(lua_State *L, S32 argCount, LuaBase::ScriptContext context)
  450. +// Actually fire the event, called by one of the fireEvent() methods above
  451. +// Returns true if there was an error, false if everything ran ok
  452. +bool EventManager::fire(lua_State *L, LuaScriptRunner *scriptRunner, const char *function, LuaBase::ScriptContext context)
  453.  {
  454.     LuaBase::setScriptContext(L, context);
  455. -   if(lua_pcall(L, argCount, 0, 0) != 0)
  456. -      throw LuaException(lua_tostring(L, -1));
  457. +   return scriptRunner->runCmd(function, 0);
  458.  }
  459.  
  460.  
  461. @@ -458,16 +394,8 @@
  462.  {
  463.     if(subscriber.context == LuaBase::RobotContext)
  464.     {
  465. -      // Figure out which, if any, bot caused the error
  466. -      Robot *robot = gServerGame->findBot(subscriber.scriptId);
  467. -
  468. -      TNLAssert(robot, "Could not find robot!");
  469. -
  470. -      if(robot)
  471. -      {
  472. -         robot->logError("Error handling event %s: %s. Shutting bot down.", eventDefs[eventType].name, errorMsg);
  473. -         delete robot;
  474. -      }
  475. +      subscriber.subscriber->logError("Error handling event %s: %s. Shutting bot down.", eventDefs[eventType].name, errorMsg);
  476. +      delete subscriber.subscriber;
  477.     }
  478.     else
  479.     {
  480. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/EventManager.h
  481. --- a/zap/EventManager.h    Sat Jan 12 13:30:52 2013 +0100
  482. +++ b/zap/EventManager.h    Sun Jan 13 11:34:27 2013 +0100
  483. @@ -29,7 +29,6 @@
  484.  #include "tnlTypes.h"
  485.  #include "tnlVector.h"
  486.  #include "LuaBase.h"    // For ScriptContext def
  487. -//#include "lua.h"
  488.  
  489.  using namespace TNL;
  490.  
  491. @@ -39,6 +38,7 @@
  492.  class Robot;
  493.  class Ship;
  494.  class LuaPlayerInfo;
  495. +class LuaScriptRunner;
  496.  class Zone;
  497.  
  498.  struct Subscription;
  499. @@ -77,16 +77,16 @@
  500.  
  501.  private:
  502.     // Some helper functions
  503. -   bool isSubscribed(const char *subscriber, EventType eventType);
  504. -   bool isPendingSubscribed(const char *subscriber, EventType eventType);
  505. -   bool isPendingUnsubscribed(const char *subscriber, EventType eventType);
  506. +   bool isSubscribed         (LuaScriptRunner *subscriber, EventType eventType);
  507. +   bool isPendingSubscribed  (LuaScriptRunner *subscriber, EventType eventType);
  508. +   bool isPendingUnsubscribed(LuaScriptRunner *subscriber, EventType eventType);
  509.  
  510. -   void removeFromSubscribedList(const char *subscriber, EventType eventType);
  511. -   void removeFromPendingSubscribeList(const char *subscriber, EventType eventType);
  512. -   void removeFromPendingUnsubscribeList(const char *subscriber, EventType eventType);
  513. +   void removeFromSubscribedList        (LuaScriptRunner *subscriber, EventType eventType);
  514. +   void removeFromPendingSubscribeList  (LuaScriptRunner *subscriber, EventType eventType);
  515. +   void removeFromPendingUnsubscribeList(LuaScriptRunner *subscriber, EventType eventType);
  516.  
  517.     void handleEventFiringError(lua_State *L, const Subscription &subscriber, EventType eventType, const char *errorMsg);
  518. -   void fire(lua_State *L, S32 argCount, LuaBase::ScriptContext context);
  519. +   bool fire(lua_State *L, LuaScriptRunner *scriptRunner, const char *function, LuaBase::ScriptContext context);
  520.        
  521.     bool mIsPaused;
  522.     S32 mStepCount;           // If running for a certain number of steps, this will be > 0, while mIsPaused will be true
  523. @@ -101,23 +101,25 @@
  524.     static EventManager *get();         // Provide access to the single EventManager instance
  525.     bool suppressEvents(EventType eventType);
  526.  
  527. -   static Vector<Subscription> subscriptions[EventTypes];
  528. -   static Vector<Subscription> pendingSubscriptions[EventTypes];
  529. -   static Vector<const char *> pendingUnsubscriptions[EventTypes];
  530. +   //static Vector<Subscription> subscriptions[EventTypes];
  531. +   //static Vector<Subscription> pendingSubscriptions[EventTypes];
  532. +   //static Vector<pendingUnsubscriptions *> pendingUnsubscriptions[EventTypes];
  533.     static bool anyPending;
  534.  
  535. -   void subscribe(const char *subscriber, EventType eventType, LuaBase::ScriptContext context, bool failSilently = false);
  536. -   void unsubscribe(const char *subscriber, EventType eventType);
  537. -   void unsubscribeImmediate(const char *, EventType eventType);    // Used when bot dies, and we know there won't be subscription conflicts
  538. -   void update();                                                   // Act on events sitting in the pending lists
  539. +   void subscribe  (LuaScriptRunner *subscriber, EventType eventType, LuaBase::ScriptContext context, bool failSilently = false);
  540. +   void unsubscribe(LuaScriptRunner *subscriber, EventType eventType);
  541. +
  542. +    // Used when bot dies, and we know there won't be subscription conflicts
  543. +   void unsubscribeImmediate(LuaScriptRunner *subscriber, EventType eventType);
  544. +   void update();                                                      // Act on events sitting in the pending lists
  545.  
  546.     // We'll have several different signatures for this one...
  547.     void fireEvent(EventType eventType);
  548.     void fireEvent(EventType eventType, U32 deltaT);      // Tick
  549.     void fireEvent(EventType eventType, Ship *ship);      // ShipSpawned, ShipKilled
  550. -   void fireEvent(const char *callerId, EventType eventType, const char *message, LuaPlayerInfo *player, bool global);     // MsgReceived
  551. -   void fireEvent(const char *callerId, EventType eventType, LuaPlayerInfo *player);  // PlayerJoined, PlayerLeft
  552. -   void fireEvent(EventType eventType, Ship *ship, Zone *zone);      // ShipEnteredZoneEvent, ShipLeftZoneEvent
  553. +   void fireEvent(LuaScriptRunner *sender, EventType eventType, const char *message, LuaPlayerInfo *playerInfo, bool global);  // MsgReceived
  554. +   void fireEvent(LuaScriptRunner *player, EventType eventType, LuaPlayerInfo *playerInfo);  // PlayerJoined, PlayerLeft
  555. +   void fireEvent(EventType eventType, Ship *ship, Zone *zone); // ShipEnteredZoneEvent, ShipLeftZoneEvent
  556.  
  557.     // Allow the pausing of event firing for debugging purposes
  558.     void setPaused(bool isPaused);
  559. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/LuaBase.cpp
  560. --- a/zap/LuaBase.cpp   Sat Jan 12 13:30:52 2013 +0100
  561. +++ b/zap/LuaBase.cpp   Sun Jan 13 11:34:27 2013 +0100
  562. @@ -103,7 +103,7 @@
  563.  
  564.     for(S32 i = 0; i < profileCount; i++)
  565.     {
  566. -      const LuaBase::LuaArgType *candidateArgList = functionArgList.argList[i];
  567. +      const LuaBase::LuaArgType *candidateArgList = functionArgList.argList[i];     // argList is a 2D array
  568.        bool validProfile = true;
  569.        S32 stackPos = 0;
  570.  
  571. @@ -262,7 +262,14 @@
  572.        case TEAM_INDX:
  573.           if(lua_isnumber(L, stackPos))
  574.           {
  575. -            lua_Integer i = lua_tointeger(L, stackPos) - 1;    // -1 because Lua indices start with 1
  576. +            lua_Integer i = lua_tointeger(L, stackPos);
  577. +            // Special check for common error because Lua 1-based arrays suck monkey balls
  578. +            if(i == 0)
  579. +                logprintf(LogConsumer::LogError, "WARNING: It appears you have tried to add an item to teamIndex 0; "
  580. +                                                 "this is almost certainly an error.\n"
  581. +                                                 "If you want to add an item to the first team, specify team 1.  Remember "
  582. +                                                 "that Lua uses 1-based arrays.");
  583. +            i--;    // Subtract 1 because Lua indices start with 1, and we need to convert to C++ 0-based index
  584.              return ((i >= 0 && i < Game::getAddTarget()->getTeamCount()) || (i + 1) == TEAM_NEUTRAL || (i + 1) == TEAM_HOSTILE);
  585.           }
  586.           return false;
  587. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/LuaScriptRunner.cpp
  588. --- a/zap/LuaScriptRunner.cpp   Sat Jan 12 13:30:52 2013 +0100
  589. +++ b/zap/LuaScriptRunner.cpp   Sun Jan 13 11:34:27 2013 +0100
  590. @@ -91,13 +91,14 @@
  591.     // send an event to a dead bot, after all...
  592.     for(S32 i = 0; i < EventManager::EventTypes; i++)
  593.        if(mSubscriptions[i])
  594. -         EventManager::get()->unsubscribeImmediate(getScriptId(), (EventManager::EventType)i);
  595. +         EventManager::get()->unsubscribeImmediate(this, (EventManager::EventType)i);
  596.  
  597.     // And delete the script's environment table from the Lua instance
  598.     deleteScript(getScriptId());
  599.  }
  600.  
  601. - const char *LuaScriptRunner::getErrorMessagePrefix() { return "SCRIPT"; }
  602. +
  603. +const char *LuaScriptRunner::getErrorMessagePrefix() { return "SCRIPT"; }
  604.  
  605.  
  606.  // Static method setting static vars
  607. @@ -143,6 +144,7 @@
  608.  
  609.  // Retrieve the environment from the registry, and put the requested function from that environment onto the stack.  Returns true
  610.  // if it works, false if the specified function could not be found.  If this fails, it will remove the non-function from the stack.
  611. +// Remember that not every failure to load a function is a problem; some functions are expected but optional.
  612.  bool LuaScriptRunner::loadFunction(lua_State *L, const char *scriptId, const char *functionName)
  613.  {
  614.     lua_getfield(L, LUA_REGISTRYINDEX, scriptId);   // Push REGISTRY[scriptId] onto the stack                -- table
  615. @@ -151,7 +153,7 @@
  616.  
  617.     // Check if the top stack item is indeed a function (as we would expect)
  618.     if(lua_isfunction(L, -1))
  619. -      return true;      // If so, return true
  620. +      return true;      // If so, return true, leaving the function on top of the stack
  621.  
  622.     // else
  623.     clearStack(L);
  624. @@ -159,22 +161,6 @@
  625.  }
  626.  
  627.  
  628. -bool LuaScriptRunner::retrieveCriticalFunction(const char *functionName)
  629. -{
  630. -   if(!loadFunction(L, getScriptId(), functionName))
  631. -   {
  632. -      TNLAssert(false, "Critical function not found -- is the lua environment corrupt?");
  633. -
  634. -      logError("Function %s() could not be found!  Terminating script.\n"
  635. -               "Your scripting environment appears corrupted.  Consider reinstalling Bitfighter.", functionName);
  636. -
  637. -      return false;
  638. -   }
  639. -
  640. -   return true;
  641. -}
  642. -
  643. -
  644.  // Primarily used for loading helper functions
  645.  bool LuaScriptRunner::loadAndRunGlobalFunction(lua_State *L, const char *key, ScriptContext context)
  646.  {
  647. @@ -184,7 +170,6 @@
  648.     setEnvironment();                            // Set the environment for the code
  649.     S32 err = lua_pcall(L, 0, 0, 0);             // Run it                                -- <<empty stack>>
  650.  
  651. -
  652.     if(err != 0)
  653.     {
  654.        logError("Failed to load startup functions %s: %s", key, lua_tostring(L, -1));
  655. @@ -300,43 +285,41 @@
  656.  // Returns true if there was an error, false if everything ran ok
  657.  bool LuaScriptRunner::runCmd(const char *function, S32 returnValues)
  658.  {
  659. -   S32 args = lua_gettop(L);  // Number of args on stack    -- <<args>>
  660. +   try
  661. +   {
  662. +      S32 args = lua_gettop(L);  // Number of args on stack    -- <<args>>
  663.  
  664. -   // Load our error handling function -- this will print a pretty stacktrace in the event things go wrong calling function.
  665. -   retrieveCriticalFunction("_stackTracer");             // -- <<args>>, _stackTracer
  666. -   bool ok = loadFunction(L, getScriptId(), function);   // -- <<args>>, _stackTracer, function
  667. -   if(!ok)
  668. -   {      
  669. -      logprintf(LogConsumer::LogError, "%s\n"
  670. -                                       "Cannot load method %s()!\n"
  671. -                                       "Terminating script.",
  672. -                                       getErrorMessagePrefix(), function, lua_tostring(L, -1));
  673. +      // Load our error handling function -- this will print a pretty stacktrace in the event things go wrong calling function.
  674. +      // _stackTracer is a function included in lua_helper_functions that manages the stack trace; it should ALWAYS be present.
  675. +      if(!loadFunction(L, getScriptId(), "_stackTracer"))       // -- <<args>>, _stackTracer
  676. +         throw LuaException("Method _stackTracer() could not be found!\n"
  677. +                            "Your scripting environment appears corrupted.  Consider reinstalling Bitfighter.");
  678. +
  679. +      if(!loadFunction(L, getScriptId(), function))             // -- <<args>>, _stackTracer, function
  680. +         throw LuaException("Cannot load method" + string(function) +"()!\n");
  681. +
  682. +      // Reorder the stack a little
  683. +      lua_insert(L, 1);                                         // -- function, <<args>>, _stackTracer
  684. +      lua_insert(L, 1);                                         // -- _stackTracer, function, <<args>>
  685. +
  686. +      S32 error = lua_pcall(L, args, returnValues, -2 - args);  // -- _stackTracer, <<return values>>
  687. +      if(error)
  688. +         throw LuaException("In method" + string(function) +"():\n" + string(lua_tostring(L, -1)));
  689. +
  690. +      lua_remove(L, 1);    // Remove _stackTracer                  -- <<return values>>
  691. +
  692. +      // Do not clear stack -- caller probably wants <<return values>>
  693. +      return false;
  694. +   }
  695. +
  696. +   catch(LuaException &e)
  697. +   {
  698. +      logprintf(LogConsumer::LogError, "%s\n%s\nTerminating script", getErrorMessagePrefix(), e.msg.c_str());
  699. +
  700.        killScript();
  701.        clearStack(L);
  702.        return true;
  703.     }
  704. -
  705. -   // Reorder the stack a little
  706. -   lua_insert(L, 1);                                         // -- function, <<args>>, _stackTracer
  707. -   lua_insert(L, 1);                                         // -- _stackTracer, function, <<args>>
  708. -
  709. -   S32 error = lua_pcall(L, args, returnValues, -2 - args);  // -- _stackTracer, <<return values>>
  710. -
  711. -   if(error != 0)
  712. -   {
  713. -      logprintf(LogConsumer::LogError, "%s\n"
  714. -                                       "In method %s():\n"
  715. -                                       "%s.\n",
  716. -                                       getErrorMessagePrefix(), function, lua_tostring(L, -1));    // Gets stack trace left by _stackTracer
  717. -      killScript();
  718. -      clearStack(L);
  719. -      return true;
  720. -   }
  721. -
  722. -   lua_remove(L, 1);    // Remove _stackTracer           -- <<return values>>
  723. -
  724. -   // Do not clear stack -- caller probably wants <<return values>>
  725. -   return false;
  726.  }
  727.  
  728.  
  729. @@ -611,7 +594,7 @@
  730.  
  731.     if(!mSubscriptions[eventType])
  732.     {
  733. -      EventManager::get()->subscribe(getScriptId(), (EventManager::EventType)eventType, context);
  734. +      EventManager::get()->subscribe(this, (EventManager::EventType)eventType, context);
  735.        mSubscriptions[eventType] = true;
  736.     }
  737.  
  738. @@ -627,7 +610,7 @@
  739.  
  740.     if(mSubscriptions[eventType])
  741.     {
  742. -      EventManager::get()->unsubscribe(getScriptId(), (EventManager::EventType)eventType);
  743. +      EventManager::get()->unsubscribe(this, (EventManager::EventType)eventType);
  744.        mSubscriptions[eventType] = false;
  745.     }
  746.  
  747. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/LuaScriptRunner.h
  748. --- a/zap/LuaScriptRunner.h Sat Jan 12 13:30:52 2013 +0100
  749. +++ b/zap/LuaScriptRunner.h Sun Jan 13 11:34:27 2013 +0100
  750. @@ -165,11 +165,8 @@
  751.  
  752.     bool loadScript();
  753.  
  754. -   bool retrieveCriticalFunction(const char *funName);   // Same, but with more oomph
  755. -
  756.     bool runCmd(const char *function, S32 returnValues);
  757.  
  758. -
  759.     const char *getScriptId();
  760.     static bool loadFunction(lua_State *L, const char *scriptId, const char *functionName);
  761.     bool loadAndRunGlobalFunction(lua_State *L, const char *key, ScriptContext context);
  762. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/Spawn.cpp
  763. --- a/zap/Spawn.cpp Sat Jan 12 13:30:52 2013 +0100
  764. +++ b/zap/Spawn.cpp Sun Jan 13 11:34:27 2013 +0100
  765. @@ -206,7 +206,16 @@
  766.  // Lua constructor
  767.  Spawn::Spawn(lua_State *L) : AbstractSpawn(Point(0,0))
  768.  {
  769. +   static LuaFunctionArgList constructorArgList = { {{ END }, { GEOM, END }, { GEOM, TEAM_INDX, END }}, 3 };
  770. +   S32 profile = checkArgList(L, constructorArgList, "Spawn", "constructor");
  771.     initialize();
  772. +   if(L && profile == 1)
  773. +      setPos(getPointOrXY(L, 1));
  774. +   else if(L && profile == 2)
  775. +   {
  776. +      setPos(getPointOrXY(L, 1));
  777. +      setTeam(getInt(L, 2));
  778. +   }
  779.  }
  780.  
  781.  
  782. @@ -289,6 +298,9 @@
  783.  /////
  784.  // Lua interface
  785.  /**
  786. +  *  @luafunc Spawn::Spawn()
  787. +  *  @luafunc Spawn::Spawn(geom)
  788. +  *  @luafunc Spawn::Spawn(geom, team)
  789.    *  @luaclass Spawn
  790.    *  @brief Marks locations where ships and robots should spawn.
  791.    *  @geom  The geometry of Spawns is a single point.
  792. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/UIGame.cpp
  793. --- a/zap/UIGame.cpp    Sat Jan 12 13:30:52 2013 +0100
  794. +++ b/zap/UIGame.cpp    Sun Jan 13 11:34:27 2013 +0100
  795. @@ -106,7 +106,7 @@
  796.                    mChatMessageDisplayer1(game,  5, true,  false, CHAT_WRAP_WIDTH,    CHAT_FONT_SIZE,    CHAT_FONT_GAP),
  797.                    mChatMessageDisplayer2(game,  5, false, false, CHAT_WRAP_WIDTH,    CHAT_FONT_SIZE,    CHAT_FONT_GAP),
  798.                    mChatMessageDisplayer3(game, 24, false, false, CHAT_WRAP_WIDTH,    CHAT_FONT_SIZE,    CHAT_FONT_GAP),
  799. -         mAnnouncementTimer(0)
  800. +                  mAnnouncementTimer(0)
  801.  {
  802.     mInScoreboardMode = false;
  803.     mFPSVisible = false;
  804. @@ -2002,7 +2002,6 @@
  805.         for(S32 i = 1; i < words.size(); i++)
  806.             s = s + words[i];
  807.    
  808. -       ClientInfo* clientInfo = game->getClientInfo();
  809.         GameType* gt = game->getGameType();
  810.                    
  811.         if(gt)
  812. @@ -2311,7 +2310,7 @@
  813.     { "addbots",     &GameUserInterface::addBotsHandler,         { xINT, STR, TEAM, STR }, 4, LEVEL_COMMANDS,  1,  2,  {"[count]","[file]","[team name or num]","[args]"}, "Add [count] bots from [file] to [team num], pass [args] to bot" },
  814.     { "kickbot",     &GameUserInterface::kickBotHandler,         {  },                     0, LEVEL_COMMANDS,  1,  1,  {  },                                       "Kick most recently added bot" },
  815.     { "kickbots",    &GameUserInterface::kickBotsHandler,        {  },                     0, LEVEL_COMMANDS,  1,  1,  {  },                                       "Kick all bots" },
  816. -   {"announce",      &GameUserInterface::announceHandler,          { STR },                  1, ADMIN_COMMANDS,  0,  1,  {"<ANNOUNCEMENT>"},      "The admin announces an important message"   },
  817. +   {"announce",        &GameUserInterface::announceHandler,        { STR },                  1, ADMIN_COMMANDS,  0,  1,  {"<ANNOUNCEMENT>"},      "The admin announces an important message"   },
  818.  
  819.     { "kick",               &GameUserInterface::kickPlayerHandler,         { NAME },       1, ADMIN_COMMANDS,  0,  1,  {"<name>"},              "Kick a player from the game" },
  820.     { "ban",                &GameUserInterface::banPlayerHandler,          { NAME, xINT }, 2, ADMIN_COMMANDS,  0,  1,  {"<name>","[duration]"}, "Ban a player from the server (IP-based, def. = 60 mins)" },
  821. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/UIGame.h
  822. --- a/zap/UIGame.h  Sat Jan 12 13:30:52 2013 +0100
  823. +++ b/zap/UIGame.h  Sun Jan 13 11:34:27 2013 +0100
  824. @@ -150,7 +150,6 @@
  825.        MessageDisplayModes
  826.     };
  827.    
  828. -   Timer mAnnouncementTimer;
  829.     string mAnnouncement;
  830.     bool mIsAnnouncementNew;
  831.  
  832. @@ -274,6 +273,8 @@
  833.     ChatMessageDisplayer mChatMessageDisplayer2;    // Short form, messages do not expire
  834.     ChatMessageDisplayer mChatMessageDisplayer3;    // Long form, messages do not expire
  835.  
  836. +   Timer mAnnouncementTimer;
  837. +
  838.     void dropItem();                       // User presses drop item key
  839.  
  840.     bool mFiring;                          // Are we firing?
  841. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/Zone.cpp
  842. --- a/zap/Zone.cpp  Sat Jan 12 13:30:52 2013 +0100
  843. +++ b/zap/Zone.cpp  Sun Jan 13 11:34:27 2013 +0100
  844. @@ -26,6 +26,7 @@
  845.  #include "Zone.h"
  846.  #include "game.h"
  847.  #include "stringUtils.h"
  848. +#include "Geometry.h"
  849.  
  850.  namespace Zap
  851.  {
  852. @@ -36,6 +37,17 @@
  853.  // Combined Lua / C++ constructor)
  854.  Zone::Zone(lua_State *L)  
  855.  {
  856. +   if(L)
  857. +   {
  858. +      static LuaFunctionArgList constructorArgList = { {{ END }, { GEOM, END }}, 2 };
  859. +      S32 profile = checkArgList(L, constructorArgList, "Zone", "constructor");
  860. +        
  861. +      if(profile == 1)
  862. +      {
  863. +         setGeom(L, 1);
  864. +      }
  865. +   }
  866. +  
  867.     setTeam(TEAM_NEUTRAL);
  868.     mObjectTypeNumber = ZoneTypeNumber;
  869.  
  870. @@ -148,7 +160,9 @@
  871.  /////
  872.  // Lua interface
  873.  
  874. -/**
  875. +/**
  876. +  *  @luafunc Zone::Zone()
  877. +  *  @luafunc Zone::Zone(geom)
  878.    *  @luaclass Zone
  879.    *  @brief Invisible objects, used mainly for generating events.
  880.    */
  881. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/barrier.cpp
  882. --- a/zap/barrier.cpp   Sat Jan 12 13:30:52 2013 +0100
  883. +++ b/zap/barrier.cpp   Sun Jan 13 11:34:27 2013 +0100
  884. @@ -31,6 +31,7 @@
  885.  #include "game.h"
  886.  #include "config.h"
  887.  #include "stringUtils.h"
  888. +#include "LuaWrapper.h"
  889.  
  890.  #ifndef ZAP_DEDICATED
  891.  #  include "OpenglUtils.h"
  892. @@ -349,6 +350,18 @@
  893.     mAlreadyAdded = false;
  894.  
  895.     setNewGeometry(geomPolyLine);
  896. +  
  897. +   if(L)
  898. +   {
  899. +      static LuaFunctionArgList constructorArgList = { {{ END }, { GEOM, INT, END }}, 2 };
  900. +      S32 profile = checkArgList(L, constructorArgList, "WallItem", "constructor");
  901. +      if(profile == 1)
  902. +      {
  903. +         setWidth(lua_tointeger(L, -1));
  904. +         lua_pop(L, 1);
  905. +         setGeom(L);
  906. +      }
  907. +   }
  908.  
  909.     LUAW_CONSTRUCTOR_INITIALIZATIONS;
  910.  }
  911. @@ -571,6 +584,8 @@
  912.  /////
  913.  // Lua interface
  914.  /**
  915. +  *  @luafunc WallItem::WallItem()
  916. +  *  @luafunc WallItem::WallItem(geom, thickness)
  917.    *  @luaclass WallItem
  918.    *  @brief Traditional wall item.
  919.    *  @descr A %WallItem is a traditional wall consisting of a series of straight-line segments.  WallItems have a width setting that
  920. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/game.cpp
  921. --- a/zap/game.cpp  Sat Jan 12 13:30:52 2013 +0100
  922. +++ b/zap/game.cpp  Sun Jan 13 11:34:27 2013 +0100
  923. @@ -141,7 +141,6 @@
  924.  
  925.     mNetInterface = new GameNetInterface(theBindAddress, this);
  926.     mHaveTriedToConnectToMaster = false;
  927. -   mDoAnonymousMasterConnection = false;
  928.  
  929.     mNameToAddressThread = NULL;
  930.  
  931. @@ -302,10 +301,9 @@
  932.  }
  933.  
  934.  
  935. -void Game::setReadyToConnectToMaster(bool ready, bool doAnonymous)
  936. +void Game::setReadyToConnectToMaster(bool ready)
  937.  {
  938.     mReadyToConnectToMaster = ready;
  939. -   mDoAnonymousMasterConnection = doAnonymous;
  940.  }
  941.  
  942.  
  943. @@ -452,6 +450,18 @@
  944.  }
  945.  
  946.  
  947. +void Game::runAnonymousMasterRequest(MasterConnectionCallback callback)
  948. +{
  949. +   TNLAssert(mAnonymousMasterServerConnection.isNull(), "An anonymous master connection is still open!");
  950. +
  951. +   // Create a new anonymous connection and set a callback method
  952. +   // You need to make sure to remember to put terminateIfAnonymous() into the master
  953. +   // response RPC that this callback calls
  954. +   mAnonymousMasterServerConnection = new AnonymousMasterServerConnection(this);
  955. +   mAnonymousMasterServerConnection->setConnectionCallback(callback);
  956. +}
  957. +
  958. +
  959.  GameType *Game::getGameType() const
  960.  {
  961.     return mGameType;    // This is a safePtr, so it can be NULL, but will never point off into space
  962. @@ -935,10 +945,6 @@
  963.                    TNLAssert(!mConnectionToMaster.isValid(), "Already have connection to master!");
  964.                    mConnectionToMaster = new MasterServerConnection(this);
  965.  
  966. -                  // Are we doing an anonymous connection?
  967. -                  if(mDoAnonymousMasterConnection)
  968. -                     mConnectionToMaster->setConnectionType(MasterConnectionTypeAnonymous);
  969. -
  970.                    mConnectionToMaster->connect(mNetInterface, mNameToAddressThread->mAddress);
  971.                 }
  972.    
  973. @@ -953,6 +959,46 @@
  974.        else
  975.           mNextMasterTryTime -= timeDelta;
  976.     }
  977. +
  978. +   processAnonymousMasterConnection();
  979. +}
  980. +
  981. +
  982. +void Game::processAnonymousMasterConnection()
  983. +{
  984. +   // Connection doesn't exist yet
  985. +   if(!mAnonymousMasterServerConnection.isValid())
  986. +      return;
  987. +
  988. +   // Connection has already been initiated
  989. +   if(mAnonymousMasterServerConnection->isInitiator())
  990. +      return;
  991. +
  992. +   // Try to open a socket to master server
  993. +   if(!mNameToAddressThread)
  994. +   {
  995. +      Vector<string> *masterServerList = mSettings->getMasterServerList();
  996. +
  997. +      // No master server addresses?
  998. +      if(masterServerList->size() == 0)
  999. +         return;
  1000. +
  1001. +      const char *addr = masterServerList->get(0).c_str();
  1002. +
  1003. +      mNameToAddressThread = new NameToAddressThread(addr);
  1004. +      mNameToAddressThread->start();
  1005. +   }
  1006. +   else
  1007. +   {
  1008. +      if(mNameToAddressThread->mDone)
  1009. +      {
  1010. +         if(mNameToAddressThread->mAddress.isValid())
  1011. +            mAnonymousMasterServerConnection->connect(mNetInterface, mNameToAddressThread->mAddress);
  1012. +
  1013. +         delete mNameToAddressThread;
  1014. +         mNameToAddressThread = NULL;
  1015. +      }
  1016. +   }
  1017.  }
  1018.  
  1019.  
  1020. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/game.h
  1021. --- a/zap/game.h    Sat Jan 12 13:30:52 2013 +0100
  1022. +++ b/zap/game.h    Sun Jan 13 11:34:27 2013 +0100
  1023. @@ -110,6 +110,7 @@
  1024.  ////////////////////////////////////////
  1025.  
  1026.  // Some forward declarations
  1027. +class AnonymousMasterServerConnection;
  1028.  class MasterServerConnection;
  1029.  class GameNetInterface;
  1030.  class GameType;
  1031. @@ -128,6 +129,8 @@
  1032.  
  1033.  struct IniSettings;
  1034.  
  1035. +typedef void (MasterServerConnection::*MasterConnectionCallback)();
  1036. +
  1037.  // Modes the player could be in during the game
  1038.  enum UIMode {
  1039.     PlayMode,               // Playing
  1040. @@ -200,7 +203,6 @@
  1041.     U32 mNextMasterTryTime;
  1042.  
  1043.     bool mReadyToConnectToMaster;
  1044. -   bool mDoAnonymousMasterConnection;
  1045.  
  1046.     Vector<Robot *> mRobots;               // Grand master list of all robots in the current game
  1047.     Rect mWorldExtents;                    // Extents of everything
  1048. @@ -229,6 +231,10 @@
  1049.     RefPtr<GameNetInterface> mNetInterface;
  1050.  
  1051.     SafePtr<MasterServerConnection> mConnectionToMaster;
  1052. +
  1053. +   // Not really a queue, but good enough for now!
  1054. +   SafePtr<AnonymousMasterServerConnection> mAnonymousMasterServerConnection;
  1055. +
  1056.     SafePtr<GameType> mGameType;
  1057.  
  1058.     bool mGameSuspended;       // True if we're in "suspended animation" mode
  1059. @@ -339,6 +345,9 @@
  1060.     void checkConnectionToMaster(U32 timeDelta);
  1061.     MasterServerConnection *getConnectionToMaster();
  1062.  
  1063. +   void runAnonymousMasterRequest(MasterConnectionCallback callback);
  1064. +   void processAnonymousMasterConnection();
  1065. +
  1066.     GameNetInterface *getNetInterface();
  1067.     virtual GridDatabase *getGameObjDatabase();
  1068.  
  1069. @@ -382,7 +391,7 @@
  1070.  
  1071.     void resetMasterConnectTimer();
  1072.  
  1073. -   void setReadyToConnectToMaster(bool ready, bool doAnonymous = false);
  1074. +   void setReadyToConnectToMaster(bool ready);
  1075.  
  1076.     // Objects in a given level, used for status bar.  On server it's objects loaded from file, on client, it's objects dl'ed from server.
  1077.     S32 mObjectsLoaded;  
  1078. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/luaLevelGenerator.cpp
  1079. --- a/zap/luaLevelGenerator.cpp Sat Jan 12 13:30:52 2013 +0100
  1080. +++ b/zap/luaLevelGenerator.cpp Sun Jan 13 11:34:27 2013 +0100
  1081. @@ -394,7 +394,7 @@
  1082.        gt->sendChatFromController(message);
  1083.  
  1084.        // Fire our event handler
  1085. -      EventManager::get()->fireEvent(getScriptId(), EventManager::MsgReceivedEvent, message, NULL, true);
  1086. +      EventManager::get()->fireEvent(this, EventManager::MsgReceivedEvent, message, NULL, true);
  1087.     }
  1088.  
  1089.     return 0;
  1090. @@ -438,7 +438,10 @@
  1091.  S32 LuaLevelGenerator::unsubscribe(lua_State *L) { return doUnsubscribe(L); }
  1092.  
  1093.  
  1094. -const LuaFunctionProfile LuaLevelGenerator::functionArgs[] = { NULL, {{{ }}, 0 } };
  1095. +const LuaFunctionProfile LuaLevelGenerator::functionArgs[] =
  1096. +{
  1097. +      { NULL, {{{ }}, 0 } }
  1098. +};
  1099.  
  1100.  REGISTER_LUA_CLASS(LuaLevelGenerator);
  1101.  
  1102. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/masterConnection.cpp
  1103. --- a/zap/masterConnection.cpp  Sat Jan 12 13:30:52 2013 +0100
  1104. +++ b/zap/masterConnection.cpp  Sun Jan 13 11:34:27 2013 +0100
  1105. @@ -74,7 +74,13 @@
  1106.  
  1107.  MasterServerConnection::MasterServerConnection()
  1108.  {
  1109. -   TNLAssert(false, "We need a default constructor, but we never actually use it!");
  1110. +   TNLAssert(false, "We need a default constructor, but we never actually use it!");
  1111. +}
  1112. +
  1113. +
  1114. +MasterServerConnection::~MasterServerConnection()
  1115. +{
  1116. +   // Do nothing
  1117.  }
  1118.  
  1119.  
  1120. @@ -245,6 +251,8 @@
  1121.  
  1122.     setMasterName(masterName.getString());
  1123.     static_cast<ClientGame *>(mGame)->setMOTD(motdString.getString());
  1124. +
  1125. +   terminateIfAnonymous();
  1126.  }
  1127.  
  1128.  
  1129. @@ -512,6 +520,57 @@
  1130.  }
  1131.  
  1132.  
  1133. +void MasterServerConnection::terminateIfAnonymous()
  1134. +{
  1135. +   if(mConnectionType != MasterConnectionTypeAnonymous)
  1136. +      return;
  1137. +
  1138. +   // Actions performed on an anonymous connection should terminate the connection
  1139. +   disconnect(ReasonAnonymous, "");
  1140. +}
  1141. +
  1142. +
  1143. +AnonymousMasterServerConnection::AnonymousMasterServerConnection(Game *game) : Parent(game)
  1144. +{
  1145. +   mConnectionType = MasterConnectionTypeAnonymous;
  1146. +   mConnectionCallback = NULL;
  1147. +}
  1148. +
  1149. +
  1150. +AnonymousMasterServerConnection::~AnonymousMasterServerConnection()
  1151. +{
  1152. +   // Do nothing
  1153. +}
  1154. +
  1155. +
  1156. +// Set a function to call upon establishing a connection
  1157. +void AnonymousMasterServerConnection::setConnectionCallback(MasterConnectionCallback callback)
  1158. +{
  1159. +   mConnectionCallback = callback;
  1160. +}
  1161. +
  1162. +
  1163. +MasterConnectionCallback AnonymousMasterServerConnection::getConnectionCallback()
  1164. +{
  1165. +   return mConnectionCallback;
  1166. +}
  1167. +
  1168. +
  1169. +void AnonymousMasterServerConnection::onConnectionEstablished()
  1170. +{
  1171. +   Parent::onConnectionEstablished();
  1172. +
  1173. +   // Run the callback
  1174. +   // This uses some funky syntax.  See sections 2.1 and 2.5 of:
  1175. +   //   http://www.newty.de/fpt/fpt.html#chapter2
  1176. +   if(mConnectionCallback)
  1177. +   {
  1178. +      (*this.*mConnectionCallback)();
  1179. +      mConnectionCallback = NULL;
  1180. +   }
  1181. +}
  1182. +
  1183. +
  1184.  };
  1185.  
  1186.  
  1187. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/masterConnection.h
  1188. --- a/zap/masterConnection.h    Sat Jan 12 13:30:52 2013 +0100
  1189. +++ b/zap/masterConnection.h    Sun Jan 13 11:34:27 2013 +0100
  1190. @@ -50,11 +50,15 @@
  1191.     Game *mGame;
  1192.     string mMasterName;
  1193.  
  1194. +   void terminateIfAnonymous();
  1195. +
  1196. +protected:
  1197.     MasterConnectionType mConnectionType;
  1198.  
  1199.  public:
  1200.     MasterServerConnection(Game *game);    // Constructor
  1201. -   MasterServerConnection();
  1202. +   MasterServerConnection();              // Default Constructor required by TNL for some reason..
  1203. +   virtual ~MasterServerConnection();     // Destructor
  1204.  
  1205.     void setConnectionType(MasterConnectionType type);
  1206.     MasterConnectionType getConnectionType();
  1207. @@ -104,13 +108,33 @@
  1208.                                                    RangedU32<0,AuthenticationStatusCount> status, Int<BADGE_COUNT> badges));
  1209.  
  1210.     void writeConnectRequest(BitStream *bstream);
  1211. -   void onConnectionEstablished();
  1212. +   virtual void onConnectionEstablished();
  1213.     void onConnectionTerminated(TerminationReason r, const char *string); // An existing connection has been terminated
  1214.     void onConnectTerminated(TerminationReason r, const char *string);    // A still-being-established connection has been terminated
  1215.  
  1216.     TNL_DECLARE_NETCONNECTION(MasterServerConnection);
  1217.  };
  1218.  
  1219. +
  1220. +typedef void (MasterServerConnection::*MasterConnectionCallback)();
  1221. +
  1222. +class AnonymousMasterServerConnection : public MasterServerConnection
  1223. +{
  1224. +   typedef MasterServerConnection Parent;
  1225. +
  1226. +private:
  1227. +   MasterConnectionCallback mConnectionCallback;
  1228. +
  1229. +public:
  1230. +   AnonymousMasterServerConnection(Game *game);    // Constructor
  1231. +   virtual ~AnonymousMasterServerConnection();
  1232. +
  1233. +   void setConnectionCallback(MasterConnectionCallback callback);
  1234. +   MasterConnectionCallback getConnectionCallback();
  1235. +
  1236. +   void onConnectionEstablished();
  1237. +};
  1238. +
  1239.  };
  1240.  
  1241.  #endif
  1242. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/projectile.cpp
  1243. --- a/zap/projectile.cpp    Sat Jan 12 13:30:52 2013 +0100
  1244. +++ b/zap/projectile.cpp    Sun Jan 13 11:34:27 2013 +0100
  1245. @@ -1017,7 +1017,20 @@
  1246.  // Combined Lua / C++ default constructor -- used in Lua and editor
  1247.  SpyBug::SpyBug(lua_State *L) : Burst(Point(0,0), Point(0,0), NULL)
  1248.  {
  1249. -   initialize(Point(0,0), NULL);
  1250. +   if(L)
  1251. +   {
  1252. +      static LuaFunctionArgList constructorArgList = { {{ END }, { GEOM, TEAM_INDX, END }}, 2 };
  1253. +      S32 profile = checkArgList(L, constructorArgList, "SpyBug", "constructor");
  1254. +      
  1255. +      if(profile == 0)
  1256. +         initialize(Point(0,0), NULL);
  1257. +      else if(profile == 1)
  1258. +      {
  1259. +         initialize(Point(0,0), NULL);
  1260. +         setPos(getPointOrXY(L, 1));
  1261. +         setTeam(getInt(L, 2));
  1262. +      }
  1263. +   }
  1264.  }
  1265.  
  1266.  
  1267. @@ -1240,6 +1253,14 @@
  1268.  
  1269.  /////
  1270.  // Lua interface
  1271. +/**
  1272. +  *  @luafunc SpyBug::SpyBug()
  1273. +  *  @luafunc SpyBug::SpyBug(geom, team)
  1274. +  *  @luaclass SpyBug
  1275. +  *  @brief Monitors a section of the map and will show enemy ships there.
  1276. +  *  @descr Can only be used/created if the Sensor module is selected.
  1277. +  *         Makes surrounding areas of the commander's map visible to player and teammates.
  1278. +  */
  1279.  //                Fn name                  Param profiles            Profile count                          
  1280.  #define LUA_METHODS(CLASS, METHOD) \
  1281.  
  1282. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/robot.cpp
  1283. --- a/zap/robot.cpp Sat Jan 12 13:30:52 2013 +0100
  1284. +++ b/zap/robot.cpp Sun Jan 13 11:34:27 2013 +0100
  1285. @@ -108,7 +108,7 @@
  1286.     // Server only from here on down
  1287.     if(getGame())  // can be NULL if this robot was never added to game (bad / missing robot file)
  1288.     {
  1289. -      EventManager::get()->fireEvent(getScriptId(), EventManager::PlayerLeftEvent, getPlayerInfo());
  1290. +      EventManager::get()->fireEvent(this, EventManager::PlayerLeftEvent, getPlayerInfo());
  1291.  
  1292.        if(getGame()->getGameType())
  1293.           getGame()->getGameType()->serverRemoveClient(mClientInfo);
  1294. @@ -172,7 +172,7 @@
  1295.        return false;
  1296.  
  1297.     // Pass true so that if this bot doesn't have a TickEvent handler, we don't print a message
  1298. -   EventManager::get()->subscribe(getScriptId(), EventManager::TickEvent, RobotContext, true);
  1299. +   EventManager::get()->subscribe(this, EventManager::TickEvent, RobotContext, true);
  1300.  
  1301.     mSubscriptions[EventManager::TickEvent] = true;
  1302.  
  1303. @@ -298,7 +298,7 @@
  1304.  
  1305.     game->addBot(this);        // Add this robot to the list of all robots (can't do this in constructor or else it gets run on client side too...)
  1306.    
  1307. -   EventManager::get()->fireEvent(getScriptId(), EventManager::PlayerJoinedEvent, getPlayerInfo());
  1308. +   EventManager::get()->fireEvent(this, EventManager::PlayerJoinedEvent, getPlayerInfo());
  1309.  }
  1310.  
  1311.  
  1312. @@ -1092,7 +1092,7 @@
  1313.        gt->sendChatFromRobot(true, message, getClientInfo());
  1314.  
  1315.        // Fire our event handler
  1316. -      EventManager::get()->fireEvent(getScriptId(), EventManager::MsgReceivedEvent, message, getPlayerInfo(), true);
  1317. +      EventManager::get()->fireEvent(this, EventManager::MsgReceivedEvent, message, getPlayerInfo(), true);
  1318.     }
  1319.  
  1320.     return 0;
  1321. @@ -1112,7 +1112,7 @@
  1322.        gt->sendChatFromRobot(false, message, getClientInfo());
  1323.  
  1324.        // Fire our event handler
  1325. -      EventManager::get()->fireEvent(getScriptId(), EventManager::MsgReceivedEvent, message, getPlayerInfo(), false);
  1326. +      EventManager::get()->fireEvent(this, EventManager::MsgReceivedEvent, message, getPlayerInfo(), false);
  1327.     }
  1328.  
  1329.     return 0;
  1330. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/speedZone.cpp
  1331. --- a/zap/speedZone.cpp Sat Jan 12 13:30:52 2013 +0100
  1332. +++ b/zap/speedZone.cpp Sun Jan 13 11:34:27 2013 +0100
  1333. @@ -63,6 +63,9 @@
  1334.  // Combined C++/Lua constructor
  1335.  SpeedZone::SpeedZone(lua_State *L)
  1336.  {
  1337. +   static LuaFunctionArgList constructorArgList = { {{ END }, { GEOM, END }, { GEOM, NUM, END }}, 3 };
  1338. +   S32 profile = checkArgList(L, constructorArgList, "SpeedZone", "constructor");
  1339. +  
  1340.     mNetFlags.set(Ghostable);
  1341.     mObjectTypeNumber = SpeedZoneTypeNumber;
  1342.  
  1343. @@ -72,6 +75,14 @@
  1344.     mUnpackInit = 0;           // Some form of counter, to know that it is a rotating speed zone
  1345.  
  1346.     preparePoints();           // If this is constructed by Lua, we need to have some default geometry in place
  1347. +  
  1348. +   if(L && profile == 1)
  1349. +      setPos(getPointOrXY(L, 1));
  1350. +   else if(L && profile == 2)
  1351. +   {
  1352. +      setPos(getPointOrXY(L, 1));
  1353. +      setSpeed(getInt(L, 2));
  1354. +   }
  1355.  
  1356.     LUAW_CONSTRUCTOR_INITIALIZATIONS;
  1357.  }
  1358. @@ -530,6 +541,9 @@
  1359.  //// Lua methods
  1360.  
  1361.  /**
  1362. +  *  @luafunc SpeedZone::SpeedZone()
  1363. +  *  @luafunc SpeedZone::SpeedZone(geom)
  1364. +  *  @luafunc SpeedZone::SpeedZone(geom, speed)
  1365.    *  @luaclass SpeedZone
  1366.    *  @brief Propels ships at high speed.
  1367.    *  @descr SpeedZones are game objects that propel ships around a level.  Each %SpeedZone has a direction point
  1368. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/teleporter.cpp
  1369. --- a/zap/teleporter.cpp    Sat Jan 12 13:30:52 2013 +0100
  1370. +++ b/zap/teleporter.cpp    Sun Jan 13 11:34:27 2013 +0100
  1371. @@ -84,14 +84,13 @@
  1372.  
  1373.  void DestManager::addDest(const Point &dest)
  1374.  {
  1375. +   TNLAssert(mOwner, "We need an owner here!");
  1376.     mDests.push_back(dest);
  1377. -
  1378.     if(mDests.size() == 1)      // Just added the first dest
  1379.     {
  1380.        mOwner->setVert(dest, 1);
  1381.        mOwner->updateExtentInDatabase();
  1382.     }
  1383. -  
  1384.     // If we're the server, update the clients
  1385.     if(mOwner->getGame() && mOwner->getGame()->isServer())
  1386.        mOwner->s2cAddDestination(dest);
  1387. @@ -176,7 +175,16 @@
  1388.  // Combined default C++/Lua constructor
  1389.  Teleporter::Teleporter(lua_State *L)
  1390.  {
  1391. -   initialize(Point(0,0), Point(0,0), NULL);
  1392. +   initialize(Point(0,0),Point(0,0),NULL);
  1393. +   // TODO: Fix this one too -- intiailze needs to be refactored because who knows
  1394. +   // how many points the teleporter is getting?  could be 1, could be 2, could be many
  1395. +   if(L)
  1396. +   {
  1397. +      static LuaFunctionArgList constructorArgList = { {{ END }, { GEOM, END }}, 2 };
  1398. +      S32 profile = checkArgList(L, constructorArgList, "Teleporter", "constructor");
  1399. +      if(profile == 1)
  1400. +         setPos(getPointOrXY(L, 1));
  1401. +   }
  1402.  }
  1403.  
  1404.  
  1405. @@ -857,6 +865,8 @@
  1406.  //// Lua methods
  1407.  
  1408.  /**
  1409. +  *  @luafunc Teleporter::Teleporter()
  1410. +  *  @luafunc Teleporter::Teleporter(geom)
  1411.    *  @luaclass Teleporter
  1412.    *  @brief Instantly transports ships from here to there.
  1413.    *  @descr A %Teleporter represents the basic teleporter object.  Every teleporter has an intake location
  1414. diff -r a8a4d5213b1e -r f6c2d27eb104 zap/textItem.cpp
  1415. --- a/zap/textItem.cpp  Sat Jan 12 13:30:52 2013 +0100
  1416. +++ b/zap/textItem.cpp  Sun Jan 13 11:34:27 2013 +0100
  1417. @@ -53,6 +53,9 @@
  1418.  // Combined Lua / C++ constructor
  1419.  TextItem::TextItem(lua_State *L)
  1420.  {
  1421. +   static LuaFunctionArgList constructorArgList = { {{ END }, { GEOM, STR, END }}, 2 };
  1422. +   S32 profile = checkArgList(L, constructorArgList, "Spawn", "constructor");
  1423. +  
  1424.     mNetFlags.set(Ghostable);
  1425.     mObjectTypeNumber = TextItemTypeNumber;
  1426.  
  1427. @@ -60,6 +63,12 @@
  1428.     mSize = 20;
  1429.  
  1430.     LUAW_CONSTRUCTOR_INITIALIZATIONS;
  1431. +  
  1432. +   if(L && profile == 1)
  1433. +   {
  1434. +      setPos(getPointOrXY(L, 1));
  1435. +      setText(getString(L, 2));
  1436. +   }
  1437.  }
  1438.  
  1439.  
  1440. @@ -426,6 +435,8 @@
  1441.  //// Lua methods
  1442.  
  1443.  /**
  1444. +  *  @luafunc TextItem::TextItem()
  1445. +  *  @luafunc TextItem::TextItem(geom, text)
  1446.    *  @luaclass TextItem
  1447.    *  @brief Display text message in level.
  1448.    *  @descr A %TextItem displays text in a level.  If the %TextItem belongs to a team, it is only visible to players on that team.
Advertisement
Add Comment
Please, Sign In to add comment