windows – Simulate mouse input message for free look in fps games

This question was previously asked in stackoverflow where no one gives an answer. I also found a similiar question here, but in my situation, I hook and simulate mouse inputs and send them to the game window.


Here is the body of the original question:

After hooking windows mouse and keyboard APIs, I wanted to simulate the mouse input message for free look technique (Move the mouse to rotate the player character’s view). Before coding, I used spy++ to monitor a fps game observing which mouse-related message was sent to the window of the game’s program. I got these following lines when I moved the mouse:

P WM_INPUT nInputCode:RIM_INPUT hRawInput:065A04FD
S WM_NCHITTEST xPos:2504 yPos:889
R WM_NCHITTEST nHittest:HTCLIENT
S WM_SETCURSOR hwnd:0016089A nHittest:HCLIENT wMouseMsg:WM_MOUSEMOVE
R WM_SETCURSOR fHaltProcessing:True
P WM_MOUSEMOVE fwKeys:0000 xPos:419 yPos:183
...

So I wrote these lines of codes (sent WM_INPUT message using codes in How to simulate raw input / Send a WM_INPUT message to an application the right way?, Simulating Raw Input on Windows, and codes in github):

// ...
// mData->px and mData->py indicating the mouse position in client window are double type.
WORD xCor = static_cast<WORD>(mData->px);
WORD yCor = static_cast<WORD>(mData->py);
LPARAM lParamMouseMove = (yCor << 16) | (xCor & 0xFFFFFFFF); // lParam for mouse movement message

// prepare for sending WM_INPUT
LONG _mouse_position.x = static_cast<long>(mData->px);
LONG _mouse_position.y = static_cast<long>(mData->py);
WPARAM wParamRawMouse = RIM_INPUT;
HGLOBAL hRaw = ::GlobalAlloc(GHND, sizeof(RAWINPUT));
RAWINPUT* pRaw = reinterpret_cast<RAWINPUT*>(::GlobalLock(hRaw));

memset((void*)pRaw, 0, sizeof(RAWINPUT));
pRaw->header.dwType = RIM_TYPEMOUSE;
pRaw->header.dwSize = sizeof(RAWINPUT);
pRaw->header.hDevice = GetRawMouseDevice();  // can return the mouse HANDLE correctly
pRaw->header.wParam = wParamRawMouse;

RAWMOUSE rawMouseInfo;
memset((void*)&rawMouseInfo, 0, sizeof(RAWMOUSE));
rawMouseInfo.usFlags = MOUSE_MOVE_RELATIVE;
rawMouseInfo.lLastX = _mouse_position.x - _last_mouse_position.x;  // get the mouse's location change
rawMouseInfo.lLastY = _mouse_position.y - _last_mouse_position.y;
rawMouseInput.data.mouse = rawMouseInfo;
rawMouseInfo.ulExtraInformation = 0;
rawMouseInfo.ulButtons = 0;
rawMouseInfo.usButtonFlags = 0;
rawMouseInfo.ulRawButtons = 0;
pRaw = &rawMouseInput;

::GlobalUnlock(hRaw);

// mainGameWnd is the main window handle
PostMessageA(mainGameWnd, WM_INPUT, wParamRawMouse, (LPARAM)hRaw);
SendMessageA(mainGameWnd, WM_NCHITTEST, 0, MAKELPARAM(_mouse_position.x, _mouse_position.y));
SendMessageA(mainGameWnd, WM_SETCURSOR, (WPARAM)mainGameWnd, MAKELPARAM(HTCLIENT,WM_MOUSEMOVE));
PostMessageA(mainGameWnd, WM_MOUSEMOVE, 0, lParamMouseMove);
// ...

And I used spy++ to monitor my own program:

P WM_INPUT nInputCode:RIM_INPUT hRawInput:0000023822711808
S WM_NCHITTEST xPos:492 yPos:373
R WM_NCHITTEST nHittest:HTNOWHERE
S WM_SETCURSOR hwnd:001B0794 nHittest:HCLIENT wMouseMsg:WM_MOUSEMOVE
R WM_SETCURSOR fHaltProcessing:True
P WM_MOUSEMOVE fwKeys:0000 xPos:492 yPos:373

It didn’t work, when I moved the mouse, the character’s view didn’t change and the cursor just moved.
So how to correctly send the four message? Do some magics beyond sending message for free look in games? By the way, this question mentions camera, I think in this hooking situation, the camera is still handled by the game and I just need to send mouse-related message to the game. No need to consider the camera. Am I right?

python – I want to a generic algorithm to play TicTacToe. It keeps stagnating at a point where it loses games. Why doesn’t it advance further?

