source: abuse/trunk/src/server2.cpp @ 97

Last change on this file since 97 was 56, checked in by Sam Hocevar, 12 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 5.6 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include "server2.hpp"
13#include "nfserver.hpp"
14#include "nfclient.hpp"
15#include "dprint.hpp"
16#include "view.hpp"
17#include "jrand.hpp"
18#include "objects.hpp"
19#include "level.hpp"
20#include "dev.hpp"
21
22extern char const *get_username();
23int start_running=0;
24
25class client_descriptor
26{
27  public :
28  client_descriptor(int client_number);
29  view *player;                // has this player been inducted yet?
30  client_descriptor *next;
31  long requested_join,cx1,cy1,cx2,cy2;
32  int cnum;
33} ;
34
35client_descriptor::client_descriptor(int client_number)
36{
37  player=NULL;
38  next=NULL;
39  requested_join=0;
40  cnum=client_number;
41}
42
43game_server *local_server=NULL;       // created on server machine, NULL on all others
44
45
46game_server::game_server(int argc, char **argv, int port)
47{
48  client_list=NULL;
49  sync_check=0;                  // should we send sync packets to client?
50
51  for (int i=1;i<argc;i++)
52  {
53    if (!strcmp(argv[i],"-sync"))
54    sync_check=1;
55  }
56}
57
58
59
60
61
62
63
64game_server::~game_server()
65{
66  for (client_descriptor *p=client_list;p;)
67  {
68    client_descriptor *q=p;
69    p=p->next;
70    delete q;
71  }
72}
73
74
75
76void game_server::receive_inputs()         // reads inputs from all non-local clients
77{
78/*  client_descriptor *last=NULL;
79  packet pk;
80  for (client_descriptor *p=client_list;p;)
81  {   
82    int delete_me=0;
83    if (p->connection)
84    {
85      if (p->player)                        // clients with players are required to send input
86      {
87        int error=!get_pkt(p->connection,pk);
88        if (!error)
89          pk.insert_into(next_out);
90        else                                // on error delete the client
91          delete_me=1;
92      } else
93      {
94        if (p->connection->ready_to_read())
95        {
96          if (!p->connection->get(pk))
97            delete_me=1;
98          else
99          {
100            uchar cmd;
101            long cx1,cy1,cx2,cy2;
102            if (!pk.read((uchar *)&cmd,1) || cmd!=SCMD_JOIN_GAME ||
103                pk.read((uchar *)&cx1,4)!=4 ||
104                pk.read((uchar *)&cy1,4)!=4 ||
105                pk.read((uchar *)&cx2,4)!=4 ||
106                pk.read((uchar *)&cy2,4)!=4)
107              delete_me=1;
108            else
109            {
110              p->cx1=lltl(cx1);
111              p->cy1=lltl(cy1);
112              p->cx2=lltl(cx2);
113              p->cy2=lltl(cy2);
114              p->requested_join=1;      // mark this client as wanting to join
115              pk.insert_into(next_out);
116            }
117
118          }
119        }
120      }
121    }
122
123    if (delete_me)
124    {
125      client_descriptor *del_me=p;
126      p=p->next;
127      if (last)
128        last->next=p;
129      else client_list=p;
130      delete del_me;           
131    } else { last=p; p=p->next; }
132
133
134  }*/
135
136}
137
138void game_server::send_inputs()            // pass collected inputs to all non-local clients
139{
140/*  client_descriptor *last=NULL;
141  if (sync_check)
142  {
143    next_out.write_uint8(SCMD_SYNC);
144    next_out.write_uint32(make_sync_uint32());
145  }
146   
147  next_out.write_uint8(SCMD_END_OF_PACKET);        // so clients knows when to stop reading
148
149  for (client_descriptor *p=client_list;p;)
150  {
151    if (p->connection && p->player)
152    {
153      int error=!p->connection->send(next_out);
154      if (error)
155      {
156        client_descriptor *del_me=p;
157        p=p->next;
158        if (last)
159          last->next=p;
160        else client_list=p;
161        delete del_me;         
162      } else { last=p; p=p->next; }
163    } else
164    {
165      last=p;
166      p=p->next;
167    }
168  }  */
169}
170
171
172void game_server::check_for_clients()
173{
174
175}
176
177
178void game_server::join_new_players()
179{
180/*  int wait=0;
181  client_descriptor *p=client_list;
182  for (;p;p=p->next) 
183    if (p->requested_join)
184    {
185
186      view *f=player_list;
187      for (;f && f->next;f=f->next);      // find last player, add one for pn
188      int i,st=0;
189      for (i=0;i<total_objects;i++)
190        if (!strcmp(object_names[i],"START"))
191          st=i;
192
193      game_object *o=create(current_start_type,0,0);
194      game_object *start=current_level->get_random_start(320,NULL);
195      if (start) { o->x=start->x; o->y=start->y; }
196      else { o->x=100; o->y=100; }
197
198      f->next=new view(o,NULL,f->player_number+1);           
199      o->set_controller(f->next);
200
201      if (start)
202        current_level->add_object_after(o,start);
203      else
204        current_level->add_object(o);
205
206      view *v=f->next;     
207
208      v->cx1=p->cx1;
209      v->cy1=p->cy1;
210      v->cx2=p->cx2;
211      v->cy2=p->cy2;
212      p->player=v;
213      v->Drawable=p->cnum;
214     
215      wait=1;
216      p->requested_join=0;
217    }
218 
219
220  if (wait)  // wait for acknowedgement from everone then delete net file
221  {
222    packet pk;
223    current_level->save("netstart.spe",1);
224    printf("%d sync for save\n",make_sync_uint32());
225
226    client_descriptor *last=NULL;
227    for (p=client_list;p;p=p->next)
228    {
229      if (p->player)
230      {
231        pk.write_uint8(SCMD_JOIN_START);
232        int error=!p->connection->send(pk);
233        if (!error)
234        {
235          while (!p->connection->ready_to_read())
236            service_net_request();
237        }
238
239        if (error || !p->connection->get(pk))
240        {
241          if (!last)
242            client_list=client_list->next;
243          else last->next=p->next;
244          delete p;
245        } else           
246          last=p;     
247      }
248    }
249    unlink("netstart.spe");
250  }*/
251}
252
253
254
255/*
256
257    server/client interaction
258
259
260  Client - get/send commands
261
262  Server - receive inputs
263           check for join request
264           if join request add SCMD_JOIN_GAME to out packet
265           send inputs
266
267  Client - read commands from server
268           process commands
269           tick_game
270           draw
271
272
273  Server (if join request) :
274          create new_player
275          save level to netstart.spe
276          wait for all clients with views to send SCMD_NEW_ACK
277          new player should read entire level, while old
278          clients seek to "player_info" and read this.
279
280
281
282
283
284
285*/
Note: See TracBrowser for help on using the repository browser.