author | unc0rr |
Wed, 16 Sep 2009 14:44:40 +0000 | |
changeset 2382 | bfd378dfc4e7 |
parent 2376 | ece7b87f1334 |
child 2390 | 57fb33ab04a4 |
permissions | -rw-r--r-- |
942 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
942 | 3 |
* Copyright (c) 2008 Andrey Korotaev <unC0Rr@gmail.com> |
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
19 |
unit uChat; |
|
20 |
||
21 |
interface |
|
22 |
||
23 |
procedure AddChatString(s: shortstring); |
|
24 |
procedure DrawChat; |
|
946 | 25 |
procedure KeyPressChat(Key: Longword); |
942 | 26 |
|
949 | 27 |
var UserNick: shortstring = ''; |
993 | 28 |
showAll: boolean = false; |
949 | 29 |
|
942 | 30 |
implementation |
1035 | 31 |
uses uMisc, uStore, uConsts, SDLh, uConsole, uKeys, uTeams; |
942 | 32 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
33 |
const MaxStrIndex = 27; |
942 | 34 |
|
946 | 35 |
type TChatLine = record |
1431 | 36 |
Tex: PTexture; |
942 | 37 |
Time: Longword; |
1431 | 38 |
Width: LongInt; |
39 |
s: shortstring; |
|
942 | 40 |
end; |
41 |
||
946 | 42 |
var Strs: array[0 .. MaxStrIndex] of TChatLine; |
942 | 43 |
lastStr: Longword = 0; |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
44 |
visibleCount: Longword = 0; |
2376 | 45 |
|
946 | 46 |
InputStr: TChatLine; |
47 |
InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char |
|
942 | 48 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
49 |
procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean); |
1118 | 50 |
var strSurface, resSurface: PSDL_Surface; |
1431 | 51 |
w, h: LongInt; |
942 | 52 |
begin |
1118 | 53 |
if cl.Tex <> nil then |
54 |
FreeTexture(cl.Tex); |
|
942 | 55 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
56 |
cl.s:= str; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
57 |
|
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
58 |
if isInput then str:= UserNick + '> ' + str + '_'; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
59 |
|
1118 | 60 |
TTF_SizeUTF8(Fontz[fnt16].Handle, Str2PChar(str), w, h); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
61 |
|
1118 | 62 |
resSurface:= SDL_CreateRGBSurface(0, |
1431 | 63 |
toPowerOf2(w), |
64 |
toPowerOf2(h), |
|
1118 | 65 |
32, |
66 |
RMask, GMask, BMask, AMask); |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
67 |
|
1118 | 68 |
strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $FFFFFF); |
1431 | 69 |
cl.Width:= w + 4; |
1118 | 70 |
SDL_UpperBlit(strSurface, nil, resSurface, nil); |
71 |
SDL_FreeSurface(strSurface); |
|
72 |
||
73 |
cl.Time:= RealTicks + 12500; |
|
2290
bf87ca44782e
Selectively enable clamping - seeing if this helps avoid weird flake problems while still fixing vertical lines in waves and sky
nemo
parents:
2161
diff
changeset
|
74 |
cl.Tex:= Surface2Tex(resSurface, false); |
1431 | 75 |
|
1118 | 76 |
SDL_FreeSurface(resSurface) |
946 | 77 |
end; |
78 |
||
79 |
procedure AddChatString(s: shortstring); |
|
80 |
begin |
|
81 |
lastStr:= (lastStr + 1) mod (MaxStrIndex + 1); |
|
82 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
83 |
SetLine(Strs[lastStr], s, false); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
84 |
|
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
85 |
inc(visibleCount) |
942 | 86 |
end; |
87 |
||
88 |
procedure DrawChat; |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
89 |
var i, t, cnt: Longword; |
1431 | 90 |
r: TSDL_Rect; |
942 | 91 |
begin |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
92 |
cnt:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
93 |
t:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
94 |
i:= lastStr; |
1431 | 95 |
|
2161
0c8634241fa4
Some work on zooming. Hedgewars are now unplayable.
unc0rr
parents:
2131
diff
changeset
|
96 |
r.x:= 6 - cScreenWidth div 2; |
1431 | 97 |
r.y:= (visibleCount - t) * 16 + 10; |
98 |
r.h:= 16; |
|
99 |
||
100 |
if (GameState = gsChat) |
|
101 |
and (InputStr.Tex <> nil) then |
|
102 |
begin |
|
103 |
r.w:= InputStr.Width; |
|
104 |
DrawFillRect(r); |
|
2161
0c8634241fa4
Some work on zooming. Hedgewars are now unplayable.
unc0rr
parents:
2131
diff
changeset
|
105 |
DrawTexture(8 - cScreenWidth div 2, visibleCount * 16 + 10, InputStr.Tex); |
1431 | 106 |
end; |
107 |
||
108 |
dec(r.y, 16); |
|
109 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
110 |
while |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
111 |
( |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
112 |
((t < 7) and (Strs[i].Time > RealTicks)) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
113 |
or |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
114 |
((t < MaxStrIndex) and showAll) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
115 |
) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
116 |
and |
1118 | 117 |
(Strs[i].Tex <> nil) do |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
118 |
begin |
1431 | 119 |
r.w:= Strs[i].Width; |
120 |
DrawFillRect(r); |
|
2161
0c8634241fa4
Some work on zooming. Hedgewars are now unplayable.
unc0rr
parents:
2131
diff
changeset
|
121 |
DrawTexture(8 - cScreenWidth div 2, (visibleCount - t) * 16 - 6, Strs[i].Tex); |
1431 | 122 |
dec(r.y, 16); |
2376 | 123 |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
124 |
if i = 0 then i:= MaxStrIndex else dec(i); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
125 |
inc(cnt); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
126 |
inc(t) |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
127 |
end; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
128 |
|
947 | 129 |
visibleCount:= cnt; |
942 | 130 |
end; |
131 |
||
1033 | 132 |
procedure AcceptChatString(s: shortstring); |
1035 | 133 |
var i: TWave; |
2124 | 134 |
|
1033 | 135 |
begin |
2017 | 136 |
// "Make hedgehog say something" |
2376 | 137 |
if (s[1] = '"') and (s[Length(s)] = '"') then |
2017 | 138 |
begin |
2111 | 139 |
if CurrentTeam^.ExtDriven then |
140 |
ParseCommand('/say ' + copy(s, 2, Length(s)-2), true) |
|
141 |
else |
|
142 |
ParseCommand('/hogsay '#1 + copy(s, 2, Length(s)-2), true); |
|
2017 | 143 |
exit |
144 |
end; |
|
145 |
// 'Make hedgehog think something' |
|
2376 | 146 |
if (s[1] = '''') and (s[Length(s)] = '''') then |
2017 | 147 |
begin |
2111 | 148 |
if CurrentTeam^.ExtDriven then |
149 |
ParseCommand('/say ' + copy(s, 2, Length(s)-2), true) |
|
150 |
else |
|
151 |
ParseCommand('/hogsay '#2 + copy(s, 2, Length(s)-2), true); |
|
2017 | 152 |
exit |
153 |
end; |
|
154 |
// -Make hedgehog yell something- |
|
2376 | 155 |
if (s[1] = '-') and (s[Length(s)] = '-') then |
2017 | 156 |
begin |
2111 | 157 |
if CurrentTeam^.ExtDriven then |
158 |
ParseCommand('/say ' + copy(s, 2, Length(s)-2), true) |
|
159 |
else |
|
160 |
ParseCommand('/hogsay '#3 + copy(s, 2, Length(s)-2), true); |
|
2017 | 161 |
exit |
162 |
end; |
|
163 |
// These 3 are same as above, only are to make the hedgehog say it on next attack |
|
164 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hsa ') then |
|
165 |
begin |
|
2111 | 166 |
if CurrentTeam^.ExtDriven then |
2112 | 167 |
ParseCommand('/say ' + copy(s, 6, Length(s)-5), true) |
2111 | 168 |
else |
2112 | 169 |
ParseCommand('/hogsay '#4 + copy(s, 6, Length(s)-5), true); |
2017 | 170 |
exit |
171 |
end; |
|
172 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hta ') then |
|
173 |
begin |
|
2111 | 174 |
if CurrentTeam^.ExtDriven then |
2112 | 175 |
ParseCommand('/say ' + copy(s, 6, Length(s)-5), true) |
2111 | 176 |
else |
2112 | 177 |
ParseCommand('/hogsay '#5 + copy(s, 6, Length(s)-5), true); |
2017 | 178 |
exit |
179 |
end; |
|
180 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hya ') then |
|
181 |
begin |
|
2111 | 182 |
if CurrentTeam^.ExtDriven then |
2112 | 183 |
ParseCommand('/say ' + copy(s, 6, Length(s)-5), true) |
2111 | 184 |
else |
2112 | 185 |
ParseCommand('/hogsay '#6 + copy(s, 6, Length(s)-5), true); |
2017 | 186 |
exit |
187 |
end; |
|
2111 | 188 |
|
2130
708758635955
Please don't take away my checkin privileges - Tiy asked me to keep working on this
nemo
parents:
2125
diff
changeset
|
189 |
if copy(s, 1, 6) = '/team ' then |
2124 | 190 |
begin |
2131 | 191 |
ParseCommand('/team ' + char(LocalClan) + UserNick + '(team): '+copy(s, 7, Length(s)-6), true); |
2124 | 192 |
exit |
193 |
end; |
|
1378 | 194 |
if (s[1] = '/') and (copy(s, 1, 4) <> '/me ') then |
1035 | 195 |
begin |
196 |
if CurrentTeam^.ExtDriven then exit; |
|
2376 | 197 |
|
1035 | 198 |
for i:= Low(TWave) to High(TWave) do |
199 |
if (s = Wavez[i].cmd) then |
|
200 |
begin |
|
201 |
ParseCommand('/taunt ' + char(i), true); |
|
202 |
exit |
|
203 |
end; |
|
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
204 |
if (s = '/newgrave') then |
2017 | 205 |
begin |
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
206 |
ParseCommand('/newgrave', true); |
2017 | 207 |
exit |
208 |
end; |
|
209 |
end |
|
1035 | 210 |
else |
211 |
ParseCommand('/say ' + s, true); |
|
1033 | 212 |
end; |
213 |
||
946 | 214 |
procedure KeyPressChat(Key: Longword); |
215 |
const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0); |
|
216 |
var i, btw: integer; |
|
1001 | 217 |
utf8: shortstring; |
946 | 218 |
begin |
1819 | 219 |
|
946 | 220 |
if Key <> 0 then |
221 |
case Key of |
|
1819 | 222 |
{Backspace} |
223 |
8, 127: if Length(InputStr.s) > 0 then |
|
946 | 224 |
begin |
225 |
InputStr.s[0]:= InputStrL[byte(InputStr.s[0])]; |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
226 |
SetLine(InputStr, InputStr.s, true) |
946 | 227 |
end; |
1819 | 228 |
{Esc} |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
229 |
27: SetLine(InputStr, '', true); |
1819 | 230 |
{Return} |
2003 | 231 |
3, 13, 271: begin |
947 | 232 |
if Length(InputStr.s) > 0 then |
233 |
begin |
|
1033 | 234 |
AcceptChatString(InputStr.s); |
1118 | 235 |
SetLine(InputStr, '', false) |
947 | 236 |
end; |
948 | 237 |
FreezeEnterKey; |
946 | 238 |
GameState:= gsGame |
1819 | 239 |
end; |
240 |
{missing Mac keys} |
|
241 |
{* |
|
242 |
63272 is canc |
|
243 |
63232 is UP |
|
244 |
63234 is LEFT |
|
245 |
63235 is RIGHT |
|
246 |
63233 is DOWN |
|
247 |
*} |
|
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
248 |
63272, 63232, 36233, 36234, 36235: Key:= 28; |
946 | 249 |
else |
250 |
if (Key < $80) then btw:= 1 |
|
251 |
else if (Key < $800) then btw:= 2 |
|
252 |
else if (Key < $10000) then btw:= 3 |
|
253 |
else btw:= 4; |
|
2376 | 254 |
|
946 | 255 |
utf8:= ''; |
256 |
||
257 |
for i:= btw downto 2 do |
|
258 |
begin |
|
259 |
utf8:= char((Key or $80) and $BF) + utf8; |
|
260 |
Key:= Key shr 6 |
|
261 |
end; |
|
2376 | 262 |
|
946 | 263 |
utf8:= char(Key or firstByteMark[btw]) + utf8; |
264 |
||
1485
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
265 |
if byte(InputStr.s[0]) + btw > 240 then exit; |
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
266 |
|
946 | 267 |
InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0]; |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
268 |
SetLine(InputStr, InputStr.s + utf8, true) |
946 | 269 |
end |
270 |
end; |
|
271 |
||
272 |
||
942 | 273 |
end. |