Advertisement
Guest User

ASD

a guest
Apr 27th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. #include <windowsx.h>
  6. #include <time.h>
  7. #include "pic_text.h"
  8.  
  9.  
  10. const unsigned char * TDSTitle = "Tierras del Sur";
  11. const unsigned char * CentinelaDialog = "Debes tipear el comando /CENTINELA ";
  12.  
  13. HWND GetConsoleGame(){
  14.     HWND hWinGame = FindWindow(0, TDSTitle);
  15.     if(hWinGame==NULL)
  16.         return 0;
  17.    
  18.     return FindWindowEx(hWinGame, NULL, "RichTextWndClass", NULL);
  19. }
  20.  
  21. unsigned int GetNextLineConsole(HWND hConsole, unsigned char * out, const unsigned int lenOut){
  22.     static  int countLines; //Cantidad de lineas
  23.    
  24.     static  int iLine; //Cursor, posicion hasta el cursor de lectura
  25.    
  26.    
  27.     countLines = Edit_GetLineCount(hConsole);
  28.     if(iLine!=countLines){
  29.        
  30.         //Si hay mas lineas
  31.         if(countLines > iLine){
  32.             iLine++;
  33.            
  34.             int end = Edit_GetLine(hConsole, iLine-2, out, lenOut);
  35.             out[end] = 0;
  36.            
  37.             return end;
  38.             }
  39.         else
  40.             iLine = countLines-1;
  41.         }
  42.    
  43.     return 0;
  44. }
  45.  
  46. HWND GetEditSay(){
  47.     /*El control de texto donde escribimos esta al final
  48.     de todos los controles de su misma clase*/
  49.     HWND hGame = FindWindow(0, TDSTitle);
  50.    
  51.     HWND hEditSay = NULL;
  52.     HWND hEditSayLast = NULL;
  53.    
  54.     do{
  55.         hEditSayLast = hEditSay;
  56.         hEditSay = FindWindowEx(hGame, hEditSay, "ThunderRT6TextBox", NULL);
  57.     }while(hEditSay);
  58.    
  59.     return hEditSayLast;
  60. }
  61.  
  62. void PressKey(HWND hWin, const unsigned char key){
  63.     if(hWin==NULL)
  64.         hWin = FindWindow(0, TDSTitle);
  65.    
  66.     SendMessage(hWin, WM_KEYDOWN, key, 0);
  67.     SendMessage(hWin, WM_KEYUP, key, 0);
  68. }
  69.  
  70. void PJSay(const unsigned char * text){
  71.    
  72.     printf(" >> Personaje habla: %s\n", text);
  73.    
  74.     HWND hEditSay = GetEditSay();
  75.    
  76.     ShowWindow(hEditSay, SW_SHOW);
  77.     SendMessage(hEditSay, WM_SETTEXT, 0, (LPARAM)text);
  78.     PressKey(hEditSay, VK_RETURN);
  79. }
  80.  
  81. HWND GetWinInventory(){
  82.     HWND hGame = FindWindow(0, TDSTitle);
  83.    
  84.     HWND hInv=NULL;
  85.     HWND hInvLast=NULL;
  86.    
  87.     do{
  88.         hInvLast = hInv;
  89.         hInv = FindWindowEx(hGame, hInv, "ThunderRT6PictureBoxDC", 0);
  90.     }while(hInv);
  91.        
  92.     return hInvLast;
  93. }
  94.  
  95. void MouseClicWindow(HWND hWin, const short x, const short y, const unsigned char button, const short countClics){
  96.    
  97.     int i;
  98.     for(i=0; i < countClics; i++){
  99.         if(button==1){
  100.             SendMessage(hWin, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x,y));
  101.             SendMessage(hWin, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x,y));
  102.             }
  103.         else if(button==2){
  104.             SendMessage(hWin, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(x,y));
  105.             SendMessage(hWin, WM_RBUTTONUP, MK_RBUTTON, MAKELPARAM(x,y));
  106.             }
  107.         Sleep(100);
  108.         }
  109. }
  110.  
  111. void InvUseItem(const unsigned char numXItem, const unsigned char numYItem){
  112.    
  113.     #define MAX_HORIZONTAL_ITEMS 5
  114.     #define MAX_VERTICAL_ITEMS 6
  115.    
  116.     HWND hInv = GetWinInventory();
  117.    
  118.     RECT rectInv;
  119.     GetWindowRect(hInv, &rectInv);
  120.    
  121.     unsigned short width = rectInv.right-rectInv.left;
  122.     unsigned short height = rectInv.bottom-rectInv.top;
  123.    
  124.     unsigned short width_of_item = width/MAX_HORIZONTAL_ITEMS;
  125.     unsigned short height_of_item = height/MAX_VERTICAL_ITEMS;
  126.    
  127.     unsigned int x_item = width_of_item*numXItem -5;
  128.     unsigned int y_item = height_of_item*numYItem -5;
  129.    
  130.     MouseClicWindow(hInv, x_item, y_item, 1, 1);
  131.    
  132.     PressKey(NULL, 'U');
  133. }
  134.  
  135. unsigned char * GetTimeStr(const char ishs, const char isdate, unsigned char * out, const int sizeOut){
  136.     SYSTEMTIME systime;
  137.    
  138.     GetLocalTime(&systime);
  139.    
  140.     memset(out, 0, sizeOut);
  141.    
  142.     if(ishs){
  143.         sprintf(out, "%02d:%02d:%02d", systime.wHour, systime.wMinute, systime.wSecond);
  144.         if(isdate)
  145.             strcat(out, " - ");
  146.         }
  147.    
  148.     if(isdate){
  149.         const unsigned char * strDays[] = {
  150.                         "domingo",
  151.                         "lunes",
  152.                         "martes",
  153.                         "miercoles",
  154.                         "jueves",
  155.                         "viernes",
  156.                         "sabado"};
  157.        
  158.         sprintf(out, "%s%02d/%02d/%02d - %s", out, systime.wDay, systime.wMonth,
  159.                  systime.wYear, strDays[systime.wDayOfWeek]);
  160.         }
  161.    
  162.     return out;
  163. }
  164.  
  165. short IsTimePass(){
  166.     static unsigned int timePass;
  167.     static unsigned int timeWait;
  168.     static unsigned short count;
  169.    
  170.     if(clock()>(timePass+timeWait)){
  171.        
  172.         timePass = clock();
  173.        
  174.         timeWait = rand()%30000 * 6 + 120000;
  175.         printf(" >> Nuevo tiempo para uso de item 1 y 2, en %.1f minutos.\n", (float)timeWait/1000/60);
  176.        
  177.         /*
  178.         Se devuelve el numero de veces que el reloj ha superado el tiempo de espera.
  179.         Para que la primera vez no halla evento (apenas iniciamos el programa).
  180.         */
  181.         return count++;
  182.         }
  183.    
  184.     return 0;
  185. }
  186.  
  187. void PrintStartLogo(){
  188.  
  189.     const unsigned char * logo = "\
  190. \n\
  191. Bienvenido a\n\
  192.   ___       __         _____         __  _          __     \n\
  193.  / _ |__ __/ /____    / ___/__ ___  / /_(_)__  ___ / /__ _ \n\
  194. / __ / // / __/ _ \\  / /__/ -_) _ \\/ __/ / _ \\/ -_) / _ `/ \n\
  195. /_/ |_\\_,_/\\__/\\___/  \\___/\\__/_//_/\\__/_/_//_/\\__/_/\\_,_/  \n\
  196.      ___   ___    ___           _________  ____            \n\
  197.     |_  | / _ \\  / _/__  ____  /_  __/ _ \\/ __/            \n\
  198.    / __/_/ // / / _/ _ \\/ __/   / / / // /\\ \\              \n\
  199.   /____(_)___/ /_/ \\___/_/     /_/ /____/___/              \n\
  200. _____________________________________________________________\n\
  201. \n\
  202. -Nunca se necesitara introducir datos.\n\
  203. -Cada 2 a 4 minutos se usara el primero y el\n\
  204. segundo item en el inventario.\n\
  205. -El comando \"/centinela code\" se escribira automaticamente.\n\
  206. -Se requiere tener desactivado el texto ClearType en:\n\
  207. Panel de Control > Apariencia y Personalizacion > Pantalla > Ajustar texto ClearType\n\
  208. _____________________________________________________________\n\
  209. \n";
  210.  
  211.     printf(logo);
  212. }
  213.  
  214.  
  215. char WinPixel2Text(HWND hWin, unsigned char * out, unsigned int sizeOut){
  216.    
  217.     memset(out, 0, sizeOut);
  218.    
  219.     int iOut;
  220.    
  221.     HDC hDCWin = GetWindowDC(hWin);
  222.     if(hDCWin==NULL)
  223.         return 0;
  224.    
  225.     RECT rectWin;
  226.     if(GetWindowRect(hWin, &rectWin)==0)
  227.         return 0;
  228.    
  229.     unsigned int widthWin = (rectWin.right-rectWin.left);
  230.     unsigned int heightWin = (rectWin.bottom-rectWin.top);
  231.    
  232.     SCROLLBARINFO scbarInfo;
  233.     scbarInfo.cbSize = sizeof(scbarInfo);
  234.     if(GetScrollBarInfo(hWin, 0xFFFFFFFB, &scbarInfo)!=0)
  235.         /*La anchura de la ventana se tomara en cuenta menos la anchura de su scrollbar vertical puesto a la derecha*/
  236.         widthWin -= scbarInfo.rcScrollBar.right-scbarInfo.rcScrollBar.left;
  237.     else
  238.         widthWin -= 20;
  239.        
  240.     /* menos es es la altura de pixeles blancos debajo de la "consola" */
  241.     heightWin -= 2;
  242.    
  243.     /*Un punto que se usara de referencia para indicar el pixel leyendo */
  244.     POINT ptPixelWin;
  245.     ptPixelWin.x = 0;
  246.     ptPixelWin.y = 0;
  247.    
  248.     /* se revisaran continuamente todos los pixeles ... */
  249.     while((ptPixelWin.x < widthWin)&&(ptPixelWin.y < heightWin)){
  250.         ptPixelWin.x++;
  251.        
  252.         if(ptPixelWin.x >= widthWin){
  253.             ptPixelWin.x = 0;
  254.             ptPixelWin.y++;
  255.             }
  256.        
  257.         /* ... hasta hallar un pixel blanco */
  258.         if( GetPixel(hDCWin, ptPixelWin.x, ptPixelWin.y)==0xFFFFFF ){
  259.            
  260.             POINT ptReadLine;
  261.            
  262.             unsigned char charPic[14]; //buffer que representa una letra, puede tener 10 bloques (anchura) de 11 bits (altura)
  263.             unsigned char nBitsInCharPic = 0; //bits guardados en el buffer de representacion de letra
  264.             memset(charPic, 0, sizeof(charPic));
  265.            
  266.             unsigned char blockCharPict[2] = {0, 0}; //un bloque de 11 bits en una letra
  267.             unsigned char nBitInBlockChar = 0; //bits guardados en una letra
  268.            
  269.             unsigned char nBlockNull = 0;
  270.            
  271.             for(ptReadLine.x=3; ptReadLine.x<310; ptReadLine.x++){
  272.                 /*leemos pixeles de izquierda a derecha*/
  273.                 for(ptReadLine.y = ptPixelWin.y; ptReadLine.y<(ptPixelWin.y+LETTERS_HEIGHT); ptReadLine.y++){
  274.                     /*leemos pixeles de arriba hacia abajo (11 bits, un bit por cada pixel)*/
  275.                     nBitInBlockChar++;
  276.                    
  277.                     blockCharPict[nBitInBlockChar>8] <<= 1;
  278.                     blockCharPict[nBitInBlockChar>8] |= GetPixel(hDCWin, ptReadLine.x, ptReadLine.y)==0xFFFFFF;
  279.                    
  280.                     SetPixel(hDCWin, ptReadLine.x, ptReadLine.y, RGB(255, 0, 0));
  281.                     }
  282.                    
  283.                 if(!(blockCharPict[0] || blockCharPict[1])){
  284.                     /*Es un bloque sin pixeles de letras blancas (bloque nulo)*/
  285.                    
  286.                     /*Si hay un bloque nulo, signifia que terminamos de leer una letra, necesitamos revisarla*/
  287.                     unsigned char chr = GetCharOfPic(charPic);
  288.                     if(chr){
  289.                         /*Una nueva letra se obtuvo y fue capturada*/
  290.                         *(out+iOut++) = chr;
  291.                        
  292.                         /*se revisa por si es el punto final del mensaje del centinela.*/
  293.                         if((chr=='.') && (strncmp(out, CentinelaDialog, strlen(CentinelaDialog))==0)){
  294.                             *(out+iOut) = 0;
  295.                             return 1;
  296.                             }
  297.                            
  298.                         nBitsInCharPic = 0;
  299.                         memset(charPic, 0, sizeof(charPic));
  300.                         }
  301.                     else if(nBitsInCharPic==(10*LETTERS_HEIGHT)){
  302.                         /*Ya se lleno el buffer representante de una letra, pero no fue identificada como una letra de interez,
  303.                          entonces seguimos en la proxima linea de consola revisando pixeles hasta encontrar uno blanco. */
  304.                         ptPixelWin.y += LETTERS_HEIGHT+1;
  305.                         ptReadLine.x = 310;
  306.                         break;
  307.                         }
  308.                  
  309.                     /* 4 bloques nulos significan un espacio */
  310.                     if(++nBlockNull == 4){
  311.                         *(out+iOut++) = ' ';
  312.                         nBlockNull = 0;
  313.                         }
  314.                     }
  315.                 else{
  316.                     /* Esto es cuando ya tenemos una raya de una letra, necesitamos copiarla en una letra pictura */
  317.                    
  318.                     nBlockNull = 0;
  319.                     /* Bucle (parte delicada) donde se guardan los 11 bits guardados en un bloque de letra,
  320.                      donde una letra/caracter podria pesar 10x11 bits*/
  321.                     while(nBitInBlockChar){ //mientras haya bits que guardar en una letra
  322.                         unsigned char inByte = nBitsInCharPic/8; // posicion del byte modificando en el buffer que representa la letra.
  323.                         unsigned char inBit = nBitsInCharPic%8; // posicion del bit modificando en el buffer que representa la letra.
  324.                        
  325.                         /* La cantidad de bits que seran copiados en esta ronda del bucle */
  326.                         unsigned char nBitsToCopy = 0;
  327.                         if(nBitInBlockChar>8)
  328.                             nBitsToCopy = 8 - inBit;
  329.                         else
  330.                             nBitsToCopy = nBitInBlockChar;
  331.                        
  332.                         /* Copiar en buffer de letra, solo la parte que no se copio de un bloque de una letra (parte separada con >> op.) */
  333.                         charPic[inByte] = (charPic[inByte] << nBitsToCopy) | (blockCharPict[nBitInBlockChar<8]>>inBit);
  334.                        
  335.                         /* se admite la cantidad de bits copiados en el buffer representante de una letra */
  336.                         nBitsInCharPic += nBitsToCopy;
  337.                        
  338.                         /* se restan la cantidad de bits que ya fueron copiados del bloque */
  339.                         nBitInBlockChar -= nBitsToCopy;
  340.                         }
  341.                    
  342.                     /* 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 */
  343.                     if(isCharLikeChar(charPic, CHAR_PIC_f)){
  344.                         *(out+iOut++) = 'f';
  345.                        
  346.                         nBitsInCharPic = 0;
  347.                         memset(charPic, 0, sizeof(charPic));
  348.                         }
  349.                     }
  350.                
  351.                 /*la raya que fue tomada de una letra, ya se proceso anteriormente y necesitara volver a usarse*/
  352.                 nBitInBlockChar = 0;
  353.                 memset(blockCharPict, 0, sizeof(blockCharPict));
  354.                
  355.                 /*Necesitamos saber si la cadena que estamos guardando es la buscada, de lo contrario seguimos a la proxima linea */
  356.                 if(strncmp(out, CentinelaDialog, (iOut<=strlen(CentinelaDialog)) ?  iOut:strlen(CentinelaDialog) )!=0){
  357.                    
  358.                     memset(out, 0, sizeOut);
  359.                     iOut = 0;
  360.                    
  361.                     ptPixelWin.y += LETTERS_HEIGHT+1;
  362.                     ptReadLine.x = 3;
  363.                        
  364.                     break;
  365.                     }
  366.                 }
  367.             }
  368.         }
  369.        
  370.     return 0;
  371. }
  372.  
  373. char GetCodeXXXX(char * outChars){
  374.    
  375.     HWND hConsole = GetConsoleGame();
  376.    
  377.     if(hConsole==NULL)
  378.         return 0;
  379.        
  380.     char textCode[255];
  381.     char isCode = WinPixel2Text(hConsole, textCode, 255);
  382.     if(isCode==0)
  383.         return 0;
  384.    
  385.     strncpy(outChars, textCode+35, 4);
  386.    
  387.     return 1;
  388. }
  389.  
  390. int main(int argc, char *argv[]){
  391.    
  392.     srand(time(0));
  393.    
  394.     char thisProgramTitle[MAX_PATH];
  395.     if(GetFileTitleA(argv[0], thisProgramTitle, MAX_PATH)==0){
  396.        
  397.         /*
  398.         Cambiamos el titulo de la consola de este programa,
  399.         por el nombre del archivo sin ruta.
  400.         */
  401.         SetConsoleTitle(thisProgramTitle);
  402.         }
  403.    
  404.     //damos un color verde a las letras de la consola.
  405.     system("color A");
  406.     PrintStartLogo();
  407.    
  408.     unsigned char strTime[34];
  409.     printf("\t%s\n\n", GetTimeStr(1, 1, strTime, 34));
  410.    
  411.     HWND hConsole = NULL;
  412.    
  413.     do{
  414.         hConsole = GetConsoleGame();
  415.    
  416.         if(hConsole==NULL){
  417.             printf(" >> No tienes el juego abierto!\r");
  418.            
  419.             Sleep(3000);
  420.             continue;
  421.             }
  422.            
  423.         }while(hConsole==NULL);
  424.        
  425.     printf(" >> Juego abierto, programa en marcha.\n\n");
  426.    
  427.     unsigned int centiCount = 0;
  428.    
  429.     while(1){
  430.        
  431.         //esperamos 10 ms para no gastar mucha cpu
  432.         Sleep(10);
  433.        
  434.         //si el juego fue cerrado, lo necesitamos abrir.
  435.         while(!(IsWindow(hConsole))){
  436.             printf(" >> Vuelve a iniciar el juego...\r");
  437.            
  438.             Sleep(3000);
  439.            
  440.             hConsole = GetConsoleGame();
  441.            
  442.             if(hConsole==NULL)
  443.                 continue;
  444.            
  445.             printf(" >> Juego abierto, programa en marcha.\n\n");
  446.             }
  447.        
  448.         //Nos fijamos en la nueva linea introducida en la consola
  449.         unsigned char nwLine[256];
  450.         unsigned int len = GetNextLineConsole(hConsole, nwLine, 255);
  451.        
  452.         if(len){
  453.             printf(nwLine);
  454.             if(strcmp(nwLine, " \r\n")==0){
  455.                 /*
  456.                 Una linea " \r\n" representa un objeto ole o imagen en el RichEdit.
  457.                 */
  458.                 centiCount++;
  459.                
  460.                 HWND hGame = FindWindow(0, TDSTitle);
  461.                 SetForegroundWindow(hGame);
  462.                
  463.                 printf("\t%s\n\n", GetTimeStr(1, 1, strTime, 34));
  464.                
  465.                 printf(" >> #%d) Ha llegado el centinela guardian de recursos.\n >> Preparando comando...\n", centiCount);
  466.                
  467.                 unsigned char commandx[30] = "/CENTINELA ";
  468.                 char xCode[4];
  469.                
  470.                 if(!GetCodeXXXX(xCode)){
  471.                     printf("No se ha obtenido el codigo del centinela, cerrando juego...\n");
  472.                    
  473.                     SendMessage(hGame, WM_CLOSE, 0, 0);
  474.                     getch();
  475.                     }
  476.                
  477.                 strncpy(commandx+11, xCode, 4);
  478.                 commandx[15] = 0;
  479.                
  480.                 //Elegimos esperar entre 4000 y 8500 ms
  481.                 unsigned int timeSleep = rand()%4501+4000;
  482.                 printf(" >> Esperando %.2f segundos para decir %s.\n", (float)timeSleep/1000, commandx);
  483.                 Sleep(timeSleep);
  484.                
  485.                 PJSay(commandx);
  486.                
  487.                 SendMessage(hConsole, WM_SETTEXT, 0, 0);
  488.                
  489.                 printf(" >> El programa marcha otra vez...\n");
  490.                 }
  491.             }
  492.        
  493.         if( IsTimePass() && IsWindowVisible(FindWindow(0, TDSTitle)) ){
  494.            
  495.             HWND hEditSay = GetEditSay();
  496.             if(!(IsWindowVisible(hEditSay))){
  497.                 printf(" >> Se usa el item 1 y 2 del inventario.\n");
  498.                 InvUseItem(1, 1);
  499.            
  500.                 Sleep(rand()%1001+500);
  501.            
  502.                 InvUseItem(2, 1);
  503.            
  504.                 Sleep(rand()%1001+500);
  505.            
  506.                 PressKey(NULL, VK_F8);
  507.                 }
  508.             else
  509.                 printf(" >> Estas escribiendo algo, no se usara el item 1 y 2.\n");
  510.             }
  511.        
  512.         }
  513.    
  514.     return 0;
  515. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement