objserial.cc

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2001  The Exult Team
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #  include <config.h>
00027 #endif
00028 
00029 #include "utils.h"
00030 #include "objserial.h"
00031 #include "servemsg.h"
00032 #include <iostream>
00033 
00034 using std::cout;
00035 using std::endl;
00036 
00037 /*
00038  *  Write out a string.
00039  */
00040 Serial_out& Serial_out::operator<<
00041   (
00042   std::string& s
00043   )
00044   {
00045   const char *str = s.c_str();
00046   int len = std::strlen(str);   // Get length.
00047   *this << len;     // First the length.
00048   std::memcpy(buf, str, len);   // Then the bytes.
00049   buf += len;
00050   return *this;
00051   }
00052 
00053 
00054 /*
00055  *  Read in a string.
00056  */
00057 Serial_in& Serial_in::operator<<
00058   (
00059   std::string& s
00060   )
00061   {
00062   int len;
00063   (*this) << len;     // Get length.
00064   s.assign((char *) buf, len);  // Set string.
00065   buf += len;
00066   return *this;
00067   }
00068 
00069 /*
00070  *  Read/write out data common to all objects.
00071  *
00072  *  Output: 1 if successful, else 0.
00073  */
00074 
00075 template <class Serial> 
00076 void Common_obj_io
00077   (
00078   Serial& io,
00079   unsigned long& addr,    // Address.
00080   int& tx, int& ty, int& tz,  // Absolute tile coords.
00081   int& shape, int& frame
00082   )
00083   {
00084   io << addr << tx << ty << tz << shape << frame;
00085   }
00086 
00087 /*
00088  *  Low-level serialization for use both by Exult and ExultStudio (so
00089  *  don't put in anything that will pull in all of Exult).
00090  *
00091  *  Output: 1 if successful, else 0.
00092  */
00093 template <class Serial> 
00094 void Object_io
00095   (
00096   Serial &io,     // Where to store data.
00097   unsigned long& addr,    // Address.
00098   int& tx, int& ty, int& tz,  // Absolute tile coords.
00099   int& shape, int& frame,
00100   int& quality,
00101   std::string& name
00102   )
00103   {
00104   Common_obj_io<Serial>(io, addr, tx, ty, tz, shape, frame);
00105   io << quality << name;
00106   }
00107 
00108 /*
00109  *  Low-level serialization for use both by Exult and ExultStudio (so
00110  *  don't put in anything that will pull in all of Exult).
00111  *
00112  *  Output: 1 if successful, else 0.
00113  */
00114 template <class Serial> 
00115 void Egg_object_io
00116   (
00117   Serial &io,     // Where to store data.
00118   unsigned long& addr,    // Address.
00119   int& tx, int& ty, int& tz,  // Absolute tile coords.
00120   int& shape, int& frame,
00121   int& type,
00122   int& criteria,
00123   int& probability,
00124   int& distance,
00125   bool& nocturnal,
00126   bool& once,
00127   bool& hatched,
00128   bool& auto_reset,
00129   int& data1, int& data2
00130   )
00131   {
00132   Common_obj_io<Serial>(io, addr, tx, ty, tz, shape, frame);
00133   io << type << criteria << probability << distance << 
00134     nocturnal << once << hatched << auto_reset << data1 << data2;
00135   }
00136 
00137 /*
00138  *  Low-level serialization for use both by Exult and ExultStudio (so
00139  *  don't put in anything that will pull in all of Exult).
00140  *
00141  *  Output: 1 if successful, else 0.
00142  */
00143 template <class Serial> 
00144 static void Npc_actor_io
00145   (
00146   Serial &io,     // Where to store data.
00147   unsigned long& addr,    // Address.
00148   int& tx, int& ty, int& tz,  // Absolute tile coords.
00149   int& shape, int& frame, int& face,
00150   std::string& name,
00151   short& npc_num,
00152   short& ident,
00153   int& usecode,
00154   int *properties,    // Must have room for 12.
00155   short& attack_mode,
00156   short& alignment,
00157   unsigned long& oflags,    // Object flags.
00158   unsigned long& siflags,   // Extra flags for SI.
00159   unsigned long& type_flags,  // Movement flags.
00160   short& num_schedules,   // # of schedule changes.
00161   Serial_schedule *schedules  // Schedule changes.  Room for 8.
00162   )
00163   {
00164   Common_obj_io<Serial>(io, addr, tx, ty, tz, shape, frame);
00165   io << face << name << npc_num << ident << usecode;
00166   int i;
00167   for (i = 0; i < 12; i++)
00168     io << properties[i];
00169   io << attack_mode << alignment << oflags << siflags << type_flags;
00170   io << num_schedules;
00171   for (i = 0; i < num_schedules; i++)
00172     io << schedules[i].time << schedules[i].type <<
00173         schedules[i].tx << schedules[i].ty;
00174   }
00175 
00176 /*
00177  *  Send out an object.
00178  *
00179  *  Output: -1 if unsuccessful.  0 if okay.
00180  */
00181 
00182 int Object_out
00183   (
00184   int fd,       // Socket.
00185   Exult_server::Msg_type id,  // Message id.
00186   unsigned long addr,   // Address.
00187   int tx, int ty, int tz,   // Absolute tile coords.
00188   int shape, int frame,
00189   int quality,
00190   std::string name
00191   )
00192   {
00193   static unsigned char buf[Exult_server::maxlength];
00194   unsigned char *ptr = &buf[0];
00195   Serial_out io(ptr);
00196   Object_io(io, addr, tx, ty, tz, shape, frame, quality,
00197     name);
00198   return Exult_server::Send_data(fd, id, buf, ptr - buf);
00199   }
00200 
00201 /*
00202  *  Decode an object.
00203  *
00204  *  Output: 0 if unsuccessful.
00205  */
00206 
00207 int Object_in
00208   (
00209   unsigned char *data,    // Data that was read.
00210   int datalen,      // Length of data.
00211   unsigned long& addr,    // Address.
00212   int& tx, int& ty, int& tz,  // Absolute tile coords.
00213   int& shape, int& frame,
00214   int& quality,
00215   std::string& name
00216   )
00217   {
00218   unsigned char *ptr = data;
00219   Serial_in io(ptr);
00220   Object_io(io, addr, tx, ty, tz, shape, frame, quality,
00221     name);
00222   return (ptr - data) == datalen;
00223   }
00224 
00225 /*
00226  *  Send out an egg object.
00227  *
00228  *  Output: -1 if unsuccessful.  0 if okay.
00229  */
00230 
00231 int Egg_object_out
00232   (
00233   int fd,       // Socket.
00234   unsigned long addr,   // Address.
00235   int tx, int ty, int tz, // Absolute tile coords.
00236   int shape, int frame,
00237   int type,
00238   int criteria,
00239   int probability,
00240   int distance,
00241   bool nocturnal,
00242   bool once,
00243   bool hatched,
00244   bool auto_reset,
00245   int data1, int data2
00246   )
00247   {
00248   static unsigned char buf[Exult_server::maxlength];
00249   unsigned char *ptr = &buf[0];
00250   Serial_out io(ptr);
00251   Egg_object_io(io, addr, tx, ty, tz, shape, frame,
00252     type, criteria, probability, distance, 
00253     nocturnal, once, hatched, auto_reset,
00254     data1, data2);
00255   return Exult_server::Send_data(fd, Exult_server::egg, buf, ptr - buf);
00256   }
00257 
00258 /*
00259  *  Decode an egg object.
00260  *
00261  *  Output: 0 if unsuccessful.
00262  */
00263 
00264 int Egg_object_in
00265   (
00266   unsigned char *data,    // Data that was read.
00267   int datalen,      // Length of data.
00268   unsigned long& addr,    // Address.
00269   int& tx, int& ty, int& tz,  // Absolute tile coords.
00270   int& shape, int& frame,
00271   int& type,
00272   int& criteria,
00273   int& probability,
00274   int& distance,
00275   bool& nocturnal,
00276   bool& once,
00277   bool& hatched,
00278   bool& auto_reset,
00279   int& data1, int& data2
00280   )
00281   {
00282   unsigned char *ptr = data;
00283   Serial_in io(ptr);
00284   Egg_object_io(io, addr, tx, ty, tz, shape, frame,
00285     type, criteria, probability, distance, 
00286     nocturnal, once, hatched, auto_reset, data1, data2);
00287   return (ptr - data) == datalen;
00288   }
00289 
00290 /*
00291  *  Send out an npc object.
00292  *
00293  *  Output: -1 if unsuccessful.  0 if okay.
00294  */
00295 
00296 int Npc_actor_out
00297   (
00298   int fd,       // Socket.
00299   unsigned long addr,   // Address.
00300   int tx, int ty, int tz,   // Absolute tile coords.
00301   int shape, int frame, int face,
00302   std::string name,
00303   short npc_num,
00304   short ident,
00305   int usecode,
00306   int properties[12],
00307   short attack_mode,
00308   short alignment,
00309   unsigned long oflags,   // Object flags.
00310   unsigned long siflags,    // Extra flags for SI.
00311   unsigned long type_flags, // Movement flags.
00312   short num_schedules,    // # of schedule changes.
00313   Serial_schedule *schedules  // Schedule changes.
00314   )
00315   {
00316   static unsigned char buf[Exult_server::maxlength];
00317   unsigned char *ptr = &buf[0];
00318   Serial_out io(ptr);
00319   Npc_actor_io(io, addr, tx, ty, tz, shape, frame, face,
00320     name, npc_num, ident, usecode, 
00321     properties, attack_mode, alignment,
00322     oflags, siflags, type_flags,
00323     num_schedules, schedules);
00324   return Exult_server::Send_data(fd, Exult_server::npc, buf, ptr - buf);
00325   }
00326 
00327 /*
00328  *  Decode an npc object.
00329  *
00330  *  Output: 0 if unsuccessful.
00331  */
00332 
00333 int Npc_actor_in
00334   (
00335   unsigned char *data,    // Data that was read.
00336   int datalen,      // Length of data.
00337   unsigned long& addr,    // Address.
00338   int& tx, int& ty, int& tz,  // Absolute tile coords.
00339   int& shape, int& frame, int& face,
00340   std::string& name,
00341   short& npc_num,
00342   short& ident,
00343   int& usecode,
00344   int properties[12],
00345   short& attack_mode,
00346   short& alignment,
00347   unsigned long& oflags,    // Object flags.
00348   unsigned long& siflags,   // Extra flags for SI.
00349   unsigned long& type_flags,  // Movement flags.
00350   short& num_schedules,   // # of schedule changes.
00351   Serial_schedule *schedules  // Schedule changes.  Room for 8.
00352   )
00353   {
00354   unsigned char *ptr = data;
00355   Serial_in io(ptr);
00356   Npc_actor_io(io, addr, tx, ty, tz, shape, frame, face,
00357     name, npc_num, ident, usecode, 
00358     properties, attack_mode, alignment,
00359     oflags, siflags, type_flags,
00360     num_schedules, schedules);
00361   return (ptr - data) == datalen;
00362   }
00363 
00364 /*
00365  *  Game info. IO.
00366  */
00367 
00368 template <class Serial> 
00369 void Game_info_io
00370   (
00371   Serial &io,     // Where to store data.
00372   int& version,     // Server/client version.
00373   int& edit_lift,     // Lift being edited.
00374   int& hide_lift,     // Lift being hidden.
00375   bool& map_editing,    // In 'map-editing' mode.
00376   bool& tile_grid,    // Showing tile grid.
00377   bool& map_modified,   // Map was changed.
00378   int& edit_mode      // Mode we're in.
00379   )
00380   {
00381   io << version << edit_lift << hide_lift << 
00382       map_editing << tile_grid << map_modified << edit_mode;
00383   }
00384 
00385 /*
00386  *  Send out game info.
00387  *
00388  *  Output: -1 if unsuccessful.  0 if okay.
00389  */
00390 
00391 int Game_info_out
00392   (
00393   int fd,       // Socket.
00394   int version,      // Server/client version.
00395   int edit_lift,      // Lift being edited.
00396   int hide_lift,      // Lift being hidden.
00397   bool map_editing,   // In 'map-editing' mode.
00398   bool tile_grid,     // Showing tile grid.
00399   bool map_modified,    // Map was changed.
00400   int edit_mode     // Mode we're in.
00401   )
00402   {
00403   static unsigned char buf[Exult_server::maxlength];
00404   unsigned char *ptr = &buf[0];
00405   Serial_out io(ptr);
00406   Game_info_io(io, version, edit_lift, hide_lift, 
00407       map_editing, tile_grid, map_modified, edit_mode);
00408   return Exult_server::Send_data(fd, Exult_server::info, buf, ptr - buf);
00409   }
00410 
00411 /*
00412  *  Decode game info.
00413  *
00414  *  Output: 0 if unsuccessful.
00415  */
00416 
00417 int Game_info_in
00418   (
00419   unsigned char *data,    // Data that was read.
00420   int datalen,      // Length of data.
00421   int& version,     // Server/client version.
00422   int& edit_lift,     // Lift being edited.
00423   int& hide_lift,     // Lift being hidden.
00424   bool& map_editing,    // In 'map-editing' mode.
00425   bool& tile_grid,    // Showing tile grid.
00426   bool& map_modified,   // Map was changed.
00427   int& edit_mode      // Mode we're in.
00428   )
00429   {
00430   unsigned char *ptr = data;
00431   Serial_in io(ptr);
00432   Game_info_io(io, version, edit_lift, hide_lift, map_editing, 
00433           tile_grid, map_modified, edit_mode);
00434   return (ptr - data) == datalen;
00435   }
00436 

Generated on Mon Jul 9 14:42:50 2007 for ExultEngine by  doxygen 1.5.1