Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rubberduckycooly
GitHub Repository: rubberduckycooly/Sonic-1-2-2013-Decompilation
Path: blob/main/RSDKv4/NativeObjects/MultiplayerHandler.cpp
817 views
1
#include "RetroEngine.hpp"
2
3
#if !RETRO_USE_ORIGINAL_CODE && RETRO_USE_NETWORKING
4
void MultiplayerHandler_Create(void *objPtr)
5
{
6
RSDK_THIS(MultiplayerHandler);
7
8
self->pingLabel = CREATE_ENTITY(TextLabel);
9
self->pingLabel->fontID = FONT_TEXT;
10
self->pingLabel->x = SCREEN_XSIZE_F / 2 - 64.0f;
11
self->pingLabel->y = -(SCREEN_YSIZE_F / 2) + 4.0f;
12
self->pingLabel->alignPtr(self->pingLabel, ALIGN_LEFT);
13
self->pingLabel->scale = 0.175;
14
self->pingLabel->useColors = true;
15
self->pingLabel->r = 0xFF;
16
17
self->state = 1;
18
}
19
void MultiplayerHandler_Main(void *objPtr)
20
{
21
RSDK_THIS(MultiplayerHandler);
22
char buf[0x30];
23
if (dcError && self->state != 3)
24
self->state = 2;
25
26
switch (self->state) {
27
case 0:
28
if (!(activeStageList & 1)) {
29
self->state = 1;
30
break;
31
}
32
33
self->timer += Engine.deltaTime;
34
if (self->pingLabel->alignOffset) {
35
self->pingLabel->alignPtr(self->pingLabel, ALIGN_LEFT);
36
self->pingLabel->x -= 28.0f;
37
}
38
39
if (self->timer >= 0.25f && !waitingForPing) {
40
waitingForPing = true;
41
self->timer = 0;
42
if (lastPing < 800.0f) {
43
sprintf(buf, "Ping: %.1fms", lastPing);
44
self->pingLabel->g = 0xFF;
45
self->pingLabel->b = 0xFF;
46
}
47
else if (lastPing < 2000.0f) {
48
self->pingLabel->g = 0xFF;
49
self->pingLabel->b = 0x30;
50
sprintf(buf, "Ping: %.2fs", lastPing / 1000);
51
}
52
else {
53
self->pingLabel->g = 0x30;
54
self->pingLabel->b = 0x30;
55
sprintf(buf, "Ping: %.0fs", self->timer);
56
}
57
SetStringToFont8(self->pingLabel->text, buf, FONT_TEXT);
58
}
59
else if (self->timer >= 10.0f) {
60
DisconnectNetwork();
61
dcError = 2; // timeout
62
self->state = 2;
63
vsPlaying = false;
64
}
65
else if (self->timer >= 5.0f && waitingForPing) {
66
self->pingLabel->g = 0xCF * (fmod(self->timer, .5) >= .25) + 0x30;
67
self->pingLabel->b = 0xCF * (fmod(self->timer, .5) >= .25) + 0x30;
68
self->pingLabel->alignPtr(self->pingLabel, ALIGN_CENTER);
69
self->pingLabel->x += 28.0f;
70
sprintf(buf, " !! %.2fs !!", 10 - self->timer);
71
SetStringToFont8(self->pingLabel->text, buf, FONT_TEXT);
72
}
73
SetRenderBlendMode(RENDER_BLEND_ALPHA);
74
RenderRect(SCREEN_XSIZE_F / 2 - 68, -(SCREEN_YSIZE_F / 2) + 16, 160, 68, 16, 0, 0, 0, 0x80);
75
// FallThrough
76
case 1:
77
// wait for regular
78
if (!vsPlaying) {
79
if (dcError) {
80
self->state = 2;
81
break;
82
}
83
RemoveNativeObject(self->pingLabel);
84
RemoveNativeObject(self->errorPanel);
85
RemoveNativeObject(self);
86
break;
87
}
88
else if (activeStageList & 1)
89
self->state = 0;
90
break;
91
92
case 2: {
93
// display error
94
StopMusic(true);
95
RemoveNativeObject(self->pingLabel);
96
RemoveNativeObjectType(RetroGameLoop_Create, RetroGameLoop_Main);
97
RemoveNativeObjectType(VirtualDPad_Create, VirtualDPad_Main);
98
RemoveNativeObjectType(FadeScreen_Create, FadeScreen_Main);
99
100
self->fadeError = CREATE_ENTITY(FadeScreen);
101
self->fadeError->state = FADESCREEN_STATE_FADEOUT;
102
self->errorPanel = CREATE_ENTITY(DialogPanel);
103
self->errorPanel->buttonCount = DLGTYPE_OK;
104
char *set = NULL;
105
switch (dcError) {
106
case 1: set = (char *)"The other player has disconnected.\rReturning to title screen."; break;
107
case 2: set = (char *)"Connection timed out.\rReturning to title screen."; break;
108
case 3: set = (char *)"This room is full.\rReturning to title screen.";
109
// fallthrough
110
case 5:
111
if (!set)
112
set = (char *)"This room doesn't exist.\rReturning to title screen.";
113
self->fadeError->timer = self->fadeError->delay;
114
for (int i = 0; i < nativeEntityCount; ++i) {
115
if (objectEntityBank[activeEntityList[i]].eventMain == MultiplayerScreen_Main) {
116
MultiplayerScreen_Destroy(&objectEntityBank[i]);
117
break;
118
}
119
}
120
break;
121
case 4: set = (char *)"Couldn't connect after 10 retries.\rReturning to title screen."; break;
122
}
123
SetStringToFont8(self->errorPanel->text, set, FONT_TEXT);
124
self->state = 3;
125
dcError = 0;
126
}
127
// FallThrough
128
case 3:
129
RenderRetroBuffer(256, 160);
130
if (self->errorPanel->state == DIALOGPANEL_STATE_EXIT)
131
self->errorPanel->state = DIALOGPANEL_STATE_IDLE;
132
if (self->fadeError->timer >= self->fadeError->delay) {
133
RenderRect(-SCREEN_CENTERX_F, SCREEN_CENTERY_F, 160.0, SCREEN_XSIZE_F, SCREEN_YSIZE_F, 0, 0, 0, 255);
134
}
135
136
if (!self->fade) {
137
if (self->errorPanel->selection) {
138
self->fade = CREATE_ENTITY(FadeScreen);
139
self->fade->state = FADESCREEN_STATE_FADEOUT;
140
}
141
}
142
143
else if (self->fade->timer > self->fade->delay) {
144
ClearNativeObjects();
145
Engine.nativeMenuFadeIn = false;
146
if (skipStartMenu) {
147
activeStageList = 0;
148
stageMode = STAGEMODE_LOAD;
149
Engine.gameMode = ENGINE_MAINGAME;
150
stageListPosition = 0;
151
CREATE_ENTITY(RetroGameLoop);
152
if (Engine.gameDeviceType == RETRO_MOBILE)
153
CREATE_ENTITY(VirtualDPad);
154
}
155
else {
156
RenderRect(-SCREEN_CENTERX_F, SCREEN_CENTERY_F, 160.0, SCREEN_XSIZE_F, SCREEN_YSIZE_F, 0, 0, 0, 255);
157
CREATE_ENTITY(SegaSplash);
158
}
159
}
160
break;
161
}
162
}
163
#endif
164
165