]> jfr.im git - irc/gameservirc.git/blob - gameserv/messages.cpp
Fixed a bunch of compilation warnings about deprecated const char *
[irc/gameservirc.git] / gameserv / messages.cpp
1 /* This file contains only functions related to sending messages over the
2 ircd socket */
3 #include "options.h"
4 #include "aClient.h"
5 #include "player.h"
6 #include "item.h"
7 #include "pouch.h"
8 #include "extern.h"
9 #include "sockhelp.h"
10 #include "flags.h"
11
12 #include <fstream>
13
14 using namespace std;
15
16 void raw(const char *fmt, ...)
17 {
18 va_list args;
19 char *input, *buffer;
20 const char *t = fmt;
21 input = new char[2048];
22 buffer = new char[1024];
23 va_start(args, fmt);
24 vsnprintf(buffer, 1024, fmt, args);
25 va_end(args);
26
27 memset(input, 0, sizeof(input)); // Initialize to NULL
28 for (t = buffer; *t; t++)
29 {
30 if (*t == '<')
31 {
32 switch(*++t) {
33 case 'S': sprintf(input, "%s%s", input, s_GameServ); break;
34 }
35 }
36 else
37 {
38 sprintf(input, "%s%c", input, *t);
39 }
40
41 }
42 #ifdef DEBUGMODE
43 log("Input: %s", input);
44 #endif
45
46 sprintf(input, "%s%s", input, "\r\n");
47 sock_puts(sock, input);
48 delete [] input;
49 delete [] buffer;
50 }
51 /* Send a NOTICE from the given source to the given nick. */
52
53 void notice(const char *source, const char *dest, const char *fmt, ...)
54 {
55 if (fmt[0] == '\0')
56 return;
57
58 char *commanduse;
59 commanduse = new char[16];
60
61 #ifdef P10
62 if (isUsePrivmsg())
63 strcpy(commanduse, "P");
64 else
65 strcpy(commanduse, "O");
66 #else
67
68 if (isUsePrivmsg())
69 strcpy(commanduse, "PRIVMSG");
70 else
71 strcpy(commanduse, "NOTICE");
72 #endif
73
74 va_list args;
75 char *input, *buffer;
76 const char *t = fmt;
77 input = new char[2048];
78 buffer = new char[1024];
79 va_start(args, fmt);
80 vsnprintf(buffer, 1024, fmt, args);
81 va_end(args);
82
83 if (dest[0] == ':')
84 {
85 dest++;
86
87 #if !defined(P10)
88 sprintf(input, ":%s %s %s :", source, commanduse, dest);
89 #else
90 sprintf(input, "%s %s %s :", gsnum, commanduse, dest);
91 #endif
92
93 dest--;
94 }
95 else
96 {
97 #if !defined(P10)
98 sprintf(input, ":%s %s %s :", source, commanduse, dest);
99 #else
100 sprintf(input, "%s %s %s :", gsnum, commanduse, dest);
101 #endif
102 }
103 log("buffer: %s", buffer);
104 for (t = buffer; *t; t++)
105 {
106 if (*t == '<')
107 {
108 if(*(++t) == 'S')
109 {
110 sprintf(input, "%s%s", input, s_GameServ);
111 }
112 }
113 else
114 {
115 sprintf(input, "%s%c", input, *t);
116 }
117
118 }
119 #ifdef DEBUGMODE
120 log("Input: %s", input);
121 #endif
122 sprintf(input, "%s%s", input, "\r\n");
123 sock_puts(sock, input);
124 delete [] commanduse;
125 delete [] input;
126 delete [] buffer;
127 }
128
129 void showstats(const char *u, const char *nick)
130 {
131 aClient *sender = find(u);
132 Player *p;
133 char *buf;
134 buf = new char[50];
135 string space;
136
137
138 if (!(p = findplayer(nick)))
139 {
140 notice(s_GameServ, u, "%s not found", nick);
141 }
142 else
143 {
144 notice(s_GameServ, sender->getNick(), "Stats for %s:", p->getName().c_str());
145
146 sprintf(buf, "Experience: %ld", p->getExp());
147 space = spaces(strlen(buf), " ");
148 notice(s_GameServ, sender->getNick(), "%s%sLevel: %d", buf, space.c_str(),
149 p->getLevel());
150
151
152 sprintf(buf, "Gold: %ld", p->getGold());
153 space = spaces(strlen(buf), " ");
154 notice(s_GameServ, sender->getNick(), "%s%sGold in Bank: %ld", buf, space.c_str(), p->getBank());
155
156
157 notice(s_GameServ, sender->getNick(), "Hit Points: %d of %d", p->getHP(),
158 p->getMaxHP());
159
160 sprintf(buf, "Strength: %d", p->getStrength());
161 space = spaces(strlen(buf), " ");
162 notice(s_GameServ, sender->getNick(), "%s%sDefense: %d",
163 buf, space.c_str(), p->getDefense());
164
165
166 sprintf(buf, "Armor: %s", (p->getArmor() ? p->getArmor()->getName().c_str() : "Nothing"));
167 space = spaces(strlen(buf), " ");
168
169 notice(s_GameServ, sender->getNick(), "%s%sWeapon: %s", buf, space.c_str(),
170 (p->getWeapon() ? p->getWeapon()->getName().c_str() : "Fists"));
171
172
173
174 sprintf(buf, "Forest Fights: %d", p->getForestFights());
175 space = spaces(strlen(buf), " ");
176 notice(s_GameServ, sender->getNick(), "%s%sPlayer Fights: %d", buf, space.c_str(), p->getPlayerFights());
177
178 if (p->getClient() == sender || isAdmin(sender))
179 {
180 showinventory(p, sender);
181 }
182 }
183
184 delete [] buf;
185 }
186
187 void display_monster(char *u)
188 {
189 if (is_playing(u))
190 {
191 aClient *user = find(u);
192 Player *ni = user->stats;
193
194 notice(s_GameServ, u, "Your Hitpoints: \ 2%d\ 2", ni->getHP());
195 notice(s_GameServ, u, "%s's Hitpoints: \ 2%d\ 2", ni->getMonster()->name.c_str(), ni->getMonster()->hp);
196 notice(s_GameServ, u, "Here are your commands:");
197 notice(s_GameServ, u, "/msg <S attack");
198 notice(s_GameServ, u, "/msg <S run");
199 notice(s_GameServ, u, "What will you do?");
200 }
201 }
202
203 void display_players(char *u)
204 {
205 aClient *user;
206 if (!(user = find(u)))
207 {
208 log("Fatal error in display_players(): Couldn't find %s", u);
209 }
210 else
211 display_players(user);
212 }
213
214 void display_players(aClient *user)
215 {
216 char *u = user->getNick();
217 if (is_playing(user) && player_fight(user))
218 {
219 aClient *battle = user->stats->getBattle();
220 notice(s_GameServ, u, "Your Hitpoints: \ 2%d\ 2", user->stats->getHP());
221 notice(s_GameServ, u, "%s's Hitpoints: \ 2%d\ 2", battle->stats->getName().c_str(), battle->stats->getHP());
222 notice(s_GameServ, u, "Here are your commands:");
223 notice(s_GameServ, u, "/msg <S attack");
224 notice(s_GameServ, u, "/msg <S run");
225 notice(s_GameServ, u, "What will you do?");
226 }
227 }
228
229 void showinventory(Player *from, aClient *to)
230 {
231 char *nick;
232 if (!to || !from)
233 {
234 return;
235 }
236
237 nick = to->getNick();
238
239 if (from->inventory->isEmpty())
240 {
241 notice(s_GameServ, nick, "You aren't carrying anything");
242 return;
243 }
244
245 list <itemContainer> *items;
246 items = from->inventory->getItems();
247
248 list <itemContainer>::iterator item_iter, item_iter2;
249 item_iter = items->begin();
250
251 notice(s_GameServ, nick, "Inventory for %s:", from->getName().c_str());
252 long count;
253 while (item_iter != items->end())
254 {
255 // Display a list of counted items so as not to show an extra line for duplicate items
256 count = 0;
257 item_iter2 = item_iter;
258 while (item_iter != items->end() && (*item_iter) == (*item_iter2))
259 {
260 if ((*item_iter) == (*item_iter2))
261 {
262 ++count;
263 }
264 else
265 {
266 break;
267 }
268 ++item_iter;
269 }
270 notice(s_GameServ, nick, "%ld.) %s (%ld)", (*item_iter2).getItem()->getID(), (*item_iter2).getItem()->getName().c_str(), count);
271 }
272 }
273
274
275 void showTavern(aClient *user)
276 {
277 Player *p;
278 list<tavernItem>::iterator item_iterator;
279 item *tempItem;
280
281 p = user->stats;
282
283 if (!p)
284 return;
285
286 item_iterator = tavern.begin();
287 if (tavern.empty())
288 {
289 notice(s_GameServ, user->getNick(), "Tavern is empty");
290 return;
291 }
292
293 while (item_iterator != tavern.end())
294 {
295 if (p->getLevel() < (*item_iterator).getLevel())
296 {
297 item_iterator++;
298 continue;
299 }
300 tempItem = (*item_iterator).getItem();
301 notice(s_GameServ, user->getNick(), "%d. %s for %ld gold", tempItem->getID(), tempItem->getName().c_str(), tempItem->price());
302 item_iterator++;
303 }
304 }
305
306 void showBankBalance(const char *u)
307 {
308 aClient *user;
309 Player *p;
310
311 if (!(user = find(u)))
312 return;
313
314 p = user->stats;
315
316 if (!p)
317 return;
318
319 notice(s_GameServ, u, "Account Balance: %ld Gold On hand: %ld", p->getBank(), p->getGold());
320 }
321
322 void display_help(const string &u, const string &file)
323 {
324 ifstream infile;
325 char *buf;
326
327 if (file.empty())
328 {
329 infile.open("helpfiles/help");
330 if (infile.fail())
331 {
332 log("Error opening helpfiles/help");
333 notice(s_GameServ, u.c_str(), "Error opening helpfiles/help");
334 return;
335 }
336 buf = new char[1024];
337 while(infile.getline(buf, 1024))
338 {
339 // Written this way, it will process <S in the helpfiles
340 // Instead of notice(s_GameServ, u, "%s", buf);
341 notice(s_GameServ, u.c_str(), buf);
342 }
343
344 // Minor recursion - as long as admin_commands exists, it won't loop
345 // continuously
346 aClient *user = find(u.c_str());
347 if (user && isAdmin(user))
348 display_help(u.c_str(), "admin_commands");
349 }
350 else
351 {
352 string filename;
353 filename = "helpfiles/";
354 filename += file;
355
356 for (unsigned int x = 10; x < filename.length(); x++)
357 filename[x] = tolower(filename[x]);
358
359 infile.open(filename.c_str());
360 if (infile.fail())
361 {
362 notice(s_GameServ, u.c_str(), "No help for \ 2%s\ 2", file.c_str());
363 return;
364 }
365 buf = new char[1024];
366 while(infile.getline(buf, 1024))
367 {
368 // Written this way, it will process <S in the helpfiles
369 // Instead of notice(s_GameServ, u, "%s", buf);
370 notice(s_GameServ, u.c_str(), buf);
371 }
372 }
373 infile.close();
374 delete [] buf;
375 }
376