Recent

Author Topic: Fairtris — a fair implementation of Classic Tetris®  (Read 16053 times)

josh

  • Hero Member
  • *****
  • Posts: 860
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #15 on: November 19, 2021, 10:08:17 pm »
hi

slight problem acronis security guard is reporting the following, when it is re-run.
not schecked but as its ini files, it would appear as though all are being modified on startup, causin it to fire.

checked with malwarebytes and virustotal all ok

Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #16 on: November 20, 2021, 12:18:09 am »
Thanks for feedback.

slight problem acronis security guard is reporting the following, when it is re-run.
not schecked but as its ini files, it would appear as though all are being modified on startup, causin it to fire.

IMO this is a ordinary false-positive — there is no reason to consider this game's files as a threat. The game is clean and contains no malware. If you would like to, you can download the sources and compile yourself, and check everything for security — the sources are open. 8)
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #17 on: December 05, 2021, 12:43:12 am »
New version available to download — Fairtris 2.3.0.1


Video mode and multi-screen support improved

This version introduces fixes related to video mode support, its resolution and aspect ratio. From now on, the video mode is always launched on the main screen and always in the current resolution (forcing to use 800×600 resolution for video mode has been removed). Additionally, unlike the previous version, regardless of the mode, the game image is always rendered in 4:3 aspect ratio, so it always looks correct, regardless of resolution and display mode.

Disabling the video mode has also changed. Previously, the window position was restored in this case, and in the latest version the window is always centered on the main screen. The windowed mode has not changed — it is still possible to move the window to any screen, as well as use the windowed full-screen mode also on any screen and with any resolution.
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

Roland57

  • Full Member
  • ***
  • Posts: 227
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #18 on: December 05, 2021, 08:08:57 am »
Very nice project. Thank you for sharing.

I tried a quick and dirty adaptation for Linux: commented out the Windows specific code, replaced directory separator in files path. Now I get an "invalid floating point operation" immediately after application start. Maybe a division by zero, because of the code removed, but I have not yet found where.

I tried to replace 0 with 1 in Fairtris.Clock.pp, but the error still occurs.

Code: Pascal  [Select][+][-]
  1. function TClock.GetCounterFrequency(): Int64;
  2. begin
  3.   Result := {0}1;
  4.   //QueryPerformanceFrequency(Result);
  5. end;
  6.  
  7.  
  8. function TClock.GetCounterValue(): Int64;
  9. begin
  10.   Result := {0}1;
  11.   //QueryPerformanceCounter(Result);
  12. end;

I cannot use the terminal for debugging, because of the fullscreen mode.

When I have time, I will make another more serious attempt.

furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #19 on: December 05, 2021, 06:26:43 pm »
For Linux and other Unix platforms, a slightly different approach should be used. QueryPerformanceFrequency and QueryPerformanceCounter do not exist on these platforms, so you need to use e.g. other functions. The class of this clock was taken from the Deep Platformer project — in this project the clock class is adapted to Linux. These functions look like this:

Code: Pascal  [Select][+][-]
  1. // from the Deep Platformer sources
  2.  
  3. function TClock.GetCounterFrequency(): Int64;
  4. begin
  5.   {$IFDEF WINDOWS}
  6.   Result := 0;
  7.   QueryPerformanceFrequency(Result);
  8.   {$ELSE}
  9.   Result := 1000000000;
  10.   {$ENDIF}
  11. end;
  12.  
  13. function TClock.GetCounterValue(): Int64;
  14. {$IFDEF UNIX}
  15. var
  16.   Counter: TTimeVal;
  17. {$ENDIF}
  18. begin
  19.   {$IFDEF WINDOWS}
  20.   Result := 0;
  21.   QueryPerformanceCounter(Result);
  22.   {$ELSE}
  23.   FPGetTimeOfDay(@Counter, nil);
  24.   Result := Int64(Counter.tv_sec) * 1000000000 + Int64(Counter.tv_usec) * 1000;
  25.   {$ENDIF}
  26. end;

In the thread hold method, the number of nanoseconds is calculated and the thread is frozen using the FPNanoSleep function. In the case of Unix, busy waiting is not needed, because FPNanoSleep supports precision down to a nanosecond (actually a microsecond, but that's enough), while under Windows, Sleep has only a millisecond precision, which is not enough — that's why busy waiting is used additionally:

Code: Pascal  [Select][+][-]
  1. // from the Deep Platformer sources
  2.  
  3. procedure TClock.WaitForNMI();
  4. var
  5.   {$IFDEF WINDOWS}
  6.   SleepTime: Single;
  7.   {$ELSE}
  8.   SleepTime: Int64;
  9.   RequestedTime, RemainingTime: TTimeSpec;
  10.   {$ENDIF}
  11. begin
  12.   if not Machine.Stopped then
  13.   begin
  14.     {$IFDEF WINDOWS}
  15.     if not FPrecise then
  16.     begin
  17.       SleepTime := 1000 / FFrameRateLimit * (1 - (FFrameTicksEnd - FFrameTicksBegin) / FTicksPerFrame) - 1;
  18.       SleepTime -= Ord(Round(SleepTime) > SleepTime);
  19.       SleepTime := Max(SleepTime, 0);
  20.  
  21.       Sleep(Round(SleepTime));
  22.     end;
  23.  
  24.     while GetCounterValue() < FFrameTicksNext do ;
  25.     {$ELSE}
  26.     SleepTime := FFrameTicksBegin + FTicksPerFrame - FFrameTicksEnd;
  27.  
  28.     if SleepTime > 0 then
  29.     begin
  30.       RemainingTime.tv_sec := SleepTime div 1000000000;
  31.       RemainingTime.tv_nsec := SleepTime mod 1000000000;
  32.  
  33.       repeat
  34.         RequestedTime := RemainingTime;
  35.  
  36.         if FPNanoSleep(@RequestedTime, @RemainingTime) = 0 then Exit;
  37.         if FPGetErrNo() <> ESysEINTR then Exit;
  38.       until False;
  39.     end;
  40.     {$ENDIF}
  41.   end
  42.   else
  43.     Sleep(50);
  44. end;

When using SDL under Windows, the Sleep function does not suspend the thread's work (I don't know why), so the specified number of milliseconds is frozen by the SDL_Delay function, which also has millisecond precision. The remainder is waited with busy waiting, calling the assembly pause instruction in loop:

Code: Pascal  [Select][+][-]
  1. // from the Fairtris sources
  2.  
  3. procedure TClock.UpdateFrameAlign();
  4. var
  5.   SleepTime: Single;
  6. begin
  7.   // calculating the number of milliseconds
  8.   SleepTime := 1000 / FFrameRateLimit * (1 - (FFrameTicksEnd - FFrameTicksBegin) / FTicksPerFrame) - 1;
  9.   SleepTime -= Ord(Round(SleepTime) > SleepTime);
  10.   SleepTime := Max(SleepTime, 0);
  11.  
  12.   // suspending the thread's work for a given number of milliseconds
  13.   SDL_Delay(Round(SleepTime));
  14.  
  15.   // suspending the thread's work for the remaining number of nanoseconds (busy waiting)
  16.   while GetCounterValue() < FFrameTicksNext do
  17.   asm
  18.     pause
  19.   end;
  20. end;

So, to make the clock compatible with Linux, you just need to use the fixed value 1,000,000,000 as the number of ticks of the clock (ticks per one second) and find the current time with high precision using the FPGetTimeOfDay function and simple calculations as I showed in the first code snippet.

However, to suspend the work of the thread for a given amount of time (a few or several milliseconds and the microsecond part), use the above code, i.e. simple calculations and the FPNanoSleep function in a loop. If the FPNanoSleep function will not stop the thread (like Sleep in Windows), then do the same as in Windows — calculate the millisecond part and freeze the thread with SDL_Delay, and eat the remaining (microsecond) part with busy waiting (loop checking the current number of ticks and the pause command).

So, probably the only thing that needs to be changed in the Fairtris code is the contents of the GetCounterFrequency and GetCounterValue methods, using the code taken from the clock in the Deep Platformer game. The UpdateFrameAlign method will most likely not need to be changed. And if FPNanoSleep can freeze the thread, then it should be used to avoid busy waiting and save even more computing power.



I cannot use the terminal for debugging, because of the fullscreen mode.

If you need the game not to use the exclusive video mode, just open the bin\settings.ini file and change its value:

Code: INI  [Select][+][-]
  1. [VIDEO]
  2. ENABLED=1 ; set "0" to disable video mode
  3.  
  4. [GENERAL]
  5. MONITOR=0
  6. LEFT=0
  7. TOP=0
  8. INPUT=0
  9. WINDOW=2
  10. THEME=0
  11. SOUNDS=0
  12. SCROLL=0
  13. REGION=0
  14. RNG=0
  15. LEVEL=0
  16.  
  17. ; ...

Additionally, you can change the initial window size by changing the WINDOW value to something else — 0 is the smallest window; 1, 2 and 3 are zoomed windows; and 4 is full screen in windowed mode (without exclusive video mode). The default value is 2, which is Zoom 3x (878×720 pixels).

Exclusive video mode can be toggled with the F11 key, while other options can be modified in the in-game settings screen and gameplay settings screen (and also using the mouse). It is not necessary to manually modify the contents of the configuration file.
« Last Edit: December 07, 2021, 04:42:23 pm by furious programming »
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

Roland57

  • Full Member
  • ***
  • Posts: 227
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #20 on: December 05, 2021, 08:41:03 pm »
@furious programming

Thank you for the detailed answer.

So, probably the only thing that needs to be changed in the Fairtris code is the contents of the GetCounterFrequency and GetCounterValue methods, using the code taken from the clock in the Deep Platformer game.

Yes, it works. :)

And if FPNanoSleep can freeze the thread, then it should be used to avoid busy waiting and save even more computing power.

No idea about that.

If you need the game not to use the exclusive video mode, just open the bin\settings.ini file and change its value:

OK, thank you.

It's great to have such a well written project using SDL2. Very good job!

Regards.

Roland


furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #21 on: December 06, 2021, 12:37:12 am »
This project was created to check what classic Tetris would look like if all the problems were fixed, and also to learn how to create games using SDL library. I think SDL is a great library, lightweight, easy to use and very efficient, so it's worth using, especially in conjunction with headers for Free Pascal and Lazarus IDE. I'm glad that you like it. 8)

The knowledge gained while creating Fairtris will be the foundation for my big game, which I intend to create in the coming years — with Free Pascal, of course. And for those who want to know more, this project provides a pretty good knowledge base and a collection of practical examples.

Not everything is done properly (some copy-paste, no comments etc) but it's just a fun project. Besides, this game works the same as the NES console (the core game logic was translated directly from the assembler), and this approach is unlikely to be used in PC games. So this game should not be considered a foundation for modern games.
« Last Edit: December 06, 2021, 07:51:34 pm by furious programming »
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

Roland57

  • Full Member
  • ***
  • Posts: 227
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #22 on: December 07, 2021, 08:57:07 pm »
@furious programming

I don't want to tell you what you have to do, but why don't you include in the source code the modifications for Linux compatibility?

P.-S. Announced Fairtris on French-speaking Lazarus forum.
« Last Edit: December 07, 2021, 09:03:55 pm by Roland57 »

furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #23 on: December 08, 2021, 01:24:07 am »
I don't want to tell you what you have to do, but why don't you include in the source code the modifications for Linux compatibility?

Because I don't know Linux at all and I don't use it, so I don't add to the source code the operation of which I have no way to check. So I'm doing the Windows version myself, and if there's someone who knows and uses Linux, they can create a fork, remove the Windows-related code, and develop the version for other platforms separately.

Quote
P.-S. Announced Fairtris on French-speaking Lazarus forum.

Nice!
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #24 on: December 12, 2021, 10:34:01 pm »
Yet another version is released — Fairtris 2.3.1.2

In this version the only change is the improved SDL subsystem initialization, so that Fairtris only uses initialization of the required SDL modules (video, audio, joystick and events). This prevents the problem with initialization of the "sensor manager" module, which occurs on some computers.
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #25 on: December 28, 2021, 04:36:59 pm »
I am preparing a new, third version of the game — all because in the coming weeks we want to organize an online tournament in Fairtris between the league seasons. And to be able to play matches under fair conditions, I need to provide both players with the same set of pieces, and to provide it, I need to add the option to enter seed for RNG to the game.

That's why I decided to add a new menu and give you the option to choose the game mode. The single-player mode remains unchanged, but in addition there will be a qualification and match mode for the tournament on points, as well as a qualification and match mode for the tournament in speedrun. I already have the mode selection menu programmed (first attachment).

The single "single player" scene menu hasn't changed, and the other four will look like on the second attachment.

For qualifications, you will be able to set a countdown timer, while for a tournament match you will be able to type the seed for pieces generator, which is usually drawn using a tetris-bot in the Twitch chat. The game interface for the gameplay scene will be properly prepared for these modes to display the current speedrun timer, etc. See the third attachment — on the left a match for points, on the right a speedrun match.

The core of the game remains one and it will be used regardless of the selected mode — all I need to do is add an additional code to it, which will take into account the time calculation, add a "kill-screen" at the 19 level for speedrun etc. In addition, the renderers will slightly expand so that they paint additional data. The new version should be ready this year.
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #26 on: January 07, 2022, 10:25:27 pm »
And finally there is a fresh version — Fairtris 3.0.0.2 beta 8)

The main novelty in this version is the expansion of the game modes from one to six. Now the game is divided into two main categories — a marathon, the aim of which is to collect as many points as possible, and speedrun, in which you should reach the level 0 to 19 as quickly as possible (i.e. clear 190 lines) in no more than ten minutes.

In addition, the marathon and speedrun qualification mode has also been implemented, which allows you to set a qualifications timer (the counter is visible during the game and in several menus). In addition to qualifying, there is also a match mode, in which the generator seed is set in order to have the same set of pieces for both players, so that the duel is fair. Seed can be entered manually from the keyboard or generate a random one (you can also copy it to the system clipboard and paste from it, using standard keyboard shortcuts).

All results are collected in two main lists (marathon scores and speedrun times), regardless of the game mode selected (casual play, qualifying or match). Additionally, the game has temporary lists for qualifiers and matches to make it easier to show how qualifying was done and how the effectiveness of match games looked like.

A completely new thing is the ability to choose the control mechanics — modern mechanics are default, characterized by high responsiveness and greater possibilities, as well as classic, in line with the original mechanics from Tetris for the NES console. Generator performance has also been improved, rendering of some menus has been improved, and some new sounds have been added.

The full functionality of the new version of Fairtris is described in the project wiki.
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

lainz

  • Hero Member
  • *****
  • Posts: 3884
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #27 on: January 07, 2022, 11:17:53 pm »
Thanks, very good game.

I just did a change on the default configuration, switched the up key with the b key, so I can play with only one hand, like in the tetris for facebook game.

furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #28 on: January 08, 2022, 04:10:29 pm »
This is what the keyboard mapping screen is for, so that everyone can conveniently set the controls the way they like. Although I suggest playing on the USB controller anyway, preferably with a shape compatible or similar to the NES controller.
« Last Edit: January 09, 2022, 05:26:32 pm by furious programming »
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

furious programming

  • Hero Member
  • *****
  • Posts: 534
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Fairtris — a fair implementation of Classic Tetris®
« Reply #29 on: January 11, 2022, 09:44:54 pm »
If anyone wanted to see Fairtris and professional players in action, there was a friendly match a few days ago where Xenophilius faced Mecex. The Mecex had tested the Fairtris before and got used to the timing and did much better because of it—at least when it comes to steering. 8)

Warm-up and free talk:
https://www.twitch.tv/videos/1257311433?t=1h49m3s

Match:
https://www.twitch.tv/videos/1257311433?t=2h1m7s
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

 

TinyPortal © 2005-2018