/
teleport.c
125 lines (111 loc) · 3.21 KB
/
teleport.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
/************************************************************************
* This file is part of Wizznic. *
* Copyright 2009-2012 Jimmy Christensen <dusted@dusted.dk> *
* Wizznic is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* Wizznic is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Wizznic. If not, see <http://www.gnu.org/licenses/>. *
************************************************************************/
#include "teleport.h"
#include "pixel.h"
#include "defs.h"
#include "strings.h"
void teleFreeList( listItem* l )
{
listItem* it = l;
while( (it=it->next) )
{
free( it->data );
}
freeList( l );
}
void teleAddToList( listItem* l, int sx, int sy, int dx, int dy )
{
telePort_t* t = malloc( sizeof(telePort_t) );
t->sx=sx;
t->sy=sy;
t->dx=dx;
t->dy=dy;
listAddData(l, (void*)t );
}
void teleAddFromString(listItem* l, const char* str)
{
char* srcStr = malloc( sizeof(char)*10 );
char* dstStr = malloc( sizeof(char)*10 );
char* tempStrX = malloc( sizeof(char)*5 );
char* tempStrY = malloc( sizeof(char)*5 );
int fail=1;
int sx,sy,dx,dy;
if(splitVals(':',str, srcStr, dstStr) )
{
if(splitVals(',', srcStr, tempStrX, tempStrY))
{
sx = atoi(tempStrX);
sy = atoi(tempStrY);
if(splitVals(',', dstStr, tempStrX, tempStrY))
{
fail=0;
dx = atoi(tempStrX);
dy = atoi(tempStrY);
teleAddToList(l,sx,sy,dx,dy);
}
}
}
if(fail)
printf("teleAddFromString failed because of string '%s'\n", str);
free(srcStr);
free(dstStr);
free(tempStrX);
free(tempStrY);
}
void teleRemoveFromList( listItem* l, int sx, int sy )
{
listItem* it = l;
telePort_t* t;
while( (it=it->next) )
{
t = (telePort_t*)it->data;
if(t->sx==sx && t->sy==sy)
{
listRemoveItem(l, it);
}
}
}
char* teleMkStrings(listItem* l, const char* prefix)
{
if(!listSize(l)) return(0);
listItem* it = l;
telePort_t* t;
int bytes = sizeof(char)*(listSize(l)*strlen("=10,10:10,10\n"));
bytes += sizeof(char)*listSize(l)*strlen(prefix);
char* str = malloc( bytes );
memset(str,0,bytes);
while( (it=it->next) )
{
t = (telePort_t*)it->data;
sprintf(str, "%s%s=%i,%i:%i,%i\n",str,prefix, t->sx,t->sy,t->dx,t->dy);
}
return(str);
}
int_fast8_t telePresent( listItem* l, int sx,int sy)
{
listItem* it = l;
telePort_t* t;
while( (it=it->next) )
{
t = (telePort_t*)it->data;
if( t->sx==sx && t->sy==sy)
{
return(1);
}
}
return(0);
}