ascii-agents 0.3.0

Terminal pixel-art office for AI coding agents
Documentation
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Y-sorted drawable enum (painter's algorithm).
//!
//! Top-down depth: every mid-ground entity carries an `anchor_y` = the
//! y-pixel row where it touches the floor (front-facing bottom edge for
//! items with thickness). Drawables sort ascending by `anchor_y` and
//! paint in order. Larger `anchor_y` = closer to camera = paints last
//! (on top). Solves the classic "character standing south of a desk
//! should appear in front of it" problem without per-pair special cases.
//!
//! What stays OUTSIDE the sort:
//!   - Background (floor / walls / lighting / corridor / room walls /
//!     entry mat / clock / shadows). All depth-independent.
//!   - Per-character attached effects (chair-behind, sleep_z,
//!     waiting_bubble, walking dust, coffee steam, screen glow)
//!     paint AS PART of their parent `Drawable` — they ride along with
//!     the entity in z-order, not as a global foreground pass.

use std::time::SystemTime;

use ascii_agents_core::sprite::blit::blit_frame;
use ascii_agents_core::sprite::format::Pack;
use ascii_agents_core::sprite::{Rgb, RgbBuffer};
use ascii_agents_core::AgentSlot;

use super::effects::{
    paint_coffee_steam, paint_pet_hearts, paint_screen_glow, paint_sleep_z, paint_thinking_dots,
    paint_waiting_bubble, paint_walking_dust,
};
use super::furniture::{
    paint_area_rug, paint_coffee_table, paint_pantry_chair, paint_pantry_table, paint_side_table,
};
use super::paint_character_at;
use crate::tui::frame_cache::FrameCache;
use crate::tui::layout::{Layout, Point, DESK_H, DESK_W};

pub(super) struct Drawable<'a> {
    pub(super) anchor_y: u16,
    pub(super) kind: DrawableKind<'a>,
}

pub(super) enum DrawableKind<'a> {
    /// Whole cubicle as one z-unit: divider + filing cabinet (every
    /// other desk) + desk sprite + trash bin + screen-glow if the
    /// occupant is Active. Bundled so the cubicle paints atomically at
    /// the desk's bottom-edge row.
    DeskCubicle {
        desk: Point,
        is_last_col: bool,
        has_cabinet: bool,
        screen_glow: Option<Rgb>,
        session_age_secs: u64,
        has_coffee: bool,
        coffee_steam: bool,
    },
    Character {
        agent: &'a AgentSlot,
        anim_name: &'static str,
        frame_idx: usize,
        anchor: Point,
        flip_x: bool,
        /// Tool-derived monitor glow color. `Some(color)` tints the
        /// skin toward that color so scanning a row of typing agents
        /// shows tool type at a glance. `None` for non-desk poses.
        glow_tint: Option<Rgb>,
        sleep_z_seed: Option<u64>,
        waiting_bubble: bool,
        thinking_dots: bool,
        walking_dust_frame: Option<usize>,
    },
    /// Lounge couch (mirror_vertical'd — back at bottom, seat at top).
    WaypointCouch {
        pos: Point,
    },
    /// Pantry counter (with coffee steam attached so steam rides above
    /// the counter in z-order). `use_large` picks the detailed 32×10
    /// kitchen sprite vs. the 20×8 compact fallback — derived from
    /// `layout.pantry_counter_size` at queue time.
    WaypointPantry {
        pos: Point,
        use_large: bool,
    },
    MeetingSofa {
        pos: Point,
        mirrored: bool,
    },
    MeetingTable {
        pos: Point,
    },
    /// Area rug — warm patterned rectangle that anchors a seating
    /// arrangement visually. Used for the meeting room (large) and
    /// the lounge (smaller). Painted BEFORE the furniture in z-order
    /// (anchor_y at top of rug) so chairs / couches sit on top.
    AreaRug {
        pos: Point,
        width: u16,
        height: u16,
    },
    /// Lounge side table (5×3 wood + magazine) next to the viewing
    /// couch. Centred at `pos`.
    LoungeSideTable {
        pos: Point,
    },
    PantryTable {
        pos: Point,
    },
    PantryChair {
        pos: Point,
    },
    Plant {
        kind: crate::tui::layout::PlantKind,
        pos: Point,
    },
    /// Aisle decor item between desk pods (plant / whiteboard / TV /
    /// phone booth / standing desk). All are obstacles in the
    /// walkable mask; phone booth + standing desk additionally exist
    /// as waypoints so agents can wander to them.
    PodDecorItem {
        kind: crate::tui::layout::PodDecor,
        pos: Point,
    },
    FloorLamp {
        pos: Point,
    },
    Door {
        pos: Point,
        /// Frame index into the `door` animation. 0 = closed,
        /// 1 = half-open, 2 = fully open. Computed stateless from
        /// agents' entry/exit windows in the orchestrator so the door
        /// transitions smoothly closed → half → open at the start of a
        /// transit and back open → half → closed at the end.
        frame_idx: usize,
    },
    WallDecor {
        kind: crate::tui::layout::WallDecor,
        pos: Point,
    },
    VendingMachine {
        pos: Point,
    },
    Printer {
        pos: Point,
    },
    Cat {
        pos: Point,
        flip: bool,
        anim_name: &'static str,
        frame_idx: usize,
        pet_elapsed_ms: Option<u64>,
    },
}

/// Cat roaming the whole office like a real cat. Each 40s cycle picks
/// a destination from all available spots (desks, pantry, meeting sofas,
/// lounge couch, corridor), walks there from the previous spot, then
/// sits or sleeps until the next cycle.
pub(super) fn cat_position(
    layout: &Layout,
    pack: &Pack,
    now: SystemTime,
    idle_desk_indices: &[usize],
    all_idle: bool,
    cat_seed: u64,
) -> Option<(Point, bool, &'static str, usize)> {
    pack.animation("cat_walk")?;
    layout.corridor?;

    let elapsed_ms = now
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0);

    const CYCLE_MS: u64 = 40_000;
    let cycle_n = (elapsed_ms / CYCLE_MS).wrapping_add(cat_seed);
    let frac = (elapsed_ms % CYCLE_MS) as f32 / CYCLE_MS as f32;

    // Gather all interesting spots the cat can visit.
    let mut spots: Vec<(Point, bool)> = Vec::new();
    for (i, desk) in layout.home_desks.iter().enumerate() {
        spots.push((
            Point {
                x: desk.x + DESK_W + 1,
                y: desk.y + DESK_H + 2,
            },
            idle_desk_indices.contains(&i),
        ));
    }
    if let Some(wp) = layout
        .waypoints
        .iter()
        .find(|w| matches!(w.kind, crate::tui::layout::WaypointKind::Pantry))
    {
        spots.push((
            Point {
                x: wp.pos.x + 4,
                y: wp.pos.y + 6,
            },
            false,
        ));
    }
    for sofa in &layout.meeting_sofas {
        spots.push((
            Point {
                x: sofa.x + 4,
                y: sofa.y + 4,
            },
            false,
        ));
    }
    if let Some(wp) = layout
        .waypoints
        .iter()
        .find(|w| matches!(w.kind, crate::tui::layout::WaypointKind::Couch))
    {
        spots.push((
            Point {
                x: wp.pos.x + 4,
                y: wp.pos.y + 6,
            },
            false,
        ));
    }
    if let Some(corridor) = layout.corridor {
        spots.push((
            Point {
                x: corridor.x + corridor.width / 2,
                y: corridor.y + corridor.height / 2,
            },
            false,
        ));
    }
    if spots.is_empty() {
        return None;
    }

    let pick = |n: u64| -> (Point, bool) {
        let h = n.wrapping_mul(0x9e37_79b9_7f4a_7c15) as usize;
        spots[h % spots.len()]
    };
    let (dest, is_idle_spot) = pick(cycle_n);
    let (prev, _) = pick(cycle_n.wrapping_sub(1));

    let frame_idx = (elapsed_ms / 220) as usize % 2;

    if frac < 0.35 {
        let t = frac / 0.35;
        let x = prev.x as f32 + (dest.x as f32 - prev.x as f32) * t;
        let y = prev.y as f32 + (dest.y as f32 - prev.y as f32) * t;
        let flip = dest.x < prev.x;
        return Some((
            Point {
                x: x as u16,
                y: y as u16,
            },
            flip,
            "cat_walk",
            frame_idx,
        ));
    }

    let anim = if all_idle || is_idle_spot {
        "cat_sleep"
    } else {
        "cat_sit"
    };
    Some((dest, false, anim, 0))
}

