00001
00007 #include "Console.h"
00008
00009 Console::Console() {
00010 history.clear();
00011 pos = 0;
00012 }
00013
00014 Console::~Console() {
00015
00016 }
00017
00018 bool Console::parse(char *str, char **cmd, char **param) {
00019 unsigned int i = 0;
00020 int len = strlen(str);
00021
00022 if (len <= 0) {
00023 return false;
00024 }
00025
00026 *cmd = &str[0];
00027
00028 if (*cmd[0] != '/')
00029 return true;
00030
00031 for (i=0; i<len; i++) {
00032 if (str[i] == ' ') {
00033 str[i] = '\0';
00034 if (i+1 < len)
00035 *param = &str[i+1];
00036 else
00037 *param = NULL;
00038 break;
00039 }
00040 }
00041
00042 return true;
00043 }
00044
00045 void Console::process() {
00046 char *str = (char *)(const char *)console;
00047 int len = strlen(str);
00048
00049 char *cmd = NULL;
00050 char *param = NULL;
00051
00052 this->parse(str, &cmd, ¶m);
00053
00054 if (cmd) {
00055
00056
00057
00058 if (!param && cmd[0] != '/') {
00059 if (gne->isRunning())
00060 gne->sendChatMsg(cmd);
00061 }
00062
00063
00064
00065
00066 else if (!strcmp(cmd,"/server")) {
00067 gne->init(true);
00068 gne->setMaxPlayers(15);
00069 gne->setPlayerInfoSelf("Death Bringer");
00070 gne->startServer();
00071 g_myPlayerID = gne->getPlayerIdSelf();
00072
00073 Player *p;
00074 p = gne->getPlayerById(g_myPlayerID);
00075 strcpy(g_players[g_myPlayerID].name, p->getName());
00076
00077 addEx(Vec3(0.0f, 0.0f, 1.0f), "Starting server");
00078 }
00079
00080
00081
00082
00083 else if (!strcmp(cmd, "/connect")) {
00084 if (!param) {
00085 addEx(Vec3(1.0f, 0.0f, 0.0f), "Missing parameter (adress)");
00086 }
00087 else {
00088 gne->init(false);
00089 gne->setMaxPlayers(15);
00090 gne->setPlayerInfoSelf("Bender");
00091 if (gne->joinServer(param) == -1) {
00092 addEx(Vec3(1.0f, 0.0f, 0.0f), "Failed to join server '%s'", param);
00093 }
00094 else {
00095 g_myPlayerID = gne->getPlayerIdSelf();
00096 Player *p;
00097 p = gne->getPlayerById(g_myPlayerID);
00098 strcpy(g_players[g_myPlayerID].name, p->getName());
00099
00100 gne->updatePlayersFromServer();
00101 addEx(Vec3(0.0f, 0.0f, 1.0f), "Joining server '%s'", param);
00102 }
00103 }
00104 }
00105
00106
00107
00108
00109 else if (!strcmp(cmd, "/fps")) {
00110 g_showFPS = !g_showFPS;
00111 addEx(Vec3(0.0f, 1.0f, 0.0f), "%s FPS", g_showFPS ? "Showing" : "Hiding");
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 else if (!strcmp(cmd, "/shadows")) {
00127 g_stencilBuffer = !g_stencilBuffer;
00128 addEx(Vec3(0.0f, 1.0f, 0.0f), "Shadows %s", g_stencilBuffer ? "activated" : "deactivated");
00129 }
00130
00131
00132
00133
00134 else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit")) {
00135 PostMessage(app->hWnd, WM_CLOSE, 0, 0);
00136 }
00137
00138
00139
00140
00141 else if (!strcmp(cmd, "/help")) {
00142 addEx(Vec3(1.0f, 0.0f, 0.0f), "No");
00143 }
00144
00145
00146
00147
00148 else if (!strcmp(cmd, "/cheat")) {
00149 addEx(Vec3(1.0f, 0.0f, 0.0f), "Cheating disabled");
00150 }
00151
00152
00153
00154
00155 else {
00156 if (!param) {
00157 addEx(Vec3(1.0f, 0.0f, 0.0f), "Unknown command: %s", cmd);
00158 }
00159 else {
00160 addEx(Vec3(1.0f, 0.0f, 0.0f), "Unknown command: %s %s", cmd, param);
00161 }
00162 }
00163 }
00164 }
00165
00166 void Console::handleKey(unsigned int key) {
00167 switch(key & 0xffff) {
00168 case VK_ESCAPE:
00169 console = "";
00170 pos = 0;
00171 g_console = false;
00172 break;
00173
00174 case 13:
00175 if (console.getLength() > 0) {
00176 process();
00177 console = "";
00178 pos = 0;
00179 g_console = false;
00180 }
00181 else {
00182 console = "";
00183 pos = 0;
00184 g_console = false;
00185 }
00186 break;
00187
00188 case VK_LEFT:
00189 if (pos > 0) pos--;
00190 break;
00191
00192 case VK_RIGHT:
00193 if (pos < console.getLength()) pos++;
00194 break;
00195
00196 case VK_BACK:
00197 if (pos > 0) {
00198 console.remove(--pos, 1);
00199 }
00200 break;
00201 }
00202 }
00203
00204 void Console::handleChar(char ch) {
00205 if (!isprint(ch))
00206 return;
00207
00208 console.insert(pos++, (const char *)&ch, 1);
00209 }
00210
00211 void Console::add(const char *str, Vec3 clr) {
00212 String s(str);
00213 ConsoleEntry ce;
00214 ce.str = s;
00215 ce.clr = clr;
00216 ce.time = g_time;
00217 history.addLast(ce);
00218
00219
00220 while (history.getCount() > 50) {
00221 history.removeNode(history.getFirst());
00222 }
00223 }
00224
00225 void Console::addEx(Vec3 clr, const char *str, ...) {
00226 ConsoleEntry ce;
00227 ce.clr = clr;
00228
00229 char text[256];
00230
00231 va_list va;
00232
00233 va_start(va, str);
00234 vsprintf(text, str, va);
00235 va_end(va);
00236
00237 String s(text);
00238 ce.str = s;
00239 ce.time = g_time;
00240 history.addLast(ce);
00241
00242
00243 while (history.getCount() > 50) {
00244 history.removeNode(history.getFirst());
00245 }
00246 }