summaryrefslogtreecommitdiff
path: root/cs2pov/automation.py
blob: ee8347dc5aaa95d22a738e19efb91bfb6bd51c44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
"""CS2 input automation using xdotool - simple, non-threaded functions."""

import os
import re
import shutil
import subprocess
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Optional


def check_xdotool():
    """Check if xdotool is available."""
    if not shutil.which("xdotool"):
        raise RuntimeError("xdotool not found. Install with: emerge x11-misc/xdotool")


def find_cs2_window(display: str = ":0") -> Optional[str]:
    """Find CS2 window ID using xdotool.

    Args:
        display: X display where CS2 is running

    Returns:
        Window ID string, or None if not found
    """
    env = os.environ.copy()
    env["DISPLAY"] = display

    try:
        result = subprocess.run(
            ["xdotool", "search", "--class", "cs2"],
            capture_output=True,
            text=True,
            timeout=5,
            env=env
        )
        if result.returncode == 0 and result.stdout.strip():
            windows = result.stdout.strip().split('\n')
            # Return first window found
            if windows and windows[0]:
                return windows[0]
    except Exception:
        pass
    return None


def send_key(key: str, display: str = ":0", window_id: Optional[str] = None) -> bool:
    """Send a key press to CS2 window.

    Args:
        key: Key to send (e.g., "F5", "space", "Return")
        display: X display
        window_id: Optional window ID (will find if not provided)

    Returns:
        True if successful
    """
    if window_id is None:
        window_id = find_cs2_window(display)
    if not window_id:
        return False

    env = os.environ.copy()
    env["DISPLAY"] = display

    try:
        result = subprocess.run(
            ["xdotool", "key", "--window", window_id, key],
            capture_output=True,
            text=True,
            timeout=5,
            env=env
        )
        return result.returncode == 0
    except Exception:
        return False


def check_demo_ended(log_path: Path, last_position: int = 0) -> tuple[bool, int]:
    """Check if demo has ended by looking for marker in console.log.

    Args:
        log_path: Path to CS2 console.log
        last_position: File position to start reading from

    Returns:
        Tuple of (demo_ended: bool, new_position: int)
    """
    if not log_path.exists():
        return False, last_position

    demo_end_pattern = re.compile(r"CGameRules - paused on tick")

    try:
        with open(log_path, 'r', errors='ignore') as f:
            f.seek(last_position)
            content = f.read()
            new_position = f.tell()

            if demo_end_pattern.search(content):
                return True, new_position

            return False, new_position
    except Exception:
        return False, last_position


def check_demo_ended_tick_aware(
    log_path: Path,
    last_position: int,
    min_end_tick: int,
) -> tuple[bool, int]:
    """Check if demo has ended, filtering out pauses from navigation.

    In tick-nav mode, 'CGameRules - paused on tick X' lines appear from our
    own deliberate pauses (calibration, death handling). This function only
    considers it a real demo end if the tick is past min_end_tick.

    Args:
        log_path: Path to CS2 console.log
        last_position: File position to start reading from
        min_end_tick: Only consider ended if paused tick >= this value.
            Typically the last alive segment's end tick.

    Returns:
        Tuple of (demo_ended: bool, new_position: int)
    """
    if not log_path.exists():
        return False, last_position

    demo_end_pattern = re.compile(r"CGameRules - paused on tick (\d+)")

    try:
        with open(log_path, 'r', errors='ignore') as f:
            f.seek(last_position)
            content = f.read()
            new_position = f.tell()

            for match in demo_end_pattern.finditer(content):
                tick = int(match.group(1))
                if tick >= min_end_tick:
                    return True, new_position

            return False, new_position
    except Exception:
        return False, last_position


@dataclass
class DemoEndInfo:
    """Information about when the demo ended."""
    tick: int
    timestamp: float  # Unix timestamp from console.log


def parse_demo_end_info(log_path: Path) -> Optional[DemoEndInfo]:
    """Parse demo end information from console.log.

    Extracts the tick number and timestamp from:
    MM/DD HH:mm:ss CGameRules - paused on tick <tick>

    Args:
        log_path: Path to console.log file

    Returns:
        DemoEndInfo with tick and timestamp, or None if not found
    """
    from datetime import datetime

    if not log_path.exists():
        return None

    # Pattern: MM/DD HH:mm:ss CGameRules - paused on tick <number>
    demo_end_pattern = re.compile(
        r"^(\d{2}/\d{2} \d{2}:\d{2}:\d{2}).*CGameRules - paused on tick (\d+)"
    )

    try:
        with open(log_path, 'r', errors='ignore') as f:
            for line in f:
                match = demo_end_pattern.match(line)
                if match:
                    timestamp_str = match.group(1)
                    tick = int(match.group(2))

                    # Parse timestamp (MM/DD HH:mm:ss) - assume current year
                    parsed = datetime.strptime(timestamp_str, "%m/%d %H:%M:%S")
                    parsed = parsed.replace(year=datetime.now().year)

                    return DemoEndInfo(tick=tick, timestamp=parsed.timestamp())
    except Exception:
        pass

    return None


def wait_for_map_load(log_path: Path, timeout: float = 120, poll_interval: float = 0.5) -> bool:
    """Wait for map to finish loading by watching console.log.

    Looks for: [Client] Created physics for <map_name>

    Args:
        log_path: Path to CS2 console.log
        timeout: Maximum time to wait in seconds
        poll_interval: Time between checks

    Returns:
        True if map load detected, False if timeout
    """
    map_load_pattern = re.compile(r"\[Client\] Created physics for")
    start_t = time.time()
    last_position = 0

    while time.time() - start_t < timeout:
        if not log_path.exists():
            time.sleep(poll_interval)
            continue

        try:
            with open(log_path, 'r', errors='ignore') as f:
                f.seek(last_position)
                content = f.read()
                last_position = f.tell()

                if map_load_pattern.search(content):
                    return True
        except Exception:
            pass

        time.sleep(poll_interval)

    return False


