aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/strategic_factory.rs
blob: aaa590c6c38fa48ecc64aa2e37871470494447c2 (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
//! The strategic factory panel, composed from the installed compound controls.

use crate::{
    game_font::{load_layout_font, BitmapFont},
    mission_pager::copy_sprite,
    ui_sprite::{atlas_region, load_ui_page, load_ui_sprites},
    warbot_card::{self, CardLine},
    RgbaImage,
};
use fparkan_vfs::Vfs;

pub const ORIGIN: [u32; 2] = [51, 0];
pub const EXTENT: [u32; 2] = [319, 480];
/// iron3d+0x98240 places the rotating preview at card origin + (185, 2).
pub const MODEL_RECT: [f32; 4] = [236.0, 23.0, 351.0, 138.0];
const ROW_TOP: u32 = 171;
const ROW_PITCH: u32 = 20;
const ROW_HEIGHT: u32 = 19;
pub const VISIBLE_PLANTS: usize = ((EXTENT[1] - ROW_TOP) / ROW_PITCH) as usize;
const BUILD: [u32; 4] = [263, 150, 313, 169];
const BATCH: [u32; 4] = [211, 150, 261, 169];
const CONSTRUCTOR: [u32; 4] = [5, 150, 55, 169];

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FactoryAction {
    Close,
    Project(usize),
    Build,
    Batch,
    Cancel,
    Designer,
    Plant(usize),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FactoryOrders {
    Ready,
    Unavailable,
    Single,
    Batch,
}

impl FactoryOrders {
    pub fn for_plant(batch: bool, active: bool, project_selected: bool, free_minds: u32) -> Self {
        if batch {
            Self::Batch
        } else if active {
            Self::Single
        } else if project_selected && free_minds > 0 {
            Self::Ready
        } else {
            Self::Unavailable
        }
    }
}

fn project_rect(index: usize) -> [u32; 4] {
    let left = 10 + index as u32 * 23;
    [left, 125, left + 19, 142]
}

pub fn hit_test(
    [x, y]: [f32; 2],
    plants: usize,
    projects: usize,
    orders: FactoryOrders,
    designer_available: bool,
) -> Option<FactoryAction> {
    let [x, y] = [x.round_ties_even() - ORIGIN[0] as f32, y.round_ties_even()];
    let hit = |[left, top, right, bottom]: [u32; 4]| {
        (left as f32..right as f32).contains(&x) && (top as f32..bottom as f32).contains(&y)
    };
    if hit([283, 0, 318, 19]) {
        return Some(FactoryAction::Close);
    }
    if hit(BUILD) {
        return match orders {
            FactoryOrders::Ready => Some(FactoryAction::Build),
            FactoryOrders::Single => Some(FactoryAction::Cancel),
            _ => None,
        };
    }
    if hit(BATCH) {
        return match orders {
            FactoryOrders::Ready => Some(FactoryAction::Batch),
            FactoryOrders::Batch => Some(FactoryAction::Cancel),
            _ => None,
        };
    }
    if designer_available && hit(CONSTRUCTOR) {
        return Some(FactoryAction::Designer);
    }
    if let Some(index) = (0..projects).find(|&index| hit(project_rect(index))) {
        return Some(FactoryAction::Project(index));
    }
    (0..plants.min(VISIBLE_PLANTS))
        .find(|&index| {
            let top = ROW_TOP + index as u32 * ROW_PITCH;
            hit([0, top, 318, top + ROW_HEIGHT])
        })
        .map(FactoryAction::Plant)
}

pub struct FactoryRow<'a> {
    pub name: &'a str,
    pub selected: bool,
}

pub struct FactoryPicture<'a> {
    pub rows: &'a [FactoryRow<'a>],
    pub project_name: Option<&'a str>,
    pub stats: &'a [Option<CardLine<'a>>],
    pub projects: usize,
    pub selected_project: Option<usize>,
    pub progress: Option<u8>,
    pub free_minds: u32,
    pub orders: FactoryOrders,
    pub designer_available: bool,
}

pub struct FactoryImages {
    font: BitmapFont,
    base: RgbaImage,
    row_cap: [RgbaImage; 2],
    row_body: RgbaImage,
    ray: RgbaImage,
    icons: [RgbaImage; 7],
    batch_icons: [RgbaImage; 2],
}