The AI is built with neat-python. The input is the gameboard as a list of fields (0:unoccupied, 1:player1, 2:player2). The AI produces 9 outputs and the index of the max(output) gets chosen as field to go. It trains against a bot who chooses fields randomly. If the field to go is already occupied, the first free field is being set as field to go.

import neat
import pygame
import os
import json
import random
import checkpoint
import winsound
import time
import matplotlib.pyplot as plt

pygame.init()
cloud = checkpoint.Checkpointer()
p = None
GEN = 0
screen = None
pygame.display.set_caption("Tic Tac Toe")
o_icon = pygame.image.load("o.png")
x_icon = pygame.image.load("x.png")

class Field:
    def __init__(self):
        self.field = (0,0,0, 0,0,0, 0,0,0)
        self.winner = None

    def reset(self):
        self.field = (0,0,0, 0,0,0, 0,0,0)
        self.winner = None

    def display(self):
        screen.fill((0,0,0))
        self.display_pieces()
        self.display_board()
        pygame.display.update()

    def display_pieces(self):
        pos_to_pix = {
            0: (0,0),
            1: (100,0),
            2: (200,0),
            3: (0,100),
            4: (100,100),
            5: (200,100),
            6: (0, 200),
            7: (100, 200),
            8: (200, 200)
        }
        for pos in range(len(self.field)):
            if self.field(pos) == 1:
                screen.blit(x_icon, pos_to_pix(pos))
            elif self.field(pos) == 2:
                screen.blit(o_icon, pos_to_pix(pos))

    @staticmethod
    def pos_to_field(pos):
        x = pos(0)
        y = pos(1)
        if x < 100:
            x = 0
        elif x < 200:
            x = 1
        else:
            x = 2
        if y < 100:
            y = 0
        elif y < 200:
            y = 1
        else:
            y = 2
        return x+y*3

    @staticmethod
    def display_board():
        pygame.draw.rect(screen, "White", (0,99, 300,2))
        pygame.draw.rect(screen, "White", (0,199, 300,2))
        pygame.draw.rect(screen, "White", (99,0, 2,300))
        pygame.draw.rect(screen, "White", (199,0, 2,300))

    def action(self, field_id, player_id):
        self.field(field_id) = player_id

    def field_is_free(self, field):
        if self.field(field) == 0:
            return True
        return False

    def check_winner(self):
        for line in range(3):
            # check horizontal
            if self.field(line*3) == self.field(line*3+1) == self.field(line*3+2) != 0:
                self.winner = self.field(line*3)
                return True, self.winner
            if self.field(line) == self.field(line+3) == self.field(line+6) != 0:
                self.winner = self.field(line)
                return True, self.winner
        if self.field(0) == self.field(4) == self.field(8) != 0:
            self.winner = self.field(0)
            return True, self.winner
        if self.field(2) == self.field(4) == self.field(6) != 0:
            self.winner = self.field(2)
            return True, self.winner
        return False

    def first_free(self):
        for index, value in enumerate(self.field):
            if value == 0:
                return index

class Player:
    def __init__(self, id, being, brain=None):
        self.id = id
        self.being = being
        self.brain = brain

class Graph:
    def __init__(self):
        self.gens = ()
        self.maxs = ()
        self.losts = ()
        self.drawns = ()
        self.wons = ()
        self.average = ()
        self.last_gens = 50
        self.vmax = None

    def add(self, gen, vmax_round, lost, drawn, won):
        self.gens.append(gen)
        self.maxs.append(vmax_round)
        self.losts.append(lost)
        self.drawns.append(drawn)
        self.wons.append(won)
        self.average.append(sum(self.maxs(-min(self.last_gens,gen):)) / min(self.last_gens,gen))
        self.vmax = max(self.maxs)

    def display(self):
        plt.figure(figsize=(10,6))
        plt.plot(self.gens, self.maxs, label="Best Fitness")
        plt.plot(self.gens, self.average, label="Average Fitness last " + str(self.last_gens) + " generations")
        plt.plot(self.gens, self.losts,  label="Lost")
        plt.plot(self.gens, self.drawns,  label="Drawn")
        plt.plot(self.gens, self.wons,  label="Won")
        plt.legend(bbox_to_anchor=(1.05, 1), loc="upper left")
        plt.grid("on")
        plt.tight_layout()
        plt.show()

    def save(self):
        with open('gens.txt', 'w') as filehandle:
            json.dump(self.gens, filehandle)
        with open('maxs.txt', 'w') as filehandle:
            json.dump(self.maxs, filehandle)
        with open('average.txt', 'w') as filehandle:
            json.dump(self.average, filehandle)
        with open('losts.txt', 'w') as filehandle:
            json.dump(self.losts, filehandle)
        with open('drawns.txt', 'w') as filehandle:
            json.dump(self.drawns, filehandle)
        with open('wons.txt', 'w') as filehandle:
            json.dump(self.wons, filehandle)

    def load(self):
        with open('gens.txt', 'r') as filehandle:
            self.gens = json.load(filehandle)
        with open('maxs.txt', 'r') as filehandle:
            self.maxs = json.load(filehandle)
        with open('average.txt', 'r') as filehandle:
            self.average = json.load(filehandle)
        with open('losts.txt', 'r') as filehandle:
            self.losts = json.load(filehandle)
        with open('drawns.txt', 'r') as filehandle:
            self.drawns = json.load(filehandle)
        with open('wons.txt', 'r') as filehandle:
            self.wons = json.load(filehandle)

