1 /* $Id: objpos.c,v 5.2 2001/11/29 14:48:12 bertg Exp $
2  *
3  * XPilot, a multiplayer gravity war game.  Copyright (C) 1991-2001 by
4  *
5  *      Bj�rn Stabell        <bjoern@xpilot.org>
6  *      Ken Ronny Schouten   <ken@xpilot.org>
7  *      Bert Gijsbers        <bert@xpilot.org>
8  *      Dick Balaska         <dick@xpilot.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 
28 #ifdef _WINDOWS
29 # include <windows.h>
30 #endif
31 
32 #define SERVER
33 #include "version.h"
34 #include "config.h"
35 #include "serverconst.h"
36 #include "global.h"
37 #include "proto.h"
38 #include "map.h"
39 #include "object.h"
40 #include "objpos.h"
41 
42 char objpos_version[] = VERSION;
43 
44 
Object_position_set_clicks(object * obj,int cx,int cy)45 void Object_position_set_clicks(object *obj, int cx, int cy)
46 {
47     struct _objposition		*pos = (struct _objposition *)&obj->pos;
48 
49 #if 0
50     if (cx < 0 || cx >= PIXEL_TO_CLICK(World.width) ||
51 	cy < 0 || cy >= PIXEL_TO_CLICK(World.height)) {
52 	printf("BUG!  Illegal object position %d,%d\n", cx, cy);
53 	*(double *)(-1) = 4321.0;
54 	abort();
55     }
56 #endif
57     pos->cx = cx;
58     pos->x = CLICK_TO_PIXEL(cx);
59     pos->bx = pos->x / BLOCK_SZ;
60     pos->cy = cy;
61     pos->y = CLICK_TO_PIXEL(cy);
62     pos->by = pos->y / BLOCK_SZ;
63 }
64 
Object_position_set_pixels(object * obj,DFLOAT x,DFLOAT y)65 void Object_position_set_pixels(object *obj, DFLOAT x, DFLOAT y)
66 {
67     Object_position_set_clicks(obj, FLOAT_TO_CLICK(x), FLOAT_TO_CLICK(y));
68 }
69 
Object_position_init_pixels(object * obj,DFLOAT x,DFLOAT y)70 void Object_position_init_pixels(object *obj, DFLOAT x, DFLOAT y)
71 {
72     Object_position_set_clicks(obj, FLOAT_TO_CLICK(x), FLOAT_TO_CLICK(y));
73     Object_position_remember(obj);
74 }
75 
Player_position_restore(player * pl)76 void Player_position_restore(player *pl)
77 {
78     Player_position_set_pixels(pl, pl->prevpos.x, pl->prevpos.y);
79 }
80 
Player_position_set_clicks(player * pl,int cx,int cy)81 void Player_position_set_clicks(player *pl, int cx, int cy)
82 {
83     struct _objposition		*pos = (struct _objposition *)&pl->pos;
84 
85 #if 0
86     if (cx < 0 || cx >= PIXEL_TO_CLICK(World.width) ||
87 	cy < 0 || cy >= PIXEL_TO_CLICK(World.height)) {
88 	printf("BUG!  Illegal player position %d,%d\n", cx, cy);
89 	*(double *)(-1) = 4321.0;
90 	abort();
91     }
92 #endif
93     pos->cx = cx;
94     pos->x = CLICK_TO_PIXEL(cx);
95     pos->bx = pos->x / BLOCK_SZ;
96     pos->cy = cy;
97     pos->y = CLICK_TO_PIXEL(cy);
98     pos->by = pos->y / BLOCK_SZ;
99 }
100 
Player_position_set_pixels(player * pl,DFLOAT x,DFLOAT y)101 void Player_position_set_pixels(player *pl, DFLOAT x, DFLOAT y)
102 {
103     Player_position_set_clicks(pl, FLOAT_TO_CLICK(x), FLOAT_TO_CLICK(y));
104 }
105 
Player_position_init_pixels(player * pl,DFLOAT x,DFLOAT y)106 void Player_position_init_pixels(player *pl, DFLOAT x, DFLOAT y)
107 {
108     Player_position_set_clicks(pl, FLOAT_TO_CLICK(x), FLOAT_TO_CLICK(y));
109     Player_position_remember(pl);
110 }
111 
Player_position_limit(player * pl)112 void Player_position_limit(player *pl)
113 {
114     int			x = pl->pos.x, ox = x;
115     int			y = pl->pos.y, oy = y;
116 
117     LIMIT(x, 0, World.width - 1);
118     LIMIT(y, 0, World.height - 1);
119     if (x != ox || y != oy) {
120 	Player_position_set_clicks(pl, PIXEL_TO_CLICK(x), PIXEL_TO_CLICK(y));
121     }
122 }
123 
Player_position_debug(player * pl,const char * msg)124 void Player_position_debug(player *pl, const char *msg)
125 {
126 #if DEVELOPMENT
127     int			i;
128 
129     printf("pl %s pos dump: ", pl->name);
130     if (msg) printf("(%s)", msg);
131     printf("\n");
132     printf("\tB %d, %d, P %d, %d, C %d, %d, O %d, %d\n",
133 	   pl->pos.bx,
134 	   pl->pos.by,
135 	   pl->pos.x,
136 	   pl->pos.y,
137 	   pl->pos.cx,
138 	   pl->pos.cy,
139 	   pl->prevpos.x,
140 	   pl->prevpos.y);
141     for (i = 0; i < pl->ship->num_points; i++) {
142 	printf("\t%2d\tB %d, %d, P %d, %d, C %d, %d, O %d, %d\n",
143 		i,
144 	       (int)((pl->pos.x + pl->ship->pts[i][pl->dir].x) / BLOCK_SZ),
145 	       (int)((pl->pos.y + pl->ship->pts[i][pl->dir].y) / BLOCK_SZ),
146 	       (int)(pl->pos.x + pl->ship->pts[i][pl->dir].x),
147 	       (int)(pl->pos.y + pl->ship->pts[i][pl->dir].y),
148 	       (int)(pl->pos.cx + FLOAT_TO_CLICK(pl->ship->pts[i][pl->dir].x)),
149 	       (int)(pl->pos.cy + FLOAT_TO_CLICK(pl->ship->pts[i][pl->dir].y)),
150 	       (int)(pl->prevpos.x + pl->ship->pts[i][pl->dir].x),
151 	       (int)(pl->prevpos.y + pl->ship->pts[i][pl->dir].y));
152     }
153 #endif
154 }
155 
156