-
Notifications
You must be signed in to change notification settings - Fork 6
/
net__startup.cpp
1504 lines (1334 loc) · 35.4 KB
/
net__startup.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************** <BR>
This file is part of Crack dot Com's free source code release of
Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
information about compiling & licensing issues visit this URL</a>
<PRE> If that doesn't help, contact Jonathan Clark at
golgotha_source@usa.net (Subject should have "GOLG" in it)
***********************************************************************/
#include "pch.h"
#ifdef NETWORK_INCLUDED
#include "net/startup.h"
#include "net/client.h"
#include "net/server.h"
#include "loaders/load.h"
#include "mess_id.h"
#include "time/timedev.h"
#include "app/app.h"
#include "resources.h"
#include "device/device.h"
#include "device/event.h"
#include "file/ram_file.h"
#include "file/get_filename.h"
#include "gui/text_input.h"
#include "gui/text.h"
#include "gui/button.h"
#include "gui/smp_dial.h"
#include "network/login.h"
#include "network/net_find.h"
#include "network/net_prot.h"
#include "network/net_addr.h"
#include "network/net_sock.h"
#include "global_id.h"
#include "g1_object.h"
#include "tick_count.h"
#include "player.h"
#include "team_api.h"
#include "memory/hashtable.h"
#ifdef _WINDOWS
#include <windows.h>
#include <winsock.h>
#endif
//#ifdef __linux
//#include <sys/socket.h>
//#include <netinet/in.h>
//#include <arpa/inet.h>
//#endif
i4_temp_file_class * network_file=0;
static w16 stank_type=0;
i4_event_reaction_class * g1_net_window_class::create_orec(int mess_id)
{
i4_object_message_event_class * om=new i4_object_message_event_class(this, mess_id);
return new i4_event_reaction_class(this, om);
}
g1_net_window_class::g1_net_window_class(w16 w, w16 h,
i4_graphical_style_class * style,
i4_net_protocol * protocol,
char * bg_res,
int poll_delay,
int poll_id)
: i4_parent_window_class(w,h),
style(style),
protocol(protocol),
poll_delay(poll_delay),
poll_id(poll_id)
{
bg=i4_load_image(i4gets(bg_res));
i4_object_message_event_class poll(this, poll_id);
poll_event_id=i4_time_dev.request_event(this, &poll, poll_delay);
}
void g1_net_window_class::receive_event(i4_event * ev)
{
if (ev->type()==i4_event::OBJECT_MESSAGE)
{
CAST_PTR(oev, i4_object_message_event_class, ev);
if (oev->object==this)
{
if (oev->sub_type==(w32)poll_id)
{
poll();
i4_object_message_event_class poll(this, poll_id);
poll_event_id=i4_time_dev.request_event(this, &poll, poll_delay);
}
else
{
object_message(oev->sub_type);
}
}
else
{
object_message(-1);
i4_parent_window_class::receive_event(ev);
}
}
else
{
i4_parent_window_class::receive_event(ev);
}
}
void g1_net_window_class::parent_draw(i4_draw_context_class &context)
{
if (bg)
{
local_image->clear(0, context);
bg->put_image(local_image, 0, 0, context);
}
}
g1_net_window_class::~g1_net_window_class()
{
i4_time_dev.cancel_event(poll_event_id);
if (bg)
{
delete bg;
}
}
g1_startup_window::g1_startup_window(w16 w, w16 h,
i4_graphical_style_class * style,
i4_net_protocol * protocol)
: g1_net_window_class(w,h, style, protocol, "net_bg_image", 500, POLL)
{
serverip=0;
hostname=new i4_text_input_class(style, i4_string_man.get(0), 190, 200, this);
add_child(g1_resources.net_hostname_x, g1_resources.net_hostname_y, hostname);
username=new i4_text_input_class(style, *g1_resources.username, 90, 10, this);
add_child(g1_resources.net_username_x, g1_resources.net_username_y, username);
i4_text_window_class * start_game_text=new i4_text_window_class(i4gets("start_server"), style);
i4_button_class * new_game;
new_game=new i4_button_class(0,start_game_text, style, create_orec(START_SERVER));
add_child(g1_resources.net_start_x, g1_resources.net_start_y, new_game);
i4_text_window_class * quit_text=new i4_text_window_class(i4gets("net_main_menu"), style);
add_child(new_game->x(), new_game->y()+new_game->height()+1,
new i4_button_class(0, quit_text, style, create_orec(QUIT_NET_GAME)));
i4_net_protocol * p=protocol;
if (p)
{
find=p->create_finder_socket(g1_resources.net_find_port, g1_resources.net_find_port);
}
else
{
find=0;
}
t_buts=0;
buts=0;
}
void g1_startup_window::free_buts()
{
for (int i=0; i<t_buts; i++)
{
remove_child(buts[i]);
delete buts[i];
}
if (buts)
{
i4_free(buts);
}
buts=0;
t_buts=0;
}
void g1_startup_window::set_server_address()
{
serverip=0;
i4_query_text_input_class q;
i4_kernel.send_event(hostname, &q);
i4_str * setting=q.copy_of_data;
q.copy_of_data=0;
char buf[100];
i4_os_string(*setting,buf,100);
serverip=inet_addr(buf); //returns ip in network order format
if (serverip==INADDR_NONE)
{
serverip=0;
}
if (find)
{
find->connect_to(serverip);
}
delete setting;
}
void g1_startup_window::grab_uname()
{
delete g1_resources.username;
i4_query_text_input_class q;
i4_kernel.send_event(username, &q);
g1_resources.username=q.copy_of_data;
q.copy_of_data=0;
}
void g1_startup_window::object_message(int id)
{
set_server_address();
switch (id)
{
case START_SERVER:
{
grab_uname();
g1_server=new g1_server_class(g1_resources.net_udp_port, protocol);
i4_user_message_event_class u(G1_SERVER_MENU);
i4_kernel.send_event(i4_current_app, &u);
} break;
case QUIT_NET_GAME:
{
grab_uname();
i4_user_message_event_class u(G1_MAIN_MENU);
i4_kernel.send_event(i4_current_app, &u);
} break;
}
if (id>=LAST)
{
grab_uname();
i4_finder_socket::server s;
if (find)
{
find->get_server(id-LAST, s);
}
else
{
i4_finder_socket::get_localhost(s);
}
s.addr->set_port(g1_resources.net_udp_port);
g1_client=new g1_client_class(s.addr, g1_resources.net_udp_port, protocol);
i4_user_message_event_class u(G1_CLIENT_JOINED_MENU);
i4_kernel.send_event(i4_current_app, &u);
}
}
void g1_startup_window::poll()
{
if (find)
{
if (find->poll())
{
free_buts();
t_buts=find->total_servers();
buts=(i4_button_class * *)I4_MALLOC(sizeof(i4_button_class *)*t_buts,"but array");
int y=g1_resources.net_found_y, x=g1_resources.net_found_x1;
for (int i=0; i<t_buts; i++)
{
i4_finder_socket::server s;
find->get_server(i, s);
i4_text_window_class * t=new i4_text_window_class(*s.notification_string, style);
buts[i]=new i4_button_class(0, t, style, create_orec(LAST + i));
add_child(x,y, buts[i]);
y+=buts[i]->height()+1;
}
}
}
//if find is null, this usually means the broadcast class couldn't be initialized
//because the port was already open, possibly by another instance of golgotha.
//Let's add "localhost" to the list in this case.
else
{
free_buts();
i4_finder_socket::server s;
i4_finder_socket::get_localhost(s);
buts=(i4_button_class * *)I4_MALLOC(sizeof(i4_button_class *),"but array");
t_buts=1;
i4_text_window_class * loc=new i4_text_window_class(*s.notification_string, style);
buts[0]=new i4_button_class(0,loc,style,create_orec(LAST));
int y1=g1_resources.net_found_y, x1=g1_resources.net_found_x1;
add_child(x1,y1,buts[0]);
}
}
g1_startup_window::~g1_startup_window()
{
if (find)
{
delete find;
}
free_buts();
}
g1_server_start_window::g1_server_start_window(w16 w, w16 h,
i4_graphical_style_class * style,
i4_net_protocol * protocol)
: g1_net_window_class(w,h, style, protocol, "server_bg_image", 100, POLL)
{
memset(names,0,sizeof(names));
i4_text_window_class * start_game_text=new i4_text_window_class(i4gets("start_net_game"),
style);
i4_object_message_event_class * sng_e=new i4_object_message_event_class(this, START_NET_GAME);
i4_event_reaction_class * sng_r=new i4_event_reaction_class(this, sng_e);
i4_button_class * new_game=new i4_button_class(0, start_game_text, style, sng_r);
add_child(480,20, new_game);
i4_text_window_class * quit_text=new i4_text_window_class(i4gets("net_main_menu"), style);
i4_object_message_event_class * q_e=new i4_object_message_event_class(this, QUIT_NET_GAME);
i4_event_reaction_class * q_r=new i4_event_reaction_class(this, q_e);
i4_button_class * q=new i4_button_class(0, quit_text, style, q_r);
add_child(new_game->x(), new_game->y()+new_game->height()+1, q);
if (protocol)
{
note=protocol->create_notifier_socket(g1_resources.net_find_port, *g1_resources.username);
}
else
{
note=0;
}
}
void g1_server_start_window::object_message(int id)
{
switch (id)
{
case START_NET_GAME:
{
i4_user_message_event_class u(G1_START_NEW_GAME);
i4_kernel.send_event(i4_current_app, &u);
g1_server->start_game();
} break;
case QUIT_NET_GAME:
{
if (g1_server)
{
delete g1_server;
g1_server=0;
}
i4_user_message_event_class u(G1_MAIN_MENU);
i4_kernel.send_event(i4_current_app, &u);
} break;
}
}
void g1_server_start_window::poll()
{
if (note)
{
note->poll();
}
if (g1_server)
{
g1_server->poll();
if (g1_server->list_changed)
{
g1_server->list_changed=i4_F;
int i;
for (i=0; i<G1_MAX_PLAYERS; i++)
{
if (names[i])
{
remove_child(names[i]);
names[i]=0;
}
}
i4_text_window_class * t;
int x=i4getn("net_joined_x"), y=i4getn("net_joined_y");
t=new i4_text_window_class(*g1_resources.username, style);
add_child(x, y, t);
y+=t->height()+1;
names[0]=t;
for (i=1; i<G1_MAX_PLAYERS; i++)
{
if (g1_server->clients[i].addr)
{
t=new i4_text_window_class(*g1_server->clients[i].username, style);
add_child(x,y,t);
y+=t->height()+1;
names[i]=t;
}
}
}
}
}
g1_server_start_window::~g1_server_start_window()
{
if (note)
{
delete note;
}
}
g1_client_wait_window::g1_client_wait_window(w16 w, w16 h,
i4_graphical_style_class * style,
i4_net_protocol * protocol)
: g1_net_window_class(w,h, style, protocol, "client_wait_image", 100, POLL)
{
i4_text_window_class * quit_text=new i4_text_window_class(i4gets("net_cancel"), style);
i4_object_message_event_class * q_e=new i4_object_message_event_class(this, QUIT_NET_GAME);
i4_event_reaction_class * q_r=new i4_event_reaction_class(this, q_e);
i4_button_class * q=new i4_button_class(0, quit_text, style, q_r);
add_child(i4getn("net_cancel_x"), i4getn("net_cancel_y"), q);
}
void g1_client_wait_window::object_message(int id)
{
if (id==QUIT_NET_GAME)
{
delete g1_client;
g1_client=0;
i4_user_message_event_class u(G1_MAIN_MENU);
i4_kernel.send_event(i4_current_app, &u);
}
}
void g1_client_wait_window::poll()
{
if (!g1_client || !g1_client->poll())
{
i4_user_message_event_class u(G1_MAIN_MENU);
i4_kernel.send_event(i4_current_app, &u);
}
}
g1_client_wait_window::~g1_client_wait_window()
{
//upon closing the wait window, we kill the client?
//very bad idea: Not very sensefull if we close it because the game has started
/*if (g1_client)
{
delete g1_client;
g1_client=0;
}
*/
}
// CLIENT.CPP
/********************************************************************** <BR>
This file is part of Crack dot Com's free source code release of
Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
information about compiling & licensing issues visit this URL</a>
<PRE> If that doesn't help, contact Jonathan Clark at
golgotha_source@usa.net (Subject should have "GOLG" in it)
***********************************************************************/
//This is a common function for server and client
//Todo: Write some functions that handle the state of the entire system
//to keep track of who has the latest version of which object
//such that the server doesn't return packets not needed by the client
//and the client can filter out packets where he has newer data than
//the server. This particularly means that an user-controlled object
//must not get remote-synced..
g1_network_time_manager_class * g1_network_time_man_ptr=0;
g1_network_time_manager_class g1_network_time_man;
i4_bool g1_network_time_manager_class::shouldupdate(w32 id, w32 totimestamp)
{
net_sync_entry * cur=net_sync.get(id);
g1_object_class * unit=g1_global_id.get(id);
if (unit && (unit->id==stank_type) && (unit->player_num==g1_player_man.local_player))
{
lasterror=OWNSUPERTANK; //must newer sync the own stank
return i4_F;
}
if ((!cur) || (cur->ticksynced<totimestamp))
{
lasterror=0;
return i4_T;
}
else
{
lasterror=OUTOFDATE;
return i4_F;
}
};
void g1_network_time_manager_class::updatecomplete(w32 id, w32 attimestamp)
{
net_sync_entry * nse=new net_sync_entry,* old;
nse->tickcalced=attimestamp;
nse->ticksynced=attimestamp;
old=net_sync.get(id);
if (old)
{
net_sync.replace(id,nse,i4_F); //adds if it doesn't exist already
delete old;
}
else
{
net_sync.insert(id,nse);
}
};
void g1_network_time_manager_class::deletedobject(w32 id)
{
net_sync_entry * old=net_sync.remove(id);
if (old)
{
delete old;
}
else
{
lasterror=OBJDOESNOTEXIST;
}
};
void process_data_packet(i4_file_class &r,i4_bool server)
{
r.read_32(); //skip empty field
w32 ticksent=r.read_32(); //to which tick does this packet belong?
//here follows: reading out the objects
g1_realtime_loader_class * rtloader=new g1_realtime_loader_class(&r,i4_F,i4_F);
w32 objfor=rtloader->read_32();
w16 typefor=0;
while (objfor!=0)
{
if (objfor==g1_global_id_manager_class::ID_DELETEPROPAGATE)
{
//don't delete if object just doesn't exist locally.
objfor=rtloader->read_32();
g1_object_class * obdel=g1_global_id.checked_get(objfor);
if (obdel)
{
obdel->set_flag(g1_object_class::THINKING,1); //Perhaps was not thinking locally
obdel->stop_thinking();
obdel->unoccupy_location();
obdel->request_remove();
}
}
else
{
typefor=rtloader->read_16();
g1_object_class * obj=g1_global_id.checked_get(objfor);
if (!obj)
{
//Object seems to be new
//obj=g1_create_object(typefor);
if (typefor<=g1_last_object_type)
{
obj=g1_object_type_array[typefor]->create_object(typefor,rtloader);
//the object will be in sync with the load
obj->occupy_location();
obj->grab_old();
i4_warning("Remotelly creating object ID %i as %s.",obj->global_id,g1_object_type_array[typefor]->name());
if (obj->get_flag(g1_object_class::THINKING))
{
//cannot request_think() without resetting this flag.
obj->set_flag(g1_object_class::THINKING,0);
obj->request_think();
}
g1_player_man.get(obj->player_num)->add_object(obj->global_id);
if (typefor==g1_get_object_type("stank"))
{
// a new enemy supertank. Needs a few special updates
g1_player_man.get(obj->player_num)->num_stank_lives()--;
g1_player_man.get(obj->player_num)->calc_upgrade_level();
g1_player_man.get(obj->player_num)->set_commander(g1_player_piece_class::cast(obj));
//g1_player_man.get(obj->player_num)->continue_wait=i4_F;
}
g1_network_time_man.updatecomplete(obj->global_id,ticksent);
}
else
{
i4_error("ERROR: Unknown object type requested.");
delete rtloader;
return;
}
//g1_global_id.assign(objfor,obj);
}
else
{
if (g1_network_time_man.shouldupdate(obj->global_id,ticksent))
{
obj->load(rtloader);
g1_network_time_man.updatecomplete(obj->global_id,ticksent);
}
else
{
//i4_warning("Skipping update of object %i, data is outdated.",obj->global_id);
obj->skipload(rtloader);
}
}
if (server)
{
obj->set_flag(g1_object_class::NEEDS_SYNC,1);
}
else
{
obj->set_flag(g1_object_class::NEEDS_SYNC,0);
}
}
objfor=rtloader->read_32();
}
delete rtloader;
}
g1_client_class * g1_client=0;
g1_client_class::g1_client_class(i4_net_address * server_address,
int use_port,
i4_net_protocol * protocol)
: server_address(server_address->copy()),
use_port(use_port)
{
listen=0;
send=0;
map_name=0;
num_players=0;
player_num=0;
stank_type=g1_get_object_type("stank");
state=JOIN_START;
if (protocol)
{
listen=protocol->listen(use_port, I4_PACKETED_DATA);
send=protocol->connect(server_address, I4_PACKETED_DATA);
}
}
void g1_client_class::send_server(w8 * buf, i4_file_class * fp)
{
send->write(buf,fp->tell());
}
i4_bool g1_client_class::poll() // returns false if server is not responding
{
if (!listen || !send)
{
return 0;
}
if (state==JOIN_START)
{
i4_time_class now;
if (now.milli_diff(last_responce_time)>40000) // if it's been 40 secs assume server dead
{
i4_message_box("Server not responding","The server is not responding any more. ",MSG_OK);
return i4_F;
}
w8 packet[512];
i4_ram_file_class r(packet, sizeof(packet));
r.write_8(G1_PK_I_WANNA_JOIN);
r.write_16(use_port);
r.write_counted_str(*g1_resources.username);
if (!send->write(packet, r.tell()))
{
return i4_F;
}
}
if (state==SYNCING) //set from the main loop as soon as the map is loaded
{
PACKET(pack,fp); //we just resend this message till we get the matching reply
fp.write_8(G1_PK_LOADING_DONE);
send_server(pack,&fp);
}
int noloops=0;
while (listen->ready_to_read() && noloops<10)
{
w8 packet[MAX_PACKET_SIZE];
i4_net_address * a;
int s=listen->read_from(packet, sizeof(packet), a);
if (a->equals(server_address))
{
i4_ram_file_class r(packet, sizeof(packet));
w8 new_message=r.read_8();
switch (new_message)
{
case G1_PK_YOU_HAVE_JOINED:
{
player_num=(w8)r.read_16();
if (map_name)
{
delete map_name;
}
map_name=r.read_counted_str();
i4_warning("We were accepted by the server to join");
state=CONNECTING;
last_responce_time.get();
} break;
case G1_PK_WE_ARE_STARTING:
{
num_players=r.read_32();
if (state==CONNECTING)
{
i4_warning("Loading game data");
//don't do this twice.
//i4_user_message_event_class u(G1_START_NEW_GAME);
i4_file_open_message_class u(G1_SAVEGAME_LOAD_OK, new i4_str (* map_name));
i4_kernel.send_event(i4_current_app, &u);
state=LOADING;
}
//li_call("hide_main_menu");
} break;
case G1_PK_GAME_START:
{
//from now on, it's the main loops task to know that
//this is a net game
if (state!=RUNNING)
{
int num_ais=r.read_32();
if (num_ais>G1_MAX_PLAYERS)
{
i4_error("ERROR: Local Golgotha version supports fewer players than this game needs. Check your version.");
num_ais=G1_MAX_PLAYERS;
}
i4_str * ainame=0;
g1_team_api_class * newai=0;
for (int ais=0; ais<num_ais; ais++)
{
//the player the next ai is for
w32 aifor=r.read_32();
ainame=r.read_counted_str();
char buf[100];
i4_os_string(*ainame,buf,100);
newai=g1_create_ai(buf,0);
if (strcmp(buf,"human")==0)
{
//this will be the local player
g1_player_man.local_player=ais;
i4_warning("We were assigned player number %i.",ais);
}
g1_player_man.get(ais)->set_ai(newai);
newai->init();
delete ainame;
}
g1_global_id.enable_networking(ID_NET_CLIENT);
state=RUNNING;
i4_warning("The game is in sync and has started");
}
//this message is sent by the server if all clients are ready
} break;
case G1_PK_GAME_DATA:
{
process_data_packet(r,i4_F);
} break;
default:
{
if (new_message>=G1_PK_ID_BASE && new_message<=G1_PK_ID_END)
{
r.seek(0);
g1_global_id.receive_packet(&r);
}
}
}
}
delete a;
noloops++;
}
return i4_T;
}
int g1_client_class::prepare_command(int command)
{
g1_global_id.poll();
switch (state)
{
case SYNCING:
case LOADING:
{
state=SYNCING;
PACKET(sp,r);
r.write_8(G1_PK_LOADING_DONE);
i4_warning("We are ready to start.");
send_server(sp,&r);
}
return NE_ERROR_NOTREADY;
case RUNNING:
{
if (!network_file)
{
network_file=new i4_temp_file_class(2000,2000);
}
switch (command)
{
case G1_PK_GAME_DATA:
{
network_file->clear();
network_file->write_8(command);
network_file->write_32(0); //placeholder for length
network_file->write_32(g1_tick_counter);
} break;
case G1_PK_PROCESS:
{
send_server(network_file->get_buffer(),network_file);
network_file->clear();
network_file->write_8(G1_PK_GAME_DATA);
network_file->write_32(0);
network_file->write_32(g1_tick_counter);
} break;
}
} break;
}
if (!g1_global_id.is_ready())
{
return NE_ERROR_NOTREADY;
}
return NE_ERROR_OK;
}
i4_bool g1_client_class::is_ready()
{
return g1_global_id.is_ready();
}
g1_client_class::~g1_client_class()
{
i4_warning("Closing network game");
delete server_address;
if (map_name)
{
delete map_name;
}
if (listen)
{
delete listen;
}
if (send)
{
delete send;
}
}
// SERVER.CPP
/********************************************************************** <BR>
This file is part of Crack dot Com's free source code release of
Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
information about compiling & licensing issues visit this URL</a>
<PRE> If that doesn't help, contact Jonathan Clark at
golgotha_source@usa.net (Subject should have "GOLG" in it)
***********************************************************************/
g1_server_class * g1_server=0;
void g1_server_class::client::cleanup()
{
if (addr)
{
delete addr;
delete username;
}
if (send)
{
delete send;
}
addr=0;
send=0;
flags=0;
remote_player_num=0;
}
g1_server_class::g1_server_class(int use_port, i4_net_protocol * protocol)
: protocol(protocol)
{
map_name=new i4_str(i4gets("tmp_savename"));
udp_port=0;
memset(clients, 0, sizeof(clients));
list_changed=i4_F;
state=WAITING_FOR_PLAYERS;
local_player_num=1; //this cannot be changed right now
stank_type=g1_get_object_type("stank");
if (protocol)
{
udp_port=protocol->listen(use_port, I4_PACKETED_DATA);
if (!udp_port)
{
i4_warning("could not bind to port, server already running?");
}
}
}
void g1_server_class::send_to(w8 * buf, i4_file_class * f,int client_id)
{
if (clients[client_id].addr)
{
clients[client_id].send->write(buf,f->tell());
}
}
void g1_server_class::send_to_all(w8 * buf,i4_file_class * f)
{
for (int i=0; i<G1_MAX_PLAYERS; i++)
{
if (clients[i].addr)
{
clients[i].send->write(buf,f->tell());
}
}
}
void g1_server_class::start_game()
{
PACKET(packet,r);
r.write_8(G1_PK_WE_ARE_STARTING);
r.write_32(4);
send_to_all(packet,&r);
send_to_all(packet,&r);
state=LOADING;
};
void g1_server_class::send_player_joined(int client_num)
{
w8 packet[512];
i4_ram_file_class r(packet, sizeof(packet));
r.write_8(G1_PK_YOU_HAVE_JOINED);
r.write_16(client_num+1);
r.write_counted_str(*map_name);
clients[client_num].send->write(packet, r.tell());
}
void g1_server_class::process_client_packet(w8 * packet,
int packet_length,
int client_num)
{
clients[client_num].last_data.get();
i4_ram_file_class r(packet, packet_length);
w8 msg=r.read_8();
switch (msg)
{
case G1_PK_I_WANNA_JOIN:
{
r.read_16(); // skip use port
delete clients[client_num].username;
clients[client_num].username=r.read_counted_str();
send_player_joined(client_num);
} break;
case G1_PK_LOADING_DONE:
{