Skip to content

Commit

Permalink
Fixes users getting locked in simulation view / simulation refactor (#…
Browse files Browse the repository at this point in the history
…5318)

# About the pull request

Initially I thought I introduced this bug, but it looks like it's
existed for a few years. Fixes #4927. Also did some minor refactoring
for the simulator.

# Explain why it's good for the game

bug bad

# Changelog

:cl:
fix: fixes users getting stuck inside of the simulator
/:cl:
  • Loading branch information
Cthulhu80 committed Dec 31, 2023
1 parent 798ed88 commit 4d7f548
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
4 changes: 1 addition & 3 deletions code/game/machinery/computer/demo_sim.dm
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
var/list/data = list()

data["configuration"] = configuration
data["looking"] = simulation.looking_at_simulation
data["dummy_mode"] = simulation.dummy_mode

data["worldtime"] = world.time
Expand Down Expand Up @@ -104,8 +103,7 @@
/obj/structure/machinery/computer/demo_sim/ui_close(mob/user)

. = ..()
if(simulation.looking_at_simulation)
simulation.stop_watching(user)
simulation.stop_watching(user)

// DEMOLITIONS TGUI SHIT END \\
Expand Down
4 changes: 1 addition & 3 deletions code/game/machinery/computer/dropship_weapons.dm
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@
/obj/structure/machinery/computer/dropship_weapons/ui_close(mob/user)
. = ..()
SEND_SIGNAL(src, COMSIG_CAMERA_UNREGISTER_UI, user)
if(simulation.looking_at_simulation)
simulation.stop_watching(user)
simulation.stop_watching(user)

/obj/structure/machinery/computer/dropship_weapons/ui_status(mob/user, datum/ui_state/state)
. = ..()
Expand Down Expand Up @@ -239,7 +238,6 @@
.["firemission_selected_laser"] = firemission_envelope.recorded_loc ? firemission_envelope.recorded_loc.get_name() : "NOT SELECTED"

.["configuration"] = configuration
.["looking"] = simulation.looking_at_simulation
.["dummy_mode"] = simulation.dummy_mode
.["worldtime"] = world.time
.["nextdetonationtime"] = simulation.detonation_cooldown
Expand Down
22 changes: 14 additions & 8 deletions code/game/sim_manager/datums/simulator.dm
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#define GRID_CLEARING_SIZE 16

/datum/simulator

// Necessary to prevent multiple users from simulating at the same time.
var/static/detonation_cooldown = 0
var/static/detonation_cooldown_time = 2 MINUTES
var/static/sim_reboot_state = TRUE

var/looking_at_simulation = FALSE
var/detonation_cooldown_time = 2 MINUTES
var/dummy_mode = CLF_MODE
var/obj/structure/machinery/camera/simulation/sim_camera
var/grid_clearing_size = 16

// garbage collection,
var/static/list/delete_targets = list()

// list of users currently inside the simulator
var/static/list/users_in_sim = list()

/*
unarmoured humans are unnencessary clutter as they tend to explode easily
and litter the sim room with body parts, best left out.
Expand All @@ -29,7 +32,7 @@

/datum/simulator/proc/start_watching(mob/living/user)

if(looking_at_simulation)
if(user in users_in_sim)
to_chat(user, SPAN_WARNING("You are already looking at the simulation."))
return
if(!sim_camera)
Expand All @@ -41,13 +44,15 @@
to_chat(user, SPAN_WARNING("You're too busy looking at something else."))
return
user.reset_view(sim_camera)
looking_at_simulation = TRUE
users_in_sim += user

/datum/simulator/proc/stop_watching(mob/living/user)
if(!(user in users_in_sim))
return
user.unset_interaction()
user.reset_view(null)
user.cameraFollow = null
looking_at_simulation = FALSE
users_in_sim -= user

/datum/simulator/proc/sim_turf_garbage_collection()

Expand All @@ -67,8 +72,8 @@
y:2 | x: 1 2 3 4 ... 16
y:1 | x: 1 2 3 4 ... 16
*/
for (var/y_pos in 1 to grid_clearing_size)// outer y
for (var/x_pos in 1 to grid_clearing_size) // inner x
for (var/y_pos in 1 to GRID_CLEARING_SIZE)// outer y
for (var/x_pos in 1 to GRID_CLEARING_SIZE) // inner x
var/turf/current_grid = locate(sim_grid_start_pos.x + x_pos,sim_grid_start_pos.y + y_pos,sim_grid_start_pos.z)

current_grid.empty(/turf/open/floor/engine)
Expand Down Expand Up @@ -101,3 +106,4 @@

addtimer(CALLBACK(src, PROC_REF(sim_turf_garbage_collection)), 30 SECONDS, TIMER_STOPPABLE)

#undef GRID_CLEARING_SIZE
19 changes: 15 additions & 4 deletions tgui/packages/tgui/interfaces/CasSim.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useBackend } from '../backend';
import { useBackend, useLocalState } from '../backend';
import { Box, Button, Section, ProgressBar, NoticeBox, Stack } from '../components';

interface CasSimData {
Expand All @@ -12,6 +12,11 @@ interface CasSimData {

export const CasSim = (_props, context) => {
const { act, data } = useBackend<CasSimData>(context);
const [simulationView, setSimulationView] = useLocalState(
context,
'simulation_view',
false
);

const timeLeft = data.nextdetonationtime - data.worldtime;
const timeLeftPct = timeLeft / data.detonation_cooldown;
Expand Down Expand Up @@ -48,21 +53,27 @@ export const CasSim = (_props, context) => {
<Section title="Cas Simulation Controls">
<Stack>
<Stack.Item grow>
{(!data.looking && (
{(!simulationView && (
<Button
fluid={1}
icon="eye"
color="good"
content="Enter simulation"
onClick={() => act('start_watching')}
onClick={() => {
act('start_watching');
setSimulationView(true);
}}
/>
)) || (
<Button
fluid={1}
icon="eye-slash"
color="good"
content="Exit simulation"
onClick={() => act('stop_watching')}
onClick={() => {
act('stop_watching');
setSimulationView(false);
}}
/>
)}
</Stack.Item>
Expand Down
19 changes: 15 additions & 4 deletions tgui/packages/tgui/interfaces/DemoSim.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { useBackend } from '../backend';
import { useBackend, useLocalState } from '../backend';
import { Button, Section, ProgressBar, NoticeBox, Box, Stack } from '../components';
import { Window } from '../layouts';

export const DemoSim = (_props, context) => {
const { act, data } = useBackend(context);
const [simulationView, setSimulationView] = useLocalState(
context,
'simulation_view',
false
);

const timeLeft = data.nextdetonationtime - data.worldtime;
const timeLeftPct = timeLeft / data.detonation_cooldown;
Expand Down Expand Up @@ -45,14 +50,17 @@ export const DemoSim = (_props, context) => {
<Section title="Detonation controls">
<Stack>
<Stack.Item grow>
{(!data.looking && (
{(!simulationView && (
<Button
fontSize="16px"
fluid={1}
icon="eye"
color="good"
content="Enter simulation"
onClick={() => act('start_watching')}
onClick={() => {
act('start_watching');
setSimulationView(true);
}}
/>
)) || (
<Button
Expand All @@ -61,7 +69,10 @@ export const DemoSim = (_props, context) => {
icon="eye-slash"
color="good"
content="Exit simulation"
onClick={() => act('stop_watching')}
onClick={() => {
act('stop_watching');
setSimulationView(false);
}}
/>
)}
</Stack.Item>
Expand Down

0 comments on commit 4d7f548

Please sign in to comment.