Repository navigation
Expand file tree
/
Copy pathstartCourseGenerator.lua
More file actions
157 lines (140 loc) · 5.64 KB
/
Copy pathstartCourseGenerator.lua
File metadata and controls
157 lines (140 loc) · 5.64 KB
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
--- Find saved Courseplay fieldwork courses, prompt the user
-- to select one to optimize.
-- Create a copy of the selected course, enter it into the courseManager.xml
-- file and then open the course generator with the copied course.
--
-- There, you can set course generation paramteres, generate and check the
-- course and finally, save it.
--
-- The new course should be available in the game, with the name
-- (Customized) original name
--
require( 'file' )
-- generated courses will be prefixed with this string
prefix="(Customized)"
managerFilename="courseManager.xml"
careerSavegameFilename="careerSavegame.xml"
courseplayCustomFieldsFilename="courseplayCustomFields.xml"
function getSelection( tab )
while true do
local selection = tonumber( io.stdin:read())
if selection == 0 then return nil end
if tab[ selection ] ~= nil then
return selection
end
end
end
if not arg[ 1 ] then
print( "Usage: lua startCourseGenerator.lua <courseplay save directory for a map | savegame directory>")
return
end
dir = arg[ 1 ]
-- see if we were given a savegame directory
careerFile = io.open( dir .."\\" .. careerSavegameFilename )
if careerFile then
-- yes, we'll work with saved fields then.
useSavedFields = true
-- then find the CP course directory based on that
mapId = getMapId( careerFile )
careerFile:close()
courseDir = dir .. "\\..\\CoursePlay_Courses\\" .. mapId
print( string.format( "This is a savedir for map %s, will save generated course to %s", mapId, courseDir ))
else
-- we only have a CP course directory, we'll work with saved courses
useSavedFields = false
courseDir = dir
end
-- gather a list of saved courses
managerFileFullPath = courseDir .. "\\" .. managerFilename
managerFile = io.open( managerFileFullPath, "r" )
if not managerFile then
print( string.format( "Can't open %s.", managerFileFullPath ))
return
end
savedCourses, nextFreeId, nextFreeSequence = getSavedCourses( managerFile )
managerFile:close()
print()
if useSavedFields then
print( "Select the saved field you want to use for the course generation:\n" )
-- using saved fields, prompt the user for the field to use and a name for the course.
savedFields = loadSavedFields( dir .. "\\" .. courseplayCustomFieldsFilename )
for i, field in ipairs( savedFields ) do
print( string.format( " [ %d ] - Field '%d'", i, field.number ))
end
print( string.format( "\nEnter number ( %d - %d ) for the selected field or 0 (zero) to exit\n", 1, #savedFields ))
io.flush()
selectedFieldIndex = getSelection( savedFields )
-- save in the root folder (except when later an existing target course is selected)
newParent=0
if not selectedFieldIndex then return end
else
-- select an existing course.
print( "Select the saved course you want to use as the basis the course generation:\n" )
-- using saved course, prompt the user to select a course
for id, course in pairs( savedCourses ) do
print( string.format( " [ %d ] - '%s' (%s)", course.sequence, course.name, course.fileName ))
end
print( string.format( "\nEnter number ( %d - %d ) for the selected course or 0 (zero) to exit\n", 1, #savedCourses ))
io.flush()
selectedOldCourseSequence = getSelection( savedCourses )
-- save in the same folder
newParent = savedCourses[ selectedOldCourseSequence ].parent
if not selectedOldCourseSequence then return end
end
print( "\nNow select where you want to save the new course:\n" )
-- list existing courses
for id, course in pairs( savedCourses ) do
print( string.format( " [ %d ] - '%s' (%s)", course.sequence, course.name, course.fileName ))
end
print( string.format( "\nEnter number ( %d - %d ) for the selected course or 0 to create a new course\n", 1, #savedCourses ))
io.flush()
selectedNewCourseSequence = getSelection( savedCourses )
-- now prepare a new course when needed
local newCourse = {}
if not selectedNewCourseSequence then
-- creating a new course
print( string.format( "Enter a name for the new course:\n" ))
io.flush()
newCourseName = io.stdin:read()
newCourse = { id=nextFreeId,
name= newCourseName,
parent=newParent,
fileName=string.format( "courseStorage%04d.xml", nextFreeSequence ),
sequence=nextFreeSequence }
else
-- no new course, overwriting an existing one
if string.match( savedCourses[ selectedNewCourseSequence ].name, prefix ) then
newCourseName = savedCourses[ selectedNewCourseSequence ].name
else
newCourseName = prefix .. savedCourses[ selectedNewCourseSequence ].name
end
newCourse = savedCourses[ selectedNewCourseSequence ]
end
-- final confirmation
print( string.format( "Is it ok to create/overwrite '%s' (%s)? [y/n]" , newCourse.name, newCourse.fileName ))
io.flush()
local answer = io.stdin:read()
if answer ~= "y" and answer ~= "Y" then
return
end
-- create basis of new course
if useSavedFields then
-- new course based on saved field
writeSavedFieldToCourseFile( savedFields[ selectedFieldIndex ], courseDir .. "\\" .. newCourse.fileName )
else
-- new course based on saved course
if selectedOldCourseSequence ~= selectedNewCourseSequence then
-- not overwriting the old one
copyCourse( dir, savedCourses[ selectedOldCourseSequence ], newCourse, managerFilename )
end
end
-- not overwriting an existing one
if not selectedNewCourseSequence then
addCourseToManagerFile( courseDir, managerFilename, newCourse)
end
-- finally, start the course generator with the new saved course file
if useSavedFields then
os.execute( 'LOVE\\love.exe . fromField "' .. courseDir .. "\\" .. newCourse.fileName .. '"' )
else
os.execute( 'LOVE\\love.exe . fromCourse "' .. courseDir .. "\\" .. newCourse.fileName .. '"' )
end