author | unc0rr |
Mon, 09 Jan 2006 20:29:14 +0000 | |
changeset 41 | 5d7a505875cd |
parent 36 | a803a00a3272 |
child 84 | 0f6669da2fcb |
permissions | -rw-r--r-- |
28 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*/ |
|
33 |
||
34 |
#ifndef NET_H |
|
35 |
#define NET_H |
|
36 |
||
37 |
#include <QObject> |
|
38 |
#include <QTcpSocket> |
|
39 |
#include <QRegExp> |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
40 |
#include <QStringList> |
36 | 41 |
#include <QTimer> |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
42 |
#include "team.h" |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
43 |
#include "rndstr.h" |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
44 |
|
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
45 |
#define MAGIC_CHAR "\x02" |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
46 |
|
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
47 |
struct netTeam |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
48 |
{ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
49 |
QString nick; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
50 |
QStringList hhs; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
51 |
}; |
28 | 52 |
|
53 |
class HWNet : public QObject |
|
54 |
{ |
|
55 |
Q_OBJECT |
|
56 |
||
57 |
public: |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
58 |
HWNet(int Resolution, bool Fullscreen); |
29 | 59 |
void Connect(const QString & hostName, quint16 port, const QString & nick); |
28 | 60 |
void Disconnect(); |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
61 |
void JoinGame(const QString & game); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
62 |
void AddTeam(const HWTeam & team); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
63 |
void StartGame(); |
28 | 64 |
|
65 |
signals: |
|
66 |
void Connected(); |
|
67 |
void AddGame(const QString & chan); |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
68 |
void EnteredGame(); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
69 |
void FromNet(const QByteArray & buf); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
70 |
void LocalCFG(const QString & team); |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
31
diff
changeset
|
71 |
void ChangeInTeams(const QStringList & teams); |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
72 |
|
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
73 |
public slots: |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
74 |
void SendNet(const QByteArray & buf); |
28 | 75 |
|
76 |
private: |
|
77 |
enum NetState { |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
78 |
nsDisconnected = 0, |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
79 |
nsConnecting = 1, |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
80 |
nsConnected = 3, |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
81 |
nsJoining = 4, |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
82 |
nsJoined = 5, |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
83 |
nsStarting = 6, |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
84 |
nsGaming = 7, |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
85 |
nsQuitting = 8 |
28 | 86 |
}; |
87 |
||
88 |
QTcpSocket NetSocket; |
|
89 |
NetState state; |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
90 |
QRegExp * IRCmsg_cmd_text; |
28 | 91 |
QRegExp * IRCmsg_number_param; |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
92 |
QRegExp * IRCmsg_who_cmd_target; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
93 |
QRegExp * IRCmsg_who_cmd_target_text; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
94 |
QRegExp * IRCmsg_who_cmd_text; |
29 | 95 |
QString mynick; |
96 |
QString opnick; |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
97 |
QString channel; |
29 | 98 |
bool isOp; |
99 |
quint32 opCount; |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
100 |
netTeam teams[5]; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
101 |
quint8 teamsCount; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
102 |
int gameResolution; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
103 |
bool gameFullscreen; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
104 |
RNDStr seedgen; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
105 |
int playerscnt; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
106 |
int configasks; |
36 | 107 |
QByteArray NetBuffer; |
108 |
QTimer * TimerFlusher; |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
109 |
|
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
110 |
void RawSendNet(const QString & buf); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
111 |
void RawSendNet(const QByteArray & buf); |
28 | 112 |
|
113 |
void ParseLine(const QString & msg); |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
114 |
void msgcmd_textHandler(const QString & msg); |
28 | 115 |
void msgnumber_paramHandler(const QString & msg); |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
116 |
void msgwho_cmd_targetHandler(const QString & msg); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
117 |
void msgwho_cmd_textHandler(const QString & msg); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
118 |
void msgwho_cmd_target_textHandler(const QString & msg); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
119 |
|
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
120 |
void hwp_opmsg(const QString & who, const QString & msg); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
121 |
void hwp_chanmsg(const QString & who, const QString & msg); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
122 |
void ConfigAsked(); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
123 |
void NetTeamAdded(const QString & msg); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
124 |
|
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
125 |
void RunGame(const QString & seed); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
126 |
|
28 | 127 |
|
128 |
private slots: |
|
129 |
void ClientRead(); |
|
130 |
void OnConnect(); |
|
131 |
void OnDisconnect(); |
|
132 |
void Perform(); |
|
133 |
void displayError(QAbstractSocket::SocketError socketError); |
|
36 | 134 |
void FlushNetBuf(); |
28 | 135 |
}; |
136 |
||
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
137 |
#define SENDCFGSTRNET(a) {\ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
138 |
QByteArray strmsg; \ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
139 |
strmsg.append(a); \ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
140 |
quint8 sz = strmsg.size(); \ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
141 |
QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg; \ |
36 | 142 |
QString _msg = MAGIC_CHAR MAGIC_CHAR + QString(enginemsg.toBase64()); \ |
143 |
hwp_chanmsg(mynick, _msg); \ |
|
144 |
RawSendNet(QString("PRIVMSG %1 :").arg(channel) + _msg); \ |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
145 |
} |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
146 |
|
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
147 |
#define SENDCFGSTRLOC(a) {\ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
148 |
QByteArray strmsg; \ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
149 |
strmsg.append(a); \ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
150 |
quint8 sz = strmsg.size(); \ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
151 |
QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg; \ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
152 |
emit FromNet(enginemsg); \ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
153 |
} |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
29
diff
changeset
|
154 |
|
28 | 155 |
#endif |