def wait_for_demo_ready(
    log_path: Path,
    timeout: float = 180,
    poll_interval: float = 0.5
) -> bool:
    """Wait for demo to be ready by watching console.log.

    Looks for: [HostStateManager] Host activate: Playing Demo

    This indicates the demo has loaded and playback is starting.

    Args:
        log_path: Path to CS2 console.log
        timeout: Maximum time to wait in seconds
        poll_interval: Time between checks

    Returns:
        True if ready state detected, False if timeout
    """
    ready_pattern = re.compile(
        r"\[HostStateManager\] Host activate: Playing Demo"
    )
    start_t = time.time()
    last_position = 0

    while time.time() - start_t < timeout:
        if not log_path.exists():
            time.sleep(poll_interval)
            continue

        try:
            with open(log_path, 'r', errors='ignore') as f:
                f.seek(last_position)
                content = f.read()
                last_position = f.tell()

                if ready_pattern.search(content):
                    return True
        except Exception:
            pass

        time.sleep(poll_interval)

    return False


def wait_for_cs2_window(display: str = ":0", timeout: float = 120, poll_interval: float = 2.0) -> Optional[str]:
    """Wait for CS2 window to appear.

    Args:
        display: X display
        timeout: Maximum time to wait in seconds
        poll_interval: Time between checks

    Returns:
        Window ID when found, or None if timeout
    """
    start = time.time()
    while time.time() - start < timeout:
        window_id = find_cs2_window(display)
        if window_id:
            return window_id
        time.sleep(poll_interval)
    return None


# =============================================================================
# Tick-based navigation primitives
# =============================================================================

def send_console_command(command: str, display: str, window_id: str) -> bool:
    """Send a console command to CS2 by opening console, typing, and closing.

    Opens console (grave key), types the command, presses Return, closes console.
    Includes small delays between steps for reliability.

    Args:
        command: Console command to send (e.g. "demo_gototick 12345")
        display: X display string
        window_id: CS2 window ID

    Returns:
        True if all xdotool steps succeeded
    """
    env = os.environ.copy()
    env["DISPLAY"] = display

    steps = [
        # Open console
        (["xdotool", "key", "--window", window_id, "grave"], 0.1),
        # Type command
        (["xdotool", "type", "--window", window_id, "--clearmodifiers", command], 0.05),
        # Press enter
        (["xdotool", "key", "--window", window_id, "Return"], 0.1),
        # Close console
        (["xdotool", "key", "--window", window_id, "grave"], 0.0),
    ]

    for cmd, delay in steps:
        try:
            result = subprocess.run(cmd, capture_output=True, text=True, timeout=5, env=env)
            if result.returncode != 0:
                return False
        except Exception:
            return False
        if delay > 0:
            time.sleep(delay)

    return True


def read_paused_tick(
    console_log_path: Path,
    last_position: int,
    timeout: float = 5.0,
) -> tuple[Optional[int], int]:
    """After a demo_pause, poll console.log for the paused tick line.

    Looks for: CGameRules - paused on tick X

    Args:
        console_log_path: Path to CS2 console.log
        last_position: File position to start reading from
        timeout: Maximum time to wait

    Returns:
        (tick, new_position) or (None, position) on timeout
    """
    pattern = re.compile(r"CGameRules - paused on tick (\d+)")
    start = time.time()

    while time.time() - start < timeout:
        if not console_log_path.exists():
            time.sleep(0.1)
            continue

        try:
            with open(console_log_path, 'r', errors='ignore') as f:
                f.seek(last_position)
                content = f.read()
                new_position = f.tell()

                match = pattern.search(content)
                if match:
                    return int(match.group(1)), new_position

                last_position = new_position
        except Exception:
            pass

        time.sleep(0.1)

    return None, last_position


def calibrate_tick_offset(
    console_log_path: Path,
    display: str,
    window_id: str,
    log_position: int,
    calibration_tick: int = 0,
    verbose: bool = False,
) -> tuple[int, int]:
    """Calibrate the tick offset by sending demo_gototick and measuring drift.

    demo_gototick X doesn't land exactly at tick X — there's a consistent
    drift per demo. We measure it by:
    1. Send demo_gototick to a known tick
    2. Pause and read actual tick from console
    3. offset = actual_tick - requested_tick
    4. Resume playback

    All future goto calls subtract this offset: demo_gototick(target - offset).

    Args:
        console_log_path: Path to CS2 console.log
        display: X display string
        window_id: CS2 window ID
        log_position: Current position in console.log
        calibration_tick: Tick to goto for calibration (default 0)
        verbose: Print debug output

    Returns:
        (offset, new_log_position). offset = actual_tick - calibration_tick.
    """
    # Seek to the calibration tick
    send_console_command(f"demo_gototick {calibration_tick}", display, window_id)
    time.sleep(2.0)

    # Pause demo via F7
    send_key("F7", display, window_id)
    time.sleep(1.0)

    # Read the paused tick from console
    actual_tick, log_position = read_paused_tick(console_log_path, log_position, timeout=5.0)

    # Let CS2 settle before unpausing
    time.sleep(0.5)

    # Resume via F6 (demo_resume — idempotent)
    send_key("F6", display, window_id)

    if actual_tick is not None:
        offset = actual_tick - calibration_tick
        if verbose:
            print(f"    Tick calibration: goto {calibration_tick} → landed at {actual_tick}, offset={offset}")
        return offset, log_position
    else:
        if verbose:
            print(f"    Tick calibration failed, using offset=0")
        return 0, log_position