A lot more work on WASM script execution
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-09-07 18:01:26 +02:00
parent f9761f61da
commit b1890681a1
102 changed files with 748 additions and 202 deletions

41
src/dynamic_data/flow/choice_queue.rs Normal file → Executable file
View File

@@ -1,5 +1,7 @@
use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::Pokemon;
use parking_lot::lock_api::MappedRwLockReadGuard;
use parking_lot::{RawRwLock, RwLock, RwLockReadGuard};
/// The ChoiceQueue is used to run choices one by one.
///
@@ -8,10 +10,11 @@ use crate::dynamic_data::Pokemon;
/// helper functions to change the turn order while doing the execution. This is needed, as several
/// moves in Pokemon actively mess with this order.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct ChoiceQueue {
/// Our storage of turn choices. Starts out completely filled, then slowly empties as turns get
/// executed.
queue: Vec<Option<TurnChoice>>,
queue: RwLock<Vec<Option<TurnChoice>>>,
/// The current index of the turn we need to execute next.
current: usize,
}
@@ -19,42 +22,47 @@ pub struct ChoiceQueue {
impl ChoiceQueue {
/// Initializes a ChoiceQueue. We expect the given queue to already be sorted here.
pub(crate) fn new(queue: Vec<Option<TurnChoice>>) -> Self {
Self { queue, current: 0 }
Self {
queue: RwLock::new(queue),
current: 0,
}
}
/// Dequeues the next turn choice to be executed. This gives ownership to the callee, and replaces
/// our own reference to the turn choice with an empty spot. It also increments the current position
/// by one.
pub fn dequeue(&mut self) -> TurnChoice {
let c = self.queue[self.current].take();
let c = self.queue.write()[self.current].take();
self.current += 1;
c.unwrap()
}
/// This reads what the next choice to execute will be, without modifying state.
pub fn peek(&self) -> &TurnChoice {
self.queue[self.current].as_ref().unwrap()
pub fn peek(&self) -> MappedRwLockReadGuard<'_, RawRwLock, TurnChoice> {
let read_lock = self.queue.read();
RwLockReadGuard::map(read_lock, |a| a[self.current].as_ref().unwrap())
}
/// Check if we have any choices remaining.
pub fn has_next(&self) -> bool {
self.current < self.queue.len()
self.current < self.queue.read().len()
}
/// This resorts the yet to be executed choices. This can be useful for dealing with situations
/// such as Pokemon changing forms just after the very start of a turn, when turn order has
/// technically already been decided.
pub fn resort(&mut self) {
let len = self.queue.len();
self.queue[self.current..len].sort_unstable_by(|a, b| b.cmp(a));
let len = self.queue.read().len();
self.queue.write()[self.current..len].sort_unstable_by(|a, b| b.cmp(a));
}
/// This moves the choice of a specific Pokemon up to the next choice to be executed.
pub fn move_pokemon_choice_next(&mut self, pokemon: &Pokemon) -> bool {
pub fn move_pokemon_choice_next(&self, pokemon: &Pokemon) -> bool {
let mut queue_lock = self.queue.write();
let mut desired_index = None;
// Find the index for the choice we want to move up.
for index in self.current..self.queue.len() {
if let Some(choice) = &self.queue[index] {
for index in self.current..queue_lock.len() {
if let Some(choice) = &queue_lock[index] {
if std::ptr::eq(choice.user().as_ref(), pokemon) {
desired_index = Some(index);
break;
@@ -72,19 +80,20 @@ impl ChoiceQueue {
}
// Take the choice we want to move forward out of it's place.
let choice = self.queue[desired_index].take().unwrap();
let choice = queue_lock[desired_index].take().unwrap();
// Iterate backwards from the spot before the choice we want to move up, push them all back
// by 1 spot.
for index in (desired_index - 1)..self.current {
self.queue.swap(index, index + 1);
queue_lock.swap(index, index + 1);
}
// Place the choice that needs to be next in the next to be executed position.
let _ = self.queue[self.current].insert(choice);
let _ = queue_lock[self.current].insert(choice);
true
}
/// Internal helper function to be easily able to iterate over the yet to be executed choices.
pub(crate) fn get_queue(&self) -> &[Option<TurnChoice>] {
&self.queue[self.current..self.queue.len()]
pub(crate) fn get_queue(&self) -> MappedRwLockReadGuard<'_, RawRwLock, [Option<TurnChoice>]> {
let read_lock = self.queue.read();
RwLockReadGuard::map(read_lock, |a| &a[self.current..self.queue.read().len()])
}
}