impl FactoryImages {
    pub fn load(vfs: &dyn Vfs, title: &str) -> Result<Self, String> {
        let [title_cap, body, close, row_off, row_on, ray, tl, tr, br, bl, h, v] = load_ui_sprites(
            vfs,
            "ui/compaund.cfg",
            [
                "ccres_ending_text",
                "ccres_body_text",
                "ccres_exit_button_pressed",
                "ccres_lamp_text_ending_off",
                "ccres_lamp_text_ending_normal",
                "ccres_ray_body",
                "ccres_frame_corner_1",
                "ccres_frame_corner_2",
                "ccres_frame_corner_3",
                "ccres_frame_corner_4",
                "ccres_frame_edge_h",
                "ccres_frame_edge_v",
            ],
        )?;
        let [exit, project, project_frame, build, stop, constructor, free, batch, stop_batch] =
            load_ui_sprites(
                vfs,
                "ui/hq.cfg",
                [
                    "exit_icon",
                    "project_icon",
                    "short_button_frame_off",
                    "build_icon",
                    "stop_build_icon",
                    "constructor_icon",
                    "brain_icon",
                    "batch_build_icon",
                    "batch_stop_build_icon",
                ],
            )?;
        let atlas = load_ui_page(vfs, "ui_menu")?;
        let plant =
            atlas_region(&atlas, [129, 94, 15, 15]).ok_or("factory glyph outside ui_menu")?;
        let font = load_layout_font(vfs)?;
        let mut base = RgbaImage {
            width: EXTENT[0],
            height: EXTENT[1],
            rgba8: vec![0; (EXTENT[0] * EXTENT[1] * 4) as usize],
        };
        let green = RgbaImage {
            width: 1,
            height: 1,
            rgba8: vec![0, 128, 0, 128],
        };
        for (sprite, rect) in [
            (&title_cap, [0, 0, 6, 19]),
            (&body, [5, 0, 284, 19]),
            (&close, [283, 0, 318, 19]),
            (&exit, [298, 3, 311, 16]),
            (&green, [5, 26, 313, 142]),
            (&h, [8, 21, 310, 26]),
            (&h, [8, 142, 310, 147]),
            (&v, [0, 29, 5, 139]),
            (&v, [313, 29, 318, 139]),
            (&tl, [0, 21, 8, 29]),
            (&tr, [310, 21, 318, 29]),
            (&br, [310, 139, 318, 147]),
            (&bl, [0, 139, 8, 147]),
        ] {
            stamp(&mut base, sprite, rect, [255; 3]);
        }
        text(&font, &mut base, title, [5, 0, 283, 19], [255; 3])?;
        Ok(Self {
            font,
            base,
            row_cap: [row_off, row_on],
            row_body: body,
            ray,
            icons: [
                project,
                project_frame,
                plant,
                build,
                stop,
                constructor,
                free,
            ],
            batch_icons: [batch, stop_batch],
        })
    }

