/*
    ORPP Client

    (C) 2005 Jason Hunt

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    Contact: huntjas2@msu.edu

    Compile with -lwsock32

    FILE: key.cpp
*/


#include <windows.h>

#pragma hdrstop

#include "key.h"
#include "display.h"
#include "network.h"
#include "camera.h"
#include "display.h"
#include "log.h"

#define R 0x80  // reverse

bool keys[256]; // key track of keys

int speed = 8;

/* inital key setup */
void reset_keys( void )
{
    for (unsigned int i = 0; i < 256; i++) 
        keys[i] = false;

    speed = 8;   
}
/* end of reset_keys */


/* deal with changed key values */
void update_movement( void  )
{
    if (keys[VK_RIGHT] && pan > 3) pan -= 3;
    if (keys[VK_LEFT] && pan < 252) pan += 3;

    // manual camera view
    if (keys['Z']) pan = 0xf0;
    if (keys['X']) pan = 0x80;
    if (keys['C']) pan = 0x10;

    if (keys['Q']) { l_motor = speed;  r_motor = 0;     } else     // forward left
    if (keys['E']) { l_motor = 0;      r_motor = speed; } else    // forward right

/*    if (keys['W']) { l_motor = speed;     r_motor = speed;     } else // forward
    if (keys['S']) { l_motor = speed | R; r_motor = speed | R; } else // reverse
    if (keys['A']) { l_motor = speed | R; r_motor = speed;     } else // left spin
    if (keys['D']) { l_motor = speed;     r_motor = speed | R; } else // right spin
        { l_motor = 0x0; r_motor = 0x0;  }*/

    if (keys['W']) send_value(0x60); else
    if (keys['S']) send_value(0x61); else
    if (keys['A']) send_value(0x62); else
    if (keys['D'])send_value(0x63); else
    if (keys[' ']) send_value(0x65); else
    send_value(0x64); 



}
/* end of update movement */


/* called when a key is pressed */
void process_key_down(unsigned char key)
{
    // toggle logging
    if (key == 'L')
    {
        set_logging(!get_logging());
        
        
        if (get_logging())    
            add_line("Logging enabled");
        else
            add_line("Logging disabled");
    }

    // toggle rebug display
    if (key == 'P') 
    {
        display_text = !display_text;        
        // resize the window if needed
        if(display_text)
            display_y -= text_area_height;
        else
            display_y += text_area_height;
    }

    // escape key
    if (key == VK_ESCAPE) 
    {
        camera_disconnect();
        shutdown_network();
        PostQuitMessage(0);
    } else

   // if (key == VK_SPACE && !keys[VK_SPACE])  fire_on(); else
    if (key == 'V') d_frame = !d_frame; else
    if (key == 'T') 
    {
        targeting = !targeting;

        if (targeting) 
            add_line("Targeting enabled");
        else
        {
            fire_off();       
            add_line("Targeting disabled");
        }            
    }   
    
    if (key == 'M') 
    {
        camera_mouse = !camera_mouse; 

    }else
    
    if (key == '1') speed = 8; else
    if (key == '2') speed = 10; else
    if (key == '3') speed = 12; else
    if (key == '4') speed = 14; else
    if (key == '5') speed = 16; 

    bool ch = ((key == VK_LEFT ) || (key == VK_RIGHT) || 
               (key == 'A' && !keys['A']) || 
               (key == 'D' && !keys['D']) || 
               (key == 'S' && !keys['S']) || 
               (key == 'W' && !keys['W']) || 
               (key == 'Z' && !keys['Z']) || 
               (key == 'X' && !keys['X']) ||
               (key == 'C' && !keys['C']) || 
               (key == 'Q' && !keys['Q']) || 
               (key == 'E' && !keys['E']) || key == ' ' && !keys[' '] );

    // Mark key
    keys[key] = true;

    if (ch)
        update_movement();
}
/* end of process key down */


/* called when a key is released */
void process_key_up( unsigned char key )
{
    if (key == VK_SPACE) fire_off();

    bool ch = key == VK_LEFT  || key == VK_RIGHT || 
              key == 'A' || key == 'D' || key == 'S' || 
              key == 'W' || key == 'Z' || key == 'X' ||
              key == 'C' || key == 'Q' || key == 'E';

    keys[key] = false;

    if (ch)
        update_movement();
}
/* end of process key up */