/// Dispatch one Drawable's paint. Effects attached to characters paint
/// inline so they ride along with the character in z-order.
pub(super) fn paint_drawable(
    d: &Drawable<'_>,
    buf: &mut RgbBuffer,
    pack: &Pack,
    cache: &mut FrameCache,
    now: SystemTime,
    theme: &crate::tui::theme::Theme,
) {
    match &d.kind {
        DrawableKind::DeskCubicle {
            desk,
            is_last_col,
            has_cabinet,
            screen_glow,
            session_age_secs,
            has_coffee,
            coffee_steam,
        } => {
            let divider = theme.office.cubicle_divider;
            if !is_last_col {
                let div_x = desk.x + DESK_W + 3;
                for dy in 0..(DESK_H + 1) {
                    let py = desk.y.saturating_sub(1) + dy;
                    if div_x < buf.width && py < buf.height {
                        buf.put(div_x, py, divider);
                    }
                }
            }
            if *has_cabinet {
                if let Some(cab) = pack
                    .animation("filing_cabinet")
                    .and_then(|a| a.frames.first())
                {
                    let cab_x = desk.x.saturating_sub(cab.width + 1);
                    let cab_y = desk.y;
                    if cab_y + cab.height <= buf.height {
                        blit_frame(cab, cab_x, cab_y, buf);
                    }
                }
            }
            if let Some(frame) = pack.animation("desk").and_then(|a| a.frames.first()) {
                blit_frame(frame, desk.x, desk.y, buf);
            }
            if let Some(bin) = pack.animation("trash_bin").and_then(|a| a.frames.first()) {
                let bin_x = desk.x + DESK_W;
                let bin_y = desk.y + 4;
                if bin_x + bin.width <= buf.width && bin_y + bin.height <= buf.height {
                    blit_frame(bin, bin_x, bin_y, buf);
                }
            }
            paint_desk_personalization(
                buf,
                *desk,
                *session_age_secs,
                *has_coffee,
                *coffee_steam,
                now,
                theme,
            );
            if let Some(tint) = screen_glow {
                paint_screen_glow(buf, desk.x, desk.y, now, *tint, theme);
            }
        }
        DrawableKind::Character {
            agent,
            anim_name,
            frame_idx,
            anchor,
            flip_x,
            glow_tint,
            sleep_z_seed,
            waiting_bubble,
            thinking_dots,
            walking_dust_frame,
        } => {
            if let Some(dust_frame) = walking_dust_frame {
                paint_walking_dust(buf, *anchor, *dust_frame, theme);
            }
            paint_character_at(
                buf, anim_name, *frame_idx, *anchor, agent, pack, *flip_x, *glow_tint, cache,
            );
            if let Some(seed) = sleep_z_seed {
                paint_sleep_z(buf, *anchor, now, *seed, theme);
            }
            if *waiting_bubble {
                paint_waiting_bubble(buf, *anchor, theme);
            }
            if *thinking_dots {
                paint_thinking_dots(buf, *anchor, now, theme);
            }
        }
        DrawableKind::WaypointCouch { pos } => {
            // Lounge couch reuses the meeting_sofa sprite (16×7) so
            // both seating areas have the same readable 3-cushion
            // silhouette. Flipped vertically so the back faces NORTH
            // (toward the windows the viewer is looking at).
            if let Some(f) = pack
                .animation("meeting_sofa")
                .and_then(|a| a.frames.first())
            {
                let cx = pos.x.saturating_sub(f.width / 2);
                let cy = pos.y.saturating_sub(f.height / 2);
                let flipped = f.mirror_vertical();
                blit_frame(&flipped, cx, cy, buf);
            }
        }
        DrawableKind::WaypointPantry { pos, use_large } => {
            // Pick the big detailed kitchen sprite when the pantry is
            // large enough; fall back to the compact 20×8 layout on
            // narrow terminals.
            let anim_name = if *use_large { "pantry" } else { "pantry_small" };
            if let Some(f) = pack.animation(anim_name).and_then(|a| a.frames.first()) {
                let cx = pos.x.saturating_sub(f.width / 2);
                let cy = pos.y.saturating_sub(f.height / 2);
                blit_frame(f, cx, cy, buf);
            }
            // Large sprite: coffee machine at sprite cols 11-18 of
            // a 32-wide sprite → world x ≈ pos.x - 2.
            // Small sprite: coffee at sprite cols 9-11 of a 20-wide
            // sprite → world x = pos.x + 1.
            let steam_dx: i16 = if *use_large { -2 } else { 1 };
            let steam_x = (pos.x as i32 + steam_dx as i32).max(0) as u16;
            paint_coffee_steam(
                buf,
                Point {
                    x: steam_x,
                    y: pos.y.saturating_sub(2),
                },
                now,
                theme,
            );
        }
        DrawableKind::MeetingSofa { pos, mirrored } => {
            if let Some(f) = pack
                .animation("meeting_sofa")
                .and_then(|a| a.frames.first())
            {
                let sx = pos.x.saturating_sub(f.width / 2);
                let sy = pos.y.saturating_sub(f.height / 2);
                if *mirrored {
                    let flipped = f.mirror_vertical();
                    blit_frame(&flipped, sx, sy, buf);
                } else {
                    blit_frame(f, sx, sy, buf);
                }
            }
        }
        DrawableKind::MeetingTable { pos } => {
            paint_coffee_table(buf, pos.x, pos.y, 11, 5, theme);
        }
        DrawableKind::AreaRug { pos, width, height } => {
            paint_area_rug(buf, pos.x, pos.y, *width, *height, theme);
        }
        DrawableKind::LoungeSideTable { pos } => {
            paint_side_table(buf, pos.x, pos.y, theme);
        }
        DrawableKind::PantryTable { pos } => {
            paint_pantry_table(buf, pos.x, pos.y, theme);
        }
        DrawableKind::PantryChair { pos } => {
            paint_pantry_chair(buf, pos.x, pos.y, theme);
        }
        DrawableKind::Plant { kind, pos } => {
            use crate::tui::layout::PlantKind;
            let anim_name = match kind {
                PlantKind::Ficus => "plant",
                PlantKind::Tall => "plant_tall",
                PlantKind::Flower => "plant_flower",
                PlantKind::Succulent => "plant_succulent",
            };
            if let Some(f) = pack.animation(anim_name).and_then(|a| a.frames.first()) {
                let px = pos.x.saturating_sub(f.width / 2);
                let py = pos.y.saturating_sub(f.height / 2);
                blit_frame(f, px, py, buf);
            }
        }
        DrawableKind::PodDecorItem { kind, pos } => {
            use crate::tui::layout::PodDecor;
            let anim_name = match kind {
                PodDecor::PlantTall => "plant_tall",
                PodDecor::Whiteboard => "whiteboard",
                PodDecor::Tv => "tv_stand",
                PodDecor::PhoneBooth => "phone_booth",
                PodDecor::StandingDesk => "standing_desk",
            };
            if let Some(f) = pack.animation(anim_name).and_then(|a| a.frames.first()) {
                let px = pos.x.saturating_sub(f.width / 2);
                let py = pos.y.saturating_sub(f.height / 2);
                blit_frame(f, px, py, buf);
            }
        }
        DrawableKind::FloorLamp { pos } => {
            if let Some(f) = pack.animation("floor_lamp").and_then(|a| a.frames.first()) {
                let px = pos.x.saturating_sub(f.width / 2);
                let py = pos.y.saturating_sub(f.height / 2);
                blit_frame(f, px, py, buf);
            }
        }
        DrawableKind::Door { pos, frame_idx } => {
            if let Some(f) = pack
                .animation("door")
                .and_then(|a| a.frames.get(*frame_idx).or_else(|| a.frames.first()))
            {
                blit_frame(f, pos.x, pos.y, buf);
            }
        }
        DrawableKind::WallDecor { kind, pos } => {
            use crate::tui::layout::WallDecor;
            let anim_name = match kind {
                WallDecor::Bookshelf => "bookshelf",
                WallDecor::BulletinBoard => "bulletin_board",
                WallDecor::ExitSign => "exit_sign",
                WallDecor::Whiteboard => "whiteboard",
                WallDecor::MeetingScreen => "meeting_screen",
            };
            if let Some(f) = pack.animation(anim_name).and_then(|a| a.frames.first()) {
                blit_frame(f, pos.x, pos.y, buf);
            }
        }
        DrawableKind::VendingMachine { pos } => {
            let body = Rgb(50, 55, 65);
            let panel = Rgb(180, 60, 60);
            let drinks = [
                Rgb(220, 50, 50),
                Rgb(50, 160, 50),
                Rgb(50, 80, 200),
                Rgb(220, 180, 40),
            ];
            let vx = pos.x.saturating_sub(2);
            let vy = pos.y.saturating_sub(3);
            for dy in 0..6u16 {
                for dx in 0..4u16 {
                    let px = vx + dx;
                    let py = vy + dy;
                    if px < buf.width && py < buf.height {
                        let color = if dy == 0 {
                            panel
                        } else if (1..=3).contains(&dy) && (1..=2).contains(&dx) {
                            let idx = ((dy - 1) * 2 + (dx - 1)) as usize;
                            if idx < drinks.len() {
                                drinks[idx]
                            } else {
                                body
                            }
                        } else if dy == 4 && dx == 2 {
                            Rgb(180, 170, 100)
                        } else if dy == 5 {
                            Rgb(40, 42, 48)
                        } else {
                            body
                        };
                        buf.put(px, py, color);
                    }
                }
            }
        }
        DrawableKind::Printer { pos } => {
            let body_white = Rgb(220, 220, 225);
            let top_dark = Rgb(60, 60, 68);
            let glass = Rgb(130, 180, 200);
            let paper = Rgb(245, 245, 240);
            let tray = Rgb(180, 180, 185);
            let px0 = pos.x.saturating_sub(2);
            let py0 = pos.y.saturating_sub(2);
            for dy in 0..4u16 {
                for dx in 0..5u16 {
                    let px = px0 + dx;
                    let py = py0 + dy;
                    if px < buf.width && py < buf.height {
                        let color = if dy == 0 {
                            if (1..=3).contains(&dx) {
                                glass
                            } else {
                                top_dark
                            }
                        } else if dy == 3 {
                            if (1..=3).contains(&dx) {
                                paper
                            } else {
                                tray
                            }
                        } else if dx == 0 || dx == 4 {
                            tray
                        } else {
                            body_white
                        };
                        buf.put(px, py, color);
                    }
                }
            }
        }
        DrawableKind::Cat {
            pos,
            flip,
            anim_name,
            frame_idx,
            pet_elapsed_ms,
        } => {
            let Some(anim) = pack.animation(anim_name) else {
                return;
            };
            let Some(frame) = anim.frames.get(*frame_idx).or(anim.frames.first()) else {
                return;
            };
            let final_frame = if *flip {
                frame.mirror_horizontal()
            } else {
                frame.clone()
            };
            let px = pos.x.saturating_sub(final_frame.width / 2);
            let py = pos.y.saturating_sub(final_frame.height / 2);
            blit_frame(&final_frame, px, py, buf);
            if let Some(elapsed) = pet_elapsed_ms {
                paint_pet_hearts(buf, *pos, *elapsed, theme);
            } else if *anim_name == "cat_sleep" {
                paint_sleep_z(buf, *pos, now, 0xCAFE, theme);
            }
        }
    }
}

