hedgewars/uPlayers.pas
author unc0rr
Mon, 22 Aug 2005 15:06:42 +0000
changeset 2 4eeab397c3c6
parent 1 30f2d1037d5d
child 4 bcbd7adb4e4b
permissions -rw-r--r--
- Support for forts game mode option. Works for demos, frontend doesn't support it. - Updated copyright info for forts
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     1
(*
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a worms-like game
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     4
 *
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     6
 *
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     9
 * with the Software without restriction, including without limitation the
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    12
 * furnished to do so, subject to the following conditions:
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    13
 *
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    15
 *    this list of conditions and the following disclaimer.
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    18
 *    and/or other materials provided with the distribution.
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    20
 *    derived from this software without specific prior written permission.
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    21
 *
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    32
 *)
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    33
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    34
unit uPlayers;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    35
interface
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    36
uses windows, WinSock;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    37
type PPlayer = ^TPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    38
     PTeam = ^TTeam;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    39
     TTeam = record
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    40
             hhs: array[0..7] of TPoint;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    41
             hhCount: LongWord;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    42
             end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    43
     TPlayer = record
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    44
               socket: TSocket;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    45
               NextPlayer, PrevPlayer: PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    46
               Name: string[31];
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    47
               inbuf: string;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    48
               isme: boolean;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    49
               CurrTeam: LongWord;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    50
               TeamCount: LongWord;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    51
               Teams: array[0..3] of TTeam
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    52
               end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    53
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    54
function AddPlayer(sock: TSocket): PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    55
procedure DeletePlayer(Player: PPlayer);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    56
function FindPlayerbySock(sock: TSocket): PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    57
procedure SendAll(s: shortstring);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    58
procedure SendAllButOne(Player: PPlayer; s: shortstring);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    59
procedure SelectFirstCFGTeam;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    60
procedure SelectNextCFGTeam;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    61
function GetTeamCount: Longword;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    62
procedure ConfCurrTeam(s: shortstring);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    63
procedure SendConfig(player: PPlayer);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    64
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    65
var CurrCFGPlayer: PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    66
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    67
implementation
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    68
uses uServerMisc, uNet, SysUtils;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    69
var PlayersList: PPlayer = nil;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    70
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    71
function AddPlayer(sock: TSocket): PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    72
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    73
New(Result);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    74
TryDo(Result <> nil, 'Error adding player!');
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    75
FillChar(Result^, sizeof(TPlayer), 0);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    76
Result.socket:= sock;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    77
Result.TeamCount:= 2;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    78
if PlayersList = nil then begin PlayersList:= Result; result.isme:= true end
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    79
                     else begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    80
                     PlayersList.PrevPlayer:= Result;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    81
                     Result.NextPlayer:= PlayersList;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    82
                     PlayersList:= Result
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    83
                     end
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    84
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    85
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    86
procedure DeletePlayer(Player: PPlayer);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    87
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    88
if Player = nil then OutError('Trying remove nil player!', false);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    89
if Player.NextPlayer <> nil then Player.NextPlayer.PrevPlayer:= Player.PrevPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    90
if Player.PrevPlayer <> nil then Player.PrevPlayer.NextPlayer:= Player.NextPlayer
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    91
                        else begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    92
                        PlayersList:= Player^.NextPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    93
                        if PlayersList <> nil then PlayersList.PrevPlayer:= nil
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    94
                        end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    95
Dispose(Player)
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    96
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    97
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    98
function FindPlayerbySock(sock: TSocket): PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    99
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   100
Result:= PlayersList;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   101
while (Result<>nil)and(Result.socket<>sock) do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   102
      Result:= Result.NextPlayer
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   103
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   104
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   105
procedure SendAll(s: shortstring);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   106
var p: PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   107
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   108
p:= PlayersList;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   109
while p <> nil do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   110
      begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   111
      SendSock(p.socket, s);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   112
      p:= p.NextPlayer
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   113
      end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   114
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   115
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   116
procedure SendAllButOne(Player: PPlayer; s: shortstring);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   117
var p: PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   118
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   119
p:= Player.NextPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   120
while p <> nil do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   121
      begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   122
      SendSock(p.socket, s);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   123
      p:= p.NextPlayer
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   124
      end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   125
p:= PlayersList;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   126
while p <> Player do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   127
      begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   128
      SendSock(p.socket, s);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   129
      p:= p.NextPlayer
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   130
      end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   131
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   132
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   133
function GetTeamCount: Longword;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   134
var p: PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   135
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   136
p:= PlayersList;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   137
Result:= 0;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   138
while p <> nil do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   139
      begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   140
      inc(Result, p.TeamCount);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   141
      p:= p.NextPlayer
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   142
      end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   143
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   144
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   145
procedure SelectFirstCFGTeam;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   146
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   147
CurrCFGPlayer:= PlayersList
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   148
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   149
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   150
procedure SelectNextCFGTeam;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   151
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   152
if CurrCFGPlayer = nil then OutError('Trying select next on nil current', true);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   153
if Succ(CurrCFGPlayer.CurrTeam) < CurrCFGPlayer.TeamCount then inc(CurrCFGPlayer.CurrTeam)
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   154
                                                          else CurrCFGPlayer:= CurrCFGPlayer.NextPlayer
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   155
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   156
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   157
procedure ConfCurrTeam(s: shortstring);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   158
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   159
if CurrCFGPlayer = nil then OutError('Trying select next on nil current', true);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   160
case s[1] of
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   161
     'h': with CurrCFGPlayer.Teams[CurrCFGPlayer.CurrTeam] do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   162
               begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   163
               hhs[hhCount].X:= PLongWord(@s[2])^;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   164
               hhs[hhCount].Y:= PLongWord(@s[6])^;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   165
               inc(hhCount);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   166
               end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   167
     end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   168
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   169
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   170
procedure SendConfig(player: PPlayer);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   171
var p: PPlayer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   172
    i, t: integer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   173
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   174
p:= PlayersList;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   175
while p <> nil do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   176
      begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   177
      for t:= 0 to Pred(player.TeamCount) do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   178
          begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   179
          SendSock(player.socket, 'eaddteam');
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   180
          if p = player then SendSock(player.socket, '@')
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   181
                        else SendSock(player.socket, 'erdriven');
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   182
          for i:= 0 to Pred(player.Teams[t].hhCount) do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   183
              SendSock(player.socket, Format('eadd hh%d %d %d %d',[i, p.Teams[t].hhs[i].X, p.Teams[t].hhs[i].Y, 0]));
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   184
          Sendsock(player.socket, Format('ecolor %d',[random($A0A0A0)+$5F5F5F]))
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   185
          end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   186
      p:= p.NextPlayer
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   187
      end
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   188
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   189
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   190
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   191
end.