Pol  Revision:cb584c9
tips.cpp
Go to the documentation of this file.
1 
8 #include <string>
9 
10 #include "../clib/clib_endian.h"
11 #include "../clib/rawtypes.h"
12 #include "globals/uvars.h"
13 #include "network/packethelper.h"
14 #include "network/packets.h"
15 #include "pktdef.h"
16 #include "pktin.h"
17 
18 namespace Pol
19 {
20 namespace Core
21 {
22 bool send_tip( Network::Client* client, const char* tipname, unsigned short tipnum )
23 {
24  size_t textlen = strlen( tipname );
25  if ( textlen > 0 && unsigned( textlen ) <= 9999 )
26  {
28  msg->WriteFlipped<u16>( textlen + 11 );
29  msg->Write<u8>( PKTOUT_A6_TYPE_TIP );
30  msg->offset += 2; // unk4,5
31  msg->WriteFlipped<u16>( tipnum );
32  msg->WriteFlipped<u16>( textlen + 1 );
33  msg->Write( tipname, static_cast<u16>( textlen + 1 ) );
34  msg.Send( client );
35  return true;
36  }
37  else
38  {
39  return false;
40  }
41 }
42 
43 void send_tip( Network::Client* client, const std::string& tiptext )
44 {
45  size_t textlen = tiptext.size();
46  if ( textlen >= 10000 )
47  textlen = 9999;
48 
50  msg->WriteFlipped<u16>( textlen + 11 );
51  msg->Write<u8>( PKTOUT_A6_TYPE_TIP );
52  msg->offset += 2; // unk4,5
53  msg->offset += 2; // tipnum
54  msg->WriteFlipped<u16>( textlen + 1 );
55  msg->Write( tiptext.c_str(), static_cast<u16>( textlen + 1 ) );
56  msg.Send( client );
57 }
58 
60 {
61  u16 tipnum = cfBEu16( msg->lasttip );
62  if ( !gamestate.tipfilenames.empty() )
63  {
64  if ( msg->prevnext )
65  {
66  ++tipnum;
67  if ( tipnum >= gamestate.tipfilenames.size() )
68  tipnum = 0;
69  }
70  else
71  {
72  --tipnum;
73  if ( tipnum >= gamestate.tipfilenames.size() )
74  tipnum = static_cast<u16>( gamestate.tipfilenames.size() ) - 1;
75  }
76 
77  send_tip( client, gamestate.tipfilenames[tipnum].c_str(), tipnum );
78  }
79 }
80 }
81 }
unsigned char u8
Definition: rawtypes.h:25
#define PKTOUT_A6_TYPE_TIP
Definition: pktdef.h:130
void handle_get_tip(Network::Client *client, PKTIN_A7 *msg)
Definition: tips.cpp:59
unsigned short u16
Definition: rawtypes.h:26
std::vector< std::string > tipfilenames
Definition: uvars.h:192
void Send(Client *client, int len=-1) const
Definition: packethelper.h:69
#define cfBEu16(x)
Definition: clib_endian.h:44
GameState gamestate
Definition: uvars.cpp:74
Definition: berror.cpp:12
void send_tip(Network::Client *client, const std::string &tiptext)
Definition: tips.cpp:43