    pub fn paint(&self, picture: FactoryPicture<'_>) -> Result<RgbaImage, String> {
        let mut image = self.base.clone();
        let [project, frame, plant, build, stop, constructor, free] = &self.icons;
        if let Some(name) = picture.project_name {
            let width = u32::from(self.font.line_metrics(name)?[0]).min(170);
            text(
                &self.font,
                &mut image,
                name,
                [10, 27, 10 + width, 43],
                [255, 255, 0],
            )?;
            warbot_card::paint_at(&self.font, &mut image, picture.stats, [10, 144, 147], 43)?;
        }
        for index in 0..picture.projects {
            let rect = project_rect(index);
            stamp(
                &mut image,
                frame,
                [rect[0] - 2, rect[1] - 2, rect[2] + 2, rect[3] + 2],
                [255; 3],
            );
            let tint = if picture.selected_project == Some(index) {
                [255; 3]
            } else {
                [128, 255, 128]
            };
            stamp(&mut image, project, rect, tint);
        }
        stamp(&mut image, free, [274, 127, 289, 142], [128, 255, 128]);
        text(
            &self.font,
            &mut image,
            &picture.free_minds.to_string(),
            [289, 125, 306, 144],
            [128, 255, 128],
        )?;
        let progress_rect = [55, 150, BATCH[0], 169];
        stamp(&mut image, &self.row_body, progress_rect, [255; 3]);
        if let Some(progress) = picture.progress {
            text(
                &self.font,
                &mut image,
                &format!("{progress}%"),
                progress_rect,
                [255; 3],
            )?;
        }
        let busy = matches!(picture.orders, FactoryOrders::Single | FactoryOrders::Batch);
        for (rect, icon, enabled) in [
            (CONSTRUCTOR, constructor, picture.designer_available),
            (
                BATCH,
                &self.batch_icons[usize::from(busy)],
                matches!(picture.orders, FactoryOrders::Ready | FactoryOrders::Batch),
            ),
            (
                BUILD,
                if busy { stop } else { build },
                matches!(picture.orders, FactoryOrders::Ready | FactoryOrders::Single),
            ),
        ] {
            let tint = if enabled { [255; 3] } else { [104; 3] };
            let [x, y, _, _] = rect;
            stamp(&mut image, &self.row_body, rect, [255; 3]);
            stamp(&mut image, icon, [x + 10, y + 2, x + 40, y + 17], tint);
        }
        for (index, row) in picture.rows.iter().take(VISIBLE_PLANTS).enumerate() {
            let y = ROW_TOP + index as u32 * ROW_PITCH;
            let bottom = y + ROW_HEIGHT;
            stamp(
                &mut image,
                &self.row_cap[usize::from(row.selected)],
                [0, y, 11, bottom],
                [255; 3],
            );
            stamp(&mut image, &self.row_body, [10, y, 38, bottom], [255; 3]);
            stamp(&mut image, plant, [13, y + 2, 28, y + 17], [255, 0, 0]);
            stamp(&mut image, &self.ray, [38, y, 318, bottom], [255; 3]);
            text(
                &self.font,
                &mut image,
                row.name,
                [38, y, 318, bottom],
                if row.selected { [255; 3] } else { [128; 3] },
            )?;
        }
        Ok(image)
    }
}

fn stamp(image: &mut RgbaImage, sprite: &RgbaImage, rect: [u32; 4], tint: [u8; 3]) {
    copy_sprite(image, sprite, rect, [false; 2], tint);
}

fn text(
    font: &BitmapFont,
    image: &mut RgbaImage,
    label: &str,
    rect: [u32; 4],
    tint: [u8; 3],
) -> Result<(), String> {
    let [left, top, right, bottom] = rect;
    let glyphs = font.centered_line(label, (right - left) as u16, (bottom - top) as u16)?;
    stamp(image, &glyphs, rect, tint);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn factory_actions_follow_project_and_row_extents() {
        use FactoryOrders::{Batch, Ready, Single, Unavailable};

        assert_eq!(
            hit_test([95.0, 132.0], 1, 2, Ready, true),
            Some(FactoryAction::Project(1))
        );
        assert_eq!(hit_test([95.0, 132.0], 1, 1, Ready, true), None);
        assert_eq!(
            hit_test([340.0, 160.0], 1, 2, Ready, true),
            Some(FactoryAction::Build)
        );
        assert_eq!(hit_test([340.0, 160.0], 1, 2, Unavailable, true), None);
        assert_eq!(FactoryOrders::for_plant(false, true, false, 0), Single);
        assert_eq!(
            hit_test([340.0, 160.0], 1, 0, Single, true),
            Some(FactoryAction::Cancel)
        );
        assert_eq!(
            hit_test([200.0, 172.0], 1, 2, Ready, true),
            Some(FactoryAction::Plant(0))
        );
        assert_eq!(hit_test([80.0, 160.0], 1, 0, Unavailable, false), None);
        assert_eq!(
            hit_test([80.0, 160.0], 1, 0, Unavailable, true),
            Some(FactoryAction::Designer)
        );
        assert_eq!(FactoryOrders::for_plant(true, false, true, 0), Batch);
        assert_eq!(hit_test([340.0, 160.0], 1, 2, Batch, true), None);
        assert_eq!(hit_test([289.0, 160.0], 1, 2, Single, true), None);
        assert_eq!(
            hit_test([289.0, 160.0], 1, 2, Ready, true),
            Some(FactoryAction::Batch)
        );
        assert_eq!(
            hit_test([289.0, 160.0], 1, 2, Batch, true),
            Some(FactoryAction::Cancel)
        );
    }
}