-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.h
More file actions
180 lines (157 loc) · 4.24 KB
/
server.h
File metadata and controls
180 lines (157 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#pragma once
void server_code(string level = "")
{
SDL_Init(SDL_INIT_TIMER | SDL_INIT_EVENTS);
game_init();
initialize_map16();
cout << yellow << "[JFKMW] Starting up a server." << white << endl;
if (level == "") {
level = "1";
//cout << "Enter a level : "; cin >> level;
}
LevelManager.LoadLevel(stoi(level, nullptr, 16));
isClient = false;
networking = true;
bool DisablePrints = false;
cout << green << "[Network] Server starting. Commands :" << endl;
cout << "F1 = Load a new level" << endl;
cout << "F2 = Sync RAM to all players" << endl;
cout << "F3 = Status Prints Toggle" << endl;
cout << "F4 = Sync Music" << endl;
cout << "F5 = Change RAM Variable" << endl;
cout << "F6 = PVP Toggle" << endl;
cout << "F7 = Dump RAM" << endl;
cout << "F8 = Dump Level data" << endl;
cout << "F9 = Reload Lua" << endl;
cout << "F10 = Player List" << endl;
cout << "K = Kick" << white << endl;
data_size_current = 0;
thread = new sf::Thread(&NetWorkLoop); thread->launch();
int FPS = 60;
while (true)
{
while (doing_read) {
SDL_Delay(1);
}
doing_write = true;
Uint32 start_time = SDL_GetTicks();
chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
game_loop();
SoundLoop();
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
total_time_ticks = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
if (!(global_frame_counter % 60) && clients.size() > 0)
{
if (DisablePrints)
{
cout << green << "[Network] latest loop : "
<< dec << total_time_ticks.count() * 1000.0 << " ms. "
<< (double(data_size_current) / 1024.0) << "kb/s in, " << (double(data_size_now) / 1024.0) << "kb/s out" << white << endl;
}
data_size_current = 0;
data_size_now = 0;
}
if (getKey(0x70))
{
string level = ""; cout << "Enter a level : "; cin >> level;
doing_write = true;
ASM.Write_To_Ram(0x3f08, stoi(level, nullptr, 16), 2);
ASM.Write_To_Ram(0x1493, 0x06, 1);
doing_write = false;
discord_message("Switched to level " + level);
}
if (getKey(0x71))
{
cout << green << "[Network] Syncing RAM to other players.." << endl;
Set_Server_RAM();
Do_RAM_Change();
}
if (getKey(0x72))
{
DisablePrints = !DisablePrints;
if (!DisablePrints)
{
cout << green << "[Network] Disabled Network latest loop prints." << endl;
}
else
{
cout << green << "[Network] Enabled Network latest loop prints." << endl;
}
}
if (getKey(0x73))
{
cout << green << "[Network] Syncing music to other players.." << endl;
need_sync_music = true;
}
if (getKey(0x74))
{
string r = ""; cout << "RAM $ to modify : "; cin >> r;
string v = ""; cout << "New Value (hex) : "; cin >> v;
RAM[stoi(r, nullptr, 16)] = uint_fast8_t(stoi(v, nullptr, 16));
}
if (getKey(0x75))
{
pvp = !pvp;
if (!pvp)
{
cout << green << "[Network] Disabled PVP." << endl;
}
else
{
cout << green << "[Network] Enabled PVP." << endl;
}
Add_Chat(pvp ? "pvp is now enabled" : "pvp is now off");
discord_message(pvp ? "pvp is now enabled" : "pvp is now off");
}
if (getKey(0x76))
{
dump_ram();
}
if (getKey(0x77))
{
dump_level_data();
}
if (getKey(0x78))
{
cout << blue << "[Network] Reloading lua" << white << endl;
doing_write = true;
lua_loadfile(last_lua_file);
lua_run_init();
doing_write = false;
}
if (getKey(0x79))
{
cout << cyan << "List of players (" << clients.size() << "): " << endl;
for (int i = 0; i < clients.size(); i++)
{
MPlayer& PLR = get_mario(i);
cout << "1. " << PLR.player_name_cut << " (" << clients[i]->getRemoteAddress() << ")" << endl;
}
cout << white;
}
if (getKey(0x4B))
{
int plr = 0;
cout << cyan << "Number of player to kick: " << endl;
cin >> plr;
if (plr > 0 && plr < (clients.size() + 1))
{
sf::TcpSocket* t = clients[plr - 1];
PreparePacket(Header_FailedToConnect);
CurrentPacket << "Kicked.";
SendPacket(t);
HandleDisconnection(t);
}
else
{
cout << red << "Not valid." << endl;
}
cout << white;
}
doing_write = false;
if (Uint32(1000 / FPS) > (SDL_GetTicks() - start_time))
{
SDL_Delay((1000 / FPS) - (SDL_GetTicks() - start_time));
}
}
}