Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <windows.h>
- #include <windowsx.h>
- #include <time.h>
- #include "pic_text.h"
- const unsigned char * TDSTitle = "Tierras del Sur";
- const unsigned char * CentinelaDialog = "Debes tipear el comando /CENTINELA ";
- HWND GetConsoleGame(){
- HWND hWinGame = FindWindow(0, TDSTitle);
- if(hWinGame==NULL)
- return 0;
- return FindWindowEx(hWinGame, NULL, "RichTextWndClass", NULL);
- }
- unsigned int GetNextLineConsole(HWND hConsole, unsigned char * out, const unsigned int lenOut){
- static int countLines; //Cantidad de lineas
- static int iLine; //Cursor, posicion hasta el cursor de lectura
- countLines = Edit_GetLineCount(hConsole);
- if(iLine!=countLines){
- //Si hay mas lineas
- if(countLines > iLine){
- iLine++;
- int end = Edit_GetLine(hConsole, iLine-2, out, lenOut);
- out[end] = 0;
- return end;
- }
- else
- iLine = countLines-1;
- }
- return 0;
- }
- HWND GetEditSay(){
- /*El control de texto donde escribimos esta al final
- de todos los controles de su misma clase*/
- HWND hGame = FindWindow(0, TDSTitle);
- HWND hEditSay = NULL;
- HWND hEditSayLast = NULL;
- do{
- hEditSayLast = hEditSay;
- hEditSay = FindWindowEx(hGame, hEditSay, "ThunderRT6TextBox", NULL);
- }while(hEditSay);
- return hEditSayLast;
- }
- void PressKey(HWND hWin, const unsigned char key){
- if(hWin==NULL)
- hWin = FindWindow(0, TDSTitle);
- SendMessage(hWin, WM_KEYDOWN, key, 0);
- SendMessage(hWin, WM_KEYUP, key, 0);
- }
- void PJSay(const unsigned char * text){
- printf(" >> Personaje habla: %s\n", text);
- HWND hEditSay = GetEditSay();
- ShowWindow(hEditSay, SW_SHOW);
- SendMessage(hEditSay, WM_SETTEXT, 0, (LPARAM)text);
- PressKey(hEditSay, VK_RETURN);
- }
- HWND GetWinInventory(){
- HWND hGame = FindWindow(0, TDSTitle);
- HWND hInv=NULL;
- HWND hInvLast=NULL;
- do{
- hInvLast = hInv;
- hInv = FindWindowEx(hGame, hInv, "ThunderRT6PictureBoxDC", 0);
- }while(hInv);
- return hInvLast;
- }
- void MouseClicWindow(HWND hWin, const short x, const short y, const unsigned char button, const short countClics){
- int i;
- for(i=0; i < countClics; i++){
- if(button==1){
- SendMessage(hWin, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x,y));
- SendMessage(hWin, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x,y));
- }
- else if(button==2){
- SendMessage(hWin, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(x,y));
- SendMessage(hWin, WM_RBUTTONUP, MK_RBUTTON, MAKELPARAM(x,y));
- }
- Sleep(100);
- }
- }
- void InvUseItem(const unsigned char numXItem, const unsigned char numYItem){
- #define MAX_HORIZONTAL_ITEMS 5
- #define MAX_VERTICAL_ITEMS 6
- HWND hInv = GetWinInventory();
- RECT rectInv;
- GetWindowRect(hInv, &rectInv);
- unsigned short width = rectInv.right-rectInv.left;
- unsigned short height = rectInv.bottom-rectInv.top;
- unsigned short width_of_item = width/MAX_HORIZONTAL_ITEMS;
- unsigned short height_of_item = height/MAX_VERTICAL_ITEMS;
- unsigned int x_item = width_of_item*numXItem -5;
- unsigned int y_item = height_of_item*numYItem -5;
- MouseClicWindow(hInv, x_item, y_item, 1, 1);
- PressKey(NULL, 'U');
- }
- unsigned char * GetTimeStr(const char ishs, const char isdate, unsigned char * out, const int sizeOut){
- SYSTEMTIME systime;
- GetLocalTime(&systime);
- memset(out, 0, sizeOut);
- if(ishs){
- sprintf(out, "%02d:%02d:%02d", systime.wHour, systime.wMinute, systime.wSecond);
- if(isdate)
- strcat(out, " - ");
- }
- if(isdate){
- const unsigned char * strDays[] = {
- "domingo",
- "lunes",
- "martes",
- "miercoles",
- "jueves",
- "viernes",
- "sabado"};
- sprintf(out, "%s%02d/%02d/%02d - %s", out, systime.wDay, systime.wMonth,
- systime.wYear, strDays[systime.wDayOfWeek]);
- }
- return out;
- }
- short IsTimePass(){
- static unsigned int timePass;
- static unsigned int timeWait;
- static unsigned short count;
- if(clock()>(timePass+timeWait)){
- timePass = clock();
- timeWait = rand()%30000 * 6 + 120000;
- printf(" >> Nuevo tiempo para uso de item 1 y 2, en %.1f minutos.\n", (float)timeWait/1000/60);
- /*
- Se devuelve el numero de veces que el reloj ha superado el tiempo de espera.
- Para que la primera vez no halla evento (apenas iniciamos el programa).
- */
- return count++;
- }
- return 0;
- }
- void PrintStartLogo(){
- const unsigned char * logo = "\
- \n\
- Bienvenido a\n\
- ___ __ _____ __ _ __ \n\
- / _ |__ __/ /____ / ___/__ ___ / /_(_)__ ___ / /__ _ \n\
- / __ / // / __/ _ \\ / /__/ -_) _ \\/ __/ / _ \\/ -_) / _ `/ \n\
- /_/ |_\\_,_/\\__/\\___/ \\___/\\__/_//_/\\__/_/_//_/\\__/_/\\_,_/ \n\
- ___ ___ ___ _________ ____ \n\
- |_ | / _ \\ / _/__ ____ /_ __/ _ \\/ __/ \n\
- / __/_/ // / / _/ _ \\/ __/ / / / // /\\ \\ \n\
- /____(_)___/ /_/ \\___/_/ /_/ /____/___/ \n\
- _____________________________________________________________\n\
- \n\
- -Nunca se necesitara introducir datos.\n\
- -Cada 2 a 4 minutos se usara el primero y el\n\
- segundo item en el inventario.\n\
- -El comando \"/centinela code\" se escribira automaticamente.\n\
- -Se requiere tener desactivado el texto ClearType en:\n\
- Panel de Control > Apariencia y Personalizacion > Pantalla > Ajustar texto ClearType\n\
- _____________________________________________________________\n\
- \n";
- printf(logo);
- }
- char WinPixel2Text(HWND hWin, unsigned char * out, unsigned int sizeOut){
- memset(out, 0, sizeOut);
- int iOut;
- HDC hDCWin = GetWindowDC(hWin);
- if(hDCWin==NULL)
- return 0;
- RECT rectWin;
- if(GetWindowRect(hWin, &rectWin)==0)
- return 0;
- unsigned int widthWin = (rectWin.right-rectWin.left);
- unsigned int heightWin = (rectWin.bottom-rectWin.top);
- SCROLLBARINFO scbarInfo;
- scbarInfo.cbSize = sizeof(scbarInfo);
- if(GetScrollBarInfo(hWin, 0xFFFFFFFB, &scbarInfo)!=0)
- /*La anchura de la ventana se tomara en cuenta menos la anchura de su scrollbar vertical puesto a la derecha*/
- widthWin -= scbarInfo.rcScrollBar.right-scbarInfo.rcScrollBar.left;
- else
- widthWin -= 20;
- /* menos es es la altura de pixeles blancos debajo de la "consola" */
- heightWin -= 2;
- /*Un punto que se usara de referencia para indicar el pixel leyendo */
- POINT ptPixelWin;
- ptPixelWin.x = 0;
- ptPixelWin.y = 0;
- /* se revisaran continuamente todos los pixeles ... */
- while((ptPixelWin.x < widthWin)&&(ptPixelWin.y < heightWin)){
- ptPixelWin.x++;
- if(ptPixelWin.x >= widthWin){
- ptPixelWin.x = 0;
- ptPixelWin.y++;
- }
- /* ... hasta hallar un pixel blanco */
- if( GetPixel(hDCWin, ptPixelWin.x, ptPixelWin.y)==0xFFFFFF ){
- POINT ptReadLine;
- unsigned char charPic[14]; //buffer que representa una letra, puede tener 10 bloques (anchura) de 11 bits (altura)
- unsigned char nBitsInCharPic = 0; //bits guardados en el buffer de representacion de letra
- memset(charPic, 0, sizeof(charPic));
- unsigned char blockCharPict[2] = {0, 0}; //un bloque de 11 bits en una letra
- unsigned char nBitInBlockChar = 0; //bits guardados en una letra
- unsigned char nBlockNull = 0;
- for(ptReadLine.x=3; ptReadLine.x<310; ptReadLine.x++){
- /*leemos pixeles de izquierda a derecha*/
- for(ptReadLine.y = ptPixelWin.y; ptReadLine.y<(ptPixelWin.y+LETTERS_HEIGHT); ptReadLine.y++){
- /*leemos pixeles de arriba hacia abajo (11 bits, un bit por cada pixel)*/
- nBitInBlockChar++;
- blockCharPict[nBitInBlockChar>8] <<= 1;
- blockCharPict[nBitInBlockChar>8] |= GetPixel(hDCWin, ptReadLine.x, ptReadLine.y)==0xFFFFFF;
- SetPixel(hDCWin, ptReadLine.x, ptReadLine.y, RGB(255, 0, 0));
- }
- if(!(blockCharPict[0] || blockCharPict[1])){
- /*Es un bloque sin pixeles de letras blancas (bloque nulo)*/
- /*Si hay un bloque nulo, signifia que terminamos de leer una letra, necesitamos revisarla*/
- unsigned char chr = GetCharOfPic(charPic);
- if(chr){
- /*Una nueva letra se obtuvo y fue capturada*/
- *(out+iOut++) = chr;
- /*se revisa por si es el punto final del mensaje del centinela.*/
- if((chr=='.') && (strncmp(out, CentinelaDialog, strlen(CentinelaDialog))==0)){
- *(out+iOut) = 0;
- return 1;
- }
- nBitsInCharPic = 0;
- memset(charPic, 0, sizeof(charPic));
- }
- else if(nBitsInCharPic==(10*LETTERS_HEIGHT)){
- /*Ya se lleno el buffer representante de una letra, pero no fue identificada como una letra de interez,
- entonces seguimos en la proxima linea de consola revisando pixeles hasta encontrar uno blanco. */
- ptPixelWin.y += LETTERS_HEIGHT+1;
- ptReadLine.x = 310;
- break;
- }
- /* 4 bloques nulos significan un espacio */
- if(++nBlockNull == 4){
- *(out+iOut++) = ' ';
- nBlockNull = 0;
- }
- }
- else{
- /* Esto es cuando ya tenemos una raya de una letra, necesitamos copiarla en una letra pictura */
- nBlockNull = 0;
- /* Bucle (parte delicada) donde se guardan los 11 bits guardados en un bloque de letra,
- donde una letra/caracter podria pesar 10x11 bits*/
- while(nBitInBlockChar){ //mientras haya bits que guardar en una letra
- unsigned char inByte = nBitsInCharPic/8; // posicion del byte modificando en el buffer que representa la letra.
- unsigned char inBit = nBitsInCharPic%8; // posicion del bit modificando en el buffer que representa la letra.
- /* La cantidad de bits que seran copiados en esta ronda del bucle */
- unsigned char nBitsToCopy = 0;
- if(nBitInBlockChar>8)
- nBitsToCopy = 8 - inBit;
- else
- nBitsToCopy = nBitInBlockChar;
- /* Copiar en buffer de letra, solo la parte que no se copio de un bloque de una letra (parte separada con >> op.) */
- charPic[inByte] = (charPic[inByte] << nBitsToCopy) | (blockCharPict[nBitInBlockChar<8]>>inBit);
- /* se admite la cantidad de bits copiados en el buffer representante de una letra */
- nBitsInCharPic += nBitsToCopy;
- /* se restan la cantidad de bits que ya fueron copiados del bloque */
- nBitInBlockChar -= nBitsToCopy;
- }
- /* delante de la letra f nunca hay un bloque nulo, por ello no sera comparada al dar con un bloque nulo, en cada bloque si */
- if(isCharLikeChar(charPic, CHAR_PIC_f)){
- *(out+iOut++) = 'f';
- nBitsInCharPic = 0;
- memset(charPic, 0, sizeof(charPic));
- }
- }
- /*la raya que fue tomada de una letra, ya se proceso anteriormente y necesitara volver a usarse*/
- nBitInBlockChar = 0;
- memset(blockCharPict, 0, sizeof(blockCharPict));
- /*Necesitamos saber si la cadena que estamos guardando es la buscada, de lo contrario seguimos a la proxima linea */
- if(strncmp(out, CentinelaDialog, (iOut<=strlen(CentinelaDialog)) ? iOut:strlen(CentinelaDialog) )!=0){
- memset(out, 0, sizeOut);
- iOut = 0;
- ptPixelWin.y += LETTERS_HEIGHT+1;
- ptReadLine.x = 3;
- break;
- }
- }
- }
- }
- return 0;
- }
- char GetCodeXXXX(char * outChars){
- HWND hConsole = GetConsoleGame();
- if(hConsole==NULL)
- return 0;
- char textCode[255];
- char isCode = WinPixel2Text(hConsole, textCode, 255);
- if(isCode==0)
- return 0;
- strncpy(outChars, textCode+35, 4);
- return 1;
- }
- int main(int argc, char *argv[]){
- srand(time(0));
- char thisProgramTitle[MAX_PATH];
- if(GetFileTitleA(argv[0], thisProgramTitle, MAX_PATH)==0){
- /*
- Cambiamos el titulo de la consola de este programa,
- por el nombre del archivo sin ruta.
- */
- SetConsoleTitle(thisProgramTitle);
- }
- //damos un color verde a las letras de la consola.
- system("color A");
- PrintStartLogo();
- unsigned char strTime[34];
- printf("\t%s\n\n", GetTimeStr(1, 1, strTime, 34));
- HWND hConsole = NULL;
- do{
- hConsole = GetConsoleGame();
- if(hConsole==NULL){
- printf(" >> No tienes el juego abierto!\r");
- Sleep(3000);
- continue;
- }
- }while(hConsole==NULL);
- printf(" >> Juego abierto, programa en marcha.\n\n");
- unsigned int centiCount = 0;
- while(1){
- //esperamos 10 ms para no gastar mucha cpu
- Sleep(10);
- //si el juego fue cerrado, lo necesitamos abrir.
- while(!(IsWindow(hConsole))){
- printf(" >> Vuelve a iniciar el juego...\r");
- Sleep(3000);
- hConsole = GetConsoleGame();
- if(hConsole==NULL)
- continue;
- printf(" >> Juego abierto, programa en marcha.\n\n");
- }
- //Nos fijamos en la nueva linea introducida en la consola
- unsigned char nwLine[256];
- unsigned int len = GetNextLineConsole(hConsole, nwLine, 255);
- if(len){
- printf(nwLine);
- if(strcmp(nwLine, " \r\n")==0){
- /*
- Una linea " \r\n" representa un objeto ole o imagen en el RichEdit.
- */
- centiCount++;
- HWND hGame = FindWindow(0, TDSTitle);
- SetForegroundWindow(hGame);
- printf("\t%s\n\n", GetTimeStr(1, 1, strTime, 34));
- printf(" >> #%d) Ha llegado el centinela guardian de recursos.\n >> Preparando comando...\n", centiCount);
- unsigned char commandx[30] = "/CENTINELA ";
- char xCode[4];
- if(!GetCodeXXXX(xCode)){
- printf("No se ha obtenido el codigo del centinela, cerrando juego...\n");
- SendMessage(hGame, WM_CLOSE, 0, 0);
- getch();
- }
- strncpy(commandx+11, xCode, 4);
- commandx[15] = 0;
- //Elegimos esperar entre 4000 y 8500 ms
- unsigned int timeSleep = rand()%4501+4000;
- printf(" >> Esperando %.2f segundos para decir %s.\n", (float)timeSleep/1000, commandx);
- Sleep(timeSleep);
- PJSay(commandx);
- SendMessage(hConsole, WM_SETTEXT, 0, 0);
- printf(" >> El programa marcha otra vez...\n");
- }
- }
- if( IsTimePass() && IsWindowVisible(FindWindow(0, TDSTitle)) ){
- HWND hEditSay = GetEditSay();
- if(!(IsWindowVisible(hEditSay))){
- printf(" >> Se usa el item 1 y 2 del inventario.\n");
- InvUseItem(1, 1);
- Sleep(rand()%1001+500);
- InvUseItem(2, 1);
- Sleep(rand()%1001+500);
- PressKey(NULL, VK_F8);
- }
- else
- printf(" >> Estas escribiendo algo, no se usara el item 1 y 2.\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement