Ai actions (#210)
This commit is contained in:
parent
6cab14b8e1
commit
3f4b8e2d84
19 changed files with 1562 additions and 162 deletions
1450
client/Cargo.lock
generated
1450
client/Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -29,6 +29,8 @@ serde-wasm-bindgen = "0.6.5"
|
|||
console_error_panic_hook = "0.1.7"
|
||||
web-sys = { version = "0.3.72", features = ["Window"] }
|
||||
js-sys = "0.3.72"
|
||||
pyroscope = "0.5.8"
|
||||
pyroscope_pprofrs = "0.2.8"
|
||||
|
||||
[[bin]]
|
||||
name = "local_client"
|
||||
|
|
|
|||
|
|
@ -239,7 +239,6 @@ fn controlling_player_click(rc: &RenderContext, mouse_pos: Vec2, pos: Position)
|
|||
pub struct Features {
|
||||
pub import_export: bool,
|
||||
pub assets_url: String,
|
||||
pub ai_autoplay: bool,
|
||||
pub ai: Option<AI>,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
#![allow(clippy::missing_panics_doc)]
|
||||
|
||||
use client::client::{Features, GameSyncRequest, GameSyncResult, init, render_and_update};
|
||||
use client::client_state::State;
|
||||
use macroquad::miniquad::window::set_window_size;
|
||||
use macroquad::prelude::{next_frame, screen_width, vec2};
|
||||
use macroquad::window::screen_height;
|
||||
use pyroscope::PyroscopeAgent;
|
||||
use pyroscope_pprofrs::{PprofConfig, pprof_backend};
|
||||
use server::action::execute_action;
|
||||
use server::advance::do_advance;
|
||||
use server::ai::AI;
|
||||
|
|
@ -16,15 +19,16 @@ use server::position::Position;
|
|||
use server::resource_pile::ResourcePile;
|
||||
use server::unit::UnitType;
|
||||
use server::utils::remove_element;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::time::Duration;
|
||||
use std::{env, vec};
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Mode {
|
||||
Local,
|
||||
AI,
|
||||
Profile,
|
||||
Test,
|
||||
}
|
||||
|
||||
|
|
@ -33,17 +37,28 @@ async fn main() {
|
|||
set_window_size(1200, 600);
|
||||
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let mode = get_mode(&args);
|
||||
let modes = get_modes(&args);
|
||||
|
||||
if modes.contains(&Mode::Profile) {
|
||||
let pprof_config = PprofConfig::new().sample_rate(100);
|
||||
let backend_impl = pprof_backend(pprof_config);
|
||||
|
||||
let agent = PyroscopeAgent::builder("http://localhost:4040", "clash")
|
||||
.backend(backend_impl)
|
||||
.build()
|
||||
.expect("Failed to initialize pyroscope");
|
||||
let _ = agent.start().unwrap();
|
||||
}
|
||||
|
||||
let ai = mode == Mode::AI;
|
||||
let mut features = Features {
|
||||
import_export: true,
|
||||
assets_url: "assets/".to_string(),
|
||||
ai_autoplay: ai,
|
||||
ai: ai.then(|| AI::new(1., Duration::from_secs(5), false)),
|
||||
ai: modes
|
||||
.contains(&Mode::AI)
|
||||
.then(|| AI::new(1., Duration::from_secs(5), false)),
|
||||
};
|
||||
|
||||
let game = if mode == Mode::Test {
|
||||
let game = if modes.contains(&Mode::Test) {
|
||||
setup_local_game()
|
||||
} else {
|
||||
let seed = if args.len() > 2 {
|
||||
|
|
@ -57,13 +72,17 @@ async fn main() {
|
|||
run(game, &mut features).await;
|
||||
}
|
||||
|
||||
fn get_mode(args: &[String]) -> Mode {
|
||||
if args.len() > 1 && args[1] == "ai" {
|
||||
Mode::AI
|
||||
} else if args.len() > 1 && args[1] == "generate" {
|
||||
Mode::Local
|
||||
} else {
|
||||
Mode::Test
|
||||
fn get_modes(args: &[String]) -> Vec<Mode> {
|
||||
match args.get(1) {
|
||||
Some(arg) => match arg.as_str() {
|
||||
"generate" => vec![Mode::Local],
|
||||
"ai" => vec![Mode::AI],
|
||||
"profile" => vec![Mode::AI, Mode::Profile],
|
||||
_ => {
|
||||
panic!("Unknown argument: {}", arg);
|
||||
}
|
||||
},
|
||||
_ => vec![Mode::Test],
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -81,14 +100,14 @@ async fn run(mut game: Game, features: &mut Features) {
|
|||
match message {
|
||||
GameSyncRequest::None => {}
|
||||
GameSyncRequest::StartAutoplay => {
|
||||
game = ai_autoplay(game, features);
|
||||
game = ai_autoplay(game, features, &mut state);
|
||||
state.show_player = game.active_player();
|
||||
sync_result = GameSyncResult::Update;
|
||||
}
|
||||
GameSyncRequest::ExecuteAction(a) => {
|
||||
let p = game.active_player();
|
||||
game = execute_action(game, a, p);
|
||||
game = ai_autoplay(game, features);
|
||||
game = ai_autoplay(game, features, &mut state);
|
||||
state.show_player = game.active_player();
|
||||
sync_result = GameSyncResult::Update;
|
||||
}
|
||||
|
|
@ -105,11 +124,11 @@ async fn run(mut game: Game, features: &mut Features) {
|
|||
}
|
||||
}
|
||||
|
||||
fn ai_autoplay(game: Game, f: &mut Features) -> Game {
|
||||
fn ai_autoplay(game: Game, f: &mut Features, state: &mut State) -> Game {
|
||||
if let Some(ai) = &mut f.ai {
|
||||
if f.ai_autoplay {
|
||||
if state.ai_autoplay {
|
||||
// todo does this block the ui?
|
||||
f.ai_autoplay = false;
|
||||
// state.ai_autoplay = false;
|
||||
let action = ai.next_action(&game);
|
||||
let player_index = game.active_player();
|
||||
return execute_action(game, action, player_index);
|
||||
|
|
|
|||
|
|
@ -330,7 +330,7 @@ pub fn show_global_controls(rc: &RenderContext, features: &Features) -> StateUpd
|
|||
}
|
||||
}
|
||||
|
||||
if features.ai_autoplay {
|
||||
if features.ai.is_some() {
|
||||
let tooltip = if rc.state.ai_autoplay {
|
||||
"Pause AI autoplay"
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -84,7 +84,6 @@ impl RemoteClient {
|
|||
import_export: false,
|
||||
assets_url: control.assets_url(),
|
||||
ai: None,
|
||||
ai_autoplay: false,
|
||||
};
|
||||
let state = init(&features).await;
|
||||
|
||||
|
|
|
|||
|
|
@ -324,8 +324,6 @@ enum MissionType {
|
|||
#[must_use]
|
||||
pub fn evaluate_position(game: &Game, evaluation_time: Duration) -> Vec<f64> {
|
||||
let mut rng = Rng::new();
|
||||
let mut game = game.clone();
|
||||
game.supports_undo = false;
|
||||
let start_time = std::time::Instant::now();
|
||||
let mut wins = vec![0; game.players.len()];
|
||||
let mut iterations = 0;
|
||||
|
|
|
|||
|
|
@ -98,9 +98,9 @@ fn base_actions(game: &Game) -> Vec<(ActionType, Vec<Action>)> {
|
|||
|
||||
// IncreaseHappiness
|
||||
let happiness = available_happiness_actions(game, p.index);
|
||||
if !happiness.is_empty() {
|
||||
let action_type = prefer_custom_action(happiness);
|
||||
|
||||
// if !happiness.is_empty() {
|
||||
// let action_type = prefer_custom_action(happiness); // todo custom action is buggy
|
||||
if let Some(action_type) = base_and_custom_action(happiness).0 {
|
||||
if let Some(h) = calculate_increase_happiness(p, &action_type) {
|
||||
actions.push((
|
||||
ActionType::Playing(PlayingActionType::IncreaseHappiness),
|
||||
|
|
@ -127,7 +127,11 @@ fn base_actions(game: &Game) -> Vec<(ActionType, Vec<Action>)> {
|
|||
.iter()
|
||||
.filter_map(|card| {
|
||||
if *card == 126 || *card == 17 || *card == 18 {
|
||||
// construct only is buggy
|
||||
// todo construct only is buggy
|
||||
return None;
|
||||
}
|
||||
if *card == 19 || *card == 20 || *card == 29 || *card == 30 {
|
||||
// todo collect only is buggy
|
||||
return None;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -63,7 +63,9 @@ pub struct Game {
|
|||
|
||||
impl Clone for Game {
|
||||
fn clone(&self) -> Self {
|
||||
Self::from_data(self.cloned_data())
|
||||
let mut game = Self::from_data(self.cloned_data());
|
||||
game.supports_undo = self.supports_undo;
|
||||
game
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -565,7 +567,6 @@ impl Game {
|
|||
self.round += 1;
|
||||
self.skip_dropped_players();
|
||||
if self.round > 3 {
|
||||
self.round = 1;
|
||||
enter_status_phase(self);
|
||||
return;
|
||||
}
|
||||
|
|
@ -598,6 +599,7 @@ impl Game {
|
|||
let winner_name = self.player_name(winner_player_index);
|
||||
self.add_info_log_group(format!("The game has ended. {winner_name} has won"));
|
||||
self.add_message("The game has ended");
|
||||
self.state = GameState::Finished;
|
||||
}
|
||||
|
||||
pub(crate) fn next_dice_roll(&mut self) -> CombatDieRoll {
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ fn test_large_civ() {
|
|||
|
||||
#[test]
|
||||
fn test_colony() {
|
||||
// Game ends after this step
|
||||
JSON.test(
|
||||
"colony",
|
||||
vec![
|
||||
|
|
|
|||
|
|
@ -440,7 +440,7 @@
|
|||
"undo_limit": 0,
|
||||
"actions_left": 3,
|
||||
"round": 3,
|
||||
"age": 1,
|
||||
"age": 6,
|
||||
"messages": [
|
||||
"The game has started"
|
||||
],
|
||||
|
|
|
|||
|
|
@ -1,39 +1,5 @@
|
|||
{
|
||||
"state": "Playing",
|
||||
"events": [
|
||||
{
|
||||
"event_type": {
|
||||
"StatusPhase": "FreeAdvance"
|
||||
},
|
||||
"player": 1,
|
||||
"last_priority_used": 0,
|
||||
"handler": {
|
||||
"priority": 0,
|
||||
"request": {
|
||||
"SelectAdvance": {
|
||||
"choices": [
|
||||
"Arts",
|
||||
"Bartering",
|
||||
"Engineering",
|
||||
"Fishing",
|
||||
"Husbandry",
|
||||
"Irrigation",
|
||||
"Math",
|
||||
"Myths",
|
||||
"Roads",
|
||||
"Sanitation",
|
||||
"Storage",
|
||||
"Tactics",
|
||||
"Writing"
|
||||
]
|
||||
}
|
||||
},
|
||||
"origin": {
|
||||
"Builtin": "Free Advance"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"state": "Finished",
|
||||
"players": [
|
||||
{
|
||||
"id": 0,
|
||||
|
|
@ -456,47 +422,20 @@
|
|||
}
|
||||
},
|
||||
"undo": [
|
||||
{
|
||||
"op": "replace",
|
||||
"path": "/events/0/event_type/StatusPhase",
|
||||
"value": "CompleteObjectives"
|
||||
},
|
||||
{
|
||||
"op": "remove",
|
||||
"path": "/events/0/handler"
|
||||
},
|
||||
{
|
||||
"op": "remove",
|
||||
"path": "/events/0/last_priority_used"
|
||||
},
|
||||
{
|
||||
"op": "add",
|
||||
"path": "/events/1",
|
||||
"value": {
|
||||
"event_type": {
|
||||
"SelectObjectives": {
|
||||
"cards": [
|
||||
{
|
||||
"ObjectiveCard": 17
|
||||
},
|
||||
{
|
||||
"ObjectiveCard": 32
|
||||
}
|
||||
],
|
||||
"objective_opportunities": [
|
||||
"Colony",
|
||||
"Outpost"
|
||||
]
|
||||
}
|
||||
},
|
||||
"handler": {
|
||||
"origin": {
|
||||
"Builtin": "Select Objective Cards to Complete"
|
||||
"path": "/events",
|
||||
"value": [
|
||||
{
|
||||
"event_type": {
|
||||
"StatusPhase": "CompleteObjectives"
|
||||
},
|
||||
"priority": 0,
|
||||
"request": {
|
||||
"SelectHandCards": {
|
||||
"choices": [
|
||||
"player": 1
|
||||
},
|
||||
{
|
||||
"event_type": {
|
||||
"SelectObjectives": {
|
||||
"cards": [
|
||||
{
|
||||
"ObjectiveCard": 17
|
||||
},
|
||||
|
|
@ -504,17 +443,43 @@
|
|||
"ObjectiveCard": 32
|
||||
}
|
||||
],
|
||||
"description": "Select cards to complete",
|
||||
"needed": {
|
||||
"end": 2,
|
||||
"start": 0
|
||||
"objective_opportunities": [
|
||||
"Colony",
|
||||
"Outpost"
|
||||
]
|
||||
}
|
||||
},
|
||||
"handler": {
|
||||
"origin": {
|
||||
"Builtin": "Select Objective Cards to Complete"
|
||||
},
|
||||
"priority": 0,
|
||||
"request": {
|
||||
"SelectHandCards": {
|
||||
"choices": [
|
||||
{
|
||||
"ObjectiveCard": 17
|
||||
},
|
||||
{
|
||||
"ObjectiveCard": 32
|
||||
}
|
||||
],
|
||||
"description": "Select cards to complete",
|
||||
"needed": {
|
||||
"end": 2,
|
||||
"start": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"last_priority_used": 0,
|
||||
"player": 1
|
||||
}
|
||||
},
|
||||
"last_priority_used": 0,
|
||||
"player": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"op": "remove",
|
||||
"path": "/messages/1"
|
||||
},
|
||||
{
|
||||
"op": "add",
|
||||
|
|
@ -527,6 +492,11 @@
|
|||
{
|
||||
"op": "remove",
|
||||
"path": "/players/1/completed_objectives"
|
||||
},
|
||||
{
|
||||
"op": "replace",
|
||||
"path": "/state",
|
||||
"value": "Playing"
|
||||
}
|
||||
],
|
||||
"completed_objectives": [
|
||||
|
|
@ -572,19 +542,23 @@
|
|||
"Player1 ended their turn with 3 actions left"
|
||||
],
|
||||
[
|
||||
"The game has entered the 1st status phase"
|
||||
"The game has entered the 6th status phase"
|
||||
],
|
||||
[
|
||||
"Player2 completed objective Colony",
|
||||
"Player2 completed objective Outpost"
|
||||
],
|
||||
[
|
||||
"The game has ended. Player2 has won"
|
||||
]
|
||||
],
|
||||
"undo_limit": 1,
|
||||
"actions_left": 3,
|
||||
"round": 1,
|
||||
"age": 1,
|
||||
"round": 4,
|
||||
"age": 6,
|
||||
"messages": [
|
||||
"The game has started"
|
||||
"The game has started",
|
||||
"The game has ended"
|
||||
],
|
||||
"rng": "184103094459271804140360825922843067546",
|
||||
"wonders_left": [
|
||||
|
|
@ -707,4 +681,4 @@
|
|||
15,
|
||||
49
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -514,7 +514,7 @@
|
|||
],
|
||||
"undo_limit": 1,
|
||||
"actions_left": 2,
|
||||
"round": 1,
|
||||
"round": 4,
|
||||
"age": 1,
|
||||
"messages": [
|
||||
"The game has started"
|
||||
|
|
|
|||
|
|
@ -385,7 +385,7 @@
|
|||
],
|
||||
"undo_limit": 1,
|
||||
"actions_left": 1,
|
||||
"round": 1,
|
||||
"round": 4,
|
||||
"age": 1,
|
||||
"messages": [
|
||||
"The game has started"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"state": "Playing",
|
||||
"state": "Finished",
|
||||
"players": [
|
||||
{
|
||||
"id": 0,
|
||||
|
|
@ -299,7 +299,7 @@
|
|||
],
|
||||
"undo_limit": 1,
|
||||
"actions_left": 0,
|
||||
"round": 1,
|
||||
"round": 7,
|
||||
"age": 6,
|
||||
"messages": [
|
||||
"The game has started",
|
||||
|
|
|
|||
|
|
@ -283,7 +283,7 @@
|
|||
"log": [],
|
||||
"undo_limit": 14,
|
||||
"actions_left": 0,
|
||||
"round": 6,
|
||||
"round": 3,
|
||||
"age": 2,
|
||||
"messages": [
|
||||
"The game has started"
|
||||
|
|
|
|||
|
|
@ -334,7 +334,7 @@
|
|||
],
|
||||
"undo_limit": 1,
|
||||
"actions_left": 0,
|
||||
"round": 1,
|
||||
"round": 4,
|
||||
"age": 2,
|
||||
"messages": [
|
||||
"The game has started"
|
||||
|
|
|
|||
|
|
@ -349,7 +349,7 @@
|
|||
],
|
||||
"undo_limit": 2,
|
||||
"actions_left": 0,
|
||||
"round": 1,
|
||||
"round": 4,
|
||||
"age": 2,
|
||||
"messages": [
|
||||
"The game has started"
|
||||
|
|
|
|||
|
|
@ -364,7 +364,7 @@
|
|||
],
|
||||
"undo_limit": 3,
|
||||
"actions_left": 0,
|
||||
"round": 1,
|
||||
"round": 4,
|
||||
"age": 2,
|
||||
"messages": [
|
||||
"The game has started"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue