File: cmdcreateplayer.c

package info (click to toggle)
xshipwars 1.32-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 17,176 kB
  • ctags: 6,357
  • sloc: ansic: 157,152; makefile: 226; sh: 75
file content (280 lines) | stat: -rw-r--r-- 7,002 bytes parent folder | download
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
#include "swserv.h"
        

#define THIS_CMD_NAME		"createplayer"


/*
 *	OPM name for newly created player objects.  Newly created player
 *	objects get the OPM values from this OPM.
 */
#define DEF_PLAYER_OPM_NAME	"New Player"


int CmdCreatePlayer(int condescriptor, char *arg)
{
        long i, con_object_num, new_obj_num;
	xsw_object_struct *con_obj_ptr, *new_obj_ptr;

        char name[XSW_OBJ_NAME_MAX];
        char password[XSW_OBJ_PASSWORD_MAX];

	int opm_num;
	xsw_object_struct *opm_ptr;

        char name1[XSW_OBJ_NAME_MAX + 80];
        char name2[XSW_OBJ_NAME_MAX + 80];

        char larg[CS_DATA_MAX_LEN];
        char stringa[XSW_OBJ_NAME_MAX + 512];
        char sndbuf[CS_DATA_MAX_LEN];
        char *strptr;


        /* Get con_object_num from connection (assumed valid) */
        con_object_num = connection[condescriptor]->object_num; 
	if(DBIsObjectGarbage(con_object_num))
	    return(-1);
	else
	    con_obj_ptr = xsw_object[con_object_num];


	/* Print usage? */
        if((arg == NULL) ? 1 : (arg[0] == '\0'))
        {
            sprintf(sndbuf,
                "Usage: `%s <name>=<password>'",
		THIS_CMD_NAME
            );
            NetSendLiveMessage(condescriptor, sndbuf);

            return(-1);
        }
	strptr = strchr(arg, '=');
        if(strptr == NULL)
        {
            sprintf(sndbuf,
                "Usage: `%s <name>=<password>'",
		THIS_CMD_NAME
            );
            NetSendLiveMessage(condescriptor, sndbuf);

            return(-1);
        }


        /* Copy arg to stringa. */
        strncpy(larg, arg, CS_DATA_MAX_LEN);
        larg[CS_DATA_MAX_LEN - 1] = '\0';
        StringStripSpaces(larg);


        /* Get name for new player object. */
        strncpy(name, larg, XSW_OBJ_NAME_MAX);
        name[XSW_OBJ_NAME_MAX - 1] = '\0';
        strptr = strchr(name, '='); 
        if(strptr != NULL)
            *strptr = '\0';
        StringStripSpaces(name);
        
        /* Sanitize name. */
        if(DBValidateObjectName(name))
        {
            sprintf(sndbuf,
                "%s: %s: Invalid name.",
		THIS_CMD_NAME,
                name
            );
            NetSendLiveMessage(condescriptor, sndbuf);

            return(-1);
        }

        /* Get password for new player object. */
        strptr = strchr(larg, '=');
        strptr += 1;
        strncpy(password, strptr, XSW_OBJ_PASSWORD_MAX);
        password[XSW_OBJ_PASSWORD_MAX - 1] = '\0';
        StringStripSpaces(password);

        /* Sanitize password. */
        if(DBValidateObjectPassword(password))
        {
            sprintf(sndbuf,
                "%s: %s: Invalid password.",
                THIS_CMD_NAME,
		password
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }


        /* Check if object's permission allows create player. */
        if(con_obj_ptr->permission.uid > ACCESS_UID_CREATEPLAYER)
        {
            sprintf(sndbuf,
            "%s: Requires access level %i: Permission denied.",
                THIS_CMD_NAME,
		ACCESS_UID_CREATEPLAYER
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }


	/* Check if another object by that name exists already. */
	for(i = 0; i < total_objects; i++)
	{
	    if(xsw_object[i] == NULL)
		continue;

	    if(xsw_object[i]->type <= XSW_OBJ_TYPE_GARBAGE)
		continue;

	    if(!strcmp(xsw_object[i]->name, name))
	    {
		sprintf(sndbuf,
                    "%s: %s: An object by that name already exists.",
		    THIS_CMD_NAME,
                    name
                );
                NetSendLiveMessage(condescriptor, sndbuf);

		return(-3);
	    }
	}


        /* Begin creating new player object. */
        new_obj_num = DBCreateObject(
            ISREF_DEFAULT,
            XSW_OBJ_TYPE_PLAYER,   
            con_object_num,
            con_obj_ptr->x,
            con_obj_ptr->y,
            con_obj_ptr->z,
            con_obj_ptr->heading,
            con_obj_ptr->pitch,
            con_obj_ptr->bank  
        );
        if(DBIsObjectGarbage(new_obj_num))
        {
            sprintf(sndbuf,
                "%s: DBCreateObject(): Unable to create object.",
		THIS_CMD_NAME
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }
	else
	{
	    new_obj_ptr = xsw_object[new_obj_num];
	}


	/*   Model newly created player object to the parameters of
	 *   the OPM for newly created player objects.
	 */

	/* Get OPM for newly created player objects. */
	opm_num = OPMGetByName(
	    DEF_PLAYER_OPM_NAME,
	    XSW_OBJ_TYPE_PLAYER
	);
	if(OPMIsGarbage(opm_num))
	{
	    opm_ptr = NULL;

            sprintf(sndbuf,
                "%s: OPMGetByName(): `%s' not defined.",
                THIS_CMD_NAME,
		DEF_PLAYER_OPM_NAME
            );   
            NetSendLiveMessage(condescriptor, sndbuf);
	}
	else
	{
	    opm_ptr = opm[opm_num];

	    OPMModelObjectPtr(new_obj_ptr, opm_ptr);
	}


	/* Set name and password. */
	strncpy(
	    new_obj_ptr->name,
	    name,
	    XSW_OBJ_NAME_MAX
	);
	new_obj_ptr->name[XSW_OBJ_NAME_MAX - 1] = '\0';

        strncpy(
            new_obj_ptr->password,
            CryptHandleEncrypt(password),   
            XSW_OBJ_PASSWORD_MAX
        );
        new_obj_ptr->password[XSW_OBJ_PASSWORD_MAX - 1] = '\0';

        /* Clear the unencrypted password from memory! */
        memset(password, '\0', XSW_OBJ_PASSWORD_MAX);


	/* Players always own themselves. */
	new_obj_ptr->owner = new_obj_num;


	/* Place new player object at the position of the creator. */
        new_obj_ptr->sect_x = con_obj_ptr->sect_x;
        new_obj_ptr->sect_y = con_obj_ptr->sect_y;
        new_obj_ptr->sect_z = con_obj_ptr->sect_z;
        new_obj_ptr->x = con_obj_ptr->x;
        new_obj_ptr->y = con_obj_ptr->y;
	new_obj_ptr->z = con_obj_ptr->z;
        new_obj_ptr->heading = con_obj_ptr->heading;
        new_obj_ptr->pitch = con_obj_ptr->pitch;
        new_obj_ptr->bank = con_obj_ptr->bank;


	new_obj_ptr->birth_time = cur_millitime;


	/* New player object has been created. */


        /* Send updates to all connections. */
	NetSendCreateObject(-1, new_obj_num);
        NetSendObjectName(-1, new_obj_num);
        NetSendObjectSect(-1, new_obj_num);


        /* Print and log. */
	strncpy(
	    name1,
	    DBGetFormalNameStr(con_object_num),
	    XSW_OBJ_NAME_MAX + 80
	);
	name1[XSW_OBJ_NAME_MAX + 80 - 1] = '\0';
        strncpy(
            name2,
            DBGetFormalNameStr(new_obj_num),
            XSW_OBJ_NAME_MAX + 80
        );
        name2[XSW_OBJ_NAME_MAX + 80 - 1] = '\0';

        sprintf(sndbuf,
            "Created new player: %s",
            name2
        );
        NetSendLiveMessage(condescriptor, sndbuf);
        sprintf(stringa,
            "%s: Created new player: %s",
            name1,
            name2
        );
        if(sysparm.log_general)
            LogAppendLineFormatted(fname.primary_log, stringa);


        return(0);
}