diff --git a/Makefile b/Makefile index 847104748b..9085cae741 100644 --- a/Makefile +++ b/Makefile @@ -424,7 +424,7 @@ endif ttd_SOURCES = \ ai.c ai_build.c ai_new.c ai_pathfinder.c ai_shared.c aircraft_cmd.c \ aircraft_gui.c airport.c airport_gui.c aystar.c bridge_gui.c \ - clear_cmd.c command.c disaster_cmd.c dock_gui.c dummy_land.c economy.c \ + clear_cmd.c command.c console.c disaster_cmd.c dock_gui.c dummy_land.c economy.c \ engine.c engine_gui.c fileio.c gfx.c graph_gui.c grfspecial.c \ industry_cmd.c industry_gui.c intro_gui.c landscape.c main_gui.c \ minilzo.c misc.c misc_cmd.c misc_gui.c music_gui.c namegen.c network.c \ diff --git a/console.c b/console.c new file mode 100644 index 0000000000..765073ed8e --- /dev/null +++ b/console.c @@ -0,0 +1,835 @@ +#include "stdafx.h" +#include "ttd.h" +#include "window.h" +#include "gui.h" +#include "gfx.h" +#include "player.h" +#include "variables.h" +#include "hal.h" +#include +#include "console.h" + + +// ** main console ** // +static bool _iconsole_inited; +static byte* _iconsole_buffer[80]; +static byte _iconsole_cbuffer[80]; +static byte _iconsole_cmdline[255]; +static byte _iconsole_cmdpos; +static byte _iconsole_mode; +static byte _iconsole_color_default = 1; +static byte _iconsole_color_error = 3; +static byte _iconsole_color_debug = 5; +static byte _iconsole_color_commands = 2; +static Window *_iconsole_win = NULL; + +// ** console cursor ** // +static bool _icursor_state; +static byte _icursor_rate; +static byte _icursor_counter; + +// ** console window ** // + +static void IConsoleWndProc(Window *w, WindowEvent *e); +static const Widget _iconsole_window_widgets[] = {{WWT_LAST}}; +static const WindowDesc _iconsole_window_desc = { + 0, 0, 2, 2, + WC_CONSOLE,0, + WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, + _iconsole_window_widgets, + IConsoleWndProc, +}; + +// ** console parser ** // + +static _iconsole_cmd * _iconsole_cmds; // list of registred commands +static _iconsole_var * _iconsole_vars; // list of registred vars + +// ** console std lib ** // +static byte _stdlib_developer=0; +static void IConsoleStdLibRegister(); +static byte * _stdlib_temp_string; +static byte * _stdlib_temp_pointer; +static uint32 _stdlib_temp_uint32; +static bool _stdlib_temp_bool; + +/* *************** */ +/* end of header */ +/* *************** */ + +void IConsoleClearCommand() +{ +int i; +for (i=0; i<255; i++) _iconsole_cmdline[i]=0; +_iconsole_cmdpos=0; +SetWindowDirty(_iconsole_win); +} + +static void IConsoleWndProc(Window *w, WindowEvent *e) +{ + switch(e->event) { + + case WE_PAINT: + GfxFillRect(w->left,w->top,w->width,w->height-1,0); + { + int i=79; + int max=(w->height/12)-1; + while ((i>79-max) && (_iconsole_buffer[i]!=NULL)) { + DoDrawString(_iconsole_buffer[i],5,w->height-((81-i)*12),_iconsole_cbuffer[i]); + i--; + } + DoDrawString((char *)&_iconsole_cmdline,5,w->height-12,_iconsole_color_commands); + } + break; + + case WE_TICK: + + if (_iconsole_mode==ICONSOLE_OPENING) { + _iconsole_mode=ICONSOLE_OPENED; + } + + _icursor_counter++; + if (_icursor_counter>_icursor_rate) { + _icursor_state=!_icursor_state; + { + int posx; + int posy; + int color; + _cur_dpi=&_screen; + if (_icursor_state) color=14; else color=0; + posx=5+GetStringWidth((char *)&_iconsole_cmdline); + posy=w->height-3; + GfxFillRect(posx,posy,posx+5,posy+1,color); + _video_driver->make_dirty(posx,posy,5,1); + } + _icursor_counter=0; + } + break; + + case WE_KEYPRESS: + e->keypress.cont=false; + if (e->keypress.keycode == WKC_TAB) + { + IConsoleSwitch(); + } else + if (e->keypress.keycode == WKC_RETURN) + { + IConsolePrintF(_iconsole_color_commands, "] %s", _iconsole_cmdline); + IConsoleCmdExec((byte *) _iconsole_cmdline); + IConsoleClearCommand(); + } else + if (e->keypress.keycode == WKC_BACKSPACE) + { + if (_iconsole_cmdpos!=0) _iconsole_cmdpos--; + _iconsole_cmdline[_iconsole_cmdpos]=0; + SetWindowDirty(w); + } else + if (IS_INT_INSIDE((e->keypress.ascii), 32, 256)) + { + _iconsole_cmdline[_iconsole_cmdpos]=e->keypress.ascii; + if (_iconsole_cmdpos!=255) _iconsole_cmdpos++; + SetWindowDirty(w); + e->keypress.keycode=0; + } else e->keypress.cont=true; + break; + + } +} + +void IConsoleInit() +{ +int i; +_iconsole_inited=true; +_iconsole_mode=ICONSOLE_CLOSED; +_iconsole_win=NULL; +_icursor_state=false; +_icursor_rate=5; +_icursor_counter=0; +for (i=0;i<80;i++) _iconsole_buffer[i]=NULL; +IConsoleStdLibRegister(); +IConsolePrint(12,"OpenTTD Game Console Build #"); +IConsolePrint(12,"---------------------------------"); +IConsoleClearCommand(); +} + +void IConsoleClear() +{ +int i; +for (i=0;i<80;i++) if (_iconsole_buffer[i]!=NULL) { + free(_iconsole_buffer[i]); + } +} + +void IConsoleFree() +{ +_iconsole_inited=false; +IConsoleClear(); +} + +void IConsoleResize() { + if (_iconsole_win!=NULL) { + _iconsole_win->height = _screen.height / 3; + _iconsole_win->width= _screen.width; + } +} + +void IConsoleSwitch() +{ + if (_iconsole_mode==ICONSOLE_CLOSED) { + _iconsole_win = AllocateWindowDesc(&_iconsole_window_desc); + _iconsole_win->height = _screen.height / 3; + _iconsole_win->width= _screen.width; + _iconsole_mode=ICONSOLE_OPENING; + } else + if (_iconsole_mode==ICONSOLE_OPENED) { + DeleteWindow(_iconsole_win); + _iconsole_win=NULL; + _iconsole_mode=ICONSOLE_CLOSED; + } + MarkWholeScreenDirty(); + MarkAllViewportsDirty(0,0,_screen.width,_screen.height); + _video_driver->make_dirty(0,0,_screen.width,_screen.height); +} + +void IConsoleClose() { +if (_iconsole_mode==ICONSOLE_OPENED) IConsoleSwitch(); +} + +void IConsoleOpen() { +if (_iconsole_mode==ICONSOLE_CLOSED) IConsoleSwitch(); +} + +void IConsolePrint(byte color_code, byte* string) +{ +byte * _ex; +byte * _new; +byte _exc; +byte _newc; +int i,j; + +if (!_iconsole_inited) return; + +_newc=color_code; + +i=strlen((char *)string); +_new=malloc(i+1); +memset(_new,0,i+1); +memcpy(_new,string,i); + +for (j=0;j=0) { + _ex=_iconsole_buffer[i]; + _exc=_iconsole_cbuffer[i]; + _iconsole_buffer[i]=_new; + _iconsole_cbuffer[i]=_newc; + _new=_ex; + _newc=_exc; + i--; + } +if (_ex!=NULL) free(_ex); + +if (_iconsole_win!=NULL) SetWindowDirty(_iconsole_win); +} + + +void IConsolePrintF(byte color_code, const char *s, ...) +{ + va_list va; + char buf[1024]; + va_start(va, s); + vsprintf(buf, s, va); + va_end(va); + IConsolePrint(color_code, (byte *) &buf); +} + +void IConsoleDebug(byte* string) { +if (_stdlib_developer>1) IConsolePrintF(_iconsole_color_debug, "DEBUG: %s", string); +} + +void IConsoleError(byte* string) { +if (_stdlib_developer>0) IConsolePrintF(_iconsole_color_error, "ERROR: %s", string); +} + +void IConsoleCmdRegister(byte * name, void * addr) { +byte * _new; +_iconsole_cmd * item; +_iconsole_cmd * item_new; +int i; + + i=strlen((char *)name); + _new=malloc(i+1); + memset(_new,0,i+1); + memcpy(_new,name,i); + +item_new = malloc(sizeof(_iconsole_cmd)); + +item_new->_next = NULL; +item_new->addr = addr; +item_new->name = _new; + +item = _iconsole_cmds; +if (item == NULL) { + _iconsole_cmds = item_new; + } else { + while (item->_next != NULL) { item = item->_next; }; + item->_next = item_new; + } +} + +static void* IConsoleCmdGetAddr(byte * name) { +_iconsole_cmd * item; + +item = _iconsole_cmds; +while (item != NULL) { + if (strcmp(item->name,name)==0) return item->addr; + item = item->_next; + } +return NULL; +} + +void IConsoleVarRegister(byte * name, void * addr, byte type) { +byte * _new; +_iconsole_var * item; +_iconsole_var * item_new; +int i; + + i=strlen((char *)name)+1; + _new=malloc(i+1); + memset(_new,0,i+1); + _new[0]='*'; + memcpy(_new+1,name,i); + +item_new = malloc(sizeof(_iconsole_var)); + +item_new->_next = NULL; +item_new->addr = addr; +item_new->name = _new; +item_new->type = type; + +item = _iconsole_vars; +if (item == NULL) { + _iconsole_vars = item_new; + } else { + while (item->_next != NULL) { item = item->_next; }; + item->_next = item_new; + } +} + +_iconsole_var * IConsoleVarGet(byte * name) { +_iconsole_var * item; + +item = _iconsole_vars; +while (item != NULL) { + if (strcmp(item->name,name)==0) return item; + item = item->_next; + } +return NULL; +} + +static void IConsoleVarStringSet(_iconsole_var * var, byte * string) { +int l; + +if (strlen((byte *) var->addr)!=0) { + free(var->addr); + } +l=strlen((char *) string); +var->addr=malloc(l+1); +memset(var->addr,0,l); +memcpy((void *) var->addr,(void *) string, l); +((byte *)var->addr)[l]=0; +} + +void IConsoleCmdExec(byte * cmdstr) { +void (*function)(byte argc, byte* argv[], byte argt[]); +byte * tokens[20]; +byte tokentypes[20]; +byte * tokenstream; +byte * tokenstream_s; +byte execution_mode; +_iconsole_var * var = NULL; + +byte var_b; // TYPE BYTE +unsigned short var_ui16; // TYPE UINT16 +unsigned int var_ui32; // TYPE UINT32 +signed short var_i16; // TYPE INT16 +signed int var_i32; // TYPE INT32 +byte * var_s; // TYPE STRING + +bool longtoken; +bool valid_token; +bool skip_lt_change; + +int c; +int i; +int l; + +//** clearing buffer **// + +for (i=0;i<20;i++) { tokens[i]=NULL; tokentypes[i]=ICONSOLE_VAR_NONE; }; +tokenstream_s=tokenstream=malloc(1024); +memset(tokenstream,0,1024); + +//** parsing **// + +longtoken=false; +valid_token=false; +skip_lt_change=false; +l=strlen((char *) cmdstr); +i=0; +c=0; +tokens[c] = tokenstream; +while (i0) if (strlen((char *) tokens[i])>0) { + if (tokens[i][0]=='*') { + var = IConsoleVarGet(tokens[i]); + if (var!=NULL) { + tokens[i]=(byte *)var->addr; + tokentypes[i]=var->type; + } + } else { + tokentypes[i]=ICONSOLE_VAR_STRING; + } + } + } + +execution_mode=0; + +function = IConsoleCmdGetAddr(tokens[0]); +if (function != NULL) { + execution_mode=1; // this is a command + } else { + var = IConsoleVarGet(tokens[0]); + if (var != NULL) { + execution_mode=2; // this is a variable + } + } + +//** executing **// + +switch (execution_mode) { +case 0: + { + // not found + IConsoleError("command or variable not found"); + } + break; +case 1: + { + // execution with command syntax + function(c,tokens,tokentypes); + } + break; +case 2: + { + // execution with variable syntax +if ((c==2) || (c==3)) { + // ** variable modifications ** // + switch (var->type) { + case ICONSOLE_VAR_BOOLEAN: + { + if (strcmp(tokens[1],"=")==0) { + if (c==3) { + *(bool *)var->addr=(atoi((char *) tokens[2])==1); + c=1; + } else { + *(bool *)var->addr=false; + c=1; + } + } + else if (strcmp(tokens[1],"++")==0) { + *(bool *)var->addr=!*(bool *)var->addr; + c=1; + } + else if (strcmp(tokens[1],"--")==0) { + *(bool *)var->addr=!*(bool *)var->addr; + c=1; + } + else { IConsoleError("operation not supported"); } + } + break; + case ICONSOLE_VAR_BYTE: + { + if (strcmp(tokens[1],"=")==0) { + if (c==3) { + *(byte *)var->addr=atoi((char *) tokens[2]); + c=1; + } else { + *(byte *)var->addr=0; + c=1; + } + } + else if (strcmp(tokens[1],"++")==0) { + (*(byte *)var->addr)++; + c=1; + } + else if (strcmp(tokens[1],"--")==0) { + (*(byte *)var->addr)--; + c=1; + } + else { IConsoleError("operation not supported"); } + } + break; + case ICONSOLE_VAR_UINT16: + { + if (strcmp(tokens[1],"=")==0) { + if (c==3) { + *(unsigned short *)var->addr=atoi((char *) tokens[2]); + c=1; + } else { + *(unsigned short *)var->addr=0; + c=1; + } + } + else if (strcmp(tokens[1],"++")==0) { + (*(unsigned short *)var->addr)++; + c=1; + } + else if (strcmp(tokens[1],"--")==0) { + (*(unsigned short *)var->addr)--; + c=1; + } + else { IConsoleError("operation not supported"); } + } + break; + case ICONSOLE_VAR_UINT32: + { + if (strcmp(tokens[1],"=")==0) { + if (c==3) { + *(unsigned int *)var->addr=atoi((char *) tokens[2]); + c=1; + } else { + *(unsigned int *)var->addr=0; + c=1; + } + } + else if (strcmp(tokens[1],"++")==0) { + (*(unsigned int *)var->addr)++; + c=1; + } + else if (strcmp(tokens[1],"--")==0) { + (*(unsigned int *)var->addr)--; + c=1; + } + else { IConsoleError("operation not supported"); } + } + break; + case ICONSOLE_VAR_INT16: + { + if (strcmp(tokens[1],"=")==0) { + if (c==3) { + *(signed short *)var->addr=atoi((char *) tokens[2]); + c=1; + } else { + *(signed short *)var->addr=0; + c=1; + } + } + else if (strcmp(tokens[1],"++")==0) { + (*(signed short *)var->addr)++; + c=1; + } + else if (strcmp(tokens[1],"--")==0) { + (*(signed short *)var->addr)--; + c=1; + } + else { IConsoleError("operation not supported"); } + } + break; + case ICONSOLE_VAR_INT32: + { + if (strcmp(tokens[1],"=")==0) { + if (c==3) { + *(signed int *)var->addr=atoi((char *) tokens[2]); + c=1; + } else { + *(signed int *)var->addr=0; + c=1; + } + } + else if (strcmp(tokens[1],"++")==0) { + (*(signed int *)var->addr)++; + c=1; + } + else if (strcmp(tokens[1],"--")==0) { + (*(signed int *)var->addr)--; + c=1; + } + else { IConsoleError("operation not supported"); } + } + break; + case ICONSOLE_VAR_STRING: + { + if (strcmp(tokens[1],"=")==0) { + if (c==3) { + IConsoleVarStringSet(var, tokens[2]); + c=1; + } else { + IConsoleVarStringSet(var, ""); + c=1; + } + } + else { IConsoleError("operation not supported"); } + } + break; + case ICONSOLE_VAR_POINTER: + { + if (strcmp(tokens[1],"=")==0) { + if (c==3) { + var->addr = tokens[2]; + c=1; + } else { + var->addr = NULL; + c=1; + } + } + else if (strcmp(tokens[1],"++")==0) { + var->addr = ((char *)var->addr)+1; + c=1; + } + else if (strcmp(tokens[1],"--")==0) { + var->addr = ((char *)var->addr)-1;; + c=1; + } + else { IConsoleError("operation not supported"); } + } + break; + } + } + if (c==1) { + // ** variable output ** // + switch (var->type) { + case ICONSOLE_VAR_BOOLEAN: + { + if (*(bool *)var->addr) { + IConsolePrintF(_iconsole_color_default, "%s = true",var->name); + } else { + IConsolePrintF(_iconsole_color_default, "%s = false",var->name); + } + } + break; + case ICONSOLE_VAR_BYTE: + { + var_b=*(byte *)var->addr; + IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_b); + } + break; + case ICONSOLE_VAR_UINT16: + { + var_ui16=*(unsigned short *)var->addr; + IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_ui16); + } + break; + case ICONSOLE_VAR_UINT32: + { + var_ui32=*(unsigned int *)var->addr; + IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_ui32); + } + break; + case ICONSOLE_VAR_INT16: + { + var_i16=*(signed short *)var->addr; + IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_i16); + } + break; + case ICONSOLE_VAR_INT32: + { + var_i32=*(signed int *)var->addr; + IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_i32); + } + break; + case ICONSOLE_VAR_STRING: + { + var_s=(byte *)var->addr; + IConsolePrintF(_iconsole_color_default, "%s = %s",var->name,var_s); + } + break; + case ICONSOLE_VAR_UNKNOWN: + case ICONSOLE_VAR_VARPTR: + case ICONSOLE_VAR_POINTER: + { + var_i32=(signed int)((byte *)var->addr); + IConsolePrintF(_iconsole_color_default, "%s = @%i",var->name,var_i32); + } + break; + } + } + } + break; +default: + { + // execution mode invalid + IConsoleError("invalid execution mode"); + } +} + +//** freeing the tokens **// +for (i=0;i<20;i++) tokens[i]=NULL; +free(tokenstream_s); + +} + +/* **************************** */ +/* default console commands */ +/* **************************** */ + + +static void IConsoleStdLibEcho(byte argc, byte * argv[], byte argt[]) { + if (argc<2) return; + IConsolePrint(_iconsole_color_default, argv[1]); +} + +static void IConsoleStdLibEchoC(byte argc, byte * argv[], byte argt[]) { + if (argc<3) return; + IConsolePrint(atoi(argv[1]), argv[2]); +} + +static void IConsoleStdLibPrintF(byte argc, byte * argv[], byte argt[]) { + if (argc<3) return; + IConsolePrintF(_iconsole_color_default, argv[1] ,argv[2],argv[3],argv[4],argv[5],argv[6],argv[7],argv[8],argv[9],argv[10],argv[11],argv[12],argv[13],argv[14],argv[15],argv[16],argv[17],argv[18],argv[19]); +} + +static void IConsoleStdLibPrintFC(byte argc, byte * argv[], byte argt[]) { + if (argc<3) return; + IConsolePrintF(atoi(argv[1]), argv[2] ,argv[3],argv[4],argv[5],argv[6],argv[7],argv[8],argv[9],argv[10],argv[11],argv[12],argv[13],argv[14],argv[15],argv[16],argv[17],argv[18],argv[19]); +} + +static void IConsoleStdLibScreenShot(byte argc, byte * argv[], byte argt[]) { + + if (argc<2) { + _make_screenshot=1; + } else { + if (strcmp(argv[1],"big")==0) { + _make_screenshot=2; + } + } +} + +static void IConsoleStdLibDebugLevel(byte argc, byte * argv[], byte argt[]) { + if (argc<2) return; + SetDebugString(argv[1]); +} + +static void IConsoleStdLibExit(byte argc, byte * argv[], byte argt[]) { + _exit_game = true; +} + +static void IConsoleStdLibListCommands(byte argc, byte * argv[], byte argt[]) { +_iconsole_cmd * item; +int l = 0; + +if (argv[1]!=NULL) l = strlen((char *) argv[1]); + +item = _iconsole_cmds; +while (item != NULL) { + if (argv[1]!=NULL) { + + if (memcmp((void *) item->name, (void *) argv[1],l)==0) + IConsolePrintF(_iconsole_color_default,"%s",item->name); + + } else { + + IConsolePrintF(_iconsole_color_default,"%s",item->name); + + } + item = item->_next; + } +} + +static void IConsoleStdLibListVariables(byte argc, byte * argv[], byte argt[]) { +_iconsole_var * item; +int l = 0; + +if (argv[1]!=NULL) l = strlen((char *) argv[1]); + +item = _iconsole_vars; +while (item != NULL) { + if (argv[1]!=NULL) { + + if (memcmp((void *) item->name, (void *) argv[1],l)==0) + IConsolePrintF(_iconsole_color_default,"%s",item->name); + + } else { + + IConsolePrintF(_iconsole_color_default,"%s",item->name); + + } + item = item->_next; + } +} + +static void IConsoleStdLibRegister() { + IConsoleCmdRegister("echo",IConsoleStdLibEcho); + IConsoleCmdRegister("echoc",IConsoleStdLibEchoC); + IConsoleCmdRegister("printf",IConsoleStdLibPrintF); + IConsoleCmdRegister("printfc",IConsoleStdLibPrintFC); + IConsoleCmdRegister("list_cmds",IConsoleStdLibListCommands); + IConsoleCmdRegister("list_vars",IConsoleStdLibListVariables); + IConsoleCmdRegister("screenshot",IConsoleStdLibScreenShot); + IConsoleCmdRegister("debug_level",IConsoleStdLibDebugLevel); + IConsoleCmdRegister("exit",IConsoleStdLibExit); + IConsoleVarRegister("developer",(void *) &_stdlib_developer,ICONSOLE_VAR_BYTE); + IConsoleVarRegister("cursor_rate",(void *) &_icursor_rate,ICONSOLE_VAR_BYTE); + IConsoleVarRegister("temp_string",(void *) &_stdlib_temp_string,ICONSOLE_VAR_STRING); + IConsoleVarRegister("temp_pointer",(void *) &_stdlib_temp_pointer,ICONSOLE_VAR_POINTER); + IConsoleVarRegister("temp_uint32",(void *) &_stdlib_temp_uint32,ICONSOLE_VAR_UINT32); + IConsoleVarRegister("temp_bool",(void *) &_stdlib_temp_bool,ICONSOLE_VAR_BOOLEAN); +} + + diff --git a/console.h b/console.h new file mode 100644 index 0000000000..6cd369a4d0 --- /dev/null +++ b/console.h @@ -0,0 +1,63 @@ +// ** console ** // + +enum { + ICONSOLE_OPENED=0, + ICONSOLE_CLOSED, + ICONSOLE_OPENING, + ICONSOLE_CLOSING, +} _iconsole_modes; + +// ** console parser ** // + +enum { + ICONSOLE_VAR_NONE=0, + ICONSOLE_VAR_BOOLEAN, + ICONSOLE_VAR_BYTE, + ICONSOLE_VAR_UINT16, + ICONSOLE_VAR_UINT32, + ICONSOLE_VAR_INT16, + ICONSOLE_VAR_INT32, + ICONSOLE_VAR_STRING, + ICONSOLE_VAR_VARPTR, + ICONSOLE_VAR_POINTER, + ICONSOLE_VAR_UNKNOWN +} _iconsole_var_types; + +typedef struct { + // -------------- // + void * addr; + byte * name; + // -------------- // + void * _next; + } _iconsole_cmd; + +typedef struct { + // --------------- // + void * addr; + byte * name; + byte type; + // -------------- // + void * _next; + } _iconsole_var; + +// ** ttd.c functions ** // +void SetDebugString(const char *s); + +// ** console functions ** // + +void IConsoleClearCommand(); +void IConsoleInit(); +void IConsoleClear(); +void IConsoleFree(); +void IConsoleResize(); +void IConsoleSwitch(); +void IConsoleClose(); +void IConsoleOpen(); +void IConsolePrint(byte color_code, byte* string); +void IConsolePrintF(byte color_code, const char *s, ...); +void IConsoleDebug(byte* string); +void IConsoleError(byte* string); +void IConsoleCmdRegister(byte * name, void * addr); +void IConsoleVarRegister(byte * name, void * addr, byte type); +void IConsoleCmdExec(byte * cmdstr); + diff --git a/docs/console.txt b/docs/console.txt new file mode 100644 index 0000000000..eee70ac186 --- /dev/null +++ b/docs/console.txt @@ -0,0 +1,46 @@ +OPENTTD INGAME CONSOLE DOCUMENTATION +==================================== + +HOTKEY: TAB + +COMMANDS: +--------- + +list_cmds [filter] +list_vars [filter] +echo [string] +echoc [color-code] [string] +printf [formatstring] [var] [var] [var].... +printfc [color-code] [formatstring] [var] [var] [var].... +screenshot ["big"] +debug_level [0-9] +exit + + +VARIABLES: +---------- +*developer +-> 0 = no output +-> 1 = console error output [like command not found] +-> 2 = console error and debug output + +*cursor_rate +-> 1-12 = defines the cursor blink interval + + +VARIABLE HANDLING: +------------------ + +*developer = 0 +*developer ++ + +*temp_string = test +*temp_string = "my little" + +printf "%s world" *temp_string + + +--------------------------------------------------- +feel free to add more commands and use this in-game +console for your debugging / enhancements + diff --git a/gfx.c b/gfx.c index 19e480a752..be4cac6227 100644 --- a/gfx.c +++ b/gfx.c @@ -1920,9 +1920,9 @@ void SetAnimatedMouseCursor(const uint16 *table) } bool ChangeResInGame(int w, int h) -{ +{ if ((_screen.width != w || _screen.height != h) && !_video_driver->change_resolution(w, h)) - return false; + return false; _cur_resolution[0] = w; _cur_resolution[1] = h; diff --git a/intro_gui.c b/intro_gui.c index 83cb5a043e..a731c4dd05 100644 --- a/intro_gui.c +++ b/intro_gui.c @@ -6,7 +6,8 @@ #include "viewport.h" #include "gfx.h" #include "player.h" -#include "command.h" +#include "command.h" +#include "console.h" extern void MakeNewGame(); extern void StartScenario(); @@ -69,7 +70,11 @@ static void SelectGameWndProc(Window *w, WindowEvent *e) { case 12: case 13: case 14: case 15: DoCommandP(0, e->click.widget - 12, 0, NULL, CMD_SET_NEW_LANDSCAPE_TYPE); break; - } + } + case WE_KEYPRESS: + switch(e->keypress.keycode) { + case WKC_TAB: IConsoleSwitch(); break; + } break; } } diff --git a/main_gui.c b/main_gui.c index f5eec080bc..edbe36fe7a 100644 --- a/main_gui.c +++ b/main_gui.c @@ -9,7 +9,8 @@ #include "command.h" #include "news.h" #include "town.h" -#include "vehicle.h" +#include "vehicle.h" +#include "console.h" #include "table/animcursors.h" @@ -1687,7 +1688,8 @@ static void MainToolbarWndProc(Window *w, WindowEvent *e) case WKC_SHIFT | WKC_F11: ShowBuildTreesToolbar(); break; case WKC_SHIFT | WKC_F12: ShowMusicWindow(); break; case WKC_CTRL | 'S': _make_screenshot = 1; break; - case WKC_CTRL | 'G': _make_screenshot = 2; break; + case WKC_CTRL | 'G': _make_screenshot = 2; break; + case WKC_TAB: IConsoleSwitch(); e->keypress.keycode=0; break; case WKC_CTRL | WKC_ALT | 'C': if(!_networking) ShowCheatWindow(); break; } } break; diff --git a/ttd.c b/ttd.c index 53efd9725c..f395acea63 100644 --- a/ttd.c +++ b/ttd.c @@ -20,7 +20,8 @@ #include "hal.h" #include "airport.h" #include "saveload.h" -#include "ai.h" +#include "ai.h" +#include "console.h" #include @@ -74,8 +75,8 @@ void CDECL debug(const char *s, ...) va_start(va, s); vsprintf(buf, s, va); va_end(va); - - fprintf(stderr, "dbg: %s\n", buf); + fprintf(stderr, "dbg: %s\n", buf); + IConsoleDebug((byte *) &buf); } void CDECL ShowInfoF(const char *str, ...) @@ -570,7 +571,7 @@ int ttd_main(int argc, char* argv[]) MxInitialize(11025, "sample.cat"); // This must be done early, since functions use the InvalidateWindow* calls - InitWindowSystem(); + InitWindowSystem(); GfxLoadSprites(); LoadStringWidthTable(); @@ -596,8 +597,13 @@ int ttd_main(int argc, char* argv[]) NetworkCoreConnectGame("auto",_network_server_port); } } + + // initialize the ingame console + IConsoleInit(); while (_video_driver->main_loop() == ML_SWITCHDRIVER) {} + + IConsoleFree(); if (_network_available) { // shutdown network-core @@ -636,7 +642,8 @@ void LoadIntroGame() _opt_mod_ptr = &_new_opt; GfxLoadSprites(); LoadStringWidthTable(); - // Setup main window + // Setup main window + IConsoleClose(); InitWindowSystem(); SetupColorsAndInitialWindow(); @@ -665,7 +672,8 @@ void MakeNewGame() GfxLoadSprites(); - // Reinitialize windows + // Reinitialize windows + IConsoleClose(); InitWindowSystem(); LoadStringWidthTable(); @@ -692,7 +700,8 @@ void MakeNewEditorWorld() GfxLoadSprites(); - // Re-init the windowing system + // Re-init the windowing system + IConsoleClose(); InitWindowSystem(); // Create toolbars @@ -726,7 +735,8 @@ void StartScenario() GfxLoadSprites(); - // Reinitialize windows + // Reinitialize windows + IConsoleClose(); InitWindowSystem(); LoadStringWidthTable(); @@ -775,7 +785,7 @@ bool SafeSaveOrLoad(const char *filename, int mode, int newgm) static void SwitchMode(int new_mode) { - _in_state_game_loop = true; + _in_state_game_loop = true; switch(new_mode) { case SM_EDITOR: // Switch to scenario editor diff --git a/ttd.dsp b/ttd.dsp index 9fc792f62c..883647814f 100644 --- a/ttd.dsp +++ b/ttd.dsp @@ -184,6 +184,10 @@ SOURCE=.\command.c # End Source File # Begin Source File +SOURCE=.\console.c +# End Source File +# Begin Source File + SOURCE=.\documentation.txt # End Source File # Begin Source File @@ -410,6 +414,10 @@ SOURCE=.\command.h # End Source File # Begin Source File +SOURCE=.\console.h +# End Source File +# Begin Source File + SOURCE=.\economy.h # End Source File # Begin Source File diff --git a/ttd.h b/ttd.h index 51052aa7cb..190651794b 100644 --- a/ttd.h +++ b/ttd.h @@ -393,7 +393,8 @@ enum { WC_INDUSTRY_DIRECTORY = 0x43, WC_MESSAGE_HISTORY = 0x44, WC_CHEATS = 0x45, - WC_PERFORMANCE_DETAIL = 0x46, + WC_PERFORMANCE_DETAIL = 0x46, + WC_CONSOLE = 0x47, }; diff --git a/window.c b/window.c index 8740bb1836..c51245972a 100644 --- a/window.c +++ b/window.c @@ -2,7 +2,8 @@ #include "ttd.h" #include "window.h" #include "gfx.h" -#include "viewport.h" +#include "viewport.h" +#include "console.h" void HandleButtonClick(Window *w, byte widget) { @@ -1204,7 +1205,9 @@ void RelocateAllWindows(int neww, int newh) vp->virtual_width = neww << vp->zoom; vp->virtual_height = newh << vp->zoom; continue; // don't modify top,left - } + } + + IConsoleResize(); if (w->window_class == WC_MAIN_TOOLBAR) { top = w->top;