Cube  Check-in [b81e2948d7]

Overview
Comment:Migrate last strings
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b81e2948d74e606b374ad69956719d748340eb128dc5e4c732781a2de71c2ba2
User & Date: js on 2025-03-15 23:42:51
Other Links: manifest | tags
Context
2025-03-16
00:05
Migrate vectors check-in: 853e760619 user: js tags: trunk
2025-03-15
23:42
Migrate last strings check-in: b81e2948d7 user: js tags: trunk
23:10
Run the run loop check-in: 9e7cb01dd0 user: js tags: trunk
Changes

Modified src/console.mm from [fbd5eecbef] to [6b830d51dc].

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
// console.cpp: the console buffer, its display, and command line control

#include "cube.h"

#include <ctype.h>

#import "KeyMapping.h"
#import "OFString+Cube.h"




struct cline {


	char *cref;








	int outtime;
};


vector<cline> conlines;






const int ndraw = 5;
const int WORDWRAP = 80;
int conskip = 0;

bool saycommandon = false;
static OFMutableString *commandbuf;

void
setconskip(int n)
{
	conskip += n;
	if (conskip < 0)
		conskip = 0;
}
COMMANDN(conskip, setconskip, ARG_1INT)

static void
conline(OFString *sf, bool highlight) // add a line to the console buffer
{

	cline cl;
	// constrain the buffer size
	cl.cref = conlines.length() > 100 ? conlines.pop().cref
	                                  : (char *)calloc(_MAXDEFSTR, 1);

	// for how long to keep line on screen

	cl.outtime = lastmillis;
	conlines.insert(0, cl);
	if (highlight) {
		// show line in a different colour, for chat etc.

		cl.cref[0] = '\f';
		cl.cref[1] = 0;
		strcat_s(cl.cref, sf.UTF8String);
	} else {


		strcpy_s(cl.cref, sf.UTF8String);



	}
	puts(cl.cref);
#ifndef OF_WINDOWS
	fflush(stdout);
#endif
}

void
conoutf(OFConstantString *format, ...)









>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
|
|
>
>
|
>
>
>
>
>




















>
|

|
<
>
|
>
|
|
|

>
|
<
|
|
>
>
|
>
>
>
|
|







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
// console.cpp: the console buffer, its display, and command line control

#include "cube.h"

#include <ctype.h>

#import "KeyMapping.h"
#import "OFString+Cube.h"

@interface ConsoleLine: OFObject
@property (readonly, copy) OFString *text;
@property (readonly) int outtime;

- (instancetype)initWithText:(OFString *)text outtime:(int)outtime;
@end

static OFMutableArray<ConsoleLine *> *conlines;

@implementation ConsoleLine
- (instancetype)initWithText:(OFString *)text outtime:(int)outtime
{
	self = [super init];

	_text = [text copy];
	_outtime = outtime;

	return self;
}

- (OFString *)description
{
	return _text;
}
@end

const int ndraw = 5;
const int WORDWRAP = 80;
int conskip = 0;

bool saycommandon = false;
static OFMutableString *commandbuf;

void
setconskip(int n)
{
	conskip += n;
	if (conskip < 0)
		conskip = 0;
}
COMMANDN(conskip, setconskip, ARG_1INT)

static void
conline(OFString *sf, bool highlight) // add a line to the console buffer
{
	OFMutableString *text;

	// constrain the buffer size
	if (conlines.count > 100) {

		text = [conlines.lastObject.text mutableCopy];
		[conlines removeLastObject];
	} else
		text = [[OFMutableString alloc] init];

	if (highlight)
		// show line in a different colour, for chat etc.
		[text appendString:@"\f"];


	[text appendString:sf];

	if (conlines == nil)
		conlines = [[OFMutableArray alloc] init];

	[conlines insertObject:[[ConsoleLine alloc] initWithText:text
	                                                 outtime:lastmillis]
	               atIndex:0];

	puts(text.UTF8String);
#ifndef OF_WINDOWS
	fflush(stdout);
#endif
}

void
conoutf(OFConstantString *format, ...)
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
			conline([string substringToIndex:WORDWRAP], n++ != 0);
			string = [string substringFromIndex:WORDWRAP];
		}
		conline(string, n != 0);
	}
}


void
renderconsole() // render buffer taking into account time & scrolling
{
	int nd = 0;
	char *refs[ndraw];
	loopv(conlines)
	{


		if (conskip ? i >= conskip - 1 || i >= conlines.length() - ndraw
		            : lastmillis - conlines[i].outtime < 20000) {
			refs[nd++] = conlines[i].cref;
			if (nd == ndraw)
				break;
		}
	}


	@autoreleasepool {
		loopj(nd)
		{
			draw_text(@(refs[j]), FONTH / 3,
			    (FONTH / 4 * 5) * (nd - j - 1) + FONTH / 3, 2);
		}
	}
}

// keymap is defined externally in keymap.cfg

static OFMutableArray<KeyMapping *> *keyMappings = nil;








>

|


|
<
|
>
>
|
|
|



|
>
>
|
|
|
|
|
<







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
			conline([string substringToIndex:WORDWRAP], n++ != 0);
			string = [string substringFromIndex:WORDWRAP];
		}
		conline(string, n != 0);
	}
}

// render buffer taking into account time & scrolling
void
renderconsole()
{
	int nd = 0;
	OFString *refs[ndraw];


	size_t i = 0;
	for (ConsoleLine *conline in conlines) {
		if (conskip ? i >= conskip - 1 || i >= conlines.count - ndraw
		            : lastmillis - conline.outtime < 20000) {
			refs[nd++] = conline.text;
			if (nd == ndraw)
				break;
		}

		i++;
	}

	loopj(nd)
	{
		draw_text(refs[j], FONTH / 3,
		    (FONTH / 4 * 5) * (nd - j - 1) + FONTH / 3, 2);

	}
}

// keymap is defined externally in keymap.cfg

static OFMutableArray<KeyMapping *> *keyMappings = nil;

Modified src/cube.h from [3f59a3114f] to [8cc717f782].

1
2
3
4
5
6
7
8
9


10
11
12
13
14
15
16
// one big bad include file for the whole engine... nasty!

#import <ObjFW/ObjFW.h>

#define gamma gamma__
#include <SDL2/SDL.h>
#undef gamma

#include "tools.h"



@class DynamicEntity;

@interface Cube: OFObject <OFApplicationDelegate>
@property (class, readonly, nonatomic) Cube *sharedInstance;
@property (readonly, nonatomic) SDL_Window *window;
@property (readonly, nonatomic) OFIRI *gameDataIRI, *userDataIRI;









>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// one big bad include file for the whole engine... nasty!

#import <ObjFW/ObjFW.h>

#define gamma gamma__
#include <SDL2/SDL.h>
#undef gamma

#include "tools.h"

#define _MAXDEFSTR 260

@class DynamicEntity;

@interface Cube: OFObject <OFApplicationDelegate>
@property (class, readonly, nonatomic) Cube *sharedInstance;
@property (readonly, nonatomic) SDL_Window *window;
@property (readonly, nonatomic) OFIRI *gameDataIRI, *userDataIRI;

Modified src/savegamedemo.mm from [8ffa70a39e] to [7c09343b90].

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
			return;
		}
		gzwrite(f, (void *)"CUBESAVE", 8);
		gzputc(f, islittleendian);
		gzputi(SAVEGAMEVERSION);
		OFData *data = [player1 dataBySerializing];
		gzputi(data.count);
		char map[260] = { 0 };
		memcpy(map, getclientmap().UTF8String,
		    min(getclientmap().UTF8StringLength, 259));
		gzwrite(f, map, _MAXDEFSTR);
		gzputi(gamemode);
		gzputi(ents.length());
		loopv(ents) gzputc(f, ents[i].spawned);
		gzwrite(f, data.items, data.count);
		OFArray<DynamicEntity *> *monsters = getmonsters();
		gzputi(monsters.count);







|

|







101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
			return;
		}
		gzwrite(f, (void *)"CUBESAVE", 8);
		gzputc(f, islittleendian);
		gzputi(SAVEGAMEVERSION);
		OFData *data = [player1 dataBySerializing];
		gzputi(data.count);
		char map[_MAXDEFSTR] = { 0 };
		memcpy(map, getclientmap().UTF8String,
		    min(getclientmap().UTF8StringLength, _MAXDEFSTR - 1));
		gzwrite(f, map, _MAXDEFSTR);
		gzputi(gamemode);
		gzputi(ents.length());
		loopv(ents) gzputc(f, ents[i].spawned);
		gzwrite(f, data.items, data.count);
		OFArray<DynamicEntity *> *monsters = getmonsters();
		gzputi(monsters.count);
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
		               cStringWithEncoding:OFLocale.encoding],
		    "rb9");
		if (!f) {
			conoutf(@"could not open %@", IRI.string);
			return;
		}


		string buf;
		gzread(f, buf, 8);
		if (strncmp(buf, "CUBESAVE", 8))
			goto out;
		if (gzgetc(f) != islittleendian)
			goto out; // not supporting save->load accross
			          // incompatible architectures simpifies things
			          // a LOT
		if (gzgeti() != SAVEGAMEVERSION ||
		    gzgeti() != DynamicEntity.serializedSize)
			goto out;
		string mapname;
		gzread(f, mapname, _MAXDEFSTR);
		nextmode = gzgeti();
		@autoreleasepool {
			// continue below once map has been loaded and client &
			// server have updated
			changemap(@(mapname));
		}







>
|










<







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
		               cStringWithEncoding:OFLocale.encoding],
		    "rb9");
		if (!f) {
			conoutf(@"could not open %@", IRI.string);
			return;
		}

		char mapname[_MAXDEFSTR] = { 0 };
		char buf[8];
		gzread(f, buf, 8);
		if (strncmp(buf, "CUBESAVE", 8))
			goto out;
		if (gzgetc(f) != islittleendian)
			goto out; // not supporting save->load accross
			          // incompatible architectures simpifies things
			          // a LOT
		if (gzgeti() != SAVEGAMEVERSION ||
		    gzgeti() != DynamicEntity.serializedSize)
			goto out;

		gzread(f, mapname, _MAXDEFSTR);
		nextmode = gzgeti();
		@autoreleasepool {
			// continue below once map has been loaded and client &
			// server have updated
			changemap(@(mapname));
		}
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

		DynamicEntity *target = players[democlientnum];
		assert(target);

		int extras;
		// read additional client side state not present in normal
		// network stream
		if (extras = gzget()) {
			target.gunselect = gzget();
			target.lastattackgun = gzget();
			target.lastaction = scaletime(gzgeti());
			target.gunwait = gzgeti();
			target.health = gzgeti();
			target.armour = gzgeti();
			target.armourtype = gzget();
			loopi(NUMGUNS) target.ammo[i] = gzget();
			target.state = gzget();
			target.lastmove = playbacktime;
			if (bdamage = gzgeti())
				damageblend(bdamage);
			if (ddamage = gzgeti()) {
				gzgetv(dorig);
				particle_splash(3, ddamage, 1000, dorig);
			}
			// FIXME: set more client state here
		}

		// insert latest copy of player into history







|










|

|







449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

		DynamicEntity *target = players[democlientnum];
		assert(target);

		int extras;
		// read additional client side state not present in normal
		// network stream
		if ((extras = gzget())) {
			target.gunselect = gzget();
			target.lastattackgun = gzget();
			target.lastaction = scaletime(gzgeti());
			target.gunwait = gzgeti();
			target.health = gzgeti();
			target.armour = gzgeti();
			target.armourtype = gzget();
			loopi(NUMGUNS) target.ammo[i] = gzget();
			target.state = gzget();
			target.lastmove = playbacktime;
			if ((bdamage = gzgeti()))
				damageblend(bdamage);
			if ((ddamage = gzgeti())) {
				gzgetv(dorig);
				particle_splash(3, ddamage, 1000, dorig);
			}
			// FIXME: set more client state here
		}

		// insert latest copy of player into history

