message.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. // Functions that process commands sent from the server.
  24. // This function is for chat messages only; it is invoked on the client when
  25. // the server does a commandToClient with the tag ChatMessage. (Cf. the
  26. // functions chatMessage* in core/scripts/server/message.cs.)
  27. // This just invokes onChatMessage, which the mod code must define.
  28. function clientCmdChatMessage(%sender, %voice, %pitch, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
  29. {
  30. onChatMessage(detag(%msgString), %voice, %pitch);
  31. }
  32. // Game event descriptions, which may or may not include text messages, can be
  33. // sent using the message* functions in core/scripts/server/message.cs. Those
  34. // functions do commandToClient with the tag ServerMessage, which invokes the
  35. // function below.
  36. // For ServerMessage messages, the client can install callbacks that will be
  37. // run, according to the "type" of the message.
  38. function clientCmdServerMessage(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
  39. {
  40. // Get the message type; terminates at any whitespace.
  41. %tag = getWord(%msgType, 0);
  42. // First see if there is a callback installed that doesn't have a type;
  43. // if so, that callback is always executed when a message arrives.
  44. for (%i = 0; (%func = $MSGCB["", %i]) !$= ""; %i++) {
  45. call(%func, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
  46. }
  47. // Next look for a callback for this particular type of ServerMessage.
  48. if (%tag !$= "") {
  49. for (%i = 0; (%func = $MSGCB[%tag, %i]) !$= ""; %i++) {
  50. call(%func, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
  51. }
  52. }
  53. }
  54. // Called by the client to install a callback for a particular type of
  55. // ServerMessage.
  56. function addMessageCallback(%msgType, %func)
  57. {
  58. for (%i = 0; (%afunc = $MSGCB[%msgType, %i]) !$= ""; %i++) {
  59. // If it already exists as a callback for this type,
  60. // nothing to do.
  61. if (%afunc $= %func) {
  62. return;
  63. }
  64. }
  65. // Set it up.
  66. $MSGCB[%msgType, %i] = %func;
  67. }
  68. // The following is the callback that will be executed for every ServerMessage,
  69. // because we're going to install it without a specified type. Any type-
  70. // specific callbacks will be executed afterward.
  71. // This just invokes onServerMessage, which can be overridden by the game
  72. function onServerMessage(%a, %b, %c, %d, %e, %f, %g, %h, %i)
  73. {
  74. echo("onServerMessage: ");
  75. if(%a !$= "") echo(" +- a: " @ %a);
  76. if(%b !$= "") echo(" +- b: " @ %b);
  77. if(%c !$= "") echo(" +- c: " @ %c);
  78. if(%d !$= "") echo(" +- d: " @ %d);
  79. if(%e !$= "") echo(" +- e: " @ %e);
  80. if(%f !$= "") echo(" +- f: " @ %f);
  81. if(%g !$= "") echo(" +- g: " @ %g);
  82. if(%h !$= "") echo(" +- h: " @ %h);
  83. if(%i !$= "") echo(" +- i: " @ %i);
  84. }
  85. function defaultMessageCallback(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
  86. {
  87. onServerMessage(detag(%msgString));
  88. }
  89. // Register that default message handler now.
  90. addMessageCallback("", defaultMessageCallback);