graph = Graph()
board = Field()

def place_piece(player, field):
    board.field(field) = player.id

def sound():
    winsound.Beep(440,200)

def train(genomes, config):
    global GEN
    GEN += 1
    best_fitness = 0
    players = (None, None)
    best_lost, best_drawn, best_won = 0,0,0

    for index, genome in genomes:
        index -= 1
        genome.fitness = 0
        net = neat.nn.FeedForwardNetwork.create(genome, config)

        players(0) = Player(1, "random")
        players(1) = Player(2, "ai")
        lost, drawn, won = 0,0,0

        for round in range(100):
            if round == 50:
                players(0) = Player(1, "ai")
                players(1) = Player(2, "random")

            field_to_go = -1
            placed = 0
            player = players(0)

            while True:
                if player.being == "random":
                    field_to_go = random.randint(0,8)
                elif player.being == "ai":
                    output = net.activate(board.field)
                    field_to_go = output.index(max(output))
                elif player.being == "human":
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            pygame.quit()
                            break
                        elif event.type == pygame.MOUSEBUTTONUP:
                            field_to_go = board.pos_to_field(pygame.mouse.get_pos())

                if field_to_go > -1:
                    if board.field_is_free(field_to_go):
                        board.action(field_to_go, player.id)
                    else:
                        board.action(board.first_free(), player.id)

                    if player == players(0):
                        player = players(1)
                    else:
                        player = players(0)
                    placed += 1

                if shown:
                    board.display()
                    time.sleep(1)

                if board.check_winner() or placed == 9:
                    if board.winner is None:
                        genome.fitness += 1
                        drawn += 1
                    elif players(board.winner-1).being == "ai":
                        genome.fitness += 2
                        won += 1
                    else:
                        genome.fitness -= 0
                        lost += 1
                    board.reset()
                    break
        if genome.fitness > best_fitness:
            best_fitness = genome.fitness
            best_lost = lost
            best_drawn = drawn
            best_won = won
    graph.add(GEN, best_fitness, best_lost, best_drawn, best_won)

def test(genome):
    result = (0,0,0)
    players = (None, None)
    for round in range(1000):
        placed = 0
        if round % 2 == 0:
            players(0) = Player(1, "ai")
            players(1) = Player(2, "human")
        else:
            players(0) = Player(1, "human")
            players(1) = Player(2, "ai")

        player = players(0)
        while True:
            field_to_go = -1
            if player.being == "random":
                field_to_go = random.randint(0,8)
            elif player.being == "ai":
                output = genome.activate(board.field)
                field_to_go = output.index(max(output))
            elif player.being == "human":
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        break
                    elif event.type == pygame.MOUSEBUTTONUP:
                        field_to_go = board.pos_to_field(pygame.mouse.get_pos())

            if field_to_go > -1:
                if board.field_is_free(field_to_go):
                    board.action(field_to_go, player.id)
                else:
                    board.action(board.first_free(), player.id)
                if player == players(0):
                    player = players(1)
                else:
                    player = players(0)
                placed += 1

            if shown:
                pygame.event.get()
                board.display()
                time.sleep(0.75)

            if board.check_winner() or placed == 9:
                if board.winner is None:
                    result(1) += 1
                elif players(board.winner-1).being == "ai":
                    result(2) += 1
                else:
                    result(0) += 1
                if shown:
                    if board.winner is None:
                        print("None")
                    else:
                        print(players(board.winner-1).being + " " + str(player.id))
                board.reset()
                break
    print(result)

