author | koda |
Fri, 18 Dec 2009 02:33:45 +0000 | |
changeset 2664 | 949c189ba568 |
parent 2630 | 079ef82eac75 |
child 2677 | 83ad68ceef72 |
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 |
||
2630 | 19 |
{$INCLUDE "options.inc"} |
20 |
||
942 | 21 |
unit uChat; |
22 |
||
23 |
interface |
|
24 |
||
25 |
procedure AddChatString(s: shortstring); |
|
26 |
procedure DrawChat; |
|
946 | 27 |
procedure KeyPressChat(Key: Longword); |
942 | 28 |
|
949 | 29 |
var UserNick: shortstring = ''; |
993 | 30 |
showAll: boolean = false; |
949 | 31 |
|
942 | 32 |
implementation |
1035 | 33 |
uses uMisc, uStore, uConsts, SDLh, uConsole, uKeys, uTeams; |
942 | 34 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
35 |
const MaxStrIndex = 27; |
942 | 36 |
|
946 | 37 |
type TChatLine = record |
1431 | 38 |
Tex: PTexture; |
942 | 39 |
Time: Longword; |
1431 | 40 |
Width: LongInt; |
41 |
s: shortstring; |
|
942 | 42 |
end; |
43 |
||
946 | 44 |
var Strs: array[0 .. MaxStrIndex] of TChatLine; |
942 | 45 |
lastStr: Longword = 0; |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
46 |
visibleCount: Longword = 0; |
2376 | 47 |
|
946 | 48 |
InputStr: TChatLine; |
49 |
InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char |
|
942 | 50 |
|
2664
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
51 |
const colors: array[#1..#4] of TSDL_Color = ( |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
52 |
(r:$FF; g:$FF; b:$FF; unused:$FF), // chat message [White] |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
53 |
(r:$FF; g:$00; b:$FF; unused:$FF), // action message [Purple] |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
54 |
(r:$90; g:$FF; b:$90; unused:$FF), // join/leave message [Lime] |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
55 |
(r:$FF; g:$FF; b:$A0; unused:$FF) // team message [Light Yellow] |
2396 | 56 |
); |
57 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
58 |
procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean); |
1118 | 59 |
var strSurface, resSurface: PSDL_Surface; |
1431 | 60 |
w, h: LongInt; |
2664
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
61 |
color: TSDL_Color; |
942 | 62 |
begin |
1118 | 63 |
if cl.Tex <> nil then |
64 |
FreeTexture(cl.Tex); |
|
942 | 65 |
|
2396 | 66 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
67 |
cl.s:= str; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
68 |
|
2396 | 69 |
if isInput then |
2664
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
70 |
begin |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
71 |
// [Light Blue] |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
72 |
color.r:= $00; |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
73 |
color.g:= $FF; |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
74 |
color.b:= $FF; |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
75 |
color.unused:= $FF; |
2396 | 76 |
str:= UserNick + '> ' + str + '_' |
2664
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
77 |
end |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
78 |
else |
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
79 |
begin |
2396 | 80 |
color:= colors[str[1]]; |
81 |
delete(str, 1, 1) |
|
2664
949c189ba568
powerpc and gameserver compilation disabled temporarily
koda
parents:
2630
diff
changeset
|
82 |
end; |
2396 | 83 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
84 |
|
1118 | 85 |
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
|
86 |
|
2622 | 87 |
resSurface:= SDL_CreateRGBSurface(0, toPowerOf2(w), toPowerOf2(h), 32, RMask, GMask, BMask, AMask); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
88 |
|
2396 | 89 |
strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), color); |
1431 | 90 |
cl.Width:= w + 4; |
1118 | 91 |
SDL_UpperBlit(strSurface, nil, resSurface, nil); |
92 |
SDL_FreeSurface(strSurface); |
|
93 |
||
94 |
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
|
95 |
cl.Tex:= Surface2Tex(resSurface, false); |
1431 | 96 |
|
1118 | 97 |
SDL_FreeSurface(resSurface) |
946 | 98 |
end; |
99 |
||
100 |
procedure AddChatString(s: shortstring); |
|
101 |
begin |
|
102 |
lastStr:= (lastStr + 1) mod (MaxStrIndex + 1); |
|
103 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
104 |
SetLine(Strs[lastStr], s, false); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
105 |
|
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
106 |
inc(visibleCount) |
942 | 107 |
end; |
108 |
||
109 |
procedure DrawChat; |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
110 |
var i, t, cnt: Longword; |
1431 | 111 |
r: TSDL_Rect; |
942 | 112 |
begin |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
113 |
cnt:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
114 |
t:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
115 |
i:= lastStr; |
1431 | 116 |
|
2161
0c8634241fa4
Some work on zooming. Hedgewars are now unplayable.
unc0rr
parents:
2131
diff
changeset
|
117 |
r.x:= 6 - cScreenWidth div 2; |
1431 | 118 |
r.y:= (visibleCount - t) * 16 + 10; |
119 |
r.h:= 16; |
|
120 |
||
121 |
if (GameState = gsChat) |
|
122 |
and (InputStr.Tex <> nil) then |
|
123 |
begin |
|
124 |
r.w:= InputStr.Width; |
|
125 |
DrawFillRect(r); |
|
2161
0c8634241fa4
Some work on zooming. Hedgewars are now unplayable.
unc0rr
parents:
2131
diff
changeset
|
126 |
DrawTexture(8 - cScreenWidth div 2, visibleCount * 16 + 10, InputStr.Tex); |
1431 | 127 |
end; |
128 |
||
129 |
dec(r.y, 16); |
|
130 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
131 |
while |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
132 |
( |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
133 |
((t < 7) and (Strs[i].Time > RealTicks)) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
134 |
or |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
135 |
((t < MaxStrIndex) and showAll) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
136 |
) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
137 |
and |
1118 | 138 |
(Strs[i].Tex <> nil) do |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
139 |
begin |
1431 | 140 |
r.w:= Strs[i].Width; |
141 |
DrawFillRect(r); |
|
2161
0c8634241fa4
Some work on zooming. Hedgewars are now unplayable.
unc0rr
parents:
2131
diff
changeset
|
142 |
DrawTexture(8 - cScreenWidth div 2, (visibleCount - t) * 16 - 6, Strs[i].Tex); |
1431 | 143 |
dec(r.y, 16); |
2376 | 144 |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
145 |
if i = 0 then i:= MaxStrIndex else dec(i); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
146 |
inc(cnt); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
147 |
inc(t) |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
148 |
end; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
149 |
|
947 | 150 |
visibleCount:= cnt; |
942 | 151 |
end; |
152 |
||
1033 | 153 |
procedure AcceptChatString(s: shortstring); |
1035 | 154 |
var i: TWave; |
2124 | 155 |
|
1033 | 156 |
begin |
2017 | 157 |
// "Make hedgehog say something" |
2376 | 158 |
if (s[1] = '"') and (s[Length(s)] = '"') then |
2017 | 159 |
begin |
2111 | 160 |
if CurrentTeam^.ExtDriven then |
161 |
ParseCommand('/say ' + copy(s, 2, Length(s)-2), true) |
|
162 |
else |
|
163 |
ParseCommand('/hogsay '#1 + copy(s, 2, Length(s)-2), true); |
|
2017 | 164 |
exit |
165 |
end; |
|
166 |
// 'Make hedgehog think something' |
|
2376 | 167 |
if (s[1] = '''') and (s[Length(s)] = '''') then |
2017 | 168 |
begin |
2111 | 169 |
if CurrentTeam^.ExtDriven then |
170 |
ParseCommand('/say ' + copy(s, 2, Length(s)-2), true) |
|
171 |
else |
|
172 |
ParseCommand('/hogsay '#2 + copy(s, 2, Length(s)-2), true); |
|
2017 | 173 |
exit |
174 |
end; |
|
175 |
// -Make hedgehog yell something- |
|
2376 | 176 |
if (s[1] = '-') and (s[Length(s)] = '-') then |
2017 | 177 |
begin |
2111 | 178 |
if CurrentTeam^.ExtDriven then |
179 |
ParseCommand('/say ' + copy(s, 2, Length(s)-2), true) |
|
180 |
else |
|
181 |
ParseCommand('/hogsay '#3 + copy(s, 2, Length(s)-2), true); |
|
2017 | 182 |
exit |
183 |
end; |
|
184 |
// These 3 are same as above, only are to make the hedgehog say it on next attack |
|
185 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hsa ') then |
|
186 |
begin |
|
2111 | 187 |
if CurrentTeam^.ExtDriven then |
2112 | 188 |
ParseCommand('/say ' + copy(s, 6, Length(s)-5), true) |
2111 | 189 |
else |
2112 | 190 |
ParseCommand('/hogsay '#4 + copy(s, 6, Length(s)-5), true); |
2017 | 191 |
exit |
192 |
end; |
|
193 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hta ') then |
|
194 |
begin |
|
2111 | 195 |
if CurrentTeam^.ExtDriven then |
2112 | 196 |
ParseCommand('/say ' + copy(s, 6, Length(s)-5), true) |
2111 | 197 |
else |
2112 | 198 |
ParseCommand('/hogsay '#5 + copy(s, 6, Length(s)-5), true); |
2017 | 199 |
exit |
200 |
end; |
|
201 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hya ') then |
|
202 |
begin |
|
2111 | 203 |
if CurrentTeam^.ExtDriven then |
2112 | 204 |
ParseCommand('/say ' + copy(s, 6, Length(s)-5), true) |
2111 | 205 |
else |
2112 | 206 |
ParseCommand('/hogsay '#6 + copy(s, 6, Length(s)-5), true); |
2017 | 207 |
exit |
208 |
end; |
|
2111 | 209 |
|
2518 | 210 |
if (copy(s, 1, 6) = '/team ') and (length(s) > 6) then |
2124 | 211 |
begin |
2403 | 212 |
ParseCommand(s, true); |
2124 | 213 |
exit |
214 |
end; |
|
1378 | 215 |
if (s[1] = '/') and (copy(s, 1, 4) <> '/me ') then |
1035 | 216 |
begin |
217 |
if CurrentTeam^.ExtDriven then exit; |
|
2376 | 218 |
|
1035 | 219 |
for i:= Low(TWave) to High(TWave) do |
220 |
if (s = Wavez[i].cmd) then |
|
221 |
begin |
|
222 |
ParseCommand('/taunt ' + char(i), true); |
|
223 |
exit |
|
224 |
end; |
|
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
225 |
if (s = '/newgrave') then |
2017 | 226 |
begin |
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
227 |
ParseCommand('/newgrave', true); |
2017 | 228 |
exit |
229 |
end; |
|
230 |
end |
|
1035 | 231 |
else |
232 |
ParseCommand('/say ' + s, true); |
|
1033 | 233 |
end; |
234 |
||
946 | 235 |
procedure KeyPressChat(Key: Longword); |
236 |
const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0); |
|
237 |
var i, btw: integer; |
|
1001 | 238 |
utf8: shortstring; |
946 | 239 |
begin |
1819 | 240 |
|
946 | 241 |
if Key <> 0 then |
242 |
case Key of |
|
1819 | 243 |
{Backspace} |
244 |
8, 127: if Length(InputStr.s) > 0 then |
|
946 | 245 |
begin |
246 |
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
|
247 |
SetLine(InputStr, InputStr.s, true) |
946 | 248 |
end; |
1819 | 249 |
{Esc} |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
250 |
27: SetLine(InputStr, '', true); |
1819 | 251 |
{Return} |
2003 | 252 |
3, 13, 271: begin |
947 | 253 |
if Length(InputStr.s) > 0 then |
254 |
begin |
|
1033 | 255 |
AcceptChatString(InputStr.s); |
1118 | 256 |
SetLine(InputStr, '', false) |
947 | 257 |
end; |
948 | 258 |
FreezeEnterKey; |
946 | 259 |
GameState:= gsGame |
1819 | 260 |
end; |
946 | 261 |
else |
262 |
if (Key < $80) then btw:= 1 |
|
263 |
else if (Key < $800) then btw:= 2 |
|
264 |
else if (Key < $10000) then btw:= 3 |
|
265 |
else btw:= 4; |
|
2376 | 266 |
|
946 | 267 |
utf8:= ''; |
268 |
||
269 |
for i:= btw downto 2 do |
|
270 |
begin |
|
271 |
utf8:= char((Key or $80) and $BF) + utf8; |
|
272 |
Key:= Key shr 6 |
|
273 |
end; |
|
2376 | 274 |
|
946 | 275 |
utf8:= char(Key or firstByteMark[btw]) + utf8; |
276 |
||
1485
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
277 |
if byte(InputStr.s[0]) + btw > 240 then exit; |
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
278 |
|
946 | 279 |
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
|
280 |
SetLine(InputStr, InputStr.s + utf8, true) |
946 | 281 |
end |
282 |
end; |
|
283 |
||
284 |
||
942 | 285 |
end. |