Skip to content

Commit

Permalink
fix: Star Rail scroll problem
Browse files Browse the repository at this point in the history
  • Loading branch information
wormtql committed May 5, 2024
1 parent f374784 commit 55d781c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
15 changes: 9 additions & 6 deletions yas-starrail/src/scanner/relic_scanner/relic_scanner.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use image::{RgbImage, GenericImageView};
use log::{error, info, warn};
use std::{ops::{Coroutine, CoroutineState}, pin::Pin, rc::Rc, cell::RefCell, sync::{mpsc::{Receiver, Sender, self}, Arc}, thread::JoinHandle, os::windows::thread, collections::HashSet, time::SystemTime};
use std::{cell::RefCell, ops::{Coroutine, CoroutineState}, pin::Pin, rc::Rc, sync::{mpsc::{self, Sender}}, time::SystemTime};

use super::relic_scanner_config::StarRailRelicScannerConfig;
use anyhow::Result;
use clap::FromArgMatches;
use image::RgbImage;
use log::{error, info};

use yas::capture::{Capturer, GenericCapturer};
use yas::game_info::GameInfo;
use yas::ocr::{ImageToText, yas_ocr_model};
use yas::positioning::Pos;
use yas::utils::color_distance;
use yas::window_info::{FromWindowInfoRepository, WindowInfoRepository};

use crate::scanner::relic_scanner::match_colors::{MATCH_COLORS, MatchColors};
use crate::scanner::relic_scanner::message_items::SendItem;
use crate::scanner::relic_scanner::relic_scanner_window_info::RelicScannerWindowInfo;
use crate::scanner::relic_scanner::relic_scanner_worker::RelicScannerWorker;
use crate::scanner::relic_scanner::scan_result::StarRailRelicScanResult;
use crate::scanner_controller::repository_layout::{ReturnResult, StarRailRepositoryScanController, StarRailRepositoryScannerLogicConfig};

use super::relic_scanner_config::StarRailRelicScannerConfig;

pub struct StarRailRelicScanner {
scanner_config: StarRailRelicScannerConfig,
Expand Down Expand Up @@ -168,9 +170,9 @@ impl StarRailRelicScanner {

pub fn get_item_count(&self) -> Result<i32> {
let count = self.scanner_config.number;
let item_name = "遗器";
let item_name = "遗器数量";

let max_count = 1500;
let max_count = 2000;
if count > 0 {
return Ok(max_count.min(count));
}
Expand All @@ -179,6 +181,7 @@ impl StarRailRelicScanner {
self.window_info.item_count_rect.to_rect_i32(),
self.game_info.window.origin()
)?;
im.save("item_count.png");
let s = self.image_to_text.image_to_text(&im, false)?;

info!("物品信息: {}", s);
Expand Down
26 changes: 15 additions & 11 deletions yas-starrail/src/scanner_controller/repository_layout/scan_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use yas::system_control::SystemControl;
use crate::scanner_controller::repository_layout::window_info::StarRailRepositoryScanControllerWindowInfo;
use anyhow::{anyhow, Result};
use clap::{ArgMatches, FromArgMatches};
use yas::utils::color_distance;
use yas::window_info::{FromWindowInfoRepository, WindowInfoRepository};
use crate::scanner_controller::repository_layout::scroll_result::ScrollResult;

Expand Down Expand Up @@ -55,13 +56,6 @@ fn get_capturer() -> Result<Rc<dyn Capturer<RgbImage>>> {
Ok(Rc::new(GenericCapturer::new()?))
}

fn color_distance(c1: &image::Rgb<u8>, c2: &image::Rgb<u8>) -> usize {
let x = c1.0[0] as i32 - c2.0[0] as i32;
let y = c1.0[1] as i32 - c2.0[1] as i32;
let z = c1.0[2] as i32 - c2.0[2] as i32;
return (x * x + y * y + z * z) as usize;
}

// constructor
impl StarRailRepositoryScanController {
pub fn new(
Expand Down Expand Up @@ -231,8 +225,9 @@ impl StarRailRepositoryScanController {
#[inline(always)]
pub fn capture_flag(&self) -> Result<[Rgb<u8>; 50]> {
let mut flag = [Rgb([0, 0, 0]); 50];
let window_origin = self.game_info.window;
let im = self.capturer.capture_rect(window_origin)?;
let window_origin = self.game_info.window.to_rect_f64().origin();
let rect = self.window_info.flag_rect.translate(window_origin);
let im = self.capturer.capture_rect(rect.to_rect_i32())?;

// Gap size between repository top and first item row varies with resolution.
// At 1920x1080, it's 20 pixels.
Expand All @@ -247,12 +242,21 @@ impl StarRailRepositoryScanController {
#[inline(always)]
pub fn check_flag(&self) -> Result<()> {
let flag = self.capture_flag()?;
// println!("{:?}", &flag[..20]);
// let mut same_count = 0;
for y in 0..self.window_info.flag_rect.height as usize {
if color_distance(&self.initial_flag[y], &flag[y]) < 10 {
return Ok(());
// same_count += 1;
return Ok(())
}
}
Err(anyhow!("Flag changed"))
// let ratio = same_count as f64 / self.window_info.flag_rect.height;
// println!("{:?}", ratio);
// if ratio > 0.5 {
// Ok(())
// } else {
Err(anyhow!("Flag changed"))
// }
}

pub fn align_row(&mut self) {
Expand Down
8 changes: 4 additions & 4 deletions yas-starrail/window_info/windows1920x1080.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
},
"starrail_relic_item_count_rect": {
"Rect": {
"top": 973,
"left": 895,
"width": 258,
"height": 34
"top": 45,
"left": 1503,
"width": 260,
"height": 40
}
},
"starrail_relic_level_rect": {
Expand Down
2 changes: 1 addition & 1 deletion yas/src/ocr/paddle_paddle_model/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl PPOCRModel {
}

impl ImageToText<RgbImage> for PPOCRModel {
fn image_to_text(&self, image: &RgbImage, is_preprocessed: bool) -> Result<String> {
fn image_to_text(&self, image: &RgbImage, _is_preprocessed: bool) -> Result<String> {
let tensor: Tensor = tract_ndarray::Array4::from_shape_fn((1, 3, image.height() as usize, image.width() as usize), |(_, c, y, x)| {
let pix = image.get_pixel(x as u32, y as u32)[c];
let v = pix as f32 / 255.0_f32;
Expand Down

0 comments on commit 55d781c

Please sign in to comment.