def run(config_path):
    global shown
    global GEN
    global screen
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
                                neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)
    nc_name = "population"

    testing = False
    shown = False
    new = True

    if testing:
        if shown:
            screen = pygame.display.set_mode((300, 300))
            pygame.display.set_caption("Tic Tac Toe")
        test(neat.nn.FeedForwardNetwork.create(cloud.restore_checkpoint("population").population.best_genome, config))
        quit()

    if new:
        p = neat.Population(config)
        p.add_reporter(neat.StdOutReporter(True))
        p.add_reporter(neat.StatisticsReporter())
    else:
        p = cloud.restore_checkpoint(nc_name).population
        graph.load()
    GEN = p.generation
    p.run(train,250)
    print(time.strftime("%H:%M:%S", time.localtime()))
    cloud.save_checkpoint(config, p, p.species, GEN, nc_name)
    graph.save()
    graph.display()
    sound()

if __name__ == "__main__":
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, "config_feedforward")
    run(config_path)

After 100 games the AI gets relatively fast to a score of 175. But it does not enhance from that point on. And still loses games what doesn’t happen when you play perfect. What am I doing wrong? Thanks for your help 🙂

enter image description here

procedural generation – How can we generate 3D architecture in a racing game’s background procedurally with respect/similar to the real world?

AFAIK they achieve this by

Parameterized Procedural Placement

They create a workflow where you create a handful assets in this case building floors and they procedurally tweak the parameters such as Floor count,size,tint and custom props such as air conditioning outside of the buildings.

I once a saw a video about ubisoft creating a procedural assasins creed city.They premade a bunch of houses and spread them randomly.

You should desing a workflow where you control how your building floors are stacked on each other and how each floor has its own specific unique props.

This is what i would do

  • I would decide what a building floor means to me.These would be my parameters: IsGroundFloor?, width and height of the floor, how many windows, a balcony?, general tint color of the floor material and unique set of models.(Such as vegatation, flowers, air conditioning or a satallite dish perhaps?).
  • I would decide how many floors should a building has.Define the minimum and the maximum
  • I would decide what will be the floor independent objects such as fire escape ladder which starts from ground to the top of the building, some garden? or fences?

with these parameters one can create wide range of different buildings considering you feed your procedural workflow with enough assets

Do Video Games Damage Our Brains for $15

Do Video Games Damage Our Brains

In the event that you play computer games much of the time, your cerebrum changes – the a greater amount of the supposed dark mass is available in the hippocampus of the mind, the better the idea organ is. The less of them are available, the higher the danger of building up a mind sickness.

The PC game League of Legends is a marvel:

The PC game “Association of Legends” is a wonder: around 100 million individuals overall play it, routinely “Haha”, as it is said among experts. The game, in which ordinarily two groups with five individuals each go up against one another. The standards are perplexing, and winning against different players requires vital reasoning.

Examination affirms numerous a beneficial outcome:

The examination distributed in mid-November 2017 of the University of York in England in Plos One magazine. Regardless of whether the players got more brilliant during that time before the screen or whether it basically helps more astute parts in the class can’t be replied, says therapist Alexander Wade:

We bet on the last mentioned.

PC games are a typical side interest for the youngsters of today. Examination on the impacts of PC betting is additionally prospering and creating many investigations consistently. All things considered, they need to realize how mind and conduct are impacted by this mass marvel.

As opposed to mainstream thinking, the outcomes ordinarily lead to PC games improving the mind capacities included. It is very evident that numerous games don’t improve the whole IQ, however improve singular mind capacities.

Just later did researchers show the constructive outcomes of betting. The individuals who play routinely for an hour are better at rapidly getting circumstances, producing new information and arranging what they have realized into classes.

The justification this is an expanded action in the hippocampus, a region that is significant for learning and might be prepared through PC games. In any case, through extreme playing, a normal of fourteen hours out of each week, the purported dark cerebrum substance endures in the gamer – diminished in places. This is in the orbital cerebrum, which has a place with the front facing projection answerable for higher errands. The more inordinate her game was, the more noteworthy the misfortune.

Just, what’s the significance here? The volume of the dark matter in which the nerve cells of the cortex sit fluctuates incredibly throughout the span of life and relies upon various variables – it is hard to say whether a change is positive or negative.

.

online dungeons-and-dragons – Role-playing Games Stack Exchange


Your privacy


By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.




Is there a concept of “pasted on theme” in video games?

If we are talking about theme, it isn’t much of an issue per se. Let us say, it is a risk. Will come back to that.

It is a good idea to prototype. And prototype until it is fun. Without final art. In fact, it is a good idea to do that per mechanic. So you know they are intrinsically engaging, not depending on art (e.g. it works even without music and cool graphics) and not depending on extrinsic rewards (e.g. achievements). By the way, those prototypes might include particle effects, camera shakes and other stuff.


