summaryrefslogtreecommitdiff
path: root/src/base/serverSendMsg.c
blob: dc5718ed52ba017f5d892aa187c0fc74820c7b61 (plain)
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

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "server.h"
#include "net_multiplayer.h"
#include "checkFront.h"
#include "serverSendMsg.h"

#ifndef PUBLIC_SERVER
#include "screen_world.h"
#endif

static void
addMsgClient(client_t * p, char *msg, int type, int id)
{
   assert(p != NULL);
   assert(msg != NULL);

   if (p->status != NET_STATUS_ZOMBIE) {
      addMsgInCheckFront(p->listSendMsg, msg, type, id);
   }
}

static void
addMsgAllClientBut(char *msg, client_t * p, int type, int id)
{
   list_t *listClient;
   client_t *thisClient;
   int i;

   assert(msg != NULL);

   listClient = getListServerClient();

   for (i = 0; i < listClient->count; i++) {
      thisClient = (client_t *) listClient->list[i];

      if (thisClient->tux != NULL && thisClient != p) {
         addMsgClient(thisClient, msg, type, id);
      }
   }
}

static void
addMsgAllClient(char *msg, int type, int id)
{
   assert(msg != NULL);

   addMsgAllClientBut(msg, NULL, type, id);
}

static void
addMsgAllClientSeesTux(char *msg, tux_t * tux, int type, int id)
{
   list_t *listHelp;
   arena_t *arena;
   space_t *space;
   int x, y, w, h;
   int i;

   assert(msg != NULL);

   arena = getCurrentArena();
   space = arena->spaceTux;

   x = tux->x - WINDOW_SIZE_X;
   y = tux->y - WINDOW_SIZE_Y;

   w = 2 * WINDOW_SIZE_X;
   h = 2 * WINDOW_SIZE_Y;

   if (x < 0)
      x = 0;
   if (y < 0)
      y = 0;
   if (w + x >= arena->w)
      w = arena->w - (x + 1);
   if (h + y >= arena->h)
      h = arena->h - (y + 1);

   listHelp = newList();

   getObjectFromSpace(space, x, y, w, h, listHelp);
   //printf("%d %d %d %d %d\n", x, y, w, h, listHelp->count);

   for (i = 0; i < listHelp->count; i++) {
      tux_t *thisTux;
      client_t *thisClient;

      thisTux = (tux_t *) listHelp->list[i];

      if (thisTux == tux) {
         continue;
      }

      thisClient = thisTux->client;

      if (thisClient != NULL) {
         addMsgClient(thisClient, msg, type, id);
      }
   }

   destroyList(listHelp);
}

void
protoSendClient(int type, client_t * client, char *msg, int type2, int id)
{
   assert(msg != NULL);

   switch (type) {
   case PROTO_SEND_ONE:
      assert(client != NULL);
      addMsgClient(client, msg, type2, id);
      break;
   case PROTO_SEND_ALL:
      assert(client == NULL);
      addMsgAllClient(msg, type2, id);
      break;
   case PROTO_SEND_BUT:
      assert(client != NULL);
      addMsgAllClientBut(msg, client, type2, id);
      break;
   case PROTO_SEND_ALL_SEES_TUX:
#ifndef PUBLIC_SERVER
      if (client != NULL) {
         addMsgAllClientSeesTux(msg, client->tux, type2, id);
      }
      else {
         addMsgAllClientSeesTux(msg,
                                getControlTux(TUX_CONTROL_KEYBOARD_RIGHT),
                                type2, id);
      }
#endif
#ifdef PUBLIC_SERVER
      addMsgAllClientSeesTux(msg, client->tux, type2, id);
#endif
      break;
   default:
      assert(!_("Type variable has a really wierd value!"));
      break;
   }
}