forked from sneezymud/dikumud
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.c
205 lines (143 loc) · 3.86 KB
/
utility.c
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
/* ************************************************************************
* file: utility.c, Utility module. Part of DIKUMUD *
* Usage: Utility procedures *
* Copyright (C) 1990, 1991 - see 'license.doc' for complete information. *
************************************************************************* */
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "structs.h"
#include <time.h>
#include "utils.h"
extern struct time_data time_info;
int MIN(int a, int b)
{
return a < b ? a:b;
}
int MAX(int a, int b)
{
return a > b ? a:b;
}
/* creates a random number in interval [from;to] */
int number(int from, int to)
{
return((random() % (to - from + 1)) + from);
}
/* simulates dice roll */
int dice(int number, int size)
{
int r;
int sum = 0;
assert(size >= 1);
for (r = 1; r <= number; r++) sum += ((random() % size)+1);
return(sum);
}
/* Create a duplicate of a string */
char *our_strdup(char *source)
{
char *new;
CREATE(new, char, strlen(source)+1);
return(strcpy(new, source));
}
/* returns: 0 if equal, 1 if arg1 > arg2, -1 if arg1 < arg2 */
/* scan 'till found different or end of both */
int str_cmp(char *arg1, char *arg2)
{
int chk, i;
for (i = 0; *(arg1 + i) || *(arg2 + i); i++)
if (chk = LOWER(*(arg1 + i)) - LOWER(*(arg2 + i)))
if (chk < 0)
return (-1);
else
return (1);
return(0);
}
/* returns: 0 if equal, 1 if arg1 > arg2, -1 if arg1 < arg2 */
/* scan 'till found different, end of both, or n reached */
int strn_cmp(char *arg1, char *arg2, int n)
{
int chk, i;
for (i = 0; (*(arg1 + i) || *(arg2 + i)) && (n>0); i++, n--)
if (chk = LOWER(*(arg1 + i)) - LOWER(*(arg2 + i)))
if (chk < 0)
return (-1);
else
return (1);
return(0);
}
/* writes a string to the log */
void log_message(char *str)
{
long ct;
char *tmstr;
ct = time(0);
tmstr = asctime(localtime(&ct));
*(tmstr + strlen(tmstr) - 1) = '\0';
fprintf(stderr, "%s :: %s\n", tmstr, str);
}
void sprintbit(long vektor, char *names[], char *result)
{
long nr;
*result = '\0';
for(nr=0; vektor; vektor>>=1)
{
if (IS_SET(1, vektor))
if (*names[nr] != '\n') {
strcat(result,names[nr]);
strcat(result," ");
} else {
strcat(result,"UNDEFINED");
strcat(result," ");
}
if (*names[nr] != '\n')
nr++;
}
if (!*result)
strcat(result, "NOBITS");
}
void sprinttype(int type, char *names[], char *result)
{
int nr;
for(nr=0;(*names[nr]!='\n');nr++);
if(type < nr)
strcpy(result,names[type]);
else
strcpy(result,"UNDEFINED");
}
/* Calculate the REAL time passed over the last t2-t1 centuries (secs) */
struct time_info_data real_time_passed(time_t t2, time_t t1)
{
long secs;
struct time_info_data now;
secs = (long) (t2 - t1);
now.hours = (secs/SECS_PER_REAL_HOUR) % 24; /* 0..23 hours */
secs -= SECS_PER_REAL_HOUR*now.hours;
now.day = (secs/SECS_PER_REAL_DAY); /* 0..34 days */
secs -= SECS_PER_REAL_DAY*now.day;
now.month = -1;
now.year = -1;
return now;
}
/* Calculate the MUD time passed over the last t2-t1 centuries (secs) */
struct time_info_data mud_time_passed(time_t t2, time_t t1)
{
long secs;
struct time_info_data now;
secs = (long) (t2 - t1);
now.hours = (secs/SECS_PER_MUD_HOUR) % 24; /* 0..23 hours */
secs -= SECS_PER_MUD_HOUR*now.hours;
now.day = (secs/SECS_PER_MUD_DAY) % 35; /* 0..34 days */
secs -= SECS_PER_MUD_DAY*now.day;
now.month = (secs/SECS_PER_MUD_MONTH) % 17; /* 0..16 months */
secs -= SECS_PER_MUD_MONTH*now.month;
now.year = (secs/SECS_PER_MUD_YEAR); /* 0..XX? years */
return now;
}
struct time_info_data age(struct char_data *ch)
{
long secs;
struct time_info_data player_age;
player_age = mud_time_passed(time(0),ch->player.time.birth);
player_age.year += 17; /* All players start at 17 */
return player_age;
}