I've decided to try my hand at Inform7 and I'm having a lot of fun playing around and seeing what is possible with it. For the moment, and using the tutorials available to me online, I'm trying to incorporate a day/night cycle and sleeping which will become available to the player only during one of the night cycles. I've run into some trouble in the way that Inform7 is ignoring my if statements and the player is able to sleep during any time of the day, which isn't what I want. I'm sure I've overlooked something silly as I've just started out, but maybe someone would be kind enough to let me in on what I could do to fix this? Thanks so much.
I apologize for any errors in my code, please keep in mind that I am new and wanting to learn.
Here is my code. . .
The sun is a backdrop. It is everywhere. The description is "Currently out of sight."
Night is a recurring scene. Night begins when play begins. Night begins when the time of day is 10:00 PM. Night begins when Dusk ends. Night ends when the time of day is 1:30 AM.
When Night begins:
say "The moon is up and the temperature drops abruptly to well below zero.";
now the description of the sun is "Currently out of sight."
The witching hour is a recurring scene. The witching hour begins when Night ends. The witching hour ends when the time of day is 5:00 AM.
When The witching hour begins:
say "You feel sleep calling you.";
now the description of the sun is "Currently out of sight.".
Day is a recurring scene. Day begins when The witching hour ends. Day ends when the time of day is 6:00 PM.
When Day begins:
say "The sun is now properly up.";
Dusk is a recurring scene. Dusk begins when Day ends. Dusk ends when the time of day is 10:00 PM.
When Dusk begins:
say "The sun is setting.";
A person is either awake or asleep. A person is usually awake.
Every turn:
if The witching hour is not happening:
instead of sleeping when the player is awake:
now the player is awake;
say "You can't sleep now. . .";
Every turn:
if The witching hour is happening:
instead of sleeping:
now the player is asleep;
say "You fall asleep. . .";
Every turn:
if The witching hour is happening:
instead of doing something other than waking up, waiting or sleeping when the player is asleep:
say ". . . You're sleeping.";
Every turn:
if The witching hour is happening:
instead of sleeping when the player is asleep:
say "Zzzz.";
Every turn:
if The witching hour is happening:
instead of waking up when the player is asleep:
now the player is awake;
say "You wake suddenly.";
Every turn:
if The witching hour is happening:
instead of doing something other than looking or sleeping when the player is awake:
say "You'd really rather just sleep. . .";
You can't put instead rules (or any other types of rules) inside other rules. The compiler should really throw an error there but unfortunately it doesn't. What you need to do is remove all Every turn: lines and combine all if conditions with the instead rule conditions. For example:
Instead of sleeping when the player is awake and the witching hour is not happening:
say "You can't sleep now. . .";
Instead of sleeping when the witching hour is happening:
now the player is asleep;
say "You fall asleep. . .";
and so on for the rest of the rules.
Related
I'm writing a little game in c++ atm.
My Game While loop is always active, in this loop,
I have a condition if the player is shooting.
Now I face the following problem,
After every shot fired, there is a delay, this delay changes over time and while the delay the player should move.
shoot
move
wait 700 ms
shoot again
atm I'm using Sleep(700) the problem is I can't move while the 700 ms, I need something like a timer, so the move command is only executed for 700 ms instead of waiting 700 ms
This depends on how your hypothetical 'sleep' is implemented. There's a few things you should know, as it can be solved in a few ways.
You don't want to put your thread to sleep because then everything halts, which is not what you want.
Plus you may get more time than sleep allows. For example, if you sleep for 700ms you may get more than that, which means if you depend on accurate times you will get burned possibly by this.
1) The first way would be to record the raw time inside of the player. This is not the best approach but it'd work for a simple toy program and record the result of std::chrono::high_resolution_clock::now() (check #include <chrono> or see here) inside the class at the time you fire. To check if you can fire again, just compare the value you stored to ...::now() and see if 700ms has elapsed. You will have to read the documentation to work with it in milliseconds.
2) A better way would be to give your game a pulse via something called 'game ticks', which is the pulse to which your world moves forward. Then you can store the gametick that you fired on and do something similar to the above paragraph (except now you are just checking if currentGametick > lastFiredGametick + gametickUntilFiring).
For the gametick idea, you would make sure you do gametick++ every X milliseconds, and then run your world. A common value is somewhere between 10ms and 50ms.
Your game loop would then look like
while (!exit) {
readInput();
if (ticker.shouldTick()) {
ticker.tick();
world.tick(ticker.gametick);
}
render();
}
The above has the following advantages:
You only update the world every gametick
You keep rendering between gameticks, so you can have smooth animations since you will be rendering at a very high framerate
If you want to halt, just spin in a while loop until the amount of time has elapsed
Now this has avoided a significant amount of discussion, of which you should definitely read this if you are thinking of going the gametick route.
With whatever route you take, you probably need to read this.
So I'm writing now some audio handling in C++ for my game and I've encountered a problem.
What i need to to is have possibility to pause sound cue at any time and get the time it has already played. For example:
I have a cue that has 10 seconds. It was playing for 3 seconds. So i need to get out from it that 3 seconds, so I can after let's say restarting the game and loading save play remaining 7 seconds of the sound. Is there any function for it? I've searched for it for quite a time but never found anything usefull.
I'm using UAudioComponent::SpawnSound(2D/Attached/AtLocation) functions.
So I'm pretty new with programming from scratch, have mainly used unity for a few years up untill now so my general programming knowledge is pretty good. Started studying game development at a university after summer though where we began programming from scratch and as a task we have to make a simple game in a 2D engine we made together in class.
So the game I decided to make was a copy of bomberman and I've gotten as far as where I'm now making the bombs functional.
The problem I'm having is that I don't know how to propperly add in a timer that counts down the time to where the bomb exlpode so the player can avoid it.
I've tried SDL_Delay and _sleep which both just pause the entire program so that doesn't work and I've searched around for more options but not really understood how things work. If I could get some expamples and links to pages that explains how to properly make something like this work (something easy and small hopfully :P) then that would be highly appreciated!
Note that we are using SDL in the engine.
Thanks!
Typically, a game uses a loop, in which you read user input (you are probably using SDL_PollEvent for that), advance the game state for a short time period and draw the screen. This loop is typically called the game loop, render loop or main loop.
A simple, accurate and typical way to delay an event (such as a timed explosion), is to store the future time into a queue. Then, each time the game state advances, check the first and therefore the oldest timestamp in the queue and if the current time is higher than the stored one, then we know that the the thing should now happen and you can call the function that executes the event without delay. Then remove the timestamp from the queue and check the next one until only future events remain or the queue is empty.
If the event delay can vary, then you'll need to use a priority queue to always get the event that should fire next.
skypjack points out in the comments that this is a problematic approach if you need to implement pausing the game. That can be solved by not measuring wall clock, but instead use a separate simulation time that drifts from the wall clock when the game is paused. They also propose a simpler solution:
store a timeToEvent (to be elapsed) and decrement it, so that you detach the game time from the real one. Once it's <= 0, it's its time.
That approach is simpler, but has more overhead for checking the expiration of deadlines.
Im working on a Real world Event Management applicationwhere Events are scheduled based on availability of venue place, none of the events should clash.
What is the best possibility to insert the next value.
Example:
Suppose Event1 is happening between 9 and 9:30 at Room A
Event2 is happening between 9 and 10:00 at Room B
Event3 can start at 9:30 at Room A since its free after 9:30 n so on......
Which algorithm can I use?
Note: This is not CPU scheduling algorithm
Thanks :)
I would go with greedy approach.
Sort all the events according to start time
For each venue:
T=earliest time
Find the earliest event at or after T and schedule it for this venue. Remove that event from the list.
Update T to end time of this event
Once this is completed you can use some kind of local search (simulated annealing based) to further optimize the assignment.
I try to start timer at specific time like 02:30. Every day it starts at 02.30.
Is it possible? Do you have any idea?
Thank a lot.
QTimer doesn't handle specific times of day natively, but you could use it in conjunction with QDateTime to get what you want. That is, use QDateTime objects to figure out how many seconds are between (right now) and 2:30 (QDateTime::msecsTo() looks particularly appropriate here), then set your QTimer to go off after that many seconds. Repeat as necessary.
Depending on the required resolution, you could use an ordinary QTimer that fires let's say every minute.
In the timerEvent, you could check if you are on the right time (using QDateTime), and trigger the necessary event.
The solution of Jeremy is indeed elegant, but it doesn't take into account the daylight savings time.
To guard against that, you should fire a timer event every hour and check the wall clock.
Calculate the delta to the target, like Jeremy proposes, and if it falls within the coming hour, set a timer to fire, and disable the hourly timer.
If not, just wait for the hourly timer to fire again.
Pseudo code:
Get wall clock time
Calculate difference between target time and wall clock
If difference < 1 hour:
Set timer to fire after difference secs
If this is a repeating event, restart the hourly timer
Else:
Start watch timer to do this calculation again after one hour