fn paint_desk_personalization(
    buf: &mut RgbBuffer,
    desk: Point,
    age_secs: u64,
    has_coffee: bool,
    coffee_steam: bool,
    now: SystemTime,
    theme: &crate::tui::theme::Theme,
) {
    if age_secs == 0 && !has_coffee {
        return;
    }
    let put = |buf: &mut RgbBuffer, x: u16, y: u16, c: Rgb| {
        if x < buf.width && y < buf.height {
            buf.put(x, y, c);
        }
    };
    if has_coffee {
        let cx = desk.x + 2;
        let cy = desk.y + 2;
        put(buf, cx, cy, theme.furniture.coffee_cup);
        put(buf, cx + 1, cy, theme.furniture.coffee_cup);
        put(buf, cx, cy + 1, theme.furniture.coffee_cup_shadow);
        put(buf, cx + 1, cy + 1, theme.furniture.coffee_cup_shadow);
        if coffee_steam {
            paint_coffee_steam(buf, Point { x: cx, y: cy }, now, theme);
        }
    }
    if age_secs >= 1800 {
        let px = desk.x + DESK_W - 2;
        let py = desk.y + 1;
        put(buf, px, py, theme.furniture.desk_plant_light);
        put(buf, px + 1, py, theme.furniture.desk_plant_dark);
        put(buf, px, py + 1, theme.furniture.desk_plant_light);
        put(buf, px + 1, py + 1, theme.furniture.desk_plant_light);
        put(buf, px, py + 2, theme.furniture.desk_plant_pot);
        put(buf, px + 1, py + 2, theme.furniture.desk_plant_pot);
    }
    if age_secs >= 3600 {
        let fx = desk.x + 1;
        let fy = desk.y;
        put(buf, fx, fy, theme.furniture.photo_frame);
        put(buf, fx + 1, fy, theme.furniture.photo_frame);
        put(buf, fx, fy + 1, theme.furniture.photo_bg);
        put(buf, fx + 1, fy + 1, theme.furniture.photo_bg);
    }
}