import sys
import pyMeow as pm

from lib.keyboard import KeyboardManager
from modules.esp import Entity, init_colors as init_esp_colors, process_entities
from modules.aimbot import AimBot

from modules.armor import ArmorHack
from modules.health import HealthHack
from modules.grenade import GrenadeHack
from modules.mtp57 import MTP57Hack
from modules.mk77 import MK77Hack

class Pointer:
    local_player = 0x17E0A8
    entity_list = 0x18AC04
    player_count = 0x18AC0C
    view_matrix = 0x17DFD0

proc = None
base = None

hacks = {
    "aimbot": True,
    "esp": True,
    "armor": True,
    "health": True,
    "grenade": True,
    "mtp57": True,
    "mk77": True
}

def init():
    global proc, base
    try:
        proc = pm.open_process("ac_client.exe")
        base = pm.get_module(proc, "ac_client.exe")["base"]
        print("Process and base initialized successfully.")
    except Exception as e:
        print(f"Error initializing process: {e}")
        sys.exit(1)

    print(f"proc : {proc}")
    print(f"base : {base}")
    
    KeyboardManager.setup()

    pm.overlay_init(target="AssaultCube", fps=144, trackTarget=True)
    init_esp_colors()

def main():

    while pm.overlay_loop():
        pm.begin_drawing()
        
        try:
            player_count = pm.r_int(proc, base + Pointer.player_count)
            local_player_addr = pm.r_int(proc, base + Pointer.local_player)
            my_team = pm.r_int(proc, local_player_addr + 0x30C)  # team offset

            if hacks["armor"] == True:
                ArmorHack.apply(proc, local_player_addr)
            if hacks["health"] == True:
                HealthHack.apply(proc, local_player_addr)
            if hacks["grenade"] == True:
                GrenadeHack.apply(proc, local_player_addr)
            if hacks["mtp57"] == True:
                MTP57Hack.apply(proc, local_player_addr)
            if hacks["mk77"] == True:
                MK77Hack.apply(proc, local_player_addr)

            valid_entities = []

            if player_count > 1:
                ent_list = pm.r_int(proc, base + Pointer.entity_list)
                ent_buffer = pm.r_ints(proc, ent_list, player_count)[1:]
                v_matrix = pm.r_floats(proc, base + Pointer.view_matrix, 16)
                
                valid_entities = process_entities(proc, ent_buffer, v_matrix, hacks.get("esp"))
            
            if hacks["aimbot"] == True:
                AimBot(proc, valid_entities, local_player_addr, my_team, KeyboardManager.is_left_shift_pressed())

        except Exception as e:
            pass
            
        pm.end_drawing()

if __name__ == "__main__":
    init()
    main()