I’ll be assuming that the game has a player character, avatar of sorts. However, similar arguments would work for strategy or tactic games.

Which also remind me… Even though you would have a theme. That does not mean you need a story. At most you need an excuse. In fact, the characters are more important for players enjoyment than the lore or the worldbuilding. Just make a good player character or characters. Give them some motivation. And perhaps some opinions on the world. And give them a problem. So they have an excuse to go do whatever.


The risk I mentioned before can be expressed with a term “Ludonarrative Dissonance”. This is a term put forth by critics, and often consider a bad thing. To be fair, the games accused of it, still sold well, so it is not a deal breaker. Futhermore, it can be an strength of a game, for example as a tool for satire.

The term “Ludonarrative Dissonance” is about the mechanics and the narrative working against each other. Although it isn’t exactly lack of Ludonarrative Harmony, which would be having them in agreement. There is an excluded middle, where mechanics and the narrative don’t work together, but don’t work against each other either.

The mechanics (have incentives that) encourage a certain behavior. You can think of your rewards and progression systems as a black box that the players are testing and learning about, as they explore the mechanics. This is a way in which the game communicates what to do to the player.

Meanwhile, the game may tell or show or suggest, what behavior is correct (and I don’t mean tutorial text, but narrative, character motivation, and plot). That is, the narrative and theme would inform what is “in character” for the player character to do. And thus this is another way the game communicates what to do to the player.

These are two messages that the game delivers to the player. Which may agree, complement or contradict each other.

Also consider that the player point of view is not a perfect match for the point of view of the characters in the story, and may care about different things than the character would care about in universe. One reason this might happen is because the player cares about the mechanics and rewards, which are often not diegetic and might be misaligned with the narrative.

See also:


One takeaway is that the game should give incentives that make the player behave as if the player cares about the thing the character cares. And should prevent or discourage the player from doing things that break the theme or the narrative.

A designer could have the character background (or at least their archetype) inform what should be easy and what should be hard for the character. Similarly, could have the intended character arc inform on what what should become easier or harder. And match the difficulty and progression of the game to those ideas. Furthermore, the designer could consider what the character wouldn’t do, and either prevent them or discourage them mechanically.

However, that advice is easier if you start with narrative and theme. Since you are starting from the mechanics. And assuming you are not changing them. I suggest you think carefully about what behavior do the mechanics encourage, and then think what theme and narrative is a good fit. That is, try to reverse engineer what would be the theme and narrative that leads to the mechanics you have. A tool at your disposal is you -and play testers- can play the game. Bring what makes you think about while playing, an how it makes you feel, and give it context in the form a narrative.

I’ll remind you that realism is not important. Games don’t have to pass a reality check. What you want is believability. Verisimilitude.

Also remember that whatever mechanics you choose would put your game in a mechanic genre. While you could match any mechanic genre with any theme. Some are easier to make work. That also means there would likely be plenty of similar games.

why aren’t games developed by TRNG?

Most if not all games are developed by pseudo-random numbers because they are efficient. Aside from the efficiency, why don’t game developers use true random number generators to create games?

product identification – What Roleplaying Games Come/Came With a Video?

I seem to recall an introductory D&D box from the mid 1990’s that had a video.

There were several D&D boxes with audio CD’s; One was a starter set, called “First Quest” (The others were supplements.)

The Dragon Strike game was released by TSR, has a hybrid play mode (That is, it’s a board game but uses a GM, and can be used as an RPG; to the non-RPGer, it can be easily mistaken for an RPG. THis is the most likely one to be so confused. It has a “Learn to Play” VHS cassette.

The DragonRaid RPG also has an audio CD to assist new players. There was, at one point, discussion of a VHS cassette showing as well as telling, but I don’t know if it got off the ground. (And while not a bad game, it’s definitely very focused on a Christian allegory mode similar to Narnia, but more obvious about it. (My copy predated the CD version, and had audio cassette.)

Il gioco di ruolo di Ken il Guerriero was sold bundled with the VHS of Fist of the Northstar in Italy; the Game doesn’t use the video, but is based upon the video.

Weird mouse behavior in games (official Steam client)

I’m playing Counter-Strike Source on KDE Neon 5.21, within official Steam client.
What I noticed is that in game mouse sometimes doesn’t react to left/right clicks at all – or the mouse button seems to jam, so the weapon I’m using continues to fire despite I lifted the finger.

Problem is not hardware, as it occurs for both mouses I’m using + doesn’t happen when playing the same game in Windows.