Moving an entity around

You’ve got the start of your algorithm right there, now try and turn that into code. Don’t get too caught up in the perceived complexity of the system or any limitations of ChoiceScript. You don’t need anything anywhere near as complicated as a path finding algorithm (A*) here.

Break the bigger problem down into smaller chunks. Make them small enough that you understand how to achieve each individual step in ChoicesScript – the system will follow. I think you’ll be pleasantly surprised how accessible implementing more complicated systems becomes if you approach the problem with the right mindset.

E.g.

  1. We need to track which room the player and monster are in, how do we store values for later reference? Variables! Easy!
*create monster_room 5
*create player_room 1
  1. We need to do a thing if the player plus monster ever end up in the same room. How do we check things? If statements! Nice.
*label player_moved
*if player_room = monster_room
  *goto monster_scene
*else
  *comment normal stuff
  1. Now it does get trickier here; we need a way to randomise the route, but we don’t know which rooms are connected yet. How do we store information again? Variables! Okay, but each room can be connected to multiple other rooms. How do we associate a room with multiple values? Multiple variables? An array? A string of characters? There’s certainly a few choices, each with pros and cons, but all will do the job.

*comment or...
*create room_1_connections "153"

*comment or...
*create_array room_connections_1 3 1 5 3

etc.
  1. Doing the randomising! ChoiceScript doesn’t have native loops, but you can repeat stuff using goto and labels. And we already know how to get random numbers in ChoiceScript…
*label loop_select_next_monster_room
*rand target_room min_room max_room
*comment if target_room is in connected rooms; implement via a nested loop? Depends on your data storage.
  *goto next_bit_of_game
*else
  *goto loop_select_next_monster_room

I really encourage people to try their hand at these things, it’s a great learning exercise. Think of writing code like writing a recipe. Getting a full dinner on a plate takes a lot of work, but almost anyone can do it if the steps are made small and detailed enough.

8 Likes

I was intrigued by this concept so I made a map in paint:
image

And then I made a mini loop labyrinth-like game:

Code for a labyrinth loop mini-game
*title Monster
*author You

*create monster_position "D"
*create mc_position "A"
*create random_room 0
*create monster_next_door false
*create luck 0
*create fresh_start true

Welcome to the labyrinth. 
*label start
*rand luck 0 1
*if not (fresh_start)
    *gosub move_monster
*if (mc_position) = (monster_position)
    The monster enters the room where you are.
    *if luck = 0
        *goto dead
    *else
        It doesn't find you and it moves to the next room. You survived.
        *goto start
    
*label next
You are currently in room ${mc_position}. 
There is a monster somewhere.
*gosub check_next_room
*if monster_next_door
    You can hear a noise coming from room ${monster_position}.

What will you do?
*choice
    #Stay and hide.
        *set fresh_start false
        You hide.
        *goto start
    #Go to another room.
        *set fresh_start false
        Alright, which room will you go to? 
        *if monster_next_door
            Remember that you can hear a noise coming from room ${monster_position}.
        *choice
            *if (mc_position = "B") or (mc_position = "C")
                #Go to room A.
                    *set mc_position "A"
                    *if (mc_position) = (monster_position)
                        Boo! The monster eats you.
                        *goto dead
                    *goto start
            *if (mc_position = "A") or (mc_position = "D") 
                #Go to room B.
                    *set mc_position "B"
                    *if (mc_position) = (monster_position)
                        Boo! The monster eats you.
                        *goto dead
                    *goto start
            *if (mc_position = "A") or (mc_position = "D") 
                #Go to room C.
                    *set mc_position "C"
                    *if (mc_position) = (monster_position)
                        Boo! The monster eats you.
                        *goto dead
                    *goto start
            *if (mc_position = "B") or (mc_position = "C") 
                #Go to room D.
                    *set mc_position "D"
                    *if (mc_position) = (monster_position)
                        Boo! The monster eats you.
                        *goto dead


*label dead
The monster found you, you died.

*ending

*comment SUBROUTINE TO MOVE THE MONSTER

*label move_monster
*rand random_room 1 2
*if (monster_position = "A") or (monster_position = "D")
    *if random_room = 1
        *set monster_position "B"
        *goto return
    *else
        *set monster_position "C"
        *goto return
*else
    *if random_room = 1
        *set monster_position "A"
        *goto return
    *else
        *set monster_position "D"
        *goto return
*label return
*return

*comment SUBROUTINE TO CHECK IF THE MONSTER IS NEXT ROOM
*label check_next_room
*if (mc_position = "A")
    *if (monster_position = "B") or (monster_position = "C")
        *set monster_next_door true
        *goto return1
    *else
        *set monster_next_door false
        *goto return1
*elseif (mc_position = "B")
    *if (monster_position = "A") or (monster_position = "D")
        *set monster_next_door true
        *goto return1
    *else
        *set monster_next_door false
        *goto return1
*elseif (mc_position = "C")
    *if (monster_position = "A") or (monster_position = "D")
        *set monster_next_door true
        *goto return1
    *else
        *set monster_next_door false
        *goto return1
*else
    *if (monster_position = "C") or (monster_position = "B")
        *set monster_next_door true
        *goto return1
    *else
        *set monster_next_door false
        *goto return1
*label return1
*return

It works most of the time, but sometimes it gives unexpected results and I don’t know how to fix it. They are not quite noticeable though. I’m of the theory that the creature moves on its own… absolutely not my fault, of course :rofl: I share it in the hope that it will serve as inspiration, at least, of what not to do :sweat_smile:

3 Likes

Huge kudos to @CJW ’ s mentoring skills!

My maze, and its more complex cousin below, are very advanced. I had to build tools to emulate loops, functions and local variables. Unfortunately, CS is not the best language to learn programming (I would suggest Python), so many solutions are more convoluted than they should be. But @CJW is right that you can start small and add complexity as you get more experienced. Good luck!

3 Likes