Modified src/serverbrowser.mm from [fe609f090e] to [fbd128e000].

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
		SDL_SemWait(resolversem);

		@synchronized(ResolverThread.class) {
			if (resolverqueries.count == 0)
				continue;

			_query = resolverqueries.lastObject;
			[resolverqueries
			    removeObjectAtIndex:resolverqueries.count - 1];
			_starttime = lastmillis;
		}

		ENetAddress address = { ENET_HOST_ANY, CUBE_SERVINFO_PORT };
		enet_address_set_host(&address, _query.UTF8String);

		@synchronized(ResolverThread.class) {







|
<







36
37
38
39
40
41
42
43

44
45
46
47
48
49
50
		SDL_SemWait(resolversem);

		@synchronized(ResolverThread.class) {
			if (resolverqueries.count == 0)
				continue;

			_query = resolverqueries.lastObject;
			[resolverqueries removeLastObject];

			_starttime = lastmillis;
		}

		ENetAddress address = { ENET_HOST_ANY, CUBE_SERVINFO_PORT };
		enet_address_set_host(&address, _query.UTF8String);

		@synchronized(ResolverThread.class) {
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
resolvercheck(OFString **name, ENetAddress *address)
{
	@synchronized(ResolverThread.class) {
		if (resolverresults.count > 0) {
			ResolverResult *rr = resolverresults.lastObject;
			*name = rr.query;
			*address = rr.address;
			[resolverresults
			    removeObjectAtIndex:resolverresults.count - 1];
			return true;
		}

		for (size_t i = 0; i < resolverthreads.count; i++) {
			ResolverThread *rt = resolverthreads[i];

			if (rt.query) {







|
<







143
144
145
146
147
148
149
150

151
152
153
154
155
156
157
resolvercheck(OFString **name, ENetAddress *address)
{
	@synchronized(ResolverThread.class) {
		if (resolverresults.count > 0) {
			ResolverResult *rr = resolverresults.lastObject;
			*name = rr.query;
			*address = rr.address;
			[resolverresults removeLastObject];

			return true;
		}

		for (size_t i = 0; i < resolverthreads.count; i++) {
			ResolverThread *rt = resolverthreads[i];

			if (rt.query) {

Modified src/serverutil.mm from [195e0442e7] to [26cbbdcb4f].

43
44
45
46
47
48
49
50

51

52
53
54
55
56
57
58
}

void
sendstring(OFString *t_, uchar *&p)
{
	@autoreleasepool {
		const char *t = t_.UTF8String;
		while (*t)

			putint(p, *t++);

		putint(p, 0);
	}
}

static const OFString *modenames[] = {
	@"SP",
	@"DMSP",







|
>

>







43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
}

void
sendstring(OFString *t_, uchar *&p)
{
	@autoreleasepool {
		const char *t = t_.UTF8String;

		for (size_t i = 0; i < _MAXDEFSTR && *t != '\0'; i++)
			putint(p, *t++);

		putint(p, 0);
	}
}

static const OFString *modenames[] = {
	@"SP",
	@"DMSP",

Modified src/tools.h from [64ce1d6c09] to [a3d0973ada].

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
#define loopl(m) loop(l, m)

#ifndef OF_WINDOWS
# define __cdecl
# define _vsnprintf vsnprintf
#endif

// easy safe strings

#define _MAXDEFSTR 260
typedef char string[_MAXDEFSTR];

inline void
strn0cpy(char *d, const char *s, size_t m)
{
	strncpy(d, s, m);
	d[(m)-1] = 0;
}

inline void
strcpy_s(char *d, const char *s)
{
	strn0cpy(d, s, _MAXDEFSTR);
}

inline void
strcat_s(char *d, const char *s)
{
	size_t n = strlen(d);
	strn0cpy(d + n, s, _MAXDEFSTR - n);
}

#define fast_f2nat(val) ((int)(val))

extern void endianswap(void *, int, int);

template <class T> struct vector {
	T *buf;
	int alen;







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







51
52
53
54
55
56
57

























58
59
60
61
62
63
64
#define loopl(m) loop(l, m)

#ifndef OF_WINDOWS
# define __cdecl
# define _vsnprintf vsnprintf
#endif


























#define fast_f2nat(val) ((int)(val))

extern void endianswap(void *, int, int);

template <class T> struct vector {
	T *buf;
	int alen;