OpenTTD in Browser (Working port)

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
ShadowTheAge
Engineer
Engineer
Posts: 2
Joined: 23 Mar 2014 19:07
Skype: Same as nick
Location: St. Petersburg, Russia

OpenTTD in Browser (Working port)

Post by ShadowTheAge »

Hello, OpenTTD developers and others!
I am currently working on OpenTTD port to flash. Flash has a poorly supported project called "Crossbridge" that provides a toolchain for compiling c/c++ code into llvm, which allows you to execute it in flash player.
However, flash is VERY different in comparison to OS so basically everything should be simulated: Filesystem, video system, sound, input, etc.
Also, crossbridge is buggy and i'm not the c/c++ developer (i'm ActionScript developer).
Also, the linker... It is sooo sloooow. Something changed? Wait 10 minutes pls...
And also, the lack of debugging capabilities

I want to give you a quick pick on what is going on :)
>>> You may check my current progress HERE <<<
You need to allow Flash to store data on your PC (small window opens at first start). This storage is used to keep your saves, config, and resources (like grf) in future.
I've packaged OpenGFX and a bunch of AIs
I recommend you to change interface-interaction-"Reverse scroll direction" option as the game loads.

Okay, now more details:
Current condition
Working alpha. Some hard things are done, a lot more to do.
The game is laggy at the beginning. Flash has some dynamic bytecode process - maybe JIT, baybe some security check. So, first execution of every piece of code is slow. You can see this behavior when you are dragging map - it lags but only at start.

Video
I've used SDL that comes with crossbridge. It works, but it is suboptimal (very). It simulates VGL VESA display, and forms image in memory (not even in actual memory but in a virtual machine memory).
The other possibility is to implement custom SDL backend (is hard because SDL is not as simple as it called)
Or to implement custom video driver inside OpenTTD, which seems easier, if only someone explains me how openttd blitter works

Input
Input is done also through VGL
Mouse is working, but it delays a bit
Keyboard is partially working. Command keys are working (arrows for example) while letters don't. Console could be opened by pressing 1.

Virtual file system
Is done. Any changed files stored in flash cache (LocalStorage) on your PC.

Audio
Not done at all. Is possible but I currently have no idea.

Network
Not done at all. Also possible (flash has support of TCP) but also flash has a very annoying security system - Before establishing a connection flash sends a security request and server must respond and allow access. So:
- To connect to openttd servers, openttd server musl listen another port and respond to flash security requests
- Same with online content distribution system
- It is possible to implement content loading with browser and transferring it to flash using ExternalInterface. But only http direct links and no tcp sockets. However, in this case, it may be required to modify openttd online content loader significantly.

Bugs
Game sometimes freezes competely. Most likely when doing some long and heavy operation but it can freeze at any time.
Also game used to crash 100% reproducible when displaying news in function news_gui.cpp - NewsTypeData::GetDisplay. When i've added logs to this function to determine crash source, crashes disappears. I've removed logs - crashes appears again. So i kept logs for now.

Misc
Flash philosophy is very different in comparison to C++. While in c++ all code executes inside main() function, flash uses asynchronousity, events and frames (you execute code only within a frame).
There are two ways to run c++ application. The most simple one is to run it in separate thread (this does not require any modification to c++ code). But flash is single-threaded, so "another thread" is basically another copy of flash runtime that does not share memory and interacts with main "thread" via messages and some other stuff. This way it is very memory-inefficient (every "thread" uses about 100 mb of ram) and it starts slowly. That's why i compiled openttd without threads, but i need to use one to run c++ code.
Another way is to initialize everything in main() and call ActionScript code (as last main() command) which never returns. Flash will continue work as normal and it is possible to call another function every once in a while, which will be a single pass of main game loop. This would be much more reliable but it requires mass changes to main(). main() should be VERY simple, just basic initialization. Any other initialization should be split in parts so code can interrupt between parts.

PS
I will add sources to GitHub, maybe tomorrow.
To save your settings you must exit game, even if it does no sense in browser :D
Last edited by ShadowTheAge on 23 Mar 2014 23:20, edited 2 times in total.
DeletedUser6
Transport Coordinator
Transport Coordinator
Posts: 294
Joined: 23 Jul 2007 15:13
Location: Somewhere in this page

Re: OpenTTD in Browser (Working port)

Post by DeletedUser6 »

Wow.

That's going to make ICT lessons a little more bearable...

It's not exactly butter-smooth, but it is certainly playable.

Matthew:out
Yay! I'm back! Did you miss me?


...


I thought not.
TrueSatan
Transport Coordinator
Transport Coordinator
Posts: 291
Joined: 16 Jul 2003 18:33

Re: OpenTTD in Browser (Working port)

Post by TrueSatan »

Nice :)
To bad it says no network. But it runs okay in my chromium browser (iron).
Maybe it would be best to pinpoint some setting like size of map etc.
So you can play with it nicely.
If the network code works someday it could be some nice mmo browser game :)
first time i would consider to play online *lol*

Hope to see more. Good job so far. :bow:
ShadowTheAge
Engineer
Engineer
Posts: 2
Joined: 23 Mar 2014 19:07
Skype: Same as nick
Location: St. Petersburg, Russia

Re: OpenTTD in Browser (Working port)

Post by ShadowTheAge »

Glad you like it!

ZirconiumX
As I said, map-drag lag disappears in a few minutes. It is possible to speed up video with a custom driver, but because it works now it is a low-priority task

TrueSatan
Unfortunately, network is the most trickiest part here. It is not enough to pass network through flash, content and game servers should also handle connection from flash as necessary
User avatar
FLHerne
Tycoon
Tycoon
Posts: 1543
Joined: 12 Jul 2011 12:09
Location: St Ives, Cambs, UK

Re: OpenTTD in Browser (Working port)

Post by FLHerne »

A similar thing has already been done (with dubious trademark/copyright infringement :? ) using Emscripten to cross-compile to Javascript.

That also suffers from being ludicrously slow, and I can't remember what features were broken.
Temporary Permanent signature filling text. Content coming soon delayed indefinitely! Oh, and I have had a screenshot thread.
Linux user (XMonad DWM/KDE, Arch), IRC obsessive and rail enthusiast. No longer building robots; now I ring church bells.
Author of an incredibly boring stickied post about NewGRFs.
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Re: OpenTTD in Browser (Working port)

Post by SirkoZ »

Very nice effort!
And it isn't slow at all. Quite playable. Well - anyone here tried playing even TTOriginal on a 386 25MHz? Now that is slow and barely bearable. :]
Eddi
Tycoon
Tycoon
Posts: 8267
Joined: 17 Jan 2007 00:14

Re: OpenTTD in Browser (Working port)

Post by Eddi »

SirkoZ wrote:anyone here tried playing even TTOriginal on a 386 25MHz?
oooh yes :p

i think i never got much beyond 1940. (starting 1930)
User avatar
sickie
Engineer
Engineer
Posts: 85
Joined: 15 Sep 2007 00:51
Location: Slovenia
Contact:

Re: OpenTTD in Browser (Working port)

Post by sickie »

Did you make any more progress? Is it still in development or is this it?
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: Baidu [Spider